Learnitweb

Create Environment Variables inside Kubernetes cluster using ConfigMap

Introduction

In this tutorial we’ll create environment variables inside Kubernetes cluster using ConfigMap.

Create a Kubernetes ConfigMap

Create a file named configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: helloworld-config
data:
  app.message: "Hello, World!!!!"

Apply the ConfigMap:

kubectl apply -f configmap.yaml

Modify HelloWorldController to Use Environment Variables

Read the hello world message from environment variables in your controller:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    @Value("${APP_MESSAGE:Default Hello}")
    private String message;

    @GetMapping("/hello")
    public String sayHello() {
        return message;
    }
}

Note: The APP_MESSAGE:Default Hello syntax ensures that if the environment variable is missing, it falls back to "Default Hello".

Note: After changing the code, you need to build the image again:

docker build -t hello-world-app .

Update Kubernetes Deployment to Inject ConfigMap

Modify deployment.yaml to mount the ConfigMap as an environment variable:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
        - name: hello-world
          image: hello-world-app  # Using locally built image
          imagePullPolicy: Never  # Prevents Kubernetes from pulling from Docker Hub
          ports:
            - containerPort: 8080
          env:
            - name: APP_MESSAGE
              valueFrom:
                configMapKeyRef:
                  name: helloworld-config
                  key: app.message

---
apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  selector:
    app: hello-world
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer  # Exposes service externally (on Docker Desktop, use port-forward)

Apply the updated deployment:

kubectl apply -f deployment.yaml

Test the Application

Now test the application:

> kubectl port-forward svc/hello-world-service 8080:80

Now, access the application:

http://localhost:8080/api/hello

Updating the ConfigMap

When you update the ConfigMap, the changes are not propagated to the pods. There are few ways to do it:

  1. Restart Pods Using Rollout Restart (Recommended)

This is the safest way to restart all pods in a deployment:

kubectl rollout restart deployment hello-world-deployment

This will restart all pods one by one without downtime.

2. Delete the Pod (Pod Will Auto-Restart)

If you want to restart a specific pod, delete it:

kubectl delete pod <pod-name>

Since the pod is managed by a Deployment, Kubernetes will automatically create a new pod.

3. Scale Down and Scale Up (For Complete Restart)

If you want to restart all pods, you can scale them to zero and then back up:

kubectl scale deployment hello-world-deployment --replicas=0
kubectl scale deployment hello-world-deployment --replicas=1

This will stop all pods and start them again.