top of page
Writer's pictureSathish Kumar

Kubernetes- Infrastructure As Code with YAML (part 1)

Updated: Jan 15, 2021



Application deployment has evolved from bare metal to VM deployment to containers. As the deployment models evolved, the management of infrastructure became easier. For instance, with VMs it is possible to deploy web, DB, and application servers on the same physical server. Further, popular VM orchestration systems like VMWARE's VCenter enable VM administrators to transparently move VMs between physical servers automatically depending on the load on the server.


Containers and orchestration systems like Kubernetes simplify the deployment of applications further. We can deploy containers, PoDs, deployment, Services/Networking i.e complete environment required for application stack by defining everything within a single file. This same file could be used to deploy the application/infrastructure on your laptop, on a server system, or in the cloud. IaC or Infrastructure as Code is a technique to manage the complete infrastructure including containers, networking, topology by specifying everything in a YAML file (in case of docker and Kubernetes).


So, what is YAML? As per Wikipedia- "YAML (a recursive acronym for YAML Ain't Markup Language) is a human-readable data serialization language." The fundamental data model in YAML is Key: Value pair, also called a dictionary. Here is an example representation of various categories of cars available in the Indian market.


Hatchback : Suzuki Baleno
Sedan : Honda City
SUV: Tata Harrier

Here is an example of an array in YAML



Hatchback : 
-Suzuki Baleno
-Hyundai i20
-Tata Altroz
Sedan : 
-Honda City
-Suzuki Ciaz
SUV: 
-Tata Harrier
- Hyundai Creta

It is possible to have dictionaries within the list in YAML.


Hatchback : 
	-Suzuki Baleno:
  		Power: 83
  		Torque: 115
	-Hyundai i20
  		Power: 82
  		Torque: 115
	-Tata Altroz
  		Power: 88
  		Torque: 200
Sedan : 
	-Honda City
		 Power: 97
  		Torque: 145
	-Suzuki Ciaz
	       Power: 103
  		Torque: 138
	
SUV: 
	-Tata Harrier
		 Power: 170
  		Torque: 350
	
	- Hyundai Creta
		 Power: 126
  		Torque: 250
	

As can be seen, the top-level key is usually a category. The next level is object name/type and finally, the leaf nodes are properties of the subcategory. To summarize, a YAML file can contain a dictionary, a list or a list of dictionaries.



Note: YAML files can be defined with any text editor. However, it is recommended to use an editor that understands YAML.  Few good ones for windows are:

Visual Studio Code: https://code.visualstudio.com/ 
ATOM editor: https://atom.io/ 
Eclipse: https://www.eclipse.org/ 
I personally use eclipse as I work on Python a lot in addition to YAML and eclipse has plugins for  both. But choice of editor is really up to you.

Switching back to the world of containers and Kubernetes, a short review of PODS, deployments, services:



PODs are the lowest level of abstraction in Kubernetes. PoDs can have one or more containers. Containers are running instances of Docker images. YAML for POD looks like following



apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    tier: webtier
spec:
  containers:
    - name: myweb
      image: httpd
    - name: db
      image: mysql

  • The API version is v1. It is important to note v1 does not support deployments.

  • Type indicates the object type, in this case, a POD

  • Metadata: Name specifies the name of POD, whereas the label is used to identify the POD to Kubernetes.

  • Spec: Spec is used to specify containers, storage volumes, environment variables, and what to do in case of failure.

Deployments are used to declare the number of running instances of PoD. Deployment is made up of PoDs.



apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app:myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: front-end
          image: httpd
          ports:
            - containerPort: 80
        - name: db
          image: mysql
  • The API version in the case of deployment is apps/v1.

  • Metadata is similar to metadata in the PODs YAML definition. However, in the case of Deployment, it is important to indicate which objects are part of the deployment. This is done by specifying the same label (app: mypp) under deployment, template and using matchLabels selector.

  • Under the spec, we notice a new property replica with a value of 2. But what are they replicas of? The answer to this is under the template section. The template defines 2 containers httpd and MySQL . Hence, the result of this deployment will be 2 PODs (replicas: 2) with each POD having an httpd and MySQL container.



Finally, the service enables external users to access PODs in deployment as per defined policy.



apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    app: myweb
spec:
  # type: ClusterIP
  ports:
    - port: 80
      targetPort: 8080
  selector:
    tier: backend

Hope this short intro to YAML and its application in Kubernetes for enabling Infrastructure as Code was informative.


Happy weekend folks!


139 views0 comments

Comments


bottom of page