Skip to main content

Creating and Managing Pods

1. Creating Pod from YAML

Example: Basic Pod Definition

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.25
    ports:
    - containerPort: 80
Field Description Required
apiVersion API version to use (v1 for core resources) Yes
kind Type of Kubernetes object (Pod) Yes
metadata.name Unique name for the Pod Yes
metadata.labels Key-value pairs for identifying the Pod No
spec.containers List of containers to run in the Pod Yes
spec.containers.name Name of the container (within Pod) Yes
spec.containers.image Docker image to use for the container Yes
spec.containers.ports Port mappings for the container No
Note: A Pod is the smallest deployable unit in Kubernetes. It can contain one or more containers that share storage and network resources.

2. Running Pod Imperatively

Command Description Example
kubectl run Creates a pod from a container image kubectl run nginx --image=nginx:latest
kubectl run --restart=Never Creates a pod that does not restart kubectl run job-pod --image=busybox --restart=Never
kubectl run --port Exposes a port for the pod kubectl run web --image=nginx --port=80

Example: Run a Pod with Environment Variables

kubectl run env-pod --image=nginx --env=ENV=production

Example: Run a Pod with Resource Limits

kubectl run resource-pod --image=nginx --requests.cpu=200m --limits.memory=512Mi

3. Listing Pods

Command Description Flags
kubectl get pods List all pods in the current namespace -o wide to show more details, --all-namespaces to list pods across all namespaces
kubectl get pods -A List all pods in all namespaces Alias for --all-namespaces
kubectl get pods -n <namespace> List pods in a specific namespace Use -n or --namespace
kubectl get pods --show-labels List pods with their labels Shows labels for each pod
kubectl get pods -o json Get pod details in JSON format Useful for scripting or automation
kubectl get pods -o yaml Get pod details in YAML format Useful for configuration or debugging

Example: Listing Pods in a Namespace

kubectl get pods -n production

Example: Listing All Pods with Labels

kubectl get pods --show-labels
Note: By default, kubectl get pods only shows the pod name, status, restart count, and age. Use -o wide for additional details like node name and IP address.

4. Describing Pod Details

Field Description Example Value
metadata.name Name of the Pod my-pod
metadata.namespace Namespace where the Pod resides default
spec.containers[*].name Name of the container within the Pod nginx
spec.containers[*].image Docker image used for the container nginx:1.21
status.phase Current phase of the Pod lifecycle Pending, Running, Succeeded, Failed
status.podIP IP address assigned to the Pod 10.244.0.5
status.hostIP IP address of the node where the Pod is scheduled 192.168.1.100
status.conditions[*].type Type of condition (e.g., PodReady, PodScheduled) PodReady
status.conditions[*].status Status of the condition (True, False, Unknown) True

Example: Describe Pod in YAML

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: default
spec:
  containers:
  - name: nginx
    image: nginx:1.21
    ports:
    - containerPort: 80
status:
  phase: Running
  podIP: 10.244.0.5
  hostIP: 192.168.1.100
Note: The status field is managed by the Kubernetes control plane and should not be manually edited.

5. Viewing Pod Logs

Command Description Flags
kubectl logs Fetches the logs of a pod -l (label selector), --since, --tail

Example: Fetching logs from a pod

kubectl logs my-pod

Example: Fetching logs with timestamp

kubectl logs my-pod --timestamps=true

Example: Fetching logs from a specific container in a pod

kubectl logs my-pod -c container-name
Note: If a pod has multiple containers, you must specify the container name using -c flag.

Example: Fetching logs for the last hour

kubectl logs my-pod --since=1h

Example: Fetching last 100 lines of logs

kubectl logs my-pod --tail=100

6. Following Log Streams

Command Description Options Example
kubectl logs Fetches logs from a pod -f to follow log output
-c <container> to specify container
kubectl logs -f my-pod
kubectl logs -p Fetches logs from a previous instance of a pod -f to follow log output kubectl logs -p -f my-pod
Note: The -f flag allows you to stream logs in real time, useful for debugging live applications.
Warning: Logs may not persist if the pod is deleted or restarted. Ensure log retention policies are in place for production environments.

7. Executing Commands in Pod

Command Description Notes
kubectl exec Execute a command in a pod Useful for debugging and running one-off tasks
kubectl exec -it Execute a command in a pod with interactive terminal Allows for real-time input/output, useful for shell sessions

Example: Executing a command in a pod

kubectl exec -it my-pod -- /bin/sh

Example: Running a command in a specific container

kubectl exec -it my-pod -c container-name -- ls /app
Note: The -it flags are used to allocate a TTY and keep stdin open, which is necessary for interactive commands.

8. Port Forwarding to Pod

Command Description Notes
kubectl port-forward kubectl port-forward <pod-name> <local-port>:<pod-port> Forward local port to a pod's port

Example: Forwarding Local Port to Pod

kubectl port-forward my-pod 8080:80
Note: The command will keep running and forward traffic until terminated.
Warning: Port forwarding is for development/testing only. Not suitable for production use.

9. Deleting Pods

Command Description Notes
kubectl delete pod <pod-name> Delete a specific pod Immediately terminates the pod
kubectl delete pod -l <label-selector> Delete pods matching a label selector Useful for bulk deletion
kubectl delete pod --all Delete all pods in the namespace Use with caution

Example: Delete a Pod by Name

kubectl delete pod nginx-deployment-7b4b5c9f8d-xyz12

Example: Delete Pods with Label Selector

kubectl delete pod -l app=nginx
Note: Deleting a pod managed by a controller (like Deployment or StatefulSet) will cause the controller to recreate it.
Warning: Use kubectl delete pod --all carefully as it will remove all pods in the namespace.

10. Using Pod Selectors

Selector Type Description Example
labelSelector Matches pods based on labels app=frontend
fieldSelector Matches pods based on field values status.phase=Running

Example: Label Selector in Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: nginx:latest

Example: Field Selector in Pod List

kubectl get pods -l status.phase=Running
Note: Pod selectors are used in various contexts like Deployment, Service, and ReplicaSet to identify and manage pods.

11. Watching Pod Status

Command Description Notes
kubectl get pods Lists all pods in the current namespace Default output includes pod name, status, restart count, and age
kubectl get pods -A Lists all pods across all namespaces Useful for cluster-wide monitoring
kubectl get pods -w Watches pods and shows updates in real time Useful for monitoring pod lifecycle events
kubectl describe pod <pod-name> Shows detailed information about a specific pod Includes events, status conditions, and configuration details

Example: Watching Pod Status in Real Time

kubectl get pods -w

Example: Describing a Pod for Detailed Information

kubectl describe pod my-app-pod
Note: The -w flag in kubectl get pods -w enables continuous watching of pod status changes, which is useful for debugging and monitoring.

12. Getting Pod YAML

Command Description Example
kubectl get pod <pod-name> -o yaml Retrieve the full YAML definition of a pod kubectl get pod my-pod -o yaml
kubectl get pod <pod-name> -o json Retrieve the full JSON definition of a pod kubectl get pod my-pod -o json
kubectl get pod <pod-name> -o yaml --export Export a pod's definition without status fields kubectl get pod my-pod -o yaml --export
Note: The --export flag removes status and metadata fields, useful for version control or replication.

13. Copying Files to Pod

Command Description Example
kubectl cp Copies files and directories to/from a pod kubectl cp /local/path pod-name:/remote/path

Example: Copying a file to a pod

kubectl cp ./local-file.txt my-pod:/tmp/file.txt

Example: Copying a directory to a pod

kubectl cp ./local-dir my-pod:/tmp/dir
Note: The pod must be running for the copy to succeed.
Warning: kubectl cp uses the pod's internal filesystem and does not support copying to or from containers that are not running.

Was this article helpful?