Kubernetes

How to Create a Kubernetes Pod

Damian Igbe
Feb. 26, 2022, 3:57 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

What is a Kubernetes pod?

In this tutorial, you'll learn how to create a Kubernetes pod, but first let's know what a pod is.

  • A pod is a group of containers scheduled onto the same host.
  • Pods serve as units of scheduling, deployment, and horizontal scaling/replication.
  • Pods share fate, and share some resources, such as storage volumes and IP addresses

You can watch the video below:

How to create a Kubernetes pod

A single-container pod can be created with the run command. Multiple-container pods can be created with the create command. When the run command is used, the pod’s properties are specified with flags on the command line. Note that when a run command is used, a Deployment is created to monitor the pod. The Deployment watches for failed pods and will start up new pods as required to maintain the specified number. If you don’t want a deployment created, you can create the pod with the create command.

To see all the run options run:

$ kubectl run --help

To create a pod using the run command:

$ kubectl run NAME
 --image=image
 [--port=port]
 [--replicas=replicas]
 [--labels=key=value,key=value, ...]
--image=IMAGE (required) is the Docker container image to use for this container.
--port=PORT is the port to expose on the container. 
--replicas=NUM is the number of replicated pods to create. If not specified, one pod will be created. 
--labels=key=value specifies one or more labels to attach to the pod.
run attaches a label of the format run=NAME used by the Deployment to target the pods created by the command.

NAME (required) is the name of the container to create. This value is also applied as:

  • the name of the Deployment, and
  • the prefix of the pod name.

For example

$ kubectl run beans --image=nginx

Creates pod with ‘prefix-beans’ and a deployment called ‘beans

$ kubectl get pods -l run=beans

How to View a pod

To view a specific pod, use the kubectl get command:

$ kubectl get pod beans

To return the name of the node on which the pod is scheduled, use the -o wide option:

$ kubectl get pod beans -o wide

For more details about a pod, including events, use describe in place of get:

$ kubectl describe pod beans

To list all pods running on a cluster:

 $ kubectl get pods

How to Delete a pod

Since the pod created was being managed and monitored by the deployment, if you delete the pod without deleting the deployment, the deployment will create another pod to replace the deleted pod. To permanently delete the pod, first delete its Deployment.

 $ kubectl get deployment

Then, delete the Deployment:

 $ kubectl delete deployment beans

Conclusion

In this tutorial, you learned how to create a Kubernetes Pod. This is the starting point for everything Kubernetes. In the next tutorial, you will learn how to create multiple pods using the create command.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role