DEV Community

Cover image for 10.Set Up Time Check Pod in Kubernetes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

10.Set Up Time Check Pod in Kubernetes

Lab Information

The Nautilus DevOps team needs a time check pod created in a specific Kubernetes namespace for logging purposes. Initially, it's for testing, but it may be integrated into an existing cluster later. Here's what's required:

Create a pod called time-check in the devops namespace. The pod should contain a container named time-check, utilizing the busybox image with the latest tag (specify as busybox:latest).

Create a config map named time-config with the data TIME_FREQ=3 in the same namespace.

Configure the time-check container to execute the command: while true; do date; sleep $TIME_FREQ;done. Ensure the result is written /opt/dba/time/time-check.log. Also, add an environmental variable TIME_FREQ in the container, fetching its value from the config map TIME_FREQ key.

Create a volume log-volume and mount it at /opt/dba/time within the container.
Enter fullscreen mode Exit fullscreen mode

Note: The kubectl utility on jump_host is configured to operate with the Kubernetes cluster.

Lab Solutions

Step 1: Create the devops namespace

First, let's create the namespace if it doesn't exist:

kubectl create namespace devops
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the ConfigMap

Create the config map with the TIME_FREQ data:

kubectl create configmap time-config --from-literal=TIME_FREQ=3 -n devops
Enter fullscreen mode Exit fullscreen mode

Verify the ConfigMap was created:

kubectl get configmap time-config -n devops -o yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Pod YAML file

Now, let's create the pod configuration with all the requirements:
vi time-check-pod.yaml

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

Step 4: Apply the Pod configuration

Create the pod in the cluster:

kubectl apply -f time-check-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify the pod is running

Check the pod status:

kubectl get pods -n devops
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify the pod details

Check the detailed configuration to ensure all requirements are met:

kubectl describe pod time-check -n devops
Enter fullscreen mode Exit fullscreen mode

Step 7: Check the logs and file creation

Wait about 10-15 seconds for the pod to generate some logs, then check:
Option 1: Check the log file content directly

kubectl exec time-check -n devops -- cat /opt/dba/time/time-check.log
Enter fullscreen mode Exit fullscreen mode

Option 2: Check the environment variable

kubectl exec time-check -n devops -- env | grep TIME_FREQ
Enter fullscreen mode Exit fullscreen mode

Option 3: Check the process is running

kubectl exec time-check -n devops -- ps aux
Enter fullscreen mode Exit fullscreen mode

Step 8: Verify the volume mount

Check that the volume is properly mounted:

kubectl exec time-check -n devops -- df -h | grep dba
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)