Computer scienceSystem administration and DevOpsKubernetes

Working with a pod

11 minutes read

In this topic, you will learn how to use pods in Kubernetes. You will know how to create, run, and delete a pod. And you will also learn how to get a list of pods in a Kubernetes cluster, view the logs from application containers in the pod, and finally get a shell to a running container in a pod.

Creating a pod

To create a pod, you need to define a pod manifest in a YAML file. The manifest includes specifications like the container image and any command to run the application in a container. Then use the kubectl create command.

apiVersion: v1
kind: Pod
metadata:
  name: busybox
spec:
  containers:
  - name: busybox1
    image: busybox
    command: ['sh', '-c', 'echo "HelloWorld from container1" && sleep 3000']
  - name: busybox2
    image: busybox
    command: ['sh', '-c', 'echo "HelloWorld from container2" && sleep 1000']

In the example shown above, the pod contains two containers with names busybox1 and busybox2. You can specify any number of containers as per the requirements.

The container specification contains various fields such as:

  • name: The container's name in the pod is useful for specifying which container you want to access
  • image: This field specifies which image to run inside the container
  • command: The command filed contains a list of strings, where the first element is the main command and subsequent elements are arguments. The container executes this command when it starts. If you do not specify this field, the container uses the default command defined in the Docker image.

To create a pod from a YAML file, use the kubectl create command:

kubectl create -f <pod-definition-yaml-file-name>

The kubectl cli also provides the --dry-run flag. The flag allows you to simulate the execution of a command without actually performing any changes in the cluster.

The --dry-run flag is useful for verifying the correctness of Kubernetes resource definitions or testing the impact of potential changes before applying them. It is also useful for correctly generating the pod definition files in formats like YAML, JSON, and others. And then you can modify those files based on your requirements. Here's an example that uses the --dry-run flag with kubectl:

kubectl apply -f pod-definition.yaml --dry-run=client

In this example, pod-definition.yaml is the YAML file that contains the definition of the pod to be created. The --dry-run=client flag instructs kubectl to simulate the execution of the command without contacting the server. By using --dry-run, you can catch any potential issues or conflicts in resource configurations before actually making changes to the cluster. It is a helpful tool for testing and validating Kubernetes deployments without impacting the running environment.

Here's an example of how you can use the --dry-run flag with kubectl to generate pod definition YAML files:

kubectl run <pod-name> --image=<container-image> --dry-run=client -o yaml > pod-definition.yaml

The command generates the pod definition without creating a pod and writes it to the pod-definition.yaml file.

Viewing pods

The kubectl get pods command allows you to list all pods in a Kubernetes cluster. This command displays information about all the pods currently running in the default namespace. You can also specify the namespace using the --namespace flag to display pods running elsewhere. Here's an example:

kubectl get pods --namespace <namespace-name>

This will output a table with details about the pods, including their name, status, and age.


NAME      READY   STATUS    RESTARTS   AGE
pod1      1/1     Running   0          5m
pod2      1/1     Running   0          10m
pod3      1/1     Running   0          15m

Each row represents a pod, and the columns provide the following information:

  • NAME: name of the pod
  • READY: number of containers in the pod that are ready versus the total number of containers in the pod
  • STATUS: current status of the pod. The possible values are Running, Pending, or CrashLoopBackOff
  • RESTARTS: number of times the containers in the pod have been restarted
  • AGE: amount of time that has passed since the pod was created

The kubectl get pod -o wide command provides additional information about the pods in your Kubernetes cluster, including their node placement and IP address. The -o wide flag displays more details about the pods. Here's an example:


NAME      READY   STATUS    RESTARTS   AGE     IP            NODE
pod1      1/1     Running   0          5m      10.244.1.10   node1
pod2      1/1     Running   0          10m     10.244.2.15   node2
pod3      1/1     Running   0          15m     10.244.1.25   node1

This output includes the following additional columns:

  • IP: ip address of the pod
  • NODE: node in a Kubernetes cluster to which the pod is deployed

To get more information about a specific pod, use kubectl describe command. Here's what the command will look like:

kubectl describe pod <pod-name> --namespace <namespace-name>

This command displays detailed information about the specified pod. The information contains the pod's name, namespace, labels, annotations, IP address, containers running within the pod, status, events, pod conditions, volumes, and more. All this information helps you gain insights into the pod's current state and troubleshoot in case any issues arise. Here's what the output of the kubectl describe command looks like:

Name:             busybox
Namespace:        default
Priority:         0
Service Account:  default
Node:             ubuntu2004.localdomain/10.0.2.15
Start Time:       Mon, 10 Jul 2023 04:55:11 +0000
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               10.42.0.11

...

The actual output will contain a lot more information compared to what is shown above. You have already looked at fields such as pod conditions in previous topics and there are many more that you will study later on.

Deleting a pod

Kubernetes automatically deletes pods after a pod's lifecycle is complete. This happens in cases like Jobs, CronJobs, etc. To delete a pod, you may use the following command:

kubectl delete pod <pod-name> --namespace <namespace-name>

You can also pass a YAML file that contains the definition of the pod to kubectl delete command if you want to delete that pod.

kubectl delete -f <yaml-file-name>

After running the command, Kubernetes initiates the deletion of the specified pod. It may take a few moments for the container to fully terminate and remove the pod from the cluster. To delete the pod forcefully and immediately, you may use the following command:

kubectl delete pod <pod-name> --grace-period=0 --force

Viewing pod logs

To view the logs for a specific pod, you may use:

kubectl logs --tail=5 <pod-name> --container <container-name>

In this command, --container flag specifies the container whose logs you want to retrieve. This flag is optional if the pod only contains a single container. --tail flag specifies how many logs you want to retrieve. In this case, it is the 5 latest logs. The --tail flag is optional. When you don't specify the flag, the command retrieves all logs from the specified container. Here's an example:

shows the output of kubectl logs command

Getting a shell to a running container in a pod

You can use the following command to get a shell to a running container in a pod:

kubectl exec -it [pod-name] --container [container-name] -- /bin/bash

This example passes /bin/bash argument to the command. If the container does not have bash, you can pass the /bin/sh argument. The double dash -- separates the arguments passed to the command from the kubectl arguments.

You can omit the --container flag if the pod contains only one container.

Once the shell is open, we can interact with the container as if we were accessing it directly.

Conclusion

In this topic, you learned the following:

  • What a pod definition looks like, how to create a pod using both kubectl create -f and kubectl run command,
  • Advantages of using the --dry-run flag,
  • How to get a list of pods running in a specific namespace in a kubernetes cluster using kubectl get pods command. How to fetch the ip addresses of each pod for a given namespace with the -o wide flag,
  • How to delete a pod using kubectl delete pod command,
  • How to view logs from a specific container in a given pod using kubectl logs command,
  • How to get a shell to a specific running container in a given pod using kubectl exec -it command, which is useful for debugging purposes.
7 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo