Your first Pod
Run something, break it, look inside it.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/firstpod.tar.gz && tar -xzf firstpod.tar.gz, which creates ~/lab/firstpod/. SHA-256: 457c117690981c02523fc70bf052beba11a33794a3458fa9d335f6f7af88c378Give Kubernetes a Pod whose image does not exist and kubectl still answers pod/web created, with a success exit code. With docker run the pull error would have landed in your terminal at once. Here kubectl apply only hands the request to the cluster, the real work happens later on some node, and any failure is recorded in the cluster, not printed for you.
So the first Pod in this course breaks on purpose. You find out why from the cluster's own records, fix it, and then look inside the running container.
A Pod from a file
A Pod wraps one or more containers. This file asks for a Pod named web with one nginx container. Look closely at the image tag, because it contains a deliberate typo:
apiVersion: v1kind: Podmetadata:name: weblabels:app: webspec:containers:- name: nginximage: nginx:1.30.60-alpineports:- containerPort: 80
apiVersion and kind say what sort of object this is, metadata.name names it, and spec.containers lists the containers, much like the arguments to docker run. The tag should be 1.30.6-alpine. It says 1.30.60-alpine, which does not exist.
The Pod exists and is ContainerCreating: it already has a node, and that node is busy getting the image. Run kubectl get pod web again every few seconds. Within about 20 seconds the status changes:
ImagePullBackOff means the pull failed and Kubernetes is waiting before it tries again. RESTARTS stays at 0 because the container never started.
Read the events
Every Pod collects events: short messages from the scheduler and the kubelet about what they did. kubectl describe prints them at the end of its output. Here sed cuts that output down to the Events part only:
Read them from top to bottom. The scheduler placed the Pod on a worker (Scheduled). The kubelet on that worker tried to pull the image (Pulling). Docker Hub answered not found, and the kubelet reported ErrImagePull. It then waits before trying again, which is the BackOff event and the ImagePullBackOff status. (x2 over 18s) means the same event happened twice in 18 seconds. The wait grows with each failure, up to five minutes, so a Pod with a bad tag never fixes itself.
kubectl events --for pod/web prints the same events on their own, which is handier when the describe output is long:
Note the exact message. not found here means the repository exists but has no such tag. A misspelled repository name, or a private one you cannot read, usually shows an authorization error instead.
Fix it
Fix the tag in the file and apply it again. A Pod lets you change its image while it exists, so the same Pod is updated in place. kubectl wait --for=condition=Ready then blocks until the Pod reports Ready, or until the timeout, and -o wide adds the IP and node columns:
pod/web configured means the existing Pod was changed, not replaced. It is now 1/1 Running. In the events the old failures stay in the history, and at the bottom comes the new sequence: Pulling the right tag, Pulled with how long the download took, then Created and Started. The image came from Docker Hub here. If it is already on the node, you see already present on machine instead of Pulling.
Look inside
Three commands cover most of what you need once a Pod runs. logs shows what the container wrote to its output, the same as docker logs:
These are nginx's start-up lines, ending with the worker processes it started. exec runs a command inside the container, like docker exec. Everything after -- is the command:
The hostname inside the container is the Pod name, web, and nginx serves its pages from /usr/share/nginx/html.
The Pod's IP address only works inside the cluster. To reach it from your machine while testing, kubectl port-forward opens a local port and tunnels it to the Pod. It keeps running until you stop it. You can run it in a second terminal, or start it in the background with & as below, where kill $! stops the last background command:
port-forward reports that it listens on 127.0.0.1:8080, then Handling connection once curl connects, and curl gets nginx's welcome page. In nginx's access log the request seems to come from 127.0.0.1, because port-forward delivers it from inside the Pod. A browser on localhost:8080 works just as well while the tunnel is open.
Delete it
A Pod you created yourself is gone once you delete it. Nothing brings it back, which is the difference from the Deployment in lesson 1.
Hands-on
Find out how long a change made inside a container lasts. pod.yaml now has the right tag.
1. Create the Pod and wait for it: kubectl apply -f pod.yaml && kubectl wait --for=condition=Ready pod/web --timeout=120s
2. Replace the start page: kubectl exec web -- sh -c 'echo hello from inside the Pod > /usr/share/nginx/html/index.html'
3. Fetch the page: kubectl port-forward pod/web 8080:80 &, then curl -s localhost:8080, then kill $!
4. Replace the Pod: kubectl delete pod web && kubectl apply -f pod.yaml && kubectl wait --for=condition=Ready pod/web --timeout=120s
5. Fetch the page again with the same three commands as in step 3
6. Clean up: kubectl delete -f pod.yaml, then kubectl get pods
What you should see: step 1 ends with pod/web condition met, and step 2 prints nothing. In step 3 curl prints hello from inside the Pod. Step 4 shows the old Pod deleted and a new one created and ready. In step 5 curl gets the default page again, with the title Welcome to nginx!: your change is gone, because a new Pod starts from the image. Lesson 17, Volumes, shows how to keep files. Step 6 ends with No resources found in default namespace.
Remember
A success message from kubectl apply only means the cluster stored your request. When a Pod does not run, its events say why, and once it runs, logs, exec and port-forward let you look inside.
pod/web created, but kubectl get pod web later shows ImagePullBackOff. What did the success message tell you?Failed to pull image ... not found. What is the most likely cause?not found came from the registry, about the name.1.30.60-alpine instead of 1.30.6-alpine gave exactly this message in the lab.RESTARTS stayed at 0 and there was no Started event.Scheduled event shows a node was found before the pull failed.kubectl exec, then delete the Pod and apply the same YAML again. What happens to your change?Welcome to nginx! after the Pod was recreated.