Kubernetes: How to Create a deployment

In this article, I will show you how to create a straightforward Kubernetes deployment! to make things simple; the deployment will be…

Kubernetes: How to Create a deployment
Photo by Matt Artz on Unsplash

In this article, I will show you how to create a straightforward Kubernetes deployment! to make things simple; the deployment will be something straightforward, a pod that will ping 1.1.1.1 using the alpine image, then we will perform various operations like scaling, getting logs, deleting pods and the deployment. I assume that you already have a Kubernetes deployment.

What is a deployment?

A Kubernetes deployment is an object in the Kubernetes cluster that describes how to deploy an application. It defines the desired state of the application, such as the number of replicas, the container image to use, and the container ports to expose. The deployment object also manages the rollout and rollback of the application, ensuring that the desired state is maintained.

Create a deployment

To create a deployment with a single pod that will ping 1.1.1.1 enter the following

$ kubectl create deployment pingpong --image alpine -- ping 1.1.1.1 
deployment.apps/pingpong created

Get the status of the deployment and pods

To get the status of the deployment, we use the “get deployment” command

$ kubectl get deployments 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE 
pingpong   1/1     1            1           47s

This means that this deployment has one pod and is ready; we can verify this on the pod level using the “get pods” command

$ kubectl get pods 
NAME                        READY   STATUS    RESTARTS   AGE 
pingpong-6d8f458c56-f2bgl   1/1     Running   0          2m29s

Scale up the deployment

To scale up the deployment to three pods running in parallel, we use the “scale deployment” command

$ kubectl scale deployment/pingpong --replicas 3

We can verify that our deployment now has three pods!

$ kubectl get deployments 
NAME       READY   UP-TO-DATE   AVAILABLE   AGE 
pingpong   3/3     3            3           9m23s
$ kubectl get pods 
NAME                        READY   STATUS    RESTARTS   AGE 
pingpong-6d8f458c56-9x47c   1/1     Running   0          91s 
pingpong-6d8f458c56-bkk9r   1/1     Running   0          91s 
pingpong-6d8f458c56-f2bgl   1/1     Running   0          10m

What happen if i delete a pod of the deployment?

What do you believe will happen if I delete a pod from the deployment? let’s see

$ kubectl delete pod pingpong-6d8f458c56-9x47c 
pod "pingpong-6d8f458c56-9x47c" deleted

Now let’s get the status of the pods again

$ kubectl get pods 
NAME                        READY   STATUS        RESTARTS   AGE 
pingpong-6d8f458c56-9x47c   1/1     Terminating   0          3m 
pingpong-6d8f458c56-bkk9r   1/1     Running       0          3m 
pingpong-6d8f458c56-f2bgl   1/1     Running       0          12m 
pingpong-6d8f458c56-rcsv2   1/1     Running       0          8s

We can see that the pod we are deleting is in the terminating phase, and another one has been created in the last 8seconds; this is happening because we have instructed Kubernetes, no matter what, always to have a deployment with three pods.

How to get logs from pods and deployments

To view the logs from a single pod, we need to know its name and then use the logs <pod_name> -f tail -1 command; if you are familiar with Linux, you probably know that the tail -f commands prints the log since the last line and then after, -f tail -1 achieves the same functionality

$ kubectl get pods 
NAME                        READY   STATUS    RESTARTS   AGE 
pingpong-6d8f458c56-bkk9r   1/1     Running   0          7m33s 
pingpong-6d8f458c56-f2bgl   1/1     Running   0          16m 
pingpong-6d8f458c56-rcsv2   1/1     Running   0          4m41s 
 
kpatronas@nautilus:~$ kubectl logs pingpong-6d8f458c56-bkk9r -f --tail 1 
64 bytes from 1.1.1.1: seq=454 ttl=37 time=9.490 ms 
64 bytes from 1.1.1.1: seq=455 ttl=37 time=9.224 ms 
64 bytes from 1.1.1.1: seq=456 ttl=37 time=9.344 ms 
64 bytes from 1.1.1.1: seq=457 ttl=37 time=11.733 ms

In case you want the full log, you can enter logs <pod_name>; thiswill print until the last line and will exit, it will not wait for new log lines to be appended

kubectl logs pingpong-6d8f458c56-bkk9r 
64 bytes from 1.1.1.1: seq=627 ttl=37 time=10.917 ms 
64 bytes from 1.1.1.1: seq=628 ttl=37 time=17.263 ms 
64 bytes from 1.1.1.1: seq=629 ttl=37 time=9.081 ms 
64 bytes from 1.1.1.1: seq=630 ttl=37 time=14.082 ms 
64 bytes from 1.1.1.1: seq=631 ttl=37 time=13.970 ms 
* 
* 
* 
64 bytes from 1.1.1.1: seq=641 ttl=37 time=13.032 ms 
$

To get the logs from deployment is somehow misleading, you dont get logs from all the pods but just from some of the pods of the deployment

$ kubectl logs -f deployment/pingpong --tail 1 
Found 3 pods, using pod/pingpong-6d8f458c56-f2bgl 
64 bytes from 1.1.1.1: seq=1406 ttl=37 time=10.273 ms 
64 bytes from 1.1.1.1: seq=1407 ttl=37 time=11.746 ms 
64 bytes from 1.1.1.1: seq=1408 ttl=37 time=9.489 ms 
64 bytes from 1.1.1.1: seq=1409 ttl=37 time=9.582 ms 
64 bytes from 1.1.1.1: seq=1410 ttl=37 time=10.793 ms 
64 bytes from 1.1.1.1: seq=1411 ttl=37 time=11.257 ms

How to delete a deployment

To delete a deployment we need to use the “delete deployment” command

$ kubectl delete deployment/pingpong 
deployment.apps "pingpong" deleted

After a while if you check for deployments and pods you should not see any!

$ kubectl get deployment/pingpong 
Error from server (NotFound): deployments.apps "pingpong" not found 
$ kubectl get pods 
No resources found in default namespace.

Conclusion

In this article, we saw how to make a simple deployment, how to scale it, and how to manage it; of course, there are many things to cover, and the production way to do things is using YAML files, but I think for tutorial purposes not using YAML can make things much easier to understand.

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…