Table of contents
Welcome to Day 32 of the 90 Days of DevOps Challenge! Today, we're going to dive deep into the concept of Deployments in Kubernetes. By the end of this guide, you'll have created and applied a Deployment file to deploy a sample todo-app on Kubernetes, leveraging the powerful features of auto-healing and auto-scaling.
What is a Deployment in Kubernetes?
A Deployment in Kubernetes is a resource object that provides declarative updates to applications. You describe your desired state for your application in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. This means Kubernetes will manage the creation, updating, and scaling of your application automatically.
Key Features of Deployments:
Auto-healing: If a pod fails, Kubernetes will automatically restart it to ensure your application remains available.
Auto-scaling: Based on load, Kubernetes can automatically adjust the number of pod replicas, ensuring your application can handle varying amounts of traffic.
Task for Today: Deploy a Sample Todo-App
We'll start by creating a Deployment file and then applying it to our Kubernetes cluster using Minikube.
Step 1: Set Up Minikube
Before creating the Deployment, ensure you have Minikube installed and running. If you haven't set it up yet, you can do so with the following commands:
Install Minikube:
- Follow the installation instructions from the Minikube official documentation.
Start Minikube:
minikube start
Step 2: Create the Deployment File
Create a new file named deployment.yml
. This file will contain the configuration for our Deployment and Service.
Deployment YAML Breakdown:
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app
spec:
replicas: 3
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: your-dockerhub-username/todo-app:latest
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
spec:
type: LoadBalancer
ports:
- port: 3000
targetPort: 3000
selector:
app: todo-app
Explanation:
apiVersion: Specifies the API version (apps/v1) of the Kubernetes resource.
kind: Defines the type of resource, which is a Deployment.
metadata: Contains metadata for the Deployment, such as the name (
todo-app
).spec: Describes the desired state of the Deployment.
replicas: Specifies the number of pod replicas (3 in this case).
selector: Defines how the Deployment finds the Pods it manages. It matches labels (
app: todo-app
).template: Describes the pods to be created.
metadata: Labels the pods with
app: todo-app
.spec: Specifies the container to run in the pods.
containers: Defines the container's properties.
name: The container name (
todo-app
).image: The Docker image to use (
your-dockerhub-username/todo-app:latest
).ports: The port on which the container will listen (3000).
Service: The second part of the YAML file defines a Service to expose the Deployment.
type: LoadBalancer to make the service accessible from outside the cluster.
ports: Defines the port configuration.
selector: Matches the pods with the label
app: todo-app
.
Step 3: Apply the Deployment
Use the following command to apply the deployment.yml
file to your Kubernetes cluster:
kubectl apply -f deployment.yml
This command tells Kubernetes to create the resources defined in the deployment.yml
file.
Step 4: Verify the Deployment
Once applied, you can verify that your Deployment and Service have been created and are running correctly using these commands:
kubectl get deployments
kubectl get pods
kubectl get services
kubectl get deployments: Displays the status of the deployments.
kubectl get pods: Shows the status of the pods.
kubectl get services: Lists the services and their statuses.
Step 5: Access the Application
To access your deployed todo-app, you need to get the external IP of the Service. Use the following command to get the Service details:
kubectl get service todo-app-service
Look for the EXTERNAL-IP
column. If you're using Minikube, you can access the service using the Minikube IP:
minikube service todo-app-service
This command opens your default web browser to the URL where the todo-app is running.
Conclusion
By completing this task, you've learned how to create and apply a Kubernetes Deployment and Service. This exercise demonstrates how Kubernetes manages the desired state of applications, ensuring high availability and scalability with features like auto-healing and auto-scaling.
Happy learning, and keep enhancing your DevOps skills! ๐