Launching your Kubernetes Cluster with Deployment
Day 32 : #90DaysOfDevOps Challange
If you're new to Kubernetes, you may be wondering what "deployment" means in the context of this powerful container orchestration system. Simply put, deployment is the process of updating or rolling out new versions of an application in a Kubernetes cluster. In this article, we'll explore deployment in Kubernetes and how it works.
What is Deployment in Kubernetes?
In Kubernetes, a deployment is a high-level abstraction that allows you to declaratively manage your application's lifecycle. You define the desired state of your application, and Kubernetes takes care of making it happen. A deployment consists of a set of instructions for creating and updating replica sets, which are the objects that actually manage the running instances of your application.
Deployments in Kubernetes are essential because they provide several benefits. First, they allow you to easily scale your application up or down, depending on demand. Second, they provide rolling updates, which means that new versions of your application can be deployed without any downtime. Finally, deployments ensure that your application runs consistently across multiple nodes in your cluster, improving reliability and availability.
How Deployment Works in Kubernetes
At a high level, a deployment in Kubernetes works like this:
You define a deployment configuration in a YAML file, which specifies the desired state of your application.
You apply the configuration to your Kubernetes cluster using the kubectl apply command.
Kubernetes creates a replica set based on the configuration, which manages the running instances of your application.
As your application needs to be updated or scaled, you modify the deployment configuration and apply it again.
Kubernetes automatically updates the replica set to match the new desired state of your application, rolling out any changes without downtime.
Let's look at an example deployment YAML file to see how this works in practice:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
This YAML file defines a deployment named myapp-deployment
, which specifies that three replicas of the myapp
container should be created. The selector and template sections ensure that the deployment only manages pods with the app: myapp
label, and the container definition specifies the image to use and the port to expose.
To apply this configuration to your Kubernetes cluster, you can save it to a file (e.g., myapp-deployment.yaml
) and run the following command:
kubectl apply -f myapp-deployment.yaml
Kubernetes will create the replica set and manage the running instances of your application based on the configuration you specified. If you need to update the application, you can modify the YAML file and apply it again, and Kubernetes will handle the rolling update automatically.
Task 1 : Add a deployment.yml file. Apply the deployment to your k8s (minikube) cluster by command kubectl apply -f deployment.yml
Here are the detailed steps to create a Deployment file to deploy a sample todo-app on Kubernetes with Auto-healing and Auto-scaling features:
Install Minikube and start it.
Minikube is a tool that allows you to run a single-node Kubernetes cluster locally. You can download it from the official Minikube website, then start it using the
minikube start
command.
Clone the django-todo-cicd repository from Github.
Django-todo-cicd is a sample web application that we will be deploying on Kubernetes. You can clone the repository from Github using the
git clone
https://github.com/Dhananjaykul/django-todo-cicd.git
command.
Build the Docker image.
Navigate to the cloned repository and build the Docker image using the
docker build -t . django-todo-cicd:latest
command.
Run the container on port 8000 and verify the app is running using the
curl -L
http://172.31.89.215:8000
command.Run the Docker container using the
docker run -d -p 8000:8000 django-todo-cicd:latest
command.Verify that the application is running by using the
curl -L
http://172.31.89.215:8000
command.
Create a new directory called k8s.
- Create a new directory called k8s in the same directory where the django-todo-cicd repository is located.
Navigate to the k8s directory.
- Navigate to the k8s directory using the
cd k8s
command.
- Navigate to the k8s directory using the
Push the Docker image to your Dockerhub account.
Push the Docker image to your Dockerhub account using the
docker push django-todo-cicd:latest
command.
Create a new pod.yaml file.
Create a new file called pod.yaml using your preferred text editor.
Copy the sample pod.yaml configuration from the official Kubernetes documentation, then replace the image name with your own Dockerhub image name.
Create a
pod.yaml
file in thek8s
directory with the following contents:
apiVersion: v1
kind: Pod
metadata:
name: todo-pod
spec:
containers:
- name: todo-app
image: dhananjaykulkarni/django-todo-cicd:latest
ports:
- containerPort: 8000
Here, we are creating a pod with the todo-app
container image that we previously pushed to Docker Hub.
Kill the previously made container.
- Kill the previously made container using the
docker ps
command to find the container ID, then thedocker kill <container-id>
command to stop it
- Kill the previously made container using the
Apply the pod.yaml file using the
kubectl apply -f pod.yaml
command.
- Apply the pod.yaml file using the
kubectl apply -f pod.yaml
command. This will create a new pod based on the configuration specified in the pod.yaml file.
- Use the
kubectl get pods
command to verify the pod is running.
Use the
kubectl get pods
command to check that the pod is running.
- Create a
deployment.yaml
file with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-deployment
labels:
app: todo-app
spec:
replicas: 2
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: dhananjaykulkarni/django-todo-cicd:latest
ports:
- containerPort: 8000
Here, we are creating a deployment with two replicas of the todo-app
container image that we previously pushed to Docker Hub.
- Apply the deployment to your Kubernetes cluster by running the following command:
kubectl apply -f deployment.yaml
This will create the deployment and start the specified number of replicas.
- To check the status of the deployment and its replicas, run the following commands:
kubectl get deployments
kubectl get pods
You should see two pods running the todo-app
container image.
- To demonstrate the auto-healing feature, kill one of the running pods by running the following command:
kubectl delete pod todo-deployment-7987844987-88mng
Kubernetes will automatically create a new pod to replace the one that was killed.
- To demonstrate the auto-scaling feature, increase the number of replicas in the deployment by running the following command:
kubectl scale deployment todo-deployment --replicas=4
This will increase the number of replicas to 4.
- To check the status of the deployment and its replicas again, run the following commands:
kubectl get deployments
kubectl get pods
You should now see four pods running the todo-app
container image.
๐ฅณ๐ฅณ๐ฅณ๐ฅณ๐ฅณ๐ฅณ๐ฅณ๐ฅณ๐ฅณ๐๐๐โจโจโจโจโจโจโจโจ๐๐๐๐๐๐๐๐
By following these steps, you have successfully created a deployment file to deploy a sample todo-app
on Kubernetes using auto-healing and auto-scaling features.
Conclusion
Deployments are a crucial part of managing applications in Kubernetes. They allow you to declaratively manage your application's lifecycle, including scaling, updating, and ensuring consistency across multiple nodes in your cluster. By defining a deployment configuration and applying it to your cluster, you can take advantage of these benefits and ensure that your application is always running smoothly.