Deploying Rust on Kubernetes: A Basic Guide for Beginners
\n\nModern development increasingly requires not only writing fast and secure code but also the ability to properly deploy it in a cloud infrastructure. Rust, known for its performance and memory safety, is an excellent choice for microservices. And Kubernetes — the standard container orchestrator — helps manage these services at scale.
\n\nIn this article, we will go through the entire process from writing a simple web server in Rust to deploying it in a Kubernetes cluster. You will learn how to package an application into a Docker image, create the necessary manifests, and run it in the cloud.
\n\n1. Preparing a Rust Application for Containerization
\n\nTo start, let's create a simple web application in Rust using the popular framework actix-web. It will return a simple JSON message — an ideal example for a microservice.
Create a new project:
\ncargo new rust-k8s-demo\ncd rust-k8s-demo\n\nAdd the dependency to Cargo.toml:
[dependencies]\nactix-web = "4"\nserde = { version = "1.0", features = ["derive"] }\ntokio = { version = "1", features = ["full"] }\n\nNow let's write the application code in src/main.rs:
use actix_web::{web, App, HttpServer, Responder, HttpResponse};\nuse serde::Serialize;\n\n#[derive(Serialize)]\nstruct HealthResponse {\n status: String,\n message: String,\n}\n\nasync fn health_check() -> impl Responder {\n HttpResponse::Ok().json(HealthResponse {\n status: "ok".to_string(),\n message: "Rust service is running on Kubernetes!".to_string(),\n })\n}\n\n#[actix_web::main]\nasync fn main() -> std::io::Result<()> {\n println!("Starting server on 0.0.0.0:8080");\n HttpServer::new(|| {\n App::new()\n .route("/health", web::get().to(health_check))\n })\n .bind("0.0.0.0:8080")?\n .run()\n .await\n}\n\nNote: we bind to 0.0.0.0:8080 because inside the container the application should listen on all interfaces. Test it locally:
cargo run\n\nOpen http://localhost:8080/health — you should see a JSON response.
2. Creating a Docker Image with the Rust Application
\n\nFor Kubernetes, we need a container. Rust compiles to a native binary, so we can use a multi-stage build to get a minimal image.
\n\nCreate a Dockerfile in the project root:
# Build stage\nFROM rust:1.75-slim-bookworm AS builder\n\nWORKDIR /app\nCOPY Cargo.toml Cargo.lock ./\nRUN mkdir src && echo "fn main() {}" > src/main.rs\nRUN cargo build --release 2>/dev/null || true\n\nCOPY src/ src/\nRUN cargo build --release\n\n# Final image\nFROM debian:bookworm-slim\nRUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*\nCOPY --from=builder /app/target/release/rust-k8s-demo /usr/local/bin/rust-k8s-demo\n\nEXPOSE 8080\nCMD ["rust-k8s-demo"]\n\nThis Dockerfile first builds dependencies, then compiles our code and copies the binary into a minimal Debian-based image. This saves space and improves security.
\n\nBuild the image:
\ndocker build -t rust-k8s-demo:latest .\n\nTest the container locally:
\ndocker run -p 8080:8080 rust-k8s-demo:latest\n\nIf everything works — push the image to a registry (e.g., Docker Hub):
\ndocker tag rust-k8s-demo:latest yourusername/rust-k8s-demo:v1\ndocker push yourusername/rust-k8s-demo:v1\n\n3. Configuring Kubernetes Manifests
\n\nNow that the image is ready, we need to create manifests for Kubernetes. We will create a Deployment (to manage replicas) and a Service (for