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.
- Kubernetes Level 01 – Day 10: Set Up Time Check Pod
- Objective
- Understanding the Architecture
- Step 1: Create Namespace
- Step 2: Create ConfigMap
- Step 3: Create Pod Manifest
- Manifest Content
- Explanation of Critical Sections
- Environment Variable from ConfigMap
- Volume Definition
- Volume Mount
- Shell Command Execution
- Step 4: Apply and Verify
- Deploy the Pod
- Check Pod Status
- Verify Log Output
- Internal Flow of Execution
- Key Outcome
Objective¶
Deploy a logging Pod with the following configuration:
- Namespace:
datacenter - Pod Name:
time-check - Image:
busybox:latest - ConfigMap:
time-config→TIME_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.logevery TIME_FREQ seconds
Understanding the Architecture¶
This setup connects four important Kubernetes components:
- Namespace → Logical isolation
- ConfigMap → External configuration
- Volume → File persistence inside Pod lifecycle
- 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_FREQrequires environment variable expansion
Without invoking the shell, variable expansion and redirection would not work properly.
The loop does:
- Write current date to file
- Sleep for TIME_FREQ seconds
- 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¶
- Namespace is created
- ConfigMap stores configuration
- Pod starts and reads environment variable
- Volume is mounted
- Shell loop executes continuously
- 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.