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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: v1
kind: ConfigMap
metadata:
name: helloworld-config
data:
app.message: "Hello, World!!!!"
apiVersion: v1 kind: ConfigMap metadata: name: helloworld-config data: app.message: "Hello, World!!!!"
apiVersion: v1
kind: ConfigMap
metadata:
  name: helloworld-config
data:
  app.message: "Hello, World!!!!"

Apply the ConfigMap:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl apply -f configmap.yaml
kubectl apply -f configmap.yaml
kubectl apply -f configmap.yaml

Modify HelloWorldController to Use Environment Variables

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
}
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; } }
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker build -t hello-world-app .
docker build -t hello-world-app .
docker build -t hello-world-app .

Update Kubernetes Deployment to Inject ConfigMap

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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)
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)
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl apply -f deployment.yaml
kubectl apply -f deployment.yaml
kubectl apply -f deployment.yaml

Test the Application

Now test the application:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> kubectl port-forward svc/hello-world-service 8080:80
> kubectl port-forward svc/hello-world-service 8080:80
> 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl rollout restart deployment hello-world-deployment
kubectl rollout restart deployment hello-world-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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl delete pod <pod-name>
kubectl delete pod <pod-name>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
kubectl scale deployment hello-world-deployment --replicas=0
kubectl scale deployment hello-world-deployment --replicas=1
kubectl scale deployment hello-world-deployment --replicas=0 kubectl scale deployment hello-world-deployment --replicas=1
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.