LearnPods and multi-container patterns

Deploy a multi-container Pod

A free lab to read. In Gazeek it runs on a real cluster built for you, with each task checked as you go. Register to run it.

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.

  1. Task 1 of 4

    Write the Pod manifest

    Goal: a file multi-container-pod.yaml in your home directory describing one Pod with two containers, nginx and redis, sharing an empty volume called html.

    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.yaml
    

    You should see: ls -l lists multi-container-pod.yaml.

    Why: containers in one Pod share a network namespace (localhost is the same for both) and can share volumes. Image tags are pinned, not latest: the exam has no internet, and neither do most real clusters.

  2. Task 2 of 4

    Apply it and wait for Running

    Goal: the Pod multi-container-pod is Running with 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 Running and 2/2.

    kubectl apply -f multi-container-pod.yaml
    kubectl get pod multi-container-pod -w
    

    You should see: READY go from 0/2 to 2/2 and STATUS to Running, 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-pod
    
  3. Task 3 of 4

    Serve "Hello Kubernetes"

    Goal: Nginx in the Pod serves a page containing Hello Kubernetes at http://localhost.

    The shared html volume 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 localhost
    

    You should see: Hello Kubernetes printed by the second command.

    Why -c nginx: the Pod has two containers, so kubectl exec needs to be told which one.

  4. Task 4 of 4

    Reach Redis from the Nginx container

    Goal: from inside the nginx container, Redis answers on 127.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 a PING:

    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.1 in 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.