Deployments and Rolling Updates
Objective: The objective of this assignment is to gain practical experience in creating Kubernetes Deployments and performing rolling updates to a web application without causing downtime. You will create a Deployment for a web application and then update it to a new version seamlessly.
Scenario: Imagine you are a DevOps engineer working on a small live project that involves deploying a web application. Your team has decided to use Kubernetes Deployments for managing the application's lifecycle. In this assignment, you will perform rolling updates to the web application to introduce a new version while ensuring uninterrupted service.
Prerequisites: Before starting this assignment, ensure that you have access to a Kubernetes cluster, `kubectl` installed, and configured to work with your cluster.
Below is a Kubernetes Deployment YAML manifest (`web-app-deployment.yaml`) for deploying a web application using the Nginx image with a version one level less than the latest. You can use this manifest as a template:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app-container
image: nginx:1.21 # Specify the desired version (e.g., 1.21) here
ports:
- containerPort: 80
In this manifest:
- `apiVersion` specifies the Kubernetes API version for the Deployment.
- `kind` specifies that this is a Deployment resource.
- `metadata` includes metadata such as the name of the Deployment.
- `spec` defines the specification for the Deployment.
- `replicas` specifies the desired number of replicas (in this case, 3).
- `selector` defines the selector that matches the pods controlled by this Deployment.
- `template` specifies the pod template used to create new pods.
- `metadata` within the template defines labels for the pod.
- `spec` within the template specifies the pod's specification.
- `containers` lists the containers within the pod. There is one container named `web-app-container`.
- `name` specifies the container's name.
- `image` specifies the container image to use, with the desired version (e.g., `nginx:1.21`).
- `ports` defines an array of port mappings, allowing port 80 to be exposed within the pod.
Replace `1.21` with the version you want to deploy, which is one level less than the latest version available.
Instructions:
Part 1: Create a Deployment
1. Write a Kubernetes Deployment YAML manifest (`web-app-deployment.yaml`) that defines a Deployment for a web application. Use the following specifications:
- Use the latest container image for the web application.
- Define resource requests and limits as appropriate.
- Set the replicas to 3 for redundancy.
2. Use `kubectl apply` to create the Deployment in your Kubernetes cluster:
kubectl apply -f web-app-deployment.yaml
3. Verify that the Deployment and its pods are created and running using `kubectl get deployment` and `kubectl get pods`.
Part 2: Perform a Rolling Update
1. Modify the Deployment YAML manifest (`web-app-deployment.yaml`) to specify an updated container image with a new version of the web application.
2. Use `kubectl apply` to apply the changes to the Deployment:
kubectl apply -f web-app-deployment.yaml
3. Monitor the rolling update progress using `kubectl get pods` and observe how Kubernetes gradually replaces old pods with new ones.
4. Verify that the updated version of the web application is running successfully.
Part 3: Clean Up (Optional)
1. After completing the assignment and practicing Deployments and rolling updates, you can clean up the resources by deleting the Deployment:
kubectl delete deployment <deployment-name>
Solution
Part 1: Create a Deployment
# Apply the Deployment YAML to create the Deployment
kubectl apply -f web-app-deployment.yaml
# Check the status of the Deployment and pods
kubectl get deployment
kubectl get pods
Part 2: Perform a Rolling Update
# Modify the Deployment YAML to specify the updated container image
kubectl apply -f web-app-deployment.yaml
# Monitor the rolling update progress
kubectl get pods
Please adapt the commands to your specific deployment and image version. This assignment provides hands-on experience in managing application updates and ensuring high availability through Kubernetes Deployments and rolling updates.