Deep dive into Kubernetes Pods - the smallest deployable units. Learn Pod lifecycle, multi-container patterns, and best practices for container management.
Pods are the fundamental building blocks of Kubernetes. A Pod represents a single instance of a running process in your cluster and can contain one or more containers that share storage and network resources. Understanding Pods deeply is essential for working effectively with Kubernetes.
In this lesson, you'll learn how Pods work, explore multi-container patterns, configure resources and probes, and master debugging techniques for troubleshooting Pod issues.
A Pod is the smallest deployable unit in Kubernetes. While you can run containers directly, Kubernetes groups containers into Pods to manage them together.
Key Characteristics:
Pod vs Container:
Why Pods?
Understanding the Pod lifecycle is crucial for managing applications and debugging issues.
Pod Phases:
Container States:
Common Conditions:
While most Pods run a single container, multi-container Pods are used when containers need to work closely together.
Sidecar Pattern:
Ambassador Pattern:
Adapter Pattern:
Init Container Pattern:
Properly configuring resource requests and limits ensures your Pods run reliably and don't starve other workloads.
Requests:
Limits:
Quality of Service (QoS) Classes:
Best Practices:
Health probes allow Kubernetes to detect and respond to unhealthy containers. Three types of probes ensure your applications stay healthy.
Liveness Probe:
Readiness Probe:
Startup Probe:
Probe Types:
Effective debugging skills are essential for resolving Pod issues quickly.
Common Issues:
Debugging Tools:
Debugging Workflow:
Create a Pod with multiple containers demonstrating the sidecar pattern for log aggregation.
1#!/bin/bash
2# Multi-Container Pod Lab
3# =======================
4
5# Create a Pod with sidecar pattern for logging
6cat <<EOF | kubectl apply -f -
7apiVersion: v1
8kind: ConfigMap
9metadata:
10 name: fluentbit-config
11data:
12 fluent-bit.conf: |
13 [SERVICE]
14 Flush 1
15 Log_Level info
16
17 [INPUT]
18 Name tail
19 Path /var/log/nginx/*.log
20 Tag nginx.*
21
22 [OUTPUT]
23 Name stdout
24 Match *
25---
26apiVersion: v1
27kind: Pod
28metadata:
29 name: web-with-logging
30 labels:
31 app: web-logging
32spec:
33 containers:
34 # Main web server
35 - name: nginx
36 image: nginx:1.21
37 ports:
38 - containerPort: 80
39 name: http
40 resources:
41 requests:
42 memory: "64Mi"
43 cpu: "100m"
44 limits:
45 memory: "128Mi"
46 cpu: "200m"
47 volumeMounts:
48 - name: logs
49 mountPath: /var/log/nginx
50 livenessProbe:
51 httpGet:
52 path: /
53 port: 80
54 initialDelaySeconds: 5
55 periodSeconds: 10
56 readinessProbe:
57 httpGet:
58 path: /
59 port: 80
60 initialDelaySeconds: 3
61 periodSeconds: 5
62
63 # Sidecar: Log collector
64 - name: fluentbit
65 image: fluent/fluent-bit:2.1
66 resources:
67 requests:
68 memory: "32Mi"
69 cpu: "50m"
70 limits:
71 memory: "64Mi"
72 cpu: "100m"
73 volumeMounts:
74 - name: logs
75 mountPath: /var/log/nginx
76 readOnly: true
77 - name: fluentbit-config
78 mountPath: /fluent-bit/etc
79
80 # Init container: Setup permissions
81 initContainers:
82 - name: init-logs
83 image: busybox
84 command: ['sh', '-c', 'mkdir -p /var/log/nginx && chmod 777 /var/log/nginx']
85 volumeMounts:
86 - name: logs
87 mountPath: /var/log/nginx
88
89 volumes:
90 - name: logs
91 emptyDir: {}
92 - name: fluentbit-config
93 configMap:
94 name: fluentbit-config
95EOF
96
97# Wait for Pod to be ready
98echo "Waiting for Pod to be ready..."
99kubectl wait --for=condition=Ready pod/web-with-logging --timeout=60s
100
101# Check Pod status
102echo -e "
103=== Pod Status ==="
104kubectl get pod web-with-logging -o wide
105
106# Check both containers
107echo -e "
108=== Container Status ==="
109kubectl describe pod web-with-logging | grep -A 15 "Containers:"
110
111# Generate some traffic
112echo -e "
113=== Generating traffic ==="
114kubectl port-forward pod/web-with-logging 8080:80 &
115sleep 3
116for i in {1..5}; do
117 curl -s http://localhost:8080 > /dev/null
118 echo "Request $i sent"
119done
120pkill -f "port-forward"
121
122# Check nginx logs
123echo -e "
124=== Nginx Logs ==="
125kubectl logs web-with-logging -c nginx --tail=5
126
127# Check fluentbit output (collected logs)
128echo -e "
129=== FluentBit Collected Logs ==="
130kubectl logs web-with-logging -c fluentbit --tail=10
131
132# Debug: Execute into containers
133echo -e "
134=== Container Inspection ==="
135echo "Files in nginx log directory:"
136kubectl exec web-with-logging -c nginx -- ls -la /var/log/nginx
137
138echo -e "
139Running processes in nginx container:"
140kubectl exec web-with-logging -c nginx -- ps aux
141
142# Check resource usage
143echo -e "
144=== Resource Usage ==="
145kubectl top pod web-with-logging --containers
146
147# Cleanup
148echo -e "
149Cleanup:"
150echo "kubectl delete pod web-with-logging"
151echo "kubectl delete configmap fluentbit-config"Your progress is saved in this browser, on this device only.