How to write YAML files for Kubernetes

What is YAML?
A YAML map is how you define key-value pairs, which is an intuitive and convenient method of creating configurations, such as in Kubernetes (A sample Kubernetes config showing a map is below).
Here, ‘apiVersion’ and ‘kind’ are the keys, and ‘v2.4’ and ‘pod’ are the respective values assigned to these keys. The 3-dashes (—) are used to denote the start of a new YAML file.
apiVersion: v2.4 kind: pod
containers: - name: webserver1 image: nginx:1.6 ports: - containerPort:80 - name: database-server image: mysql-3.2 ports: - containerPort:3306 - name: rss-reader image: rss-php-nginx:2.8 ports: - containerPort:86
How to create a kubernetes Pod using YAML
For instance, below is the YAML code to create a pod named mywebapp1 that has 2 containers: One is a web server and the other is a database server. It is also assigned to a specific volume named websvr-storage:
Filename: /k8s/pods/pod1.YAML
apiVersion: v1 kind: Pod metadata: name: mywebapp1 labels: role: webserver-role app: nginx spec: containers: - name: webserver1 image: nginx:1.6 ports: - containerPort:80 - name: database-server image: mysql-3.2 ports: - containerPort:3306 volumes: - name: websvr-storage emptyDir: {}
How to create a Kubernetes Deployment using YAML
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment1 labels: app: nginx spec: replicas: 3 selector: matchlabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: webserver1 image: nginx:1.6 ports: - containerPort:80 - name: database-server image: mysql-3.2 ports: - containerPort:3306
- We have specified that in our desired state, this deployment should always have 3 replicas.
- Because deployments are sets of objects, we must specify what objects are to be created by this deployment. This is done under the template: specification. And to ensure we are creating only the correct pods in this deployment, we use the .selector.matchlabels: specification. In this case, only those pods that match our desired label (which is app:nginx) will be included in this deployment.
- We have also specified that we want 2 pods to be created in this deployment. These pods are webserver1 and database-server. And their respective specs are also included here, just as they were when creating the individual pods.
- And finally, to actually create the deployment, use the kubectl command:
kubectl create -f /k8s/deployments/deployment1.yaml
How to create a Kubernetes Service using YAML
For example, when a user searches from our website’s frontend using HTML, that search request is directed to a set of pods where our database is defined. If the ‘database’ pod goes down and has to be recreated, the frontend should be completely unaware of this. The request should still be acknowledged and fulfilled as usual. And this is achieved by means of Kubernetes services. A service is an abstract way to expose an application running on a set of Pods as a network service.
Let’s take a look at a sample service definition in Kubernetes YAML:
apiVersion: v1 kind: Service metadata: name: nginx-service labels: app: nginx spec: ports: - nodePort: 30500 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: NodePort
- Our service name is ‘nginx-service’. We specify that this is a service using the kind: Service specification
- We have defined a selector that has the app: nginx label. Using this, the service will be able to match and identify the pods in our Nginx deployment, since the deployment and the pods also have the exact same label. This is how all incoming requests to this Nginx service will be automatically routed to the Nginx deployment.
- This Kubernetes service is of type NodePort (30500) and it points to our Nginx deployment. Using this NodePort, you will be able to access the Nginx service on all Kubernetes nodes via port 30500.
Is YAML for Kubernetes really easy?
This is a complexity that development teams should handle along with product development, and with time it becomes a burden. This is why you need to choose the right tools that make you focus more on your development and spend less time managing your infrastructure.
How CloudPlex addresses your pain
The screenshot is a visual interface used by the developer to create the same application as above. As you can see, the developer can create the application simply by dragging and dropping containers from a palette to the canvass and configuring the containers visually in the Config Panel:

