1. Answers
  2. Dynamic configuration updates without restarting pods

How do I perform dynamic configuration updates without restarting pods?

To achieve dynamic configuration updates without restarting pods in Kubernetes, you can use ConfigMaps and Secrets. These resources allow you to decouple configuration artifacts from image content to keep containerized applications portable.

By mounting ConfigMaps or Secrets as volumes or using environment variables, you can update the configuration of your running applications dynamically. Kubernetes provides mechanisms to automatically update the pods when the ConfigMaps or Secrets change.

Here’s how you can set up dynamic configuration updates using Pulumi:

  1. Create a ConfigMap: This will hold your configuration data.
  2. Create a Pod: This will use the ConfigMap for its configuration.
  3. Mount the ConfigMap as a volume: This allows the pod to access the configuration data.
  4. Set up a watcher: This ensures that the pod gets updated when the ConfigMap changes.

Below is a TypeScript Pulumi program that demonstrates these steps:

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a ConfigMap
const configMap = new k8s.core.v1.ConfigMap("example-config", {
    metadata: { name: "example-config" },
    data: {
        "example.property": "initial value"
    },
});

// Create a Pod that uses the ConfigMap
const pod = new k8s.core.v1.Pod("example-pod", {
    metadata: {
        name: "example-pod",
    },
    spec: {
        containers: [
            {
                name: "example-container",
                image: "nginx",
                volumeMounts: [
                    {
                        name: "config-volume",
                        mountPath: "/etc/config",
                    },
                ],
                env: [
                    {
                        name: "EXAMPLE_PROPERTY",
                        valueFrom: {
                            configMapKeyRef: {
                                name: configMap.metadata.name,
                                key: "example.property",
                            },
                        },
                    },
                ],
            },
        ],
        volumes: [
            {
                name: "config-volume",
                configMap: {
                    name: configMap.metadata.name,
                },
            },
        ],
    },
});

// Watch for changes to the ConfigMap
const configWatcher = new k8s.core.v1.Pod("config-watcher", {
    metadata: {
        name: "config-watcher",
    },
    spec: {
        containers: [
            {
                name: "config-watcher",
                image: "busybox",
                command: ["sh", "-c", "while true; do cat /etc/config/example.property; sleep 10; done"],
                volumeMounts: [
                    {
                        name: "config-volume",
                        mountPath: "/etc/config",
                    },
                ],
            },
        ],
        volumes: [
            {
                name: "config-volume",
                configMap: {
                    name: configMap.metadata.name,
                },
            },
        ],
    },
});

// Export the Pod name and ConfigMap name
export const podName = pod.metadata.name;
export const configMapName = configMap.metadata.name;

Explanation:

  1. ConfigMap: We create a ConfigMap named example-config with a key-value pair.
  2. Pod: We create a Pod named example-pod that mounts the ConfigMap as a volume and also uses it as an environment variable.
  3. Mounting ConfigMap: The ConfigMap is mounted at /etc/config inside the container, and the key example.property is made available as an environment variable.
  4. Watcher Pod: We create a simple watcher Pod that continuously reads the value of example.property from the ConfigMap to demonstrate dynamic updates.

When you update the ConfigMap, Kubernetes will automatically update the mounted file in the Pod, allowing your application to detect and react to configuration changes without needing a restart.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up