K8s YAML Simplified
--
YAML
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
# This is a comment # Key value pairs
name: Ajay
city: Delhi# Dictionary
person:
name: Ajay
city: Delhi# List
cities
- Delhi
- Bombay
- Jaipur
- Hyderabad
The above-learned concepts about YAML are sufficient to write most of the YAML configuration files.
K8s Config 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.
apiVersion:
kind:
metadata:
spec:
Create Pod
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
apiVersion: v1
kind: Pod
metadata:
name: hello-world-app
labels:
app: hello-world-app # Key value pairs
spec:
containers: # List object
- name: hello-world-app
image: tutum/hello-world
ports:
- containerPort: 80
protocol: "TCP"
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:
kubectl apply -f <config file or folder>
Let’s use above command to create our first pod
kubectl apply -f pod_config.yaml
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.
kubectl get pods
kubectl get pod
kubectl get pod
Output:
Awesome our pod is running.
Create Service
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
apiVersion: v1
kind: Service
metadata:
name: hello-world-app-service
spec:
type: LoadBalancer # ClusterIp, # NodePort
selector:
app: hello-world-app
ports:
- name: http
port: 80 # Service Port
targetPort: 80 # Container Port
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:
kubectl apply -f pod_service_config.yaml
Output:
Check if service has been created successfuly
kubectl get svc
kubectl get services
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.
Scaling
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 replica set
Create a file with the name pod_replica_config.yaml with below content
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: hello-world-app-rs
spec:
replicas: 3
selector:
matchLabels:
app: hello-world-app
template:
metadata:
name: hello-world-app
labels:
app: hello-world-app
spec:
containers:
- name: hello-world-app-container
image: tutum/hello-world
ports:
- containerPort: 80
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:
kubectl apply -f pod_replica_config.yaml
Output:
Check your replica status using any of the below command
kubectl get replicasets
kubectl get rs
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
kubectl get pods
Output:
You can see that three pod instances are running and pod name is derived from the replica set name.