CKAD — Managing Multi-Container Pods
Working with Multi-Container Pods
When a Pod has multiple containers, most kubectl commands need the -c flag to target a specific container. This page covers the essential operational commands you need for the CKAD exam.
Viewing Logs from a Specific Container
# Logs from a specific container
kubectl logs app-with-logging -c app
kubectl logs app-with-logging -c log-shipper
# Follow logs in real time
kubectl logs app-with-logging -c log-shipper -f
# Previous container logs (after a restart)
kubectl logs app-with-logging -c app --previous
# Logs from all containers at once
kubectl logs app-with-logging --all-containers=true
# Last 20 lines from a specific container
kubectl logs app-with-logging -c app --tail=20
Executing Commands in a Specific Container
# Run a command in the app container
kubectl exec app-with-logging -c app -- ls /var/log
# Interactive shell in a specific container
kubectl exec -it app-with-logging -c app -- /bin/sh
# Check a file in a specific container
kubectl exec app-with-logging -c log-shipper -- cat /var/log/app.log
Important: If you omit the -c flag in a multi-container Pod, kubectl uses the first container defined in the spec.
Checking Init Container Status
# View init container status in Pod description
kubectl describe pod web-app | grep -A 10 "Init Containers"
# Check init container logs
kubectl logs web-app -c wait-for-db
# Watch Pod progression through init stages
kubectl get pod web-app -w
Debugging Multi-Container Pods
Common Status Messages
| Status | Meaning |
|---|---|
Init:0/3 |
0 of 3 init containers have completed |
Init:1/3 |
1 of 3 init containers has completed |
Init:Error |
An init container has failed |
PodInitializing |
All init containers done, main containers starting |
Running |
All containers are running |
CrashLoopBackOff |
A container keeps crashing and restarting |
Debugging Workflow
# 1. Check overall Pod status
kubectl get pod <name>
# 2. Get detailed event log
kubectl describe pod <name>
# 3. Check init container logs (if stuck in Init state)
kubectl logs <name> -c <init-container-name>
# 4. Check main container logs
kubectl logs <name> -c <container-name>
# 5. Check if containers can reach each other via localhost
kubectl exec <name> -c <container-name> -- wget -q -O- http://localhost:<port>
Pattern Comparison Summary
| Pattern | Purpose | Runs | Example |
|---|---|---|---|
| Sidecar | Extends main container | Alongside main container | Log shipper, proxy, monitoring agent |
| Init | Setup before main container | Before main container, sequentially | Wait for dependency, download config, run migrations |
| Ambassador | Proxy for external services | Alongside main container | DB connection pool, API gateway |
| Adapter | Transform output format | Alongside main container | Log formatter, metrics converter |
Quick Reference: Essential kubectl Commands
| Task | Command |
|---|---|
| Logs from specific container | kubectl logs <pod> -c <container> |
| Follow logs | kubectl logs <pod> -c <container> -f |
| Previous container logs | kubectl logs <pod> -c <container> --previous |
| All container logs | kubectl logs <pod> --all-containers=true |
| Exec into container | kubectl exec -it <pod> -c <container> -- /bin/sh |
| Run command in container | kubectl exec <pod> -c <container> -- <cmd> |
| Describe Pod | kubectl describe pod <pod> |
| Watch Pod status | kubectl get pod <pod> -w |
Exam tip: Speed matters on the CKAD exam. Memorize the -c <container-name> flag pattern — you will use it frequently when working with multi-container Pods.
Next: Put it all together with Multi-Container Pods Lab →