Deploy a multi-container Pod
You have a single-node cluster. The control plane runs your workloads too, so everything you create lands on control-1.
You will build one Pod with two containers: an Nginx web server and a Redis store. They share an emptyDir volume, Nginx serves a page that says Hello Kubernetes, and you prove Redis is reachable from inside the Nginx container.
This is a worked example: every step shows the command. Read why each one matters; the guided lab after this hides them.
Task 1 of 4
Write the Pod manifest
Goal: a file
multi-container-pod.yamlin your home directory describing one Pod with two containers,nginxandredis, sharing an empty volume calledhtml.Do it: save this as
multi-container-pod.yaml(Create file does it for you; Show command shows how):apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - name: nginx image: nginx:1.25 ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: redis image: redis:7 ports: - containerPort: 6379 volumes: - name: html emptyDir: {}Then check the file is there:
ls -l multi-container-pod.yamlYou should see:
ls -llistsmulti-container-pod.yaml.Why: containers in one Pod share a network namespace (
localhostis the same for both) and can share volumes. Image tags are pinned, notlatest: the exam has no internet, and neither do most real clusters.Task 2 of 4
Apply it and wait for Running
Goal: the Pod
multi-container-podisRunningwith both containers ready (READY 2/2).Do it: apply the file, then watch the Pod start. Press Ctrl+C to stop the watch once it says
Runningand2/2.kubectl apply -f multi-container-pod.yaml kubectl get pod multi-container-pod -wYou should see:
READYgo from0/2to2/2andSTATUStoRunning, usually within a minute. Press Ctrl+C to stop watching.If it sits at
1/2: this shows which container is unhappy and why. It is the first command to reach for in the exam:kubectl describe pod multi-container-podTask 3 of 4
Serve "Hello Kubernetes"
Goal: Nginx in the Pod serves a page containing
Hello Kubernetesathttp://localhost.The shared
htmlvolume starts empty, so Nginx has nothing to serve yet: you write the page into it.Do it:
kubectl exec multi-container-pod -c nginx -- sh -c 'echo "Hello Kubernetes" > /usr/share/nginx/html/index.html' kubectl exec multi-container-pod -c nginx -- curl -s localhostYou should see:
Hello Kubernetesprinted by the second command.Why
-c nginx: the Pod has two containers, sokubectl execneeds to be told which one.Task 4 of 4
Reach Redis from the Nginx container
Goal: from inside the
nginxcontainer, Redis answers on127.0.0.1:6379, with no Service in between.Do it: the Nginx image has no
redis-cli, so use bash's built-in TCP connection to send Redis aPING:kubectl exec multi-container-pod -c nginx -- bash -c 'exec 3<>/dev/tcp/127.0.0.1/6379; printf "PING\r\n" >&3; head -c 7 <&3'You should see:
+PONG.Why it works: both containers share the Pod's network namespace, so
127.0.0.1in Nginx is the same address Redis listens on. Leave the Pod running: Check my work looks for it.
Run it on your own cluster
Register free and start with the taster hours when they are open. Your work is checked task by task.