Understanding Deployments
- The Deployment is the standard for running applications in Kubernetes
- It offers features that add to the scalability and reliability of the application
- Offers scalability scaling the number of application instances
- Updates and Update Strategy: zero-downtime application updates
- It protects Pods and will automatically restart them if anything goes wrong
- Use
kubectl create deploy
to create Deployments - Replication Controller & Replica Set is not able to do update & Rollback apps in the cluster.
- We have a restart policy but that's for containers not for pods. So if we delete a pod it's gone not gonna start again.
For Exmaple
kubectl create deployment --image=nginx --replicas=3
The default label will be the name of deployment itself.
Overview
- Deployment object act as a supervisor for pods, giving you free-grained control over how and when a new pod is rolled out, updated or rolled back to a previous state.
- When using deployment object, we first define the state of the app, then Kubernetes cluster schedules mentioned app instance on the specific nodes
- Kubernetes then monitors, if the node hosting an instance goes down or pod is deleted, the deployment controller replaces it.
- This provides a self hosting mechanism to address machine failure or maintenance.
Use cases of Deployment object
- Create deployment to roll out a replica set - the replica set creates pods in the background. Check the status of the rollout to see if it succeeds or not
- Declare the new state of the pods - By updating the podTemplateSpec of the deployment. A new replica set is created and the deployment manages moving the pods from the old replica set to the new one at a controlled rate. Each new replica set update the revision of the deployment.
- Rollback to an earlier deployment revision - If the current state of the deployment is not stable. Each rollback updates the revision of the deployment
- Scale up the deployment to facilitates more load.
- Pause the deployment to apply multiple fixes to its podTemplateSpec and then resume it to start a new rollout.
- Clean up older ReplicaSets that you don't need anymore
- If there are problems in the deployment, Kubernetes will automatically roll back to the previous version, however you can also explicitly rollback to a specific revision as in our case to revision 1 (the original pod version).
Deployment Failure cases
Your deployment may get stuck trying to deploy its newest ReplicaSet without ever completing. This can occur due to some of the following factors -
- Insufficient quota
- Readiness probe failure
- Image pull error
- Insufficient permission
- Limit ranges
- Application runtime missing
Implementation
-
You can rollback to a specific version by specifying it with
--to-revision
-
For example -
kubectl rollout undo deploy/my-deployment --to-revision=2
The name of the replicaset and pod is always formatted as [DEPLOYMENT_NAME]-[REPLICASET (Random string)]-[POD-NAME (Random string)]
- When you display the inspect of deployment the following fields are displayed -
Field | Description |
---|---|
NAME | List the names of the deployment in the namespace |
READY | Display how many replicas of application are available to your users if follows the pattern Ready/Desired |
UP TO DATE | Display the number of replicas that have been updated to achieve the desired state |
AVAILABLE | Display how many replicas of the application are available to your users |
AGE | Display the amount of time that the application has been running |
Example of Deployment8
kind: Deployment
apiVersion: apps/v1
metadata:
name: example-9
spec:
replicas: 2
selector:
matchLabels:
name: xander
template:
metadata:
name: container0
labels:
name: xander
spec:
containers:
- name: c00
image: ubuntu
command: [
# Use a list for the command
"/bin/bash",
"-c",
"while true; do echo Hello-from-ubuntu; sleep 5 ; done",
]
Create a deployment
kubectl apply -f k8s_example_9.yml
controlplane $ kubectl apply -f k8s_example_9.yml
deployment.apps/example-9 created
Details of deployment
kubectl describe deployment example-9
controlplane $ kubectl describe deployment example-9
Name: example-9
Namespace: default
CreationTimestamp: Wed, 16 Oct 2024 17:07:03 +0000
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: name=xander
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: name=xander
Containers:
c00:
Image: ubuntu
Port: <none>
Host Port: <none>
Command:
/bin/bash
-c
while true; do echo Hello-from-Ubuntu; sleep 5 ; done
.
.
NewReplicaSet: example-9-6ddd44564c (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set example-9-6ddd44564c to 2
Details of replicaset created by deployment
kubectl get rs
controlplane $ kubectl get rs
NAME DESIRED CURRENT READY AGE
example-9-6ddd44564c 2 2 2 18m
Details of pods created by deployment in the replicaset
kubectl get pods
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-9-6ddd44564c-c62ph 1/1 Running 0 19m
example-9-6ddd44564c-djbcd 1/1 Running 0 19m
Scaling using deployment
kubectl scale --replicas=1 deployment example-9
kubectl get deployment
kubectl get rs
kubectl get pods
controlplane $ kubectl scale --replicas=1 deployment example-9
deployment.apps/example-9 scaled
controlplane $ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
example-9 1/1 1 1 22m
controlplane $ kubectl get rs
NAME DESIRED CURRENT READY AGE
example-9-6ddd44564c 1 1 1 22m
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-9-6ddd44564c-c62ph 1/1 Running 0 22m
example-9-6ddd44564c-djbcd 1/1 Terminating 0 22m
Verify the OS of the pod
kubectl exec -it example-9-6ddd44564c-djbcd -- /bin/bash
controlplane $ kubectl exec -it example-9-6ddd44564c-djbcd -- cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
.
.
.
Trying Rollback
Let's create a new revision by changing the image type in the manifest. So, that we will have two versions of the same deployment and then try to rollout and rollback to previous version
kind: Deployment
apiVersion: apps/v1
metadata:
name: example-9
spec:
replicas: 2
selector:
matchLabels:
name: xander
template:
metadata:
name: container0
labels:
name: xander
spec:
containers:
- name: c00
image: centos
command: [
# Use a list for the command
"/bin/bash",
"-c",
"while true; do echo Hello-from-CentOS; sleep 5 ; done",
]
Create a deployment
kubectl apply -f k8s_example_9.yml
controlplane $ kubectl apply -f k8s_example_9.yml
deployment.apps/example-9 created
Details of deployment
kubectl describe deployment example-9
Notice the changes compare the highlighted changes with the first revision
controlplane $ kubectl describe deployment example-9
Name: example-9
Namespace: default
CreationTimestamp: Wed, 16 Oct 2024 17:07:03 +0000
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 2
Selector: name=xander
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: name=xander
Containers:
c00:
Image: centos
Port: <none>
Host Port: <none>
Command:
/bin/bash
-c
while true; do echo Hello-from-CentOS; sleep 5 ; done
Environment: <none>
Mounts: <none>
Volumes: <none>
Node-Selectors: <none>
Tolerations: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: example-9-6ddd44564c (0/0 replicas created)
NewReplicaSet: example-9-6755695d56 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 31m deployment-controller Scaled up replica set example-9-6ddd44564c to 2
Normal ScalingReplicaSet 3m56s deployment-controller Scaled up replica set example-9-6ddd44564c to 2 from 1
Normal ScalingReplicaSet 3m56s deployment-controller Scaled up replica set example-9-6755695d56 to 1
Normal ScalingReplicaSet 3m48s (x2 over 9m10s) deployment-controller Scaled down replica set example-9-6ddd44564c to 1 from 2
Normal ScalingReplicaSet 3m48s deployment-controller Scaled up replica set example-9-6755695d56 to 2 from 1
Normal ScalingReplicaSet 3m34s deployment-controller Scaled down replica set example-9-6ddd44564c to 0 from 1
Details of replicaset created by deployment
kubectl get rs
controlplane $ kubectl get rs
NAME DESIRED CURRENT READY AGE
example-9-6755695d56 2 2 2 47s
example-9-6ddd44564c 0 0 0 28m
Details of pods created by deployment in the replicaset
kubectl get pods
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-9-6755695d56-vxkkq 1/1 Running 0 67s
example-9-6755695d56-xrm46 1/1 Running 0 59s
Verify the OS of the pod
kubectl exec -it example-9-6755695d56-vxkkq -- /bin/bash
controlplane $ kubectl exec -it example-9-6755695d56-vxkkq -- cat /etc/os-release
NAME="CentOS Linux"
VERSION="8"
.
.
.
Now check for revision
kubectl rollout status deployment example-9
kubectl rollout history deployment example-9
controlplane $ kubectl rollout status deployment example-9
Waiting for deployment "example-9" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "example-9" rollout to finish: 1 old replicas are pending termination...
deployment "example-9" successfully rolled out
controlplane $ kubectl rollout history deployment example-9
deployment.apps/example-9
REVISION CHANGE-CAUSE
1 <none>
2 <none>
Now rollback to previous version and check for the pods OS version
kubectl rollout undo deployment example-9
controlplane $ kubectl rollout undo deployment example-9
deployment.apps/example-9 rolled back
The current revision is 2 (can be verified from kubectl describe deployment example-9
command)
At the time of rollback the number of pods will be the number of desired state mentioned in manifest, even if you have scaled down (manually) in the previous version
Understanding Scalability
- In very old versions of Kubernetes, the Deployment did not exist
- To manage scalability, the ReplicaSet resource was used With the introduction of the apps API extension, the Deployment was introduced
- From that moment, scalability was managed in the Deployment and no longer in the ReplicaSet
- To manage scalability however, the Deployment creates a ReplicaSet
- Do NOT manage ReplicaSets directly, manage through the Deployment only
Manually managing scalability
- Use
kubectl scale deployment my-deployment --repIicas=4
to scale the number of currently running replicas - Alternatively, use
kubectl edit deployment my-deployment
to edit the number of replicas manually -Notice that kubectl edit allows modification of a limited number of options only - To start a deployment with a specific number of replicas, use
kubectl create deploy ... --replicas=3
Commands
Command | Description |
---|---|
kubectl get deployment | To Check deployment was created or not |
kubectl describe deployment example-9 | To check, how deployment create replica set & pods |
kubectl get rs | List all replica sets in the current namespace. |
kubectl scale --replicas=1 deployment example-9 | To scale up or scale down |
kubectl logs -f POD_NAME | View the logs of a specific pod. |
kubectl rollout status deployment example-9 | Check the status of the deployment's rollout process |
kubectl rollout history deployment example-9 | View the history of revisions for the specified deployment. |
kubectl rollout undo deployment example-9 | Roll back the deployment to the previous revision. |
kubectl rollout undo deployment example-9 --to-revision=2 | Roll back the deployment to the specific revision. |