top of page
Writer's pictureSathish Kumar

Kubernetes- Infrastructure As Code Part 2- Creating PODs with YAML

Updated: Jan 15, 2021


In the previous article, I gave a brief introduction to YAML. In this article, I am going to show you how to get things done with YAML in Kubernetes- in fancy terms using "Declarative definition to manage Kubernetes objects".


If you haven't created a Kubernetes cluster already and intend to follow along, I recommend that you create one with the steps described here. If you do not want to go through the hassle of setting up multiple VMs/servers and then configuring a cluster Minikube or Docker Desktop (On windows) will work just fine. The other thing, you need is a good YAML editor- most people recommend Visual Studio Code for this. VS Code is free and has a built-in syntax checker for Kubernetes YAML definitions. My personal editor of choice is Eclipse with Kubernetes YAML plugin.


As you can see from below, my cluster is up and I have 2 nodes.



root@sathish-vm2:/home/sathish# kubectl get pods --all-namespaces
NAMESPACE     NAME                                  READY   STATUS    RESTARTS   AGE
kube-system   coredns-f9fd979d6-d8wzr               1/1     Running   0          37m
kube-system   coredns-f9fd979d6-xcxzc               1/1     Running   0          37m
kube-system   etcd-sathish-vm2                      1/1     Running   0          37m
kube-system   kube-apiserver-sathish-vm2            1/1     Running   0          37m
kube-system   kube-controller-manager-sathish-vm2   1/1     Running   0          37m
kube-system   kube-flannel-ds-cq56k                 1/1     Running   0          37m
kube-system   kube-flannel-ds-x7prc                 1/1     Running   0          35m
kube-system   kube-proxy-lcf25                      1/1     Running   0          35m
kube-system   kube-proxy-tf8z8                      1/1     Running   0          37m
kube-system   kube-scheduler-sathish-vm2            1/1     Running   0          37m

root@sathish-vm2:/home/sathish# kubectl get nodes
NAME          STATUS   ROLES    AGE   VERSION
sathish-vm1   Ready    <none>   35m   v1.19.0
sathish-vm2   Ready    master   38m   v1.19.0

I am going to create a POD with YAML. My POD definition files will reside in "/home/Sathish/pods" on "sathish-vm2" which is my master.


To edit the POD YAML definition, I am going to use Eclipse IDE. Within Eclipse IDE, I have installed the "Remote Systems" plugin which allows me to directly edit files on a remote node with SSH.



As with any piece of code, it is a good idea to start with a skeletal structure and add details within. Following is my skeletal structure for any YAML definition for Kubernetes.



apiVersion:
kind: 
metadata:
spec:
  • API version tells the version of API. For PODs it is v1.

  • Kind indicates the type of object (for example Pod).

  • Under metadata, section objects can be named.

  • Spec (or specification) section allows us to specify container images, ports, environment variables, etc.

I am going to deploy a MySQL container. Here is the complete YAML file (mypod.yaml).


apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    tier: mysql
spec:
  containers:
    - name: mydb
      image: mysql
      env:
       - name: MYSQL_ROOT_PASSWORD
         value: sathish123


It is important to align sections properly within the file. Also, notice space between ":" and values. Using an editor (or plugin) that understands Kubernetes YAML definitions helps in reducing errors to a great extent.



Note: Refer to "Environment Variables" here for a list of variables you  can change when deploying mysql. In my case, I am just setting the MySQL_ROOT password.

Now we can use either kubectl create or kubectl apply with the "-f" parameter to specify this definition file.


root@sathish-vm2:/home/sathish/pods# kubectl apply -f mypod.yaml
root@sathish-vm2:/home/sathish/pods# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
mysql   1/1     Running   0          10m

Let's check the newly created POD by getting a shell. For this, we can use kubectl exec

root@sathish-vm2:/home/sathish/pods# kubectl exec --stdin --tty mysql -- /bin/bash
root@mysql:/# hostname
mysql
root@mysql:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Note that the root password for MySQL is what we specified in the YAML file under the env section, which in my case is "sathish123".


That's it for today folks, in the next article I will talk about how to create and manage replicasets with YAML.


133 views0 comments

留言


bottom of page