Creating a deployment on a Kubernetes cluster involves defining the deployment configuration in YAML files and then applying those files to the cluster using the kubectl
command-line tool. The YAML file typically includes information such as the container image, ports, replicas, and other configuration details.
The step-by-step guide to achieve this task is given below:
Note: To learn more about Kubernetes clusters, check out our Answer: What is Kubernetes cluster? What are worker and master nodes?
Firstly, we’ll create a standard YAML file to describe our deployment:
apiVersion: apps/v1kind: Deploymentmetadata:name: deploymentspec:replicas: 3 # Number of desired replicasselector:matchLabels:app: apachetemplate:metadata:labels:app: apachespec:containers:- name: apacheimage: your-docker-image:tag # Replace with your actual Docker image and tagports:- containerPort: 80 # Port your application is running on
The line-by-line explanation of the code is given below:
Lines 1–4: We start off by specifying the API version for the Kubernetes resource. In this case, it's the standard apps/v1
API version. Also, we set the type of the Kubernetes resource being defined which is Deployment
, and set the name of the cluster to be deployment
under the metadata
section.
Lines 5–9: Here, we specify the number of replicas, which is 3
, as well as the deployment should manage pods with the label app: myapp
under the selector
section.
Lines 10–19: The pod template used by the deployment to create new pods is defined with the template
section. Similar to the code above, we make sure that the deployment manages pods with the label app: myapp
. The pod is then specified with the name apache
, along with its image tag and port number.
For the code to be tested below, we will use the image
name to be equal to httpd
.
Once the YAML file is good to go, we'll run the following command to apply the deployment configuration to the cluster:
kubectl apply -f deployment.yaml
This will create the deployment and the specified number of replica pods on our Kubernetes cluster.
Note: Make sure the YAML file is stored in the relevant directory before executing the command above in the terminal. This can be done using the
cd
command in the terminal to change the directory to the YAML file's directory.
We can verify the status of our deployment with the command below:
kubectl get deployments
Additionally, we can check the status of the pods created by the deployment:
kuberctl get pods
There are some interesting things to note here when deploying a YAML file. One is that we can scale the number of replicas created by the file deployment. To achieve this, we can execute the command below:
kubectl scale deployment myapp-deployment --replicas=5
As the –replicas
parameter is set to 5
. This will scale the deployment to five replicas.
After the deployment and replica scaling, the deployment can be deleted via the following command:
kubectl delete deployment myapp-deployment
Note: In place of
myapp-deployment
, write the name of the cluster that is deployed for scaling or deletion.
The code explained in the steps above can be tested below. Click the “Run” button to see it in action.
apiVersion: apps/v1 kind: Deployment metadata: name: deployment spec: replicas: 3 selector: matchLabels: app: apache template: metadata: labels: app: apache spec: containers: - name: apache image: httpd ports: - containerPort: 80
In conclusion, deploying applications on a Kubernetes cluster using YAML files provides a reproducible approach to managing the desired state of our applications. The YAML files serve as configuration blueprints, encapsulating details such as container specifications, replicas, and other deployment settings. By employing the kubectl apply
command, we can seamlessly apply these configurations to the Kubernetes cluster, ensuring consistency and scalability.
Unlock your potential: Kubernetes Deployment and Advanced Operations series, all in one place!
To deepen your understanding of Kubernetes deployment and management, explore our series of Answers below:
How to create a Kubernetes cluster locally with Minikube
Learn how to set up a Kubernetes cluster on your local machine using Minikube for development and testing.
How to create deployment on a Kubernetes cluster via YAML files
Master the process of defining and managing deployments in Kubernetes using YAML configuration files.
What is an Ingress controller in Kubernetes?
Understand the role of an Ingress controller in managing external access and routing traffic within a Kubernetes cluster.
How to implement namespace quotas in Kubernetes
Discover how to apply resource limits to specific namespaces to optimize resource allocation and prevent overuse.
How to implement Kubernetes resource quotas
Learn how to enforce limits on CPU, memory, and storage consumption across your Kubernetes cluster.
Add users using certificates in a Kubernetes cluster
Explore the process of adding and authenticating users in Kubernetes by issuing and managing certificates.
What is service discovery in Kubernetes?
Understand how Kubernetes enables seamless service discovery, allowing applications to communicate efficiently.
How to put a database in Kubernetes
Learn the best practices for deploying and managing databases within a Kubernetes environment.
How to run WordPress on Kubernetes
Follow a step-by-step guide to deploying a scalable and resilient WordPress site on Kubernetes.
Free Resources