Kubernetes pods, It's all you know about.

Yashod Perera
5 min readSep 21, 2021

--

Photo by Michael Jasmund on Unsplash

Pod is the smallest instance in Kubernetes that you are working with. This post will cover everything you need to learn about pods and how to deploy it.

What is a pod why not containers?

We use pods in k8s to deploy our application. Someone may ask why not to use containers in k8s and the answer is there are many types of containers such as docker, podman, LUX, containerd and many more hence pod act as a wrapper. We can have multiple containers inside the pod and most of the cases those might be supporting containers.

Let’s deploy a nginx server inside the cluster using kubectl run nginx --image=nginx . We can check the pod using kubectl get pods command.

But as a practice we use a file to deploy anything in k8s because in production environments we need to simplify the process. Following is a sample pod deployment file.

Any deployment file have 4 main fields which are,

  • apiVersion — define which k8s version that we are using. Ex — For pods we are using v1 and for Replicaset we use apps/v1
  • kind — What is the component that we are specifying in the file. Ex — Can be any component such as pod, service, replicaset, job etc.
  • metadata — This will include the meta information for the pod such as name or any additional information which can be mention under label.
  • spec — Component specification fall under this section and for the pod it define what is the image that we are using what ports are exposed.

Above pod can be deployed using kubectl apply -f pod-nginx-definition.yaml. After deploying we can get the pod information using kubectl describe pod nginx-server and it output following information.

All the pod related data is visualised here.

If you need to remove the pod you created you can simply do that by kubectl delete pods nginx-server .

Please read kubernetes architecture post if you are not aware of it.

Replica-set

In k8s we usually deploy multiple instances of the same application which is known as replication. Replicas are made using replica sets in k8s and replica set is responsible of creating given number of replicas and maintain the number of replicas (If one crash replica set will create another pod).

Replica set help to provide high availability and load balancing. Following is a nginx replicaset which have 3 replicas.

  • apiVersion of the replicaset is apps/v1 .
  • kind is replicaset itself.
  • metadata are name and labels and labels can be any key value pairs.
  • In the spec there are 3 sections to be mentioned which are, replicas — number of instances that you need, selector — identify which pods are in the replicaset, template — define the pod.

Deployment of the replicaset can be done using kubectl apply -f <filename>.

Important

Selector plays a major role, it tracks all the pods with the given label (In our case type=test-prod) and make sure there are corresponding number of replicas are available.

Ex — If there are already two pods which have the label type=test-prod then after apply above replica it will only create a one pod since the requirement is to have 3 and 2 are already there.

// to get the replicaset
kubectl get replicasets
// output
NAME DESIRED CURRENT READY AGE
nginx-server 3 3 3 15h
// to get pods
kubectl get pods
//output
NAME READY STATUS RESTARTS AGE
nginx-server-56c8v 1/1 Running 0 15h
nginx-server-62t78 1/1 Running 0 15h
nginx-server-zz4k2 1/1 Running 0 15h
// to delete replicaset
kubectl delete replicaset nginx-server
// If you need to increase the number of replicas. Update the file and,
kubectl replace -f file.yaml
// easy way of scale but this will not change the file.
kubectl scale — replicas=6 replicaset replicasetName

Deployment

End of the day what you need to be done a deployment which can be done using k8s deployments. Deployment supports,

  • rolling updates — default deployment strategy.
  • recreate strategy.
  • rollbacks — undo to the last revision.

Let’s do a simple deployment and do some deployment updates and undo it.

Let’s apply the deployment file using kubectl apply -f nginx-first-deployment.yaml and you can view all the pods, replicasets and deployment created using kubectl get all.

// output
NAME READY STATUS RESTARTS AGE
pod/nginx-server-668687b897-hv6s9 1/1 Running 0 14s
pod/nginx-server-668687b897-wclxx 1/1 Running 0 14s
pod/nginx-server-668687b897-zhv69 1/1 Running 0 14s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-server 3/3 3 3 14s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-server-668687b897 3 3 3 14s

Once you create a deployment there is a revision made for that which can be check using kubectl rollout history deployment/<deployment-name>.

kubectl rollout history deployment/nginx-server// output
deployment.apps/nginx-server
REVISION CHANGE-CAUSE
1 <none>

Let’ change the image and do the deployment.

// to change the image of the deployment
kubectl set image deployment/nginx-server nginx=nginx:perl --record
// change again
kubectl set image deployment/nginx-server nginx=nginx:1.16 --record
// let's see the history
kubectl rollout history deployment/nginx-server
// output
deployment.apps/nginx-server
REVISION CHANGE-CAUSE
1 kubectl set image deployment/nginx-server nginx=nginx:perl
--record=true
2 kubectl set image deployment/nginx-server nginx=nginx:1.16
--record=true

The replica update will done using rolling update which create a pod and remove existing to help zero downtime.

If you need to undo your deployment you can simply do that using kubectl rollout undo deployment/<deployment-name>.

kubectl rollout undo deployment/nginx-server// output
deployment.apps/nginx-server rolled back
// check the history
kubectl rollout history deployment/nginx-server
// output
deployment.apps/nginx-server
REVISION CHANGE-CAUSE
2 kubectl set image deployment/nginx-server nginx=nginx:1.16
--record=true
3 kubectl set image deployment/nginx-server nginx=nginx:perl
--record=true

As you can see the image is rollback to perl image and the first revision is removed and add a new revision for the rollback to keep track of the deployments.

Hope you got a basic understanding of k8s pods, replicasets and deployments. In the next post will talk about services and access our nginx server.

If you have found this helpful please hit that 👏 and share it on social media :)

--

--

Yashod Perera
Yashod Perera

Written by Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor

No responses yet