Kubernetes_pods
Chapter 18: Kubernetes Pods
Section titled “Chapter 18: Kubernetes Pods”Pods are the smallest deployable units in Kubernetes. A pod can contain one or more containers that share storage and network.
What is a Pod?
Section titled “What is a Pod?”┌─────────────────────────────────────────────────────────────────────────────┐│ Kubernetes Pod │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌───────────────────────────────────────────────────────────────────┐ ││ │ Pod │ ││ │ │ ││ │ ┌─────────────────┐ ┌─────────────────┐ │ ││ │ │ Container 1 │ │ Container 2 │ │ ││ │ │ (nginx) │ │ (sidecar) │ │ ││ │ │ │ │ │ │ ││ │ │ Port: 80 │ │ Port: 9090 │ │ ││ │ └────────┬────────┘ └────────┬────────┘ │ ││ │ │ │ │ ││ │ └──────────┬───────────┘ │ ││ │ │ │ ││ │ ┌──────▼──────┐ │ ││ │ │ Shared │ │ ││ │ │ Network │ │ ││ │ │ (localhost)│ │ ││ │ └─────────────┘ │ ││ │ │ ││ │ ┌─────────────────────────────────────────────────┐ │ ││ │ │ Shared Storage Volumes │ │ ││ │ └─────────────────────────────────────────────────┘ │ ││ │ │ ││ └───────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Pod Lifecycle
Section titled “Pod Lifecycle”┌─────────────────────────────────────────────────────────────────────────────┐│ 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 ││ │└─────────────────────────────────────────────────────────────────────────────┘Creating Pods
Section titled “Creating Pods”Using kubectl
Section titled “Using kubectl”# Create pod from command linekubectl run nginx --image=nginx:latest
# Create pod with specific portkubectl run nginx --image=nginx:latest --port=80
# Create pod with environment variableskubectl run myapp --image=myapp:latest --env="ENV=production"Using YAML
Section titled “Using YAML”apiVersion: v1kind: Podmetadata: name: nginx labels: app: nginxspec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 resources: limits: memory: "128Mi" cpu: "500m" requests: memory: "64Mi" cpu: "250m"# Apply the podkubectl apply -f pod.yaml
# Get pod statuskubectl get pod nginx
# Describe podkubectl describe pod nginx
# View logskubectl logs nginx
# Execute command in podkubectl exec -it nginx -- /bin/shMulti-Container Pods
Section titled “Multi-Container Pods”apiVersion: v1kind: Podmetadata: name: web-with-loggerspec: 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/nginxPod Templates
Section titled “Pod Templates”┌─────────────────────────────────────────────────────────────────────────────┐│ 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 ││ │└─────────────────────────────────────────────────────────────────────────────┘Pod Commands
Section titled “Pod Commands”# List podskubectl get pods
# List pods with more detailskubectl get pods -o wide
# List pods in all namespaceskubectl get pods -A
# Watch podskubectl get pods -w
# Get pod YAMLkubectl get pod nginx -o yaml
# Delete podkubectl delete pod nginx
# Edit podkubectl edit pod nginx
# Logs from podkubectl logs nginx
# Follow logskubectl logs -f nginx
# Logs from specific containerkubectl logs nginx -c container-name
# Execute command in podkubectl exec nginx -- ls
# Interactive shellkubectl exec -it nginx -- /bin/shPod Health Checks
Section titled “Pod Health Checks”Liveness Probe
Section titled “Liveness Probe”apiVersion: v1kind: Podmetadata: name: myappspec: containers: - name: myapp image: myapp:latest livenessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 30 periodSeconds: 10Readiness Probe
Section titled “Readiness Probe”spec: containers: - name: myapp image: myapp:latest readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5Startup Probe
Section titled “Startup Probe”spec: containers: - name: myapp image: myapp:latest startupProbe: httpGet: path: /started port: 8080 failureThreshold: 30 periodSeconds: 10Init Containers
Section titled “Init Containers”apiVersion: v1kind: Podmetadata: name: myappspec: initContainers: - name: init-myservice image: busybox command: ['sh', '-c', 'until nslookup myservice; do echo waiting; sleep 2; done;']
containers: - name: myapp image: myapp:latestSummary
Section titled “Summary”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