Day 32 Task: Launching your Kubernetes Cluster with Deployment
Table of contents
🔹What is Deployment in k8s
In Kubernetes (K8s), a Deployment is a resource object that provides declarative updates to applications. It allows you to describe an application's life cycle, such as which images to use for the app, the number of replicas, and how to update them, in a declarative way. Deployments are part of the Kubernetes "Apps" API group and serve as a higher-level abstraction over pods.
Here are some key characteristics and features of Deployments:
Declarative Configuration: You define the desired state of your application in a Deployment manifest (usually a YAML file) rather than specifying a sequence of commands.
Replica Sets: Deployments manage Replica Sets, which in turn manage the actual pods running your application. This ensures that the desired number of replicas (instances) are always available.
Scaling: You can easily scale your application up or down by changing the number of desired replicas in the Deployment manifest. Kubernetes will automatically adjust the number of pods accordingly.
Rolling Updates: Deployments support rolling updates, which allow you to update your application without downtime. You specify a new version of the app in the manifest, and Kubernetes will incrementally replace the old pods with new ones.
Rollback: If a new version of your application causes issues, you can roll back to a previous version by simply updating the Deployment manifest with the desired image version.
Health Checks: Deployments can perform readiness and liveness checks on pods using probes. This ensures that pods are healthy and ready to serve traffic before being included in the load balancing.
Rolling Restarts: You can perform rolling restarts of your application to apply configuration changes or perform maintenance tasks.
History and Revision Tracking: Deployments maintain a history of all revisions, making it easy to track changes and rollbacks.
🔹Task-1:
Create one Deployment file to deploy a sample todo-app on K8s using "Auto-healing" and "Auto-Scaling" feature
step1: Create a deploy.yml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app-deployment
spec:
replicas: 3 # Number of desired replicas
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: your-todo-app-image:latest # Replace with your actual todo-app image
ports:
- containerPort: 8000
step2: Create the deployment
kubectl apply -f deploy.yml
step3: To set the CPU utilization target for auto-scaling in a Kubernetes Deployment, you can use the kubectl autoscale
command. Here's how you can do it:
kubectl autoscale deployment todo-app-deployment --cpu-percent=50 --min=2 --max=5
kubectl get hpa
This command will enable auto-scaling for your Deployment based on CPU utilization, with the specified parameters.
step4: To update the autoscaling settings, you can run the kubectl autoscale
command with the new values, just like you did when initially setting it up. For example:
kubectl edit hpa todo-app-deployment
step5: To remove auto-scaling completely, you can delete the HPA resource associated with your Deployment:
kubectl delete hpa todo-app-deployment
Subscribe to my newsletter
Read articles from Gopal Gautam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gopal Gautam
Gopal Gautam
Hii I am a backend/DevOps engineer.I have a experience with development and automation.I mostly work with Python, django, Cloud based technologies.