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 |
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 |
Note: If a pod has multiple containers, you must specify the container name using
-c flag.
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: 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 |
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 |
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
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 |
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 |
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.