Introduction: Why C++ and Kubernetes are a Powerful Tandem
Modern cloud infrastructure is increasingly built on microservice architecture and containerization. Kubernetes has become the de facto standard for container orchestration. However, many developers mistakenly believe that Kubernetes is only for interpreted languages (Python, JavaScript) or JVM. In reality, deploying C++ applications in Kubernetes is not only possible but also offers significant advantages: high performance, minimal memory consumption, and full control over resources.
In this guide, we will walk through how to package a C++ server into a Docker image, create the necessary manifests for Kubernetes, and run it in a cluster. You will learn how to handle configurations and secrets, as well as how to properly configure Health Checks for reliable application operation.
1. Preparing a C++ Application for Containerization
Before discussing Kubernetes, we need to prepare the C++ application itself. For an example, let's take a simple HTTP server based on the libmicrohttpd library. This server will respond to requests and read configuration from environment variables — this is a standard approach for 12-factor applications.
1.1. Code for a Simple HTTP Server in C++
Let's create a file main.cpp:
#include <iostream>#include <string>#include <microhttpd.h>#include <cstdlib>
static enum MHD_Result answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { const char *page = "<html><body>Hello from C++ on Kubernetes!</body></html>"; struct MHD_Response *response = MHD_create_response_from_buffer(strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT); int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret == MHD_YES ? MHD_OK : MHD_NO;}
int main() { const char* port_str = std::getenv("PORT"); unsigned int port = port_str ? std::stoul(port_str) : 8080;
std::cout << "Starting server on port " << port << std::endl; struct MHD_Daemon *daemon = MHD_start_daemon(MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, port, nullptr, nullptr, &answer_to_connection, nullptr, MHD_OPTION_END); if (nullptr == daemon) { std::cerr << "Failed to start daemon" << std::endl; return 1; }
// Infinite wait while (true) { sleep(1); }
MHD_stop_daemon(daemon); return 0;}This code reads the port from the environment variable PORT (default value is 8080) and starts the HTTP server. Note: we use an infinite loop to prevent the process from terminating — this is important for working in a container.
1.2. Building the Application
For building, we will need CMake. Let's create CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)project(cpp-k8s-server)
find_package(libmicrohttpd REQUIRED)
add_executable(server main.cpp)target_link_libraries(server ${LIBMICROHTTPD_LIBRARIES})target_include_directories(server PRIVATE ${LIBMICROHTTPD_INCLUDE_DIRS})Now the application is ready for containerization.
2. Creating a Docker Image for the C++ Application
Kubernetes works with containers, so we need a Docker image. For C