AskLearn
Loading...
← Back to Course
Beginner

Working with Pods

Basic workload management

Working with Pods

What is a Pod?

  • Smallest deployable unit
  • Wraps one or more containers
  • Containers in a pod share network and storage
  • Usually contains one main container

Creating Pods

Imperative Way

# Create a simple pod
kubectl run my-pod --image=nginx

# Create pod with port
kubectl run my-app --image=nginx --port=80

Declarative Way (YAML)

apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx-container
    image: nginx:1.21
    ports:
    - containerPort: 80

Multi-Container Pod Example

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: web-server
    image: nginx
    ports:
    - containerPort: 80
  - name: log-agent
    image: fluent/fluent-bit
    volumeMounts:
    - name: log-volume
      mountPath: /var/log
  volumes:
  - name: log-volume
    emptyDir: {}

Pod Lifecycle

  1. Pending: Pod created but not scheduled
  2. Running: Pod scheduled and at least one container is running
  3. Succeeded: All containers terminated successfully
  4. Failed: All containers terminated, at least one failed
  5. Unknown: Pod state cannot be determined

Useful Pod Commands

# Get pods with more info
kubectl get pods -o wide

# Watch pod status
kubectl get pods --watch

# Get pod logs
kubectl logs <pod-name>

# Execute commands in pod
kubectl exec -it <pod-name> -- /bin/bash

# Port forwarding
kubectl port-forward <pod-name> 8080:80

Next Steps

Learn about Services to expose your pods to network traffic.