Skip to main content

Lab 02 · A container that will not stay up

This lab runs on the kind cluster from Build a Lab Cluster on Your Laptop. Check kubectl config current-context first: it should say kind-lab.

Goal​

A small app reads its log level from a ConfigMap and prints its config file. It never becomes ready. Find both mistakes and get the Pod to Running with no restarts.

Setup​

kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Namespace
metadata:
name: lab-02
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: lab-02
data:
log-level: info
app.conf: |
greeting=hello
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
namespace: lab-02
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "echo \"log level: $LOG_LEVEL\"; cat /etc/app/settings.conf && sleep 86400"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
volumeMounts:
- name: config
mountPath: /etc/app
volumes:
- name: config
configMap:
name: app-config
EOF

Symptoms​

kubectl -n lab-02 get pods
NAME READY STATUS RESTARTS AGE
app-6d4b8c9f7b-zk2pw 0/1 CreateContainerConfigError 0 30s

The Pod name suffix will differ. Once you fix the first problem, the new Pod shows a different status, so keep watching kubectl get pods after each change.

Investigate​

kubectl -n lab-02 describe pod -l app=app # State, Last State, Events
kubectl -n lab-02 get configmap app-config -o yaml
kubectl -n lab-02 logs deploy/app # Current container
kubectl -n lab-02 logs deploy/app --previous # The container that exited before it

Ask two questions in order: did the container start at all, and if it did, why did it exit?

Hints​

Hint 1

CreateContainerConfigError means the kubelet could not even build the container's configuration, so there are no logs yet. The reason is in the events of kubectl describe pod. Compare the key it names with the keys in the ConfigMap.

Hint 2

ConfigMap keys are case-sensitive strings. LOG_LEVEL and log-level are two different keys. After you fix the key, the Pod moves on to Error and then CrashLoopBackOff.

Hint 3

In describe, look at Last State: Terminated, Reason Error, Exit Code 1. Then read kubectl logs --previous. Which file does the command open, and which files does the ConfigMap volume actually put in /etc/app?

Solution​

Solution

Problem 1: a ConfigMap key that does not exist. The Pod's events show a Failed event like this:

Warning Failed ... Error: couldn't find key LOG_LEVEL in ConfigMap lab-02/app-config

The env var points at key LOG_LEVEL, but the ConfigMap only has log-level and app.conf. A missing referenced key stops the container from being created, so the status is CreateContainerConfigError and nothing ever runs. (Marking the reference optional: true would let it start with the variable unset, but here the app needs it.)

Problem 2: a command that exits 1. With the key fixed, the container starts, prints one line and exits. kubectl logs --previous shows:

log level: info
cat: can't open '/etc/app/settings.conf': No such file or directory

A ConfigMap volume creates one file per key, so /etc/app holds log-level and app.conf. cat fails with exit code 1, the && skips sleep, and the shell exits 1. The kubelet restarts it with a growing back-off delay, which shows as CrashLoopBackOff.

Fix both lines and apply the Deployment again:

kubectl apply -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
namespace: lab-02
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "echo \"log level: $LOG_LEVEL\"; cat /etc/app/app.conf && sleep 86400"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: log-level
volumeMounts:
- name: config
mountPath: /etc/app
volumes:
- name: config
configMap:
name: app-config
EOF

Verify​

kubectl -n lab-02 rollout status deployment app
kubectl -n lab-02 get pods
kubectl -n lab-02 logs deploy/app
deployment "app" successfully rolled out
NAME READY STATUS RESTARTS AGE
app-7f5c6d8b9c-m4r7t 1/1 Running 0 15s
log level: info
greeting=hello

RESTARTS stays at 0. Wait a minute and check again to be sure.

Clean up​

kubectl delete ns lab-02

Check yourself

  1. A Pod shows CreateContainerConfigError and kubectl logs has nothing useful. Why?
  2. A Pod is in CrashLoopBackOff and the current container has just restarted. Which command shows why the last one died?