Deploying C++ on Kubernetes: A Basic Practical Guide
Kubernetes (K8s) has become the de facto standard for container orchestration. Although it is most often associated with Go, Java, or Python, C++ is also actively used for high-load services where performance and low memory consumption are critical. In this article, we will walk through the practical process of deploying a C++ application in a Kubernetes cluster: from writing a simple HTTP server to creating manifests and scaling.
1. Preparing the C++ Application and Docker Image
First, let's create a minimal C++ application that will listen for HTTP requests. We'll use the cpp-httplib library (header-only) for simplicity.
Source Code (server.cpp)
#include <httplib.h>#include <iostream>
int main() { using namespace httplib; Server svr;
svr.Get("/", [](const Request& req, Response& res) { res.set_content("Hello from C++ on Kubernetes!", "text/plain"); });
svr.Get("/health", [](const Request& req, Response& res) { res.set_content("OK", "text/plain"); });
std::cout << "Server started on port 8080" << std::endl; svr.listen("0.0.0.0", 8080); return 0;}Dockerfile (Multi-stage Build)
To keep the image compact and secure, we use a multi-stage build:
# Build stageFROM gcc:13-bookworm AS builderWORKDIR /appCOPY server.cpp .RUN g++ -std=c++17 -O2 -lpthread server.cpp -o server
# Final imageFROM debian:bookworm-slimWORKDIR /appCOPY --from=builder /app/server .EXPOSE 8080CMD ["./server"]Build the image:
docker build -t cpp-k8s-demo:latest .Test it locally:
docker run -p 8080:8080 cpp-k8s-democurl http://localhost:8080/2. Writing Kubernetes Manifests
For deployment, we will need two main resources: Deployment (managing pods) and Service (network access).
Deployment (deployment.yaml)
Defines how many replicas to run, the update policy, and the container image.
apiVersion: apps/v1kind: Deploymentmetadata: name: cpp-app-deployment labels: app: cpp-appspec: replicas: 3 selector: matchLabels: app: cpp-app template: metadata: labels: app: cpp-app spec: containers: - name: cpp-app image: cpp-k8s-demo:latest imagePullPolicy: IfNotPresent ports: - containerPort: 8080 livenessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 5 periodSeconds: 10 resources: requests: memory: "64Mi" cpu: "100m" limits: memory: "128Mi" cpu: "200m"Important: imagePullPolicy: IfNotPresent is suitable for local testing. For production, use Always or specify a version tag.
Service (service.yaml)
Provides access to the pods inside the cluster (ClusterIP) or externally (NodePort/LoadBalancer).
apiVersion: v1kind: Servicemetadata: name: cpp-app-servicespec: selector: app: cpp-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer3. Deploying to the Cluster and Verification
Assume you already have a running cluster (Minikube, kind, or cloud-based). Execute the commands:
# Load the image into the cluster (for Minikube)minikube image load cpp-k8s-demo:latest
# Apply the manifestskubectl apply -f deployment.yamlkubectl apply -f service.yaml
# Check the statuskubectl get pods -l app=cpp-appkubectl get svc cpp-app-service
Blogs
Book Recommendations
Read also
How to Get a Job as a Rust Developer: Tips and Resume
Functional Programming in C#: Advanced Techniques and Patterns
Function Overloading in TypeScript: A 2025 Guide with Examples
Architectural Patterns in C++: How to Write Code You Won't Be Ashamed to Show
JavaScript Debugging: Techniques, Tools, and Tips for Python Developers