section3-working-with-k8-pods3

Mastering K8 commands: Section 3

  |  

0 Comments k8s Kubernetes DevOps

Working with Pods in Kubernetes

Pods are the atomic units of scheduling in Kubernetes. They encapsulate containers, facilitate shared contexts, and orchestrate their execution within the cluster. In this section, we’ll delve into essential commands for managing Pods, with insights that practitioners in the field will find useful.

kubectl run

The kubectl run command is a convenient method to quickly create a pod for ephemeral tasks or testing purposes.

Example:shell

kubectl run nginx --image=nginx:latest --port=80

This command creates a new pod running the latest Nginx image and exposes port 80 for traffic.

Usage Note: While kubectl run is suited for creating temporary pods, for more permanent applications, it’s recommended to use kubectl apply with a YAML file to create deployments or other workload resources.

kubectl get pods

Use kubectl get pods to list all pods in the current namespace. You can customize the output with several options.

Examples:shell

kubectl get pods
kubectl get pods --all-namespaces
kubectl get pods --watch

Debugging:

To obtain detailed output, including the node on which the pod is running:shell

kubectl get pods -o wide

This provides additional information such as the IP addresses of the pods and the nodes on which they are running.

kubectl describe pod

The kubectl describe pod command provides a detailed view of a pod’s current state, its events, and configurations.

Example:shell

kubectl describe pod <pod-name>

Investigation: The Events section within the output is particularly valuable for diagnosing issues, as it records all the events in chronological order related to the pod’s lifecycle and operations.

kubectl logs

Retrieve the logs from a pod with kubectl logs. This is essential for understanding the behavior of applications within your pods.

Examples:shell

kubectl logs <pod-name>
kubectl logs <pod-name> -c <container-name>

Monitoring:

To follow the logs and receive real-time updates, use:shell

kubectl logs -f <pod-name>

Historical Logs:

If you need to access logs from a container that has crashed, you can use:shell

kubectl logs --previous <pod-name>

Mastering K8s Series Navigation

Previous: ← Section 2: Cluster Management Commands
Next: Section 4: Managing Deployments →