K8s YAML Simplified

YAML is a popular language for writing configuration files. The primary reason is it’s human-readable and simple to understand.

You can save a YAML file with `YAML or` YML` extensions.

There are four major concepts in the YAML:

  • Key-value pairs
  • Comments
  • Dictionary or Map
  • List or Array

YAML sample file

The above-learned concepts about YAML are sufficient to write most of the YAML configuration files.

There are following ways to create K8s resources

  • Imperative approach : Create K8s resources with commands. E.g. kubectl run hello-world-pod --image tutum/hello-world
  • Declarative approach : Create K8s resources with YAML config files.

In this section, we will focus on the declarative approach.

Key concepts of the K8s YAML configuration files:

  • apiVersion: It specifies the version from which K8s configuration meta will be fetched.
  • kind: It specifies the type of the K8s objects. The most common K8s objects are pods, replica sets, deployments, and services.
  • metadata: It primarily includes the object names and labels.
  • spec: It specifies the configurations of the K8s objects.

Based on the above key concepts of the K8s configuration file, below is the base template that you should use to define the K8s object.

As we already know, Pod is the basic deployment unit of K8s. Let’s create a pod.

Create a file with the name pod_config.yaml with below content

Anatomy:

  • API version here specify that pod configuration is loaded from core/v1.
  • Kind specify that we are are creating a pod.
  • Metadata specifies that we are creating a pod with name hello-world-app and have label app: hello-world-app
  • Spec tells that pod have a single container based on image tutum/hello-world and port 80 is exposed on the container.

You have defined your first configuration file to create a pod.

But to create the actual pod instance using this configuration file you have to apply this configuration on the K8s cluster.

There is a single universal command to apply configuration which helps you in creating any K8s object instances.

There is a universal command:

Let’s use above command to create our first pod

Output:

Congrats!!! you have created your pod successfully.

Really?? How do I know that I have created a pod?

You can check the pod status with running any of the following command.

Output:

Awesome our pod is running.

As we have deployed a web application as part of the pod creation, I know you are wondering to access your application from the browser.

To access your pod from the external world, you have to create a service instance.

Service YAML

Create a file with the name pod_service_config.yaml with below content

Most of the options you might have already guessed correctly. But let’s understand the anatomy again for the services.

Anatomy

  • Kind specifies that we are creating a service
  • Metadata specifies the name of the service
  • Spec specifies that we are creating a load balancer instances. Services have multiple types Load balancer, Cluster IP, NodePort, Ingress etc. We will deepdive into it in further sessions.

Doubt

How does the service know about the pod? We didn’t mention the name or IP address of the pod in the service definition?

If you remember we have specified the labels on the pods and same labels we have used in the service definition. Service use those labels for taking the routing decisions.

Let’s create our service with our Universal command:

Output:

Check if service has been created successfuly

Output:

Using the external IP from the above output you can access your website from your browser.

Wow !!! you have created an end to end application.

Now you want to scale your application either to increase the availability of the application or to address the load demand.

The solution is simple — run multiple instances of your pods

But how?? Should I go and write multiple pod configuration?

No!!

K8s has concept of replica set which helps in running multiple instances of the pod.

Create a file with the name pod_replica_config.yaml with below content

Anatomy:

  • Kind for replica set is ReplicaSet
  • Metadata specifies the name of the replica
  • Spec has three properties: replicas, selector and template. Replica specifies the number of instances of the pod, selector specifies the label that will be used for running multiple instance of the pod by the controller, and template specifies the definition of the pod which is similar to the pod definition we created above.

Now create a replica set using our universal command:

Output:

Check your replica status using any of the below command

Output:

You can observe that desired state of your pod was 3 and current state is also 3 which means 3 pods are running.

Let’s confirm that with the pod command

Output:

You can see that three pod instances are running and pod name is derived from the replica set name.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store