Kubernetes Deployment

K8s Pods

Suppose you have a Kubernetes cluster set up on your machine , lets assume of nginx. You create a Pod using YAML and it looks like this :

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

You give the command to create a pod :

 kubectl apply -f pod.yaml

and it says the pod has been created .

To list the pods use the command :

kubectl get pods

And it will list the pods .

Everything is working Perfectly Fine.

Now if u run :

kubectl delete pod <pod-name>

It will delete the pod .

Now we know that Kubernetes is a container orchestration platform which orchestrates and manages the containers, Then why did it delete the pod ?

The answer is We have to create the right kind of resource ,which is called Deployment .

K8s Deployment

We will take the same nginx example to show what it's different from a pod.

Here's what a deployment looks like:

 apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

In YAML file we create a Deployment kind instead of Pod , we give a label and number of replicas.

The replicas define how much number of replicas we want and that much containers are always up and running . The replicasets are a kubernetes controller which implemets the autohealing process . The controller is something which ensures that the actual state specified in yaml manifest is same as the desired state .

Selector field defines how crated replicasets finds which pods to manage.matchLabels field is a map of {key,value} pairs .Which should satisfy some conditions in order to match the pairs .

Now,Create a deployment :

 kubectl apply -f deployment.yaml

When we create a deployment ,we are actually creating a pod which has a replicaset.

Now list the pods and the deployments you will see the nginx is a pod as well as a deployment.

Now ,delete the pod :

kubectl delete pod deployment.yaml

and simultaneously watch for the pods .

kubectl get pod -w

you will see that when the pod is getting deleted and simultaneously replicaset is creating another pod . And it ensures that the replicas defined in the yaml manifests are always up.

Diff between Pods and Deployments .

Pod

Deployment

1.Pod is not controlled by the Controller Manager.

1.Deployment is controlled by the controller Manager .

2.Pod can be deleted by the delete pod command .

2.Deployment cannot be deleted by the delete pod command .

3.Pod cannot be called as a Deployment.

3.Deployment is also a Pod and a Deployment.

4.Pods are managed by the replicasets of the controller Manager.

4.Deployment has a replica set which controls the pods .

Diagram

Here's a diagram to give more detailed overview of a Deployment:

In this way , Deployment Satisfies the condition of AutoHealing

Deployment also satisfies the condition of AutoScaling using Horizontal Pod AutoScaler(HPA).

There are many other ways That proves , Deployment is Always Better than creating a pod .

You can also Create a Pod for some Temporary Usage but its always better to use a Deployment.

You can Know more about Kubernetes Deployment on K8s Docs ..

Thanks For Reading ...

Credits to - Abhishek Veeramalla

Connect with me on Twitter

0
Subscribe to my newsletter

Read articles from Atharva Kulkarni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Atharva Kulkarni
Atharva Kulkarni

Exploring Devops,Curious About Opensource , Learning contantly ,