A Kubernetes Deployment is a crucial resource object that provides a declarative approach to managing application updates, rollbacks, and scaling. Deployments allow you to define an application's lifecycle, ensuring your desired state is always maintained by controlling the creation and replacement of Pods.
Step 1: Creating Your First Deployment
Let’s begin by setting up a simple Deployment that runs an Nginx web server.
Write the Deployment YAML Configuration
First, create a YAML file named deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.17
ports:
- containerPort: 80
Apply the Deployment
To create the Deployment, run:
kubectl apply -f deployment.yaml
Once applied, you can verify the status of your Deployment, ReplicaSets, and Pods:
kubectl get deploy
kubectl get rs
kubectl get pod
To check the current Nginx version running in a specific Pod, use:
kubectl describe pod <podname>
Step 2 : Updating the Deployment
With your Deployment up and running, let's update the Nginx version from nginx:1.17
to nginx:1.18
.
Open the deployment.yaml
file and update the Nginx image tag:
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.18
ports:
- containerPort: 80
Apply the updated Deployment:
kubectl apply -f deployment.yaml
Verify the updated Deployment and Pods:
kubectl get deploy
kubectl get pod
To confirm the Nginx version has been updated:
kubectl describe pod <podname>
Step 3 : Rolling Back the Deployment
Kubernetes makes it easy to roll back to a previous version of your Deployment.
Listing ReplicaSets:
Identify the ReplicaSet associated with the version you want to roll back to:
kubectl get rs
Performing the Rollback
To roll back to the previous version:
kubectl rollout undo deployment/my-deployment
Verify the rollback by checking the ReplicaSets and Pods:
kubectl get rs
kubectl get pod
Check the Nginx version to ensure the rollback was successful:
kubectl describe pod <podname>
Now, Check the nginx version,
Step 4 : Clean up
Once you're done, you can clean up the resources:
kubectl delete deploy nginx-deployment
Conclusion
Kubernetes Deployments provide a powerful way to manage the lifecycle of your applications. With the ability to easily update, roll back, and scale your applications, Deployments are an essential tool for any Kubernetes user