Creating K8s Scripts — Deep Dive to Cluster Orchestration
Now as we already know how to manage and spin-up a basic kubernetes cluster, let’s move on the way to manage the cluster more dynamically and learn the business required cluster and application deployment concepts.
Installations and basics of Kubernetes Cluster Management:
There are several ways to spin-up the clusters and put them under the care of kubernetes, but in the real world deployment scenarios, we as a firm instead of deploying a single container over the cluster generally are required to do several configurations on the pod before actually deploying it.
Even there are several options that are not available in kubernetes cli commands which are generally frequently used in the real world scenario, and moreover we want our deployable infrastructure to be documentable, easily portable, and highly sharable over several systems.
To achieve all those use-cases, K8s supports a declarative language to perform tasks which is YAML (Yet Another Mark Up Language). For K8s tools, the YAML or yml is a DSL (Domain Specific Language) which is developed by HasiCorp itself. The major advantage which YAML provides is the language being user friendly, easy to understand, read and flexibility (we can use lists and arrays also)
Let’s perform the tasks which we already performed previously but this time using YAML scripts.
The task is to create the POD with an nginx cluster image running under it. We will also add some labels to the pod for easier management of the pod, which is also a good practice as by adding labels to the pods their management becomes easier and K8s in background keep the health checks and even manages the pods by providing them with labels only.
Let’s start our k8s cluster using command:
minikube start
Now that we have started our minikube cluster we can see the minikube image stored in our Virtual Machine running
BASICS FOR YAML FILE
In the .yaml
file for the Kubernetes object you want to create, you'll need to set values for the following fields:
apiVersion
- Which version of the Kubernetes API you're using to create this objectkind
- What kind of object you want to createmetadata
- Data that helps uniquely identify the object, including aname
string,UID
, and optionalnamespace
spec
- What state you desire for the object
The precise format of the object spec
is different for every Kubernetes object, and contains nested fields specific to that object. The Kubernetes API Reference can help you find the spec format for all of the objects you can create using Kubernetes. For example, the spec
format for a Pod can be found in PodSpec v1 core, and the spec
format for a Deployment can be found in Deployment Spec v1 apps.
As we now know the basics of HCL (HashiCorp Language), let’s get on to our use case. The .yaml
file for our use case will look like:
Here the image used is of nginx. To run the file and launch the cluster we have to use command:
kubectl create -f <fileName.yml>
As we can see our pod is successfully created with the nginx container running inside it.
What to do next
Now as we have the basics of K8s, it’s use cases and basic scripting and cli knowledge let’s move on to learn some typical and powerful concepts of K8s i.e Replica Set, Replication Controllers and PVC (Persistent Volume Claim) which are generally required to ensure the uptime of cluster and their smooth running.
Previous — Basic of K8s Cluster
Next — Replication Set, Controllers K8s Powerful concepts
Today, most software exists, not to solve a problem, but to interface with other software.
— IO Angell