In Kubernetes, deployments provide a way to manage how your application runs. They offer several advantages over manually starting pods. Deployments can automatically start multiple instances of your application (for high availability), replace unhealthy pods with healthy ones (for self-healing), and even update your application to a new version in a controlled way (with rollouts). Overall, deployments make it easier to run your applications reliably in Kubernetes.
In this tutorial, we will explore how to use the kubectl create deployment command to create and manage deployments in Kubernetes.
Table of Contents
Basic Syntax of kubectl create deployment
The kubectl create deployment command creates a deployment resource in Kubernetes. Here is the basic syntax:
# kubectl create deployment deployment_name --image=image_name
- deployment_name: The name of the deployment you want to create.
- image_name: The Docker image that will be used for the deployment.
Creating a Deployment with a Docker Image
The most straightforward way to create a deployment is by specifying its name and the Docker image. Let’s say we want to create a deployment named nginx-deployment using the nginx image. Use the following command:
# kubectl create deployment nginx-deployment --image=nginx
This will create an Nginx deployment in a default namespace.
deployment.apps/nginx-deployment created
To verify that the deployment has been created, list all deployments:
# kubectl get deployments
Output.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 10s
Creating a Deployment with Specific Replicas
You can specify the number of replicas for your deployment using the –replicas flag. For example, to create a deployment named nginx-deployment with three replicas, use:
# kubectl create deployment nginx-deployment --image=nginx --replicas=3
Output.
deployment.apps/nginx-deployment created
To verify that the deployment has been created with the specified replicas, list all deployments:
# kubectl get deployments
Output.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 10s
Creating a Deployment with Environment Variables
You can set environment variables for your deployment using the –env flag. For example, to create a deployment named nginx-deployment with an environment variable ENV=production, use:
# kubectl create deployment nginx-deployment --image=nginx --env="ENV=production"
Output.
deployment.apps/nginx-deployment created
To verify the environment variables, describe the deployment and look for the ENV variable:
# kubectl describe deployment nginx-deployment
Output.
...
Environment:
ENV: production
...
Creating a Deployment Using a YAML Configuration File
Another method to create a deployment is by using a YAML configuration file. Create a file named nginx-deployment.yaml with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
To create the deployment from this file, use:
# kubectl apply -f nginx-deployment.yaml
Output.
deployment.apps/nginx-deployment created
To verify that the deployment has been created, list all deployments:
# kubectl get deployments
Output.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 10s
Scaling a Deployment
You can scale a deployment by updating the number of replicas. For example, to scale the nginx-deployment to 5 replicas, use:
# kubectl scale deployment nginx-deployment --replicas=5
To verify that the deployment has been scaled, list all deployments:
# kubectl get deployments
Output.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 20s
Setting Resource Limits for a Deployment
You can set resource limits for your deployment to ensure that it does not consume more resources than specified. For example, to create a deployment with CPU and memory limits, update your YAML file as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
Apply the updated YAML file:
# kubectl apply -f nginx-deployment.yaml
To verify the resource limits, describe the deployment and look for the resource limits section:
# kubectl describe deployment nginx-deployment
Output.
...
Limits:
cpu: 500m
memory: 128Mi
...
Rolling Back the Deployment
If you need to roll back a deployment to a previous version, Kubernetes makes this process straightforward. Assume you have updated the nginx-deployment and now need to revert to the previous version.
First, list the revision history:
# kubectl rollout history deployment/nginx-deployment
Output.
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 none
2 none
To roll back to the previous revision (e.g., revision 1), use:
# kubectl rollout undo deployment/nginx-deployment --to-revision=1
Output.
deployment.apps/nginx-deployment rolled back
To verify that the rollback was successful, describe the deployment and check the revision history:
# kubectl describe deployment nginx-deployment
Conclusion
In this article, we explored various methods to use the kubectl create deployment command in Kubernetes. We started with the basic syntax and moved on to different scenarios, including creating deployments with specific Docker images, setting replicas, using environment variables, and creating deployments from YAML files. We also covered advanced topics such as scaling deployments and setting resource limits.
FAQs
1. How can I specify the Docker image when creating a deployment?
Use the --image flag to specify the Docker image. Example: kubectl create deployment deployment_name --image=image_name.
2. How do I create a deployment with multiple replicas?
You can specify the number of replicas using the --replicas flag. Example: kubectl create deployment deployment_name --image=image_name --replicas=3.
3. How can I update a deployment with a new Docker image?
You can update a deployment's image using the kubectl set image command.
4. What happens if a deployment fails?
Kubernetes will automatically attempt to restart failed pods and replace them with healthy ones, ensuring the application continues to run.