SDE25 WS2024/25
objectives:
- Create Dockerfile: a multi-stage Dockerfile was created to build and containerize the web application.
- Set up CI/CD Pipeline:
- Linting: Runs on every push to any branch.
- Building: Runs on every push to any branch.
- Auditing: Runs only on merges to the
main
orrelease
branches.
- Push Docker Image to Docker Hub: The Docker image has been successfully pushed to Docker Hub.
- Deploy on Azure Container Apps
The Docker image for this project is available on Docker Hub:
- Repository: erna67/devops-webapp
- Tag: release
To run the application locally using Docker, use the following commands:
docker pull erna67/devops-webapp:release
docker run -p 3000:3000 erna67/devops-webapp:release
The application will be available at http://localhost:3000
.
Tasks:
- Deploy a containerized web application on your local Kubernetes cluster.
- Implement a rolling update strategy for zero-downtime deployments.
- Scale the application by adjusting replica counts using Kubernetes commands.
- The application should be accessed by your local machine
For utilizing a local Kubernetes cluster, I have used Minikube, as I already had it installed on my machine.
The following steps were taken to deploy the application on Minikube:
-
Start Minikube:
minikube start
-
Create a Kubernetes service: see
k8s/deployment.yaml
-
Apply the deployment:
kubectl apply -f k8s/deployment.yaml
-
Verify that the deployment is running:
kubectl get pods NAME READY STATUS RESTARTS AGE devops-webapp-deployment-d7b6b98c7-69n5m 1/1 Running 0 2m6s devops-webapp-deployment-d7b6b98c7-727tn 1/1 Running 0 2m6s devops-webapp-deployment-d7b6b98c7-fhg7c 1/1 Running 0 2m6s
-
Create a Service to expose the application
kubectl apply -f k8s/service.yaml
-
Verify that the service is running:
kubectl get services
-
Access the application: Minikube provides a convenient command to access services. This command will automatically open the web browser to access the application using the correct Minikube IP and port:
minikube service devops-webapp-service
We can leverage the rolling update mechanism that Kubernetes provides by default. This approach updates pods incrementally, ensuring that some pods are always available, hence minimizing downtime.
We can just add the following command to the deployment file to update the image:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
Explanation:
strategy.type: RollingUpdate
: This specifies that the deployment strategy will be a rolling update.rollingUpdate.maxUnavailable: 1
: This controls how many pods can be unavailable during the update. Setting it to 1 means that one pod can be taken down at a time.rollingUpdate.maxSurge: 1
: This controls how many new pods can be created over the desired count of replicas during the update. Setting it to 1 means that one new pod will be created before an old one is taken down, ensuring there is always a sufficient number of available pods.
Updating the Deployment
To perform a rolling update, we can just update the container image or other properties of the deployment and reapply the configuration. For example, if we have a new version of the container image, we can edit the deployment YAML file with the new version:
spec:
containers:
- name: devops-webapp
image: erna67/devops-webapp:release-v2
Then, we can apply the updated configuration:
kubectl apply -f k8s/deployment.yaml
Kubernetes will start the rolling update:
- It will bring up a new pod with the updated image.
- Once the new pod is ready (i.e., passes readiness checks), it will take down an old pod.
- This process continues until all pods are updated.
Verify the Rolling Update
To verify that the rolling update is happening correctly, we can check the status of the pods:
kubectly rollout status deployment/devops-webapp-deployment
This step involves adjusting the number of replicas (pods) for the deployment.
We can do this with kubectl
commands:
-
Manually scale the application Using
kubectl scale
kubectl scale deployment devops-webapp-deployment --replicas=5
This sets the number of replicas to
5
. -
Scaling by editing the deployment YAML: We can also update the
replicas
field in the deployment YAML file and apply the changes:apiVersion: apps/v1 kind: Deployment metadata: name: devops-webapp-deployment spec: replicas: 5
Apply the changes:
kubectl apply -f k8s/deployment.yaml
After scaling, we can check the status of the deployment to verify that the number of replicas has changed:
> kubectl get deployment devops-webapp-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
devops-webapp-deployment 5/5 5 5 37m
After running the command minikube service devops-webapp-service
, the application is available at the following address:
|-----------|-----------------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-----------------------|-------------|---------------------------|
| default | devops-webapp-service | 80 | http://192.168.49.2:30407 |
|-----------|-----------------------|-------------|---------------------------|
* Starting tunnel for service devops-webapp-service.
|-----------|-----------------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-----------------------|-------------|------------------------|
| default | devops-webapp-service | | http://127.0.0.1:54122 |
|-----------|-----------------------|-------------|------------------------|
* Opening service default/devops-webapp-service in default browser...
! Because you are using a Docker driver on windows, the terminal needs to be open to run it.
We can see that the app is indeed available: