| Crates.io | lmrc-k3s |
| lib.rs | lmrc-k3s |
| version | 0.3.16 |
| created_at | 2025-11-26 17:51:49.895145+00 |
| updated_at | 2025-12-11 13:29:19.15266+00 |
| description | K3s management library for the LMRC Stack - manage K3s Kubernetes clusters via SSH |
| homepage | https://gitlab.com/lemarco/lmrc-stack/tree/main/libs/k3s-manager |
| repository | https://gitlab.com/lemarco/lmrc-stack |
| max_upload_size | |
| id | 1951933 |
| size | 86,798 |
Part of the LMRC Stack - Infrastructure-as-Code toolkit for building production-ready Rust applications
A Rust library for managing K3s Kubernetes clusters via SSH.
ssh-manager crateAdd this to your Cargo.toml:
[dependencies]
lmrc-k3s = "0.1"
tokio = { version = "1.0", features = ["full"] }
use lmrc_k3s::K3sManager;
#[tokio::main]
async fn main() -> Result<(), lmrc_k3s::K3sError> {
// Create a manager with default settings
let manager = K3sManager::builder()
.token("my-secure-token")
.build();
// Install K3s on master node (force=false won't reinstall if already present)
manager.install_master("192.168.1.10", None, false).await?;
// Join worker nodes
manager.join_worker("192.168.1.11", "192.168.1.10", None).await?;
manager.join_worker("192.168.1.12", "192.168.1.10", None).await?;
// Download kubeconfig
manager.download_kubeconfig("192.168.1.10", "kubeconfig").await?;
// Check cluster state
if let Some(state) = manager.get_cluster_state() {
println!("Cluster has {} ready workers", state.ready_workers_count());
}
Ok(())
}
use lmrc_k3s::K3sManager;
#[tokio::main]
async fn main() -> Result<(), lmrc_k3s::K3sError> {
let manager = K3sManager::builder()
.token("my-cluster-token")
.build();
// Install master (force=false won't reinstall if already present)
manager.install_master("192.168.1.10", None, false).await?;
// Join workers
manager.join_worker("192.168.1.11", "192.168.1.10", None).await?;
Ok(())
}
let manager = K3sManager::builder()
.version("v1.28.5+k3s1")
.token("my-secure-token")
.disable(vec![
"traefik".to_string(),
"servicelb".to_string(),
])
.build();
// Install master with private IP for internal communication
manager.install_master(
"203.0.113.10", // Public IP for SSH access
Some("10.0.0.10"), // Private IP for cluster communication
false // Don't force reinstall
).await?;
// Join worker with private IP
manager.join_worker(
"203.0.113.11", // Worker public IP
"10.0.0.10", // Master private IP (for K3s API)
Some("10.0.0.11") // Worker private IP
).await?;
// Check if K3s is installed
if manager.is_installed("192.168.1.10").await? {
println!("K3s is installed");
// Get node status
let nodes = manager.get_nodes("192.168.1.10").await?;
println!("Nodes: {}", nodes);
}
// Uninstall from master
manager.uninstall("192.168.1.10", true).await?;
// Uninstall from worker
manager.uninstall("192.168.1.11", false).await?;
The library supports declarative cluster management - define your desired state and let lmrc-k3s handle the rest!
use lmrc_k3s::{DesiredClusterConfig, K3sManager, WorkerConfig};
// Define desired state
let desired = DesiredClusterConfig {
master_ip: "192.168.1.10".to_string(),
master_private_ip: None,
workers: vec![
WorkerConfig::new("192.168.1.11".to_string()),
WorkerConfig::new("192.168.1.12".to_string()),
WorkerConfig::new("192.168.1.13".to_string()),
],
k3s_version: "v1.28.5+k3s1".to_string(),
token: "my-token".to_string(),
disabled_components: vec!["traefik".to_string()],
};
// Reconcile - will add/remove workers to match desired state
manager.reconcile(&desired).await?;
// Later, scale up by adding to desired state
desired.workers.push(WorkerConfig::new("192.168.1.14".to_string()));
manager.reconcile(&desired).await?; // Adds worker .14
// Scale down by removing from desired state
desired.workers.retain(|w| w.ip != "192.168.1.13");
manager.reconcile(&desired).await?; // Removes worker .13 gracefully
Benefits:
The repository includes several complete examples:
basic_setup.rs - Simple cluster setup with one master and two workersprivate_network.rs - Cloud deployment with private networkingcluster_status.rs - Check cluster status and node informationdeclarative_reconciliation.rs - Declarative cluster management with automatic reconciliationRun examples with:
cargo run --example basic_setup
cargo run --example declarative_reconciliation
This crate uses the ssh-manager crate for SSH connections. Ensure you have:
~/.ssh/)Public Key Authentication (Default):
// Uses ~/.ssh/id_rsa by default
let manager = K3sManager::builder()
.token("my-token")
.build();
// Or specify a custom key path
let manager = K3sManager::builder()
.token("my-token")
.ssh_key_path("/path/to/custom/id_rsa")
.build();
Password Authentication:
let manager = K3sManager::builder()
.token("my-token")
.ssh_username("root")
.ssh_password("your-password")
.build();
Custom SSH Username:
let manager = K3sManager::builder()
.token("my-token")
.ssh_username("admin") // Default is "root"
.build();
The main struct for managing K3s clusters.
Cluster Setup:
builder() - Create a new builder for K3sManagerinstall_master(server_ip, private_ip, force) - Install K3s on a master nodejoin_worker(worker_ip, master_ip, worker_private_ip) - Join a worker to the clusterdownload_kubeconfig(master_ip, output_path) - Download kubeconfig from masteruninstall(server_ip, is_master) - Uninstall K3s from a nodeDeclarative Management:
reconcile(desired) - Reconcile cluster to match desired configurationplan_reconciliation(desired) - Create a reconciliation plan without applyingapply_reconciliation(desired, plan) - Apply a reconciliation planremove_worker(master_ip, worker_ip) - Gracefully remove a worker nodeStatus & Monitoring:
is_installed(server_ip) - Check if K3s is installed on a nodeget_nodes(master_ip) - Get cluster node statusget_node_info_list(master_ip) - Get detailed node information listget_cluster_state() - Get current cluster state with node informationrefresh_cluster_state(master_ip) - Refresh cluster state from the master nodeBuilder pattern for creating K3sManager instances.
Cluster Configuration:
version(version) - Set K3s version (default: "v1.28.5+k3s1")token(token) - Set cluster token (required)disable(components) - Set components to disableSSH Configuration:
ssh_username(username) - Set SSH username (default: "root")ssh_key_path(path) - Set SSH private key path (default: "~/.ssh/id_rsa")ssh_password(password) - Set SSH password for password-based authenticationBuild:
build() - Build the K3sManager instancePart of the LMRC Stack project. Licensed under either of:
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)