Kicking ass the Kubernetes way. Part 1(Clusters)

Samuel Okediji
May 15 · 8 min read

The Google Kubernetes Engine (GKE) would be our reference point in this series. The GKE is a managed Kubernetes service on the GCP. What better way of learning how to use the platform that is K8s other than learning it from the creators themselves.

You may have heard about clusters in relation with Kubernetes and yet still don’t know what they are. I know many developer’s who shy away from Infrastructure or Cloud tooling as a results of the number of supposed Tech Jargon one has to be familiar with to get interested in using the tools. My job is to help demystify this jargon.. atleast to the best of my ability.

Now what is a Cluster and what role do they play in Kubernetes. Don’t forget Kubernetes itself is a open-source container orchestration system for automating application deployment, scaling, and management of portable VMs in form of containers.

A Cluster is said to consist of at least one cluster master and multiple worker machines called nodes.

Let’s paint a mental picture of what cluster are in K8s.

(GKE cluster)

One can see that there is the master node as well as multiple worker (or Nodes) in a cluster. This is a classic setup.

Another thing one needs to take cognizance of is the command line that is mostly use to edit, view, and delete pods in cluster popularly known as the kubectl command. In case you wondering what is that command? The Kubectl is simply a utility used by administrators to control Kubernetes clusters.

Now let’s create our first GKE cluster shall we?

PRACTICE GUIDE

Navigate your way down to Google cloud console, move to cloud shell and punch in this command. For many who are scared to use the GCP cause of billing. Do not worry about that, cause as a new user you are opened to 300USD worth of credit for your one year free trial — this is more than enough to handle all the cost for this series.

export my_zone=us-central1-a export my_cluster=standard-cluster-1

This command helps set up zone to which your clusters would go live on.

Move on to the next step which is to create a GKE cluster. In Cloud Shell, type the following command to create a Kubernetes cluster.

gcloud container clusters create $my_cluster — num-nodes 3 — zone $my_zone — enable-ip-alias

This will take a moment to get created. After which your cluster is being deployed. Proceed to GCP Console Kubernetes Engine > Clusters page should look like the screenshot.

(Output you should be getting after deploying a cluster)

Next stop is having to provision the number of nodes that are to be in the created cluster. We can only do this by modifying the number of cluster. Now moving on to your Cloud Shell environment to modify the number of nodes in a GKE cluster. Punch in the following commands like a Boss!

gcloud container clusters resize $my_cluster — zone $my_zone — size=4

(When prompted with Do you want to continue (Y/n), press yto confirm.)

How many nodes did we just provision for from the above command?

Yeah.. Your guess is as good as mine. We provisioned for four nodes. Is this becoming interesting? You see it’s really no rocket science any longer?

You can go look up the Kubernetes Engine > Clusters page that the cluster now has four nodes.

Now lets kick some more ….

At this point we have to connect to the created cluster externally. In this task, you use Cloud Shell to authenticate to a GKE cluster and then inspect the kubectl configuration files.

First we have to authenticate with credentials of current user to create Kubeconfig file. Then proceed to punch in these command.

gcloud container clusters get-credentials $my_cluster — zone $my_zone

This command creates a .kube directory in your home directory if it doesn't already exist. In the .kube directory, the command creates a file named config if it doesn't already exist, which is used to store the authentication and configuration information. The config file is typically called the kubeconfig file. In the .kube directory, the command creates a file named config if it doesn't already exist, which is used to store the authentication and configuration information. The config file is typically called the kubeconfig file.

We can then proceed to open the kubeconfig file with the nano text editor. Punch the following to edit file:

nano ~/.kube/config

You can now examine all of the authentication and endpoint configuration data that is stored in the file. Information for both clusters should appear. The information was populated during cluster creation.

Punch CTRL+X to exit the nano editor.

We can the INSPECT THE GKE CLUSTER

For inspection of the GKE cluster

Punch:

kubectl config view

Then go ahead to execute the following command to print out the cluster information for the active context:

kubectl cluster-info

The output gives you a dump of cluster IP and more.

Now lets move to more fun thingy like deploying Pods to Cluster

INSPECTING GKE CLUSTER USING kubectl:

Execute the following command to view the resource usage across the nodes of the cluster:

kubectl top nodes

Another top command (kubectl top pods) shows similar information across all the deployed Pods in the cluster.

