How to create and maintain a Kubernetes deployment using a YAML file

Omar Egal
4 min readSep 28, 2022

--

Objectives:

  1. Create a deployment that runs the nginx image using a YAML file
  2. Display the details of this deployment via the command line
  3. Update the yaml file to scale the deployment to 4 nginx containers
  4. 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

YAML output of kubectl version command

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

The output depicts the version number and container runtime

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

  1. Navigate to the directory with the week17project.yaml file
  2. Use the command kubectl create -f week17project.yaml
Output of kubectl create command → created

The deployment has been created. We can verify this with the command kubectl get deployments.apps -o wide

Output of kubectl get deployments.app

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

  1. Use a text editor to make a change to the spec section of the YAML file. Change replicas from 1 to 6, then save the change. I will just use the nano text editor to do so.
  2. 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.
Here the number of replicas is changes from 1 to 6
Output of kubectl apply on edited manifest file → configured

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.

2 outputs for kubectl get deployments.apps ran twice

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.

Output for kubectl get pods command

We have successfully met the objectives.

--

--

Omar Egal

Tech Training | DevOps Engineer ☁️ | AWS Certified Solutions Architect Associate | AWS Certified Developer Associate | HashiCorp Certified Terraform Associate