In this tutorial, we will walk you through the process of creating your first Kubernetes Deployment. Learn how to define, deploy, and manage a Deployment to run your containerized application effectively in a Kubernetes cluster.
What is a Kubernetes Deployment?
A Kubernetes Deployment is a resource that defines how to run your application, including the number of replicas, the container image to use, and update strategies. Deployments ensure high availability and enable scaling and rolling updates for your application, making it a fundamental part of managing workloads in Kubernetes.
Steps to Create Your First Deployment
- Step 1: Define the Deployment in a YAML file.
- Step 2: Apply the Deployment using
kubectl
. - Step 3: Verify that the Deployment is running.
- Step 4: Access the application.
Step 1: Define the Deployment
Create a YAML file to define your Deployment. Below is an example YAML file to deploy an NGINX web server:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21.6
ports:
- containerPort: 80
Explanation:
- apiVersion: Specifies the Kubernetes API version for Deployments.
- kind: Indicates that this resource is a Deployment.
- metadata: Provides metadata such as the name of the Deployment and labels.
- spec: Defines the desired state of the Deployment, including:
- replicas: Number of Pod replicas to run.
- selector: Matches Pods with specific labels.
- template: Describes the Pod specifications, including the container image and exposed ports.
Step 2: Apply the Deployment
Save the file as nginx-deployment.yaml
and deploy it using the following command:
kubectl apply -f nginx-deployment.yaml
This command creates the Deployment and schedules the Pods as defined in the YAML file.
Step 3: Verify the Deployment
Check the status of your Deployment using:
kubectl get deployments
View the Pods created by the Deployment:
kubectl get pods
You should see the Deployment and Pods running successfully.
Step 4: Access the Application
Expose the Deployment using a Service to access it. Use the following command to create a Service of type NodePort
:
kubectl expose deployment nginx-deployment --type=NodePort --port=80
Get the details of the Service to find the assigned port:
kubectl get services
Access the application in your browser using the Node’s IP address and the NodePort, e.g., http://
.
Best Practices for Deployments
- Use Specific Tags: Use specific container image versions instead of
latest
to avoid unexpected updates. - Set Resource Limits: Define resource requests and limits to optimize resource usage and avoid overloading nodes.
- Monitor Deployment: Use Kubernetes monitoring tools like Prometheus to track the performance and health of your Deployment.
Congratulations! You have successfully created your first Kubernetes Deployment. Deployments provide a robust and scalable way to manage containerized applications in Kubernetes, making them an essential tool for any Kubernetes user.