Cloud Native 8 min read

Using ConfigMap to Dynamically Configure Nginx in Kubernetes

This article explains what a Kubernetes ConfigMap is, when to use it, and provides step‑by‑step instructions with YAML examples for creating a ConfigMap, mounting it in an Nginx deployment, and making the server_name parameter dynamic using pod metadata.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using ConfigMap to Dynamically Configure Nginx in Kubernetes

ConfigMap is a Kubernetes resource that lets you store configuration data separately from application code, enabling easier management and updates without changing the code or container image.

ConfigMaps typically hold key‑value pairs or whole configuration files and can be created declaratively with a YAML file or via the Kubernetes API. Once created, a ConfigMap can be mounted as a volume or exposed as environment variables to containers.

When to use ConfigMap? It is especially useful when running multiple instances of the same application that require different configuration settings. ConfigMaps centralize configuration data and allow dynamic updates without redeploying the application.

Using ConfigMap to manage an Nginx application in Kubernetes

Step 1: Create ConfigMap

Create a ConfigMap to store your Nginx configuration by defining a YAML file named nginx-config.yaml :

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    server {
      listen 80;
      server_name example.com;
      root /usr/share/nginx/html;
      index index.html;
      # Add your dynamic configuration here using the $variable_name syntax
      # Example: access_log /var/log/nginx/access.log $log_format;
    }

This creates a ConfigMap called nginx-config containing an nginx.conf file.

Step 2: Deploy Nginx and mount the ConfigMap

Create a deployment YAML named nginx-deployment.yaml that mounts the ConfigMap as a volume:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-config

The deployment creates a single replica of an Nginx container that mounts the ConfigMap at /etc/nginx/conf.d/default.conf .

Step 3: Apply ConfigMap and Deployment

Use kubectl apply to create the ConfigMap and the deployment:

kubectl apply -f nginx-config.yaml
kubectl apply -f nginx-deployment.yaml

This creates the ConfigMap and deploys Nginx with the specified configuration.

Using dynamic parameter values

To make the server_name value dynamic and set it to each pod’s hostname, you can expose the pod’s metadata.name as an environment variable.

Step 1: Modify the ConfigMap

Update nginx-config.yaml to use a placeholder:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    server {
      listen 80;
      server_name ${SERVER_NAME};
      root /usr/share/nginx/html;
      index index.html;
      # Add your dynamic configuration here using the $variable_name syntax
      # Example: access_log /var/log/nginx/access.log $log_format;
    }

The placeholder ${SERVER_NAME} will be replaced at runtime.

Step 2: Update the Deployment

Add an environment variable that pulls the pod name:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80
          env:
            - name: SERVER_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-config

This adds the SERVER_NAME environment variable whose value is the pod’s metadata.name .

Step 3: Apply the changes

Run the apply commands again:

kubectl apply -f nginx-config.yaml
kubectl apply -f nginx-deployment.yaml

The updated ConfigMap and deployment will render the server_name in Nginx as the pod’s hostname, providing a dynamic, pod‑specific configuration. Note that metadata.name is unique for each pod by default.

cloud-nativedeploymentkubernetesdevopsNginxYAMLConfigMap
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.