An introduction to Kubernetes pods
An introduction to Kubernetes pods
Discover what a pod whilst beginning to understand how and when you can create and use pods within your clusters.
What is a pod?
In this video, we'll be discussing pods, the smallest unit in Kubernetes. It is a group of one or more containers and a specification telling how the containers will be running inside the Kubernetes cluster.
On the right-hand side, you can see a YAML file. You can see that there is a YAML file, and in that file, there are a few essential points, like the API version. The API version of the pod is v1, and the kind is the object. We want to create a pod, and therefore the kind will be pod here. After kind comes the metadata, where we define the pod's name, labels, and other things. It is the label we want to use in the future, along with the pod's name. So, the pod will be created with the name called sample-pod.
Next comes the spec section. All the containers we want to define inside the pod come under the spec sections. You can have one or more containers over there. To keep the scenario simple, we are using a single container with the image nginx name as sample-pod-container with a containerPort 80.
How can we create a pod?
A pod will have its IP address, and all the containers inside it will share the same storage network and IP address. We will go through a demo to see that in detail. We already have a file that I just showed you. You can see that this is a sample file, and I do not have any pods running inside the cluster and the default namespace. Hence, we can create the pod by using the command
kubectl create -f sample-pod.yaml. We will supply the YAML file, and the pod will get created.
We can check the creation of the pod using
kubectl get pods, and we have a pod running. If we want to see additional details, we will use the
kubectl get pods -o wide command. It tells you the pod name, the status, the number of ready containers, the number of restarts that happen, the IP that it's assigned, and the node on which it's running. Then you have other readiness and the gates, if applicable, if specified in the YAML file, which we'll discuss in future lectures. Now, a pod can also be created using the imperative way. We will use the command
kubectl run demo --image=nginx --port=80 to create it through the imperative way. Again, a pod is created, and if we do not specify any namespace in the YAML file, it gets created in the default namespace. Now, if we use the
kubectl get pods command, we will see the two pods listed as the output. So, we have a sample pod created by the YAML file, and then I have a demo pod created using the imperative way.
Working with a pod
If we want to see the pod logs, we will use the
kubectl logs -f demo command. The
-f parameter helps follow the logs, and the demo is the pod's name. The output is just the logs that are coming off the container. If we want to view the events of the pod, then we'll use the describe command. Hence, the command will be
kubectl describe pod demo. The output will contain the description and the events. If anything goes wrong, the first thing that usually we see in the Kubernetes world is we describe the Kubernetes object. Then, we get to see the number of events. In the events, we can see the successful assigning of the node, successfully pulling the image along with creating the container and starting the container. All the things that are happening from the Kubelet scheduler come in the event section. We'll discuss other sections in the future videos, like the taints and annotations, the toleration node selectors, the keywords class, and even the readiness probes.
Next, what we have is if we want to delete the pod, we'll just simply use the
kubectl delete pod demo command. The pod gets deleted. Now, if I do
kubectl get pods, I'll see only one pod that is running. If we want to exec into the pod, just like the docker exec, we'll use the
kubectl exec -it [POD_NAME] bash command. With this, you can see I'm now inside the running pod.
For this lecture, we have seen a few of the commands to create a pod from the YAML file, creation through the imperative way, and how to interact with the pod. We have also seen the logs and how we can delete a pod, exec it, and describe it. Thanks for watching, see you in the next lecture.