What Is Deployment In Kubernetes
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed ReplicaSet in Kubernetes.
https://cloudaffaire.com/what-is-replicaset-in-kubernetes/
In this blog post, we will discuss Deployment which provides declarative updates for Pods and ReplicaSets.
What is Deployment?
A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.
Deployments represent a set of multiple, identical Pods with no unique identities. A Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. In this way, Deployments help ensure that one or more instances of your application are available to serve user requests. Deployments are managed by the Kubernetes Deployment controller.
Deployments use a Pod template, which contains a specification for its Pods. The Pod specification determines how each Pod should look like: what applications should run inside its containers, which volumes the Pods should mount, its labels, and more. When a Deployment’s Pod template is changed, new Pods are automatically created one at a time.
Deployments are well-suited for stateless applications that use ReadOnlyMany or ReadWriteMany volumes mounted on multiple replicas, but are not well-suited for workloads that use ReadWriteOnce volumes. For stateful applications using ReadWriteOnce volumes, use StatefulSets. StatefulSets are designed to deploy stateful applications and clustered applications that save data to persistent storage, such as Compute Engine persistent disks. StatefulSets are suitable for deploying Kafka, MySQL, Redis, ZooKeeper, and other applications needing unique, persistent identities and stable hostnames.
Deployment manifest file syntax:
1 2 3 4 5 6 7 8 9 10 |
## ---------------------- ## Deployment YAML syntax ## ---------------------- apiVersion: apps/v1 kind: Deployment metadata: spec: |
deploymentMetadata attributes:
- annotations: Key value map stored with a resource for arbitrary metadata.
- clusterName: The name of the cluster which the object belongs to.
- creationTimestamp: Timestamp representing the server time when this object was created.
- deletionGracePeriodSeconds: Seconds allowed for gracefully terminate before forcefully removed from the system.
- deletionTimestamp: RFC 3339 date and time at which this resource will be deleted. Is not directly settable by a client.
- finalizers: Must be empty before the object is deleted from the registry.
- generateName: Optional prefix, used by the server, to generate a unique name ONLY IF the Name field has not been provided.
- generation: A sequence number representing a specific generation of the desired state. Populated by the system. Read-only.
- initializers: An initializer is a controller which enforces some system invariant at object creation time.
- labels: Map of string keys and values that can be used to organize and categorize (scope and select) objects.
- name: Name must be unique within a namespace.
- namespace: Namespace defines the space within each name must be unique, default namespace is default.
- ownerReferences: List of objects depended by this object.
- resourceVersion: An opaque value that represents the internal version of this object used to determine when objects have changed.
- selfLink: SelfLink is a URL representing this object. Populated by the system. Read-only.
- uid: UID is the unique in time and space value for this object
deploymentSpec attributes:
- minReadySeconds: Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0
- paused: Indicates that the deployment is paused.
- progressDeadlineSeconds: The maximum time in seconds for a deployment to make progress before it is considered to be failed. The deployment controller will continue to process failed deployments and a condition with a ProgressDeadlineExceeded reason will be surfaced in the deployment status. Note that progress will not be estimated during the time a deployment is paused. Defaults to 600s.
- replicas: Replicas is the number of desired replicas. This is a pointer to distinguish between explicit zero and unspecified. Defaults to 1.
- selector: Selector is a label query over pods that should match the Replicas count. If Selector is empty, it is defaulted to the labels present on the Pod template. Label keys and values that must match in order to be controlled by this replication controller, if empty defaulted to labels on Pod template.
- strategy: The deployment strategy to use to replace existing pods with new ones.
- template: Template is the object that describes the pod that will be created if insufficient replicas are detected.
Deployment Demo:
Step 1: Create a deployment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
############################## ## Deployment In Kubernetes ## ############################## ## Prerequisites ## One Kubernetes Cluster with atleast one node ## You can install your own or use online versions using below links ## https://cloudaffaire.com/how-to-install-kubernetes-cluster-in-linux/ ## https://www.katacoda.com/courses/kubernetes/playground ## ----------------- ## Create deployment ## ----------------- ## Create the deployment manifest file mkdir myapp && cat < apiVersion: apps/v1 kind: Deployment metadata: name: mydeployment labels: app: myapp spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - image: nginx:1.14.2 name: mycontainer ports: - containerPort: 80 name: http EOF ## Note: for evaluation of multiple lebels, use matchExpressions instead of matchLabels. ## For simplicity, we are using a single lebel app: myapp ## Validate the yaml configuration file kubectl create --dry-run --validate -f myapp/mydeploymentv1.yaml ## Create the deployment kubectl apply -f myapp/mydeploymentv1.yaml --record=true |
Step 2: View deployment details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
## ----------------------- ## View deployment details ## ----------------------- ## View the deployment kubectl get deployment mydeployment kubectl get deploy ## abbreviation of deployment kubectl get deploy -l app=myapp ##list deploy by lebel kubectl get deploy -o wide ##get detailed output ## View deploy configuration kubectl get deploy mydeployment -o yaml ## View deploy details kubectl describe deploy mydeployment ## View deploy lebels kubectl get deploy --show-labels ## View replicaset kubectl get rs -l 'app in (myapp)' ## NAME DESIRED CURRENT READY AGE ## mydeployment-789b58897b 3 3 3 37s ## View the pod kubectl get pod -l app=myapp -o wide kubectl get pods -l 'app in (myapp)' -o wide ## NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES ## mydeployment-789b58897b-424ds 1/1 Running 0 85s 10.244.2.33 system2 ## mydeployment-789b58897b-gqvsb 1/1 Running 0 85s 10.244.2.34 system2 ## mydeployment-789b58897b-sr7xs 1/1 Running 0 85s 10.244.1.25 system3 |
Step 3: Update your deployment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
## ---------------------- ## Update your deployment ## ---------------------- ## Update your deployment image configuration kubectl set image deployment/mydeployment mycontainer=nginx:1.16.1 --record ## Get the rollout status kubectl rollout status deployment.v1.apps/mydeployment ## Waiting for deployment "mydeployment" rollout to finish: 1 out of 3 new replicas have been updated... ## Waiting for deployment "mydeployment" rollout to finish: 1 out of 3 new replicas have been updated... ## Waiting for deployment "mydeployment" rollout to finish: 1 out of 3 new replicas have been updated... ## Waiting for deployment "mydeployment" rollout to finish: 2 out of 3 new replicas have been updated... ## Waiting for deployment "mydeployment" rollout to finish: 2 out of 3 new replicas have been updated... ## Waiting for deployment "mydeployment" rollout to finish: 2 out of 3 new replicas have been updated... ## Waiting for deployment "mydeployment" rollout to finish: 1 old replicas are pending termination... ## Waiting for deployment "mydeployment" rollout to finish: 1 old replicas are pending termination... ## deployment "mydeployment" successfully rolled out ## View replicaset kubectl get rs -l 'app in (myapp)' ## NAME DESIRED CURRENT READY AGE ## mydeployment-778dbbb45f 3 3 3 2m31s ## mydeployment-789b58897b 0 0 0 8m7s ## View the pod kubectl get pod -l app=myapp -o wide kubectl get pods -l 'app in (myapp)' -o wide ## NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES ## mydeployment-778dbbb45f-knhzp 1/1 Running 0 3m12s 10.244.1.26 system3 ## mydeployment-778dbbb45f-rz4g9 1/1 Running 0 2m48s 10.244.1.27 system3 ## mydeployment-778dbbb45f-sr4x5 1/1 Running 0 2m45s 10.244.2.35 system2 ## View deploy details kubectl describe deploy mydeployment |
Step 4: Rolling back a deployment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## ------------------------- ## Rolling Back a Deployment ## ------------------------- ## get deployment revisions kubectl rollout history deployment.v1.apps/mydeployment ## REVISION CHANGE-CAUSE ## 1 kubectl apply --filename=myapp/mydeploymentv1.yaml --record=true ## 2 kubectl set image deployment/mydeployment mycontainer=nginx:1.16.1 --record=true ## View deployment revisions details kubectl rollout history deployment.v1.apps/mydeployment --revision=1 kubectl rollout history deployment.v1.apps/mydeployment --revision=2 ## Rollback changes to this deployment kubectl rollout undo deployment.v1.apps/mydeployment #or kubectl rollout undo deployment.v1.apps/mydeployment --to-revision=1 ## View deploy details kubectl describe deploy mydeployment |
Step 5: Scale up and scale down number of replicas controlled by this deployment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## ------------------------------------------ ## Scale up and scale down number of replicas ## ------------------------------------------ ## Scale up number of pods to three kubectl scale deployment.v1.apps/mydeployment --replicas=5 ## View pods kubectl get pods -l 'app in (myapp)' -o wide ## Scale down number of pods to one kubectl scale deployment.v1.apps/mydeployment --replicas=2 ## View pods kubectl get pods -l 'app in (myapp)' -o wide |
Step 6: Make a connection to the pod managed by deployment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## ------------------------------------- ## Connect to your pods under replicaset ## ------------------------------------- ## Expose your rs kubectl expose deployment mydeployment --type=NodePort --port=80 ## Get pod ip address kubectl get pods -l 'app in (myapp)' -o wide ## NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES ## mydeployment-789b58897b-6wv2g 1/1 Running 0 14m 10.244.2.42 system2 ## mydeployment-789b58897b-8twsn 1/1 Running 0 14m 10.244.1.34 system3 ## Call your pod uisng pod ip address (nginx web server) curl 10.244.2.42:80 curl 10.244.1.34:80 |
Step 7: Cleanup.
1 2 3 4 5 6 7 8 9 10 11 12 |
## ------- ## Cleanup ## ------- ## Delete the nodeport service kubectl delete service mydeployment ## Delete the deployment kubectl delete deployment mydeployment ## delete myapp directory rm -rf myapp |
Hope you enjoyed this article. In real world sceanrios we hardly deploy a pod directly. Instead pods are deployed using pod controller. In the next blog post, we will discuss one of the pod controller services.
To get more details on Kubernetes you can follow the below link.
https://kubernetes.io/docs/home/
To get more details on Docker you can follow the below links.
https://cloudaffaire.com/category/devops/docker/