Sunday 26 July 2020

Kubernetes: Deployments

Deployment is a Kubernetes object, it provides declarative updates for the Pods and ReplicaSets.

 

Deployment will take care of the following features.

a.   Scalability

b.   Deployment Strategies

c.    Make sure that the desired number of Pods are always available.

 

Deployment uses the labels to check the availability of a Pod.

 

For example, you may want to replace all the Pods in a ReplicaSet with a new version of the image, you can do this via Deployment Object. You may want to rollback all your changes, you can do this via Deployment Object.

Using the Deployment object, we can perform rolling updates of the application seamlessly (Zero downtime for the applications). In real-world applications, you do not create a Pod and ReplicaSet definition file. You create a deployment definition file only, which in turn create replica sets and Pods

 

Let’s create a deployment.

 

Step 1: Create ‘employeeServiceDeployment.yml’ file.

 

employeeServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

template:

It has the Pod specification.

 

replicas: 5

Specifies the number of copies of Pod to run. Here 5 Pods of the application ‘jboss/wildfly’ will run.

 

selector:

    matchLabels:

      app: employee-service

Kubernetes use matchLabels to identify how labels are identified against a Pod.


Step 2: Create a deployment from the definition file.

$kubectl create -f employeeServiceDeployment.yml 
deployment.apps/employee-service-deployment created

Step 3: List all the deployments.

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   3/5     5            3           15s

As you see the output, deployment with name ‘employee-service-deployment’ is created and 3 Pods out of 5 are up and running.

 

Wait for some time and list deployments again. You will see that all the Pods are up and running.

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           72s

Deployment automatically creates a replicaset. To confirm this, list all the replicasets.

$kubectl get replicaset
NAME                                    DESIRED   CURRENT   READY   AGE
employee-service-deployment-dff6c9cdf   5         5         5       3m15s

Query Pods.

$kubectl get pods
NAME                                          READY   STATUS    RESTARTS   AGE
employee-service-deployment-dff6c9cdf-2wrg9   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-8m7cr   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-kv9ck   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-pbfpr   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-s4nh4   1/1     Running   0          2m6s

Step 4: To know more information about the deployment, execute the command ‘kubectl describe deployment’.

$kubectl describe deployment
Name:                   employee-service-deployment
Namespace:              default
CreationTimestamp:      Fri, 05 Jun 2020 00:02:53 +0530
Labels:                 app=employee-service-deployment
                        author=krishna
                        serviceType=webservice
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=employee-service
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=employee-service
           author=krishna
           serviceType=webservice
  Containers:
   employee-service-container:
    Image:        jboss/wildfly
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   employee-service-deployment-dff6c9cdf (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled up replica set employee-service-deployment-dff6c9cdf to 5

Step 5: You can delete a deployment by executing the below command.

 

kubectl delete deployment {deploymentName}

$kubectl get deployment
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           13m
$
$kubectl delete deployment employee-service-deployment
deployment.apps "employee-service-deployment" deleted
$
$kubectl get deployment
No resources found in default namespace.



Previous                                                    Next                                                    Home

No comments:

Post a Comment