Deploying Rust on Kubernetes: A Practical Basic Guide

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 Practical Basic Guide



Rust is one of the fastest and safest systems programming languages. Kubernetes (k8s) is the de facto standard for container orchestration. Their combination allows you to create extremely efficient, lightweight, and fault-tolerant services. In this article, we will walk through the complete deployment cycle of a Rust application in a Kubernetes cluster: from building a Docker image to scaling.



Why Rust and Kubernetes are an Ideal Pair



Rust compiles to a native binary file and does not require a virtual machine (like Java) or an interpreter (like Python). This results in a minimal image size and low memory consumption. In the context of Kubernetes, this means:

  • Fast pod startup (often less than 1 second).
  • Cluster resource savings.
  • Fewer vulnerabilities due to the absence of heavy runtimes.


Step 1: Create a Simple Rust Application



For an example, let's write an HTTP server using the popular framework actix-web. Create a new project:

cargo new rust-k8s-democd rust-k8s-demo


Add dependencies to Cargo.toml:

[dependencies]actix-web = "4"serde = { version = "1", features = ["derive"] }


Let's write a simple handler in src/main.rs:

use actix_web::{get, web, App, HttpServer, Responder};use std::env;

#[get("/")]async fn index() -> impl Responder { let message = env::var("MESSAGE").unwrap_or_else(|_| "Hello from Rust!".to_string()); format!("{{ \\"message\\": \\"{}\\" }}", message)}

#[actix_web::main]async fn main() -> std::io::Result<()> { let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string()); let addr = format!("0.0.0.0:{}", port); println!("Starting server at {}", addr);

HttpServer::new(|| { App::new() .service(index) }) .bind(&addr)? .run() .await}


The application reads the MESSAGE and PORT environment variables — this will be useful for configuration via Kubernetes ConfigMap.



Step 2: Build the Docker Image



We use multi-stage build to minimize the image size. Create a Dockerfile:

# Build stageFROM rust:1.75-slim-bookworm AS builder

WORKDIR /appCOPY Cargo.toml Cargo.lock ./RUN mkdir src && echo "fn main() {}" > src/main.rsRUN cargo build --release 2>/dev/null || true

COPY src/ src/RUN cargo build --release

# Final imageFROM debian:bookworm-slimRUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*COPY --from=builder /app/target/release/rust-k8s-demo /usr/local/bin/rust-k8s-demo

EXPOSE 8080CMD ["rust-k8s-demo"]


Build the image and push it to a registry (e.g., Docker Hub):

docker build -t yourusername/rust-k8s-demo:latest .docker push yourusername/rust-k8s-demo:latest


Step 3: Kubernetes Manifests



Let's create a k8s folder and place the manifests in it.



3.1. ConfigMap for Configuration

Create the file k8s/configmap.yaml:

apiVersion: v1kind: ConfigMapmetadata:  name: rust-app-configdata:  MESSAGE: "Hello from Rust on Kubernetes!"  PORT: "8080"


3.2. Deployment

Create the file k8s/deployment.yaml:

apiVersion: apps/v1kind: Deploymentmetadata:  name: rust-appspec:  replicas: 3  selector:    matchLabels:      app: rust-app  template:    metadata:      labels:        app: rust-app    spec:      containers:      - name: rust-app        image: yourusername/rust-k8s-demo:latest        ports:        - containerPort: 8080        envFrom:        - configMapRef:            name: rust-app-config                

Blogs

Book Recommendations