Objectives:
- Create a deployment that runs the nginx image using a YAML file
- Display the details of this deployment via the command line
- Update the yaml file to scale the deployment to 4 nginx containers
- Verify the change via the command line
Prerequisites:
- Kubernetes CLI and tools installed, you can find out how to get started setting up here.
What is Kubernetes?
Kubernetes, also known as ‘k8s’, is an open-source container orchestration tool used to automate the deployment, scaling and management of containerised applications.
Verify k8s version
I will be using the KodeKloud playground for this tutorial as I did not want to run anything locally.
Let’s start off by getting the version of k8s using the command kubectl version -o=yaml
Note: This version v1.25.0 requires a runtime that conforms with the Container Runtime interface (CRI). The KodeKloud Kubernetes Playground
environment utilises containerd
as CRI.
You can find out the container runtime used on a node using the command kubectl get nodes -o wide
Step 1: Create the YAML file for the deployment
Create a yaml file for the deployment. You can refer to the excellent documentation to help you, click here. I created a YAML file and push it to my GitHub repo.
Above is the content of the YAML file that will be used to create and deploy our containers.
A major advantage of declaring a desired state in YAML file is that changes can be made and pushed to a repository e.g. in GitHub, this allows for code reuse, versioning and collaboration.
Step 2: Create the deployment from the YAML file
- Navigate to the directory with the week17project.yaml file
- Use the command
kubectl create -f week17project.yaml
The deployment has been created. We can verify this with the command kubectl get deployments.apps -o wide
We have 1 nginx container available. Now lets scale this deployment by making a change to the manifest file and the applying the changes.
Step 3: Make and apply a change to the number of replicas in the manifest file
- Use a text editor to make a change to the
spec
section of the YAML file. Changereplicas
from 1 to 6, then save the change. I will just use the nano text editor to do so. - Apply the changes to the edited YAML manifest file using the command
kubectl apply -f week17project.yaml
This will allow our deployment to scale whilst maintaining everything else in the file.
We have applied the changes to the number of replicas the changes have been configured. Now let’s verify the changes.
Use the command kubectl get deployments.apps -o wide
to see the changes.
We can see that when we first ran the command, the number of available replicas were 3 out of 6. Once we ran the command again, the number of available replicas became 6 out of 6.
We have successfully met the objectives.