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 this link.
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.
Free Resources