Skip to content

Kubernetes Level 01 – Day 10: Set Up Time Check Pod

This document explains how to configure a Pod that reads configuration dynamically from a ConfigMap, runs a continuous loop command using that configuration, and writes output to a mounted volume.

The goal is to combine namespace isolation, environment configuration, volumes, and shell execution in a single working example.






Objective

Deploy a logging Pod with the following configuration:

  • Namespace: datacenter
  • Pod Name: time-check
  • Image: busybox:latest
  • ConfigMap: time-configTIME_FREQ=5
  • Environment Variable: TIME_FREQ
  • Volume Name: log-volume
  • Mount Path: /opt/dba/time
  • Action: Continuously write date output to /opt/dba/time/time-check.log every TIME_FREQ seconds



Understanding the Architecture

This setup connects four important Kubernetes components:

  1. Namespace → Logical isolation
  2. ConfigMap → External configuration
  3. Volume → File persistence inside Pod lifecycle
  4. Container command → Loop execution using shell

The Pod reads the frequency value from a ConfigMap and uses it to control how often logs are written.




Step 1: Create Namespace

Ensure the namespace exists before creating resources inside it.

kubectl create namespace datacenter

Namespaces isolate resources logically within the cluster.




Step 2: Create ConfigMap

Create a ConfigMap to store the execution interval.

kubectl create configmap time-config \
  --from-literal=TIME_FREQ=5 \
  -n datacenter

This creates a key-value pair:

TIME_FREQ=5

The Pod will later consume this value as an environment variable.




Step 3: Create Pod Manifest

Create the YAML file:

vi pod.yaml

Manifest Content

apiVersion: v1
kind: Pod
metadata:
  name: time-check
  namespace: datacenter
spec:
  containers:
  - name: time-check
    image: busybox:latest
    env:
    - name: TIME_FREQ
      valueFrom:
        configMapKeyRef:
          name: time-config
          key: TIME_FREQ
    volumeMounts:
    - name: log-volume
      mountPath: /opt/dba/time
    command: ["/bin/sh", "-c"]
    args:
    - while true; do date >> /opt/dba/time/time-check.log; sleep $TIME_FREQ; done
  volumes:
  - name: log-volume
    emptyDir: {}



Explanation of Critical Sections

Environment Variable from ConfigMap

valueFrom:
  configMapKeyRef:
    name: time-config
    key: TIME_FREQ

This pulls the TIME_FREQ value directly from the ConfigMap and injects it into the container environment.

The container does not hardcode the value. It reads it dynamically.




Volume Definition

volumes:
- name: log-volume
  emptyDir: {}

emptyDir creates a temporary storage space available as long as the Pod exists.

It survives container restarts but is deleted when the Pod is deleted.




Volume Mount

volumeMounts:
- name: log-volume
  mountPath: /opt/dba/time

This makes the emptyDir storage accessible inside the container at the given path.




Shell Command Execution

command: ["/bin/sh", "-c"]
args:
- while true; do date >> /opt/dba/time/time-check.log; sleep $TIME_FREQ; done

Why /bin/sh -c is necessary:

  • The >> operator requires shell processing
  • $TIME_FREQ requires environment variable expansion

Without invoking the shell, variable expansion and redirection would not work properly.

The loop does:

  1. Write current date to file
  2. Sleep for TIME_FREQ seconds
  3. Repeat indefinitely



Step 4: Apply and Verify

Deploy the Pod

kubectl apply -f pod.yaml

Check Pod Status

kubectl get pods -n datacenter

Status should be Running.

Verify Log Output

kubectl exec -it time-check -n datacenter -- cat /opt/dba/time/time-check.log

You should see timestamps printed at 5-second intervals.




Internal Flow of Execution

  1. Namespace is created
  2. ConfigMap stores configuration
  3. Pod starts and reads environment variable
  4. Volume is mounted
  5. Shell loop executes continuously
  6. Timestamps are appended to file

If the container restarts, the file remains intact because emptyDir belongs to the Pod lifecycle, not the container.




Key Outcome

The time-check Pod runs inside the datacenter namespace. It dynamically reads configuration from a ConfigMap and writes time logs into a mounted volume at controlled intervals. This demonstrates configuration decoupling, storage usage, and runtime scripting inside Kubernetes.