Skip to content

Kubernetes_pods

Pods are the smallest deployable units in Kubernetes. A pod can contain one or more containers that share storage and network.

┌─────────────────────────────────────────────────────────────────────────────┐
│ Kubernetes Pod │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ Pod │ │
│ │ │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ Container 1 │ │ Container 2 │ │ │
│ │ │ (nginx) │ │ (sidecar) │ │ │
│ │ │ │ │ │ │ │
│ │ │ Port: 80 │ │ Port: 9090 │ │ │
│ │ └────────┬────────┘ └────────┬────────┘ │ │
│ │ │ │ │ │
│ │ └──────────┬───────────┘ │ │
│ │ │ │ │
│ │ ┌──────▼──────┐ │ │
│ │ │ Shared │ │ │
│ │ │ Network │ │ │
│ │ │ (localhost)│ │ │
│ │ └─────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ Shared Storage Volumes │ │ │
│ │ └─────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Pod Lifecycle │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Pending │─────▶│ Running │─────▶│ Succeeded│─────▶│ Deleted │ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
│ │ │ │ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
│ │ Container│ │ All con-│ │ Container │ │
│ │ Creating │ │ tainers │ │ completed │ │
│ └─────────┘ │ Running │ │ successfully │ │
│ └─────────┘ └─────────────┘ │
│ │
│ Possible States: │
│ - Pending: Pod created, containers being scheduled │
│ - Running: All containers running │
│ - Succeeded: All containers exited successfully │
│ - Failed: At least one container failed │
│ - Unknown: Pod status cannot be determined │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# Create pod from command line
kubectl run nginx --image=nginx:latest
# Create pod with specific port
kubectl run nginx --image=nginx:latest --port=80
# Create pod with environment variables
kubectl run myapp --image=myapp:latest --env="ENV=production"
pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
Terminal window
# Apply the pod
kubectl apply -f pod.yaml
# Get pod status
kubectl get pod nginx
# Describe pod
kubectl describe pod nginx
# View logs
kubectl logs nginx
# Execute command in pod
kubectl exec -it nginx -- /bin/sh
multi-container-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: web-with-logger
spec:
containers:
- name: web
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
- name: logger
image: busybox
command: ["sh", "-c", "tail -f /var/log/nginx/access.log"]
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
┌─────────────────────────────────────────────────────────────────────────────┐
│ Pod Template Structure │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ apiVersion: v1 │
│ kind: Pod │
│ metadata: │
│ name: my-pod │
│ labels: │
│ app: my-app │
│ spec: │
│ containers: │
│ - name: main-container │
│ image: nginx │
│ ports: │
│ - containerPort: 80 │
│ env: │
│ - name: ENV │
│ value: production │
│ resources: │
│ requests: │
│ memory: "64Mi" │
│ cpu: "250m" │
│ limits: │
│ memory: "128Mi" │
│ cpu: "500m" │
│ volumeMounts: │
│ - name: data │
│ mountPath: /data │
│ volumes: │
│ - name: data │
│ emptyDir: {} │
│ restartPolicy: Always │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# List pods
kubectl get pods
# List pods with more details
kubectl get pods -o wide
# List pods in all namespaces
kubectl get pods -A
# Watch pods
kubectl get pods -w
# Get pod YAML
kubectl get pod nginx -o yaml
# Delete pod
kubectl delete pod nginx
# Edit pod
kubectl edit pod nginx
# Logs from pod
kubectl logs nginx
# Follow logs
kubectl logs -f nginx
# Logs from specific container
kubectl logs nginx -c container-name
# Execute command in pod
kubectl exec nginx -- ls
# Interactive shell
kubectl exec -it nginx -- /bin/sh
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:latest
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
spec:
containers:
- name: myapp
image: myapp:latest
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
spec:
containers:
- name: myapp
image: myapp:latest
startupProbe:
httpGet:
path: /started
port: 8080
failureThreshold: 30
periodSeconds: 10
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'until nslookup myservice; do echo waiting; sleep 2; done;']
containers:
- name: myapp
image: myapp:latest

In this chapter, you learned:

  • What Kubernetes Pods are
  • Pod lifecycle and states
  • Creating pods with kubectl and YAML
  • Multi-container pods
  • Pod health checks (liveness, readiness, startup)
  • Init containers