Table of contents
What is Deployment?
A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets or to remove existing Deployments and adopt all their resources with new Deployments.
Use cases:
To maintain high availability.
Easy to scale.
Maintains the desired state of the application.
Easy to update and revert the application.
Machine used in this example:
Deployed a Kubernetes cluster on the VM machine.
One node cluster (One master node and one worker node)
Kubernetes version:- v1.27.2
OS:- ubuntu 20.04.6 LTS
Container runtime:- containerd
Creating a Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: httpd
name: httpd
spec:
replicas: 4 #number of pods created after deployment is created
selector:
matchLabels:
app: httpd
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: httpd
spec:
containers:
- image: docker.io/httpd #container-image
name: httpd #container-name
ports:
- containerPort: 80 #container-image-port
resources: {}
status: {}
Save this yaml file on your k8s master.
Create a Deployment using file httpd-dep.yml
kubectl apply -f (file-name)
We can use apply and create to create a kind of resource in k8s.
The major difference between create and apply is as follows:-
Create: The "create" command is used to create new resources in Kubernetes. When you use the "create" command, you provide a YAML or JSON file that describes the desired state of the resource you want to create. The command creates the resource based on the provided file, generating a new Kubernetes object in the cluster. If a resource with the same name already exists, the "create" command will return an error.
Apply: The "apply" command is used to create or update resources in Kubernetes. It allows you to apply changes to existing resources by specifying a YAML or JSON file that contains the desired state of the resource, similar to the "create" command. However, the key difference is that the "apply" command will make the necessary updates to the existing resource, rather than returning an error for an existing resource.
The "apply" command detects the differences between the desired state described in the file and the current state of the resource in the cluster. It updates the resource to match the desired state, adding or modifying fields as necessary. If the resource doesn't exist, the "apply" command will create it.
Get the deployment details:
kubectl get deployment (deployment-name) -o wide --show-labels
In this deployment, there are four pods running which means the desired state for this deployment is set to four replicas.
Get the pods of the deployment:
kubectl get pods
Delete deployment:
kubectl delete deployment (deployment-name)
Bonus:
How to create a deployment using cli
kubectl create deployment (deployment-name) --image=(image-name) --port=(image-port)
This command will create a deployment as per the user-defined image and the port should be the listening port of the image.
For example:- docker.io/httpd port is 80.
How to save the yaml file of the deployment.
kubectl create deployment (deployment-name) --image=(image-name) --port=(image-port) --dry-run=client -o yaml > filename.yml
By running a dry run, you can check for syntax errors, validate resource configurations, verify the intended changes, and identify potential issues or conflicts that may arise when applying the command. This helps in avoiding unintended consequences or mistakes that could impact your running applications or infrastructure.