Deploying Rust on Kubernetes: A Basic Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Deploying Rust on Kubernetes: A Basic Guide for Beginners

\n\n

Modern 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\n

In 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\n

1. Preparing a Rust Application for Containerization

\n\n

To 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.

\n\n

Create a new project:

\n
cargo new rust-k8s-demo\ncd rust-k8s-demo
\n\n

Add the dependency to Cargo.toml:

\n
[dependencies]\nactix-web = "4"\nserde = { version = "1.0", features = ["derive"] }\ntokio = { version = "1", features = ["full"] }
\n\n

Now let's write the application code in src/main.rs:

\n
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\n

Note: we bind to 0.0.0.0:8080 because inside the container the application should listen on all interfaces. Test it locally:

\n
cargo run
\n\n

Open http://localhost:8080/health — you should see a JSON response.

\n\n

2. Creating a Docker Image with the Rust Application

\n\n

For 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\n

Create a Dockerfile in the project root:

\n
# 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\n

This 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\n

Build the image:

\n
docker build -t rust-k8s-demo:latest .
\n\n

Test the container locally:

\n
docker run -p 8080:8080 rust-k8s-demo:latest
\n\n

If everything works — push the image to a registry (e.g., Docker Hub):

\n
docker tag rust-k8s-demo:latest yourusername/rust-k8s-demo:v1\ndocker push yourusername/rust-k8s-demo:v1
\n\n

3. Configuring Kubernetes Manifests

\n\n

Now that the image is ready, we need to create manifests for Kubernetes. We will create a Deployment (to manage replicas) and a Service (for

Blogs

Book Recommendations