Now execute the following command to enable bash autocompletion for kubectl

source <(kubectl completion bash)

What happened there? Did Thanos Just snapped again? LOL!

Double tap your TAB key and see the magic suffice.

Now lets do this! Let us get pods running in the cluster.

Deploy Pods to GKE clusters

Execute the following command to deploy the latest version of nginx as a Pod named nginx-1:

kubectl run nginx-1 — image nginx:latest

Now you should know this command creates a Pod named nginx with a container running the nginx image. When a repository isn’t specified, the default behavior is to try and find the image either locally or in the Docker public registry. In this case, the image is pulled from the Docker public registry. Now lets get them pods.

Execute the command:

kubectl get pods

You have seen the output?

(out put for kubectl get pods)

Using variables like this can help you minimize human error when typing long names. You must type your Pod’s unique name in place of [your_pod_name]

Now we can execute this by changing our pod variable name.

export my_nginx_pod=[insert_urpod_name]

Confirm that you have set the environment variable successfully by having the shell echo the value back to you.

echo $my_nginx_pod

Go on to execute the following command to view the complete details of the Pod you just created.

kubectl describe pod $my_nginx_pod

The output should look like the following example:

(OUTPUT -do not copy this)

Name: nginx-1–74c7bbdb84-nvwsc Namespace: default Node: gke-standard-cluster-1-default-pool-bc4ec334–0hmk/10.128.0.5 Start Time: Sun, 16 Dec 2018 14:29:38 -0500 Labels: pod-template-hash=3073668640 run=nginx-1 Annotations: kubernetes.io/limit-ranger=LimitRanger plugin set: cpu … Status: Running IP: 10.8.3.3 Controlled By: ReplicaSet/nginx-1–74c7bbdb84 Containers: nginx-1: Container ID: docker://dce87d274e6d25300b07ec244c265d42806579fee… Image: nginx:latest Image ID: docker-pullable://nginx@sha256:87e9b6904b4286b8d41… Port: <none> Host Port: <none> State: Running Started: Sun, 16 Dec 2018 14:29:44 -0500 Ready: True Restart Count: 0 Requests: cpu: 100m Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-tok… Conditions: Type Status Initialized True Ready True PodScheduled True Volumes: default-token-nphcg: Type: Secret (a volume populated by a Secret) SecretName: default-token-nphcg Optional: false QoS Class: Burstable Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message — — — — — — — — — — — — — Normal Sche… 1m default-scheduler Successf… Normal Succ… 1m kubelet, gke-standard-cl… MountVol… Normal Pull… 1m kubelet, gke-standard-cl… pulling … Normal Pull… 1m kubelet, gke-standard-cl… Successf… Normal Crea… 1m kubelet, gke-standard-cl… Created … Normal Star… 1m kubelet, gke-standard-cl… Started …

Push a file into a container

To be able to serve static content through the nginx web server, you must create and place a file into the container.

Punch in

nano ~/test.html

Add the following text (shell script) to the empty test.htmlfile

<html> <header><title>This is title</title></header> <body> Hello world </body> </html>

Press CTRL+X, then press Y and enter to save the file and exit the nano editor.

Punch in:

kubectl cp ~/test.html $my_nginx_pod:/usr/share/nginx/html/test.html

This command copies the test.html file from the local home directory to the /usr/share/nginx/html directory of the first container in the nginx Pod. You could specify other containers in a multi-container Pod by using the -c option, followed by the name of the container.

Finally execute the following command to create a service to expose our nginx Pod externally:

kubectl expose pod $my_nginx_pod — port 80 — type LoadBalancer

Go ahead in execute the following command to view details about services in the cluster:

kubectl get services

(OUTPUT SHOULD DISPLAY)

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.11.240.1 <none> 443/TCP 1h nginx-1–7…wsc LoadBalancer 10.11.240.87 <pending> 80:31695/TCP 3s

That should get the job done.

Kubernetes is largely becoming the new standard for deploying and managing software in the cloud. The sheer prowess Kubernetes provides allows for there to be a steep learning curve. As a newbie trying to parse,the official documentation can be overwhelming. Medium post such as these did help you on your journey.

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Samuel Okediji

Written by

Back-end web developer| Linux fanatic |Ensuring Automation on the GCP| Pro-Distributed Systems| Tourist

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts