deployment

Why do we need Deployment Objects in Kubernetes?

    In Kubernetes, Deployments will handle everything ReplicaSet does, plus additional features like RollOut, RollBack, and pause deployments. We have seen in the previous section that ReplicaSet will take care of PODS creation and monitoring to ensure the replicas count is the same as the desired count mentioned in the manifest file(yaml file).



How to create Deployments in Kubernetes?

Please download your code from my github link: deployment.yaml

The yaml is the same as the ReplicaSet yaml, but the Kind Object has a slight change. Instead of "ReplicaSet", the Kind object should be "Deployment." Because the ReplicaSet is the child object to the Deployment Object. Please refer to the above diagram. 

Please go through my "ReplicaSet" section, where I briefly explained the Yaml walkthrough.

deployment.yaml
--------------------
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploymentdemo
  labels:
    type: web
spec:
  replicas: 6
  selector:
    matchLabels:
      type: web1
  template:
    metadata:
      name: nginxpods
      labels:
        type: web1
    spec:
      containers:
      - image: iam7hills/learnkubernetes:podsdemo-1.0
        name: podsdemo

kubectl create -f deployment.yaml
deployment.apps/deploymentdemo created

To verify the deployment, just execute the following command

kubectl get rs
NAME                                        DESIRED   CURRENT       READY   AGE
deploymentdemo-6997bc7c5d       6                     6                 0           4s

In the above output, the READY status should also be 6, that means, you need to give some more time to reach the Ready State to become 6 as desired state like below.

kubectl get rs
NAME                                            DESIRED   CURRENT   READY   AGE
deploymentdemo-6997bc7c5d       6                     6                 6           43s

kubectl get pods
NAME                                                      READY   STATUS        RESTARTS     AGE
deploymentdemo-6997bc7c5d-chpr9   1/1         Running               0              15m
deploymentdemo-6997bc7c5d-fqsf7    1/1        Running                0              15m
deploymentdemo-6997bc7c5d-lfp96   1/1         Running                0              15m
deploymentdemo-6997bc7c5d-pqps9  1/1         Running                0              15m
deploymentdemo-6997bc7c5d-qqqlt   1/1         Running                0              15m
deploymentdemo-6997bc7c5d-wm45n1/1         Running               0              15m


kubectl get deployments
NAME                     READY       UP-TO-DATE       AVAILABLE   AGE
deploymentdemo       6/6                 6                        6               15m

Application will be available for testing only if the READY, UP-TO-DATE, and AVAILABLE all should match. Let us assume if your output as below, 

Error case Scenario:

Let us assume, I am the developer, who mistakenly updated the podsdemo-2.0 in the yaml, which is not even exist in the docker registry.

- image: iam7hills/learnkubernetes:podsdemo-2.0

kubectl get deployments
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deploymentdemo           5/6                 3                        5               24m

kubectl get pods
NAME                                                          READY       STATUS                     RESTARTS   AGE
deploymentdemo-6997bc7c5d-chpr9          1/1         Running                            0              27m
deploymentdemo-6997bc7c5d-lfp96           1/1         Running                            0              27m
deploymentdemo-6997bc7c5d-pqps9          1/1         Running                            0              27m
deploymentdemo-6997bc7c5d-qqqlt           1/1         Running                            0              27m
deploymentdemo-6997bc7c5d-wm45n        1/1         Running                            0              27m
deploymentdemo-75f6466945-bj4x4           0/1         ImagePullBackOff           0              3m26s
deploymentdemo-75f6466945-phhrw          0/1         ImagePullBackOff           0              3m25s
deploymentdemo-75f6466945-tcjnq             0/1         ImagePullBackOff           0              3m26s


What actually happens here? It tries to update the pods sequentially; the moment it sees the failure, it stops, and your application runs with 5 pods. In this case, your application is still available to the users with five pods running. That is the beauty of having pods deployed through Deployment objects. We will see this in detail in the next section, rollout strategy, and rollback.