Get, Set and Go with Kubernetes


What on earth is Kubernetes and why should we care?

The Wikipedia definition says

Kubernetes (commonly referred to as “K8s”) is an open source container cluster manager originally designed by Google and donated to the Cloud Native Computing Foundation”.

The name originates from the Greek vocabulary; the meaning of the word “kubernetes” in Greek means “Helmsman”; in other words it means “Governor”. So it was only natural when Google adopted the name to their open source container orchestration tool. As of today, Kubernetes has become almost the de-facto standard when it comes to container orchestration tasks. With Kubernetes, the deployment, scaling and management of containers have become a breeze. So, if you are a container-geek and live and breathe containers day-in and day-out, chances are you are already familiar with Kubernetes. For rest of the population, this post should be a good starting point!

Kubernetes Tool sets

With the very brief introduction out of the way, let’s talk about the different options of running Kubernetes. You can run Kubernetes in one of the following ways:

i) Install and run on your local macine

ii) Run on a cloud environment such as AWS/Google Cloud/Azure

In this post, I am going to describe how to set it up on a local machine running Windows. Similar procedures can be applied when running on a Mac.

Install Minikube:

Minikube is kubernetes in a nutshell on your laptop, literally. It was aimed at facilitating running a Kubernetes cluster locally without the presence of a cloud environment.

The minikube can be downloaded from here. You can check the latest release and download instructions here.

For Windows environment, download the latest minikube-version.exe installer file.

Rename the file to minikube.exe and put it in C:\minikube

Open a cmd terminal or powershell

Run: cd C:\minikube and enter minikube start. The output of the command should be as below:

minikube.exe start
 
 C:\minikube>minikube.exe start
 Starting local Kubernetes cluster…
 Starting VM…
 SSH-ing files into VM…
 Setting up certs…
 Starting cluster components…
 Connecting to cluster…
 Setting up kubeconfig…
 Kubectl is now configured to use the cluster.

At this point, the local kubernetes cluster is up and running.

Verify that the cluster is running by typing checking the status

minikube status should reveal

C:\minikube>minikube status
minikubeVM: Running
localkube: Running

If by any chance the cluster is not running, then start it by minikube start

Download Kubectl:

Kubectl is a useful command line tool for Kubernetes. You can download kubectl from the following locations:

Windows version

Mac version

Linux version

Ok…now you have all the ammo to fire up containers locally at scale!


Deploy and Run Microservices in a kubernetes cluster

There are two very basic micro services that are running as docker containers in my local machine. These are

A. Spring-boot microservices written in java

Docker-hub image

B. Node.js microservices

Docker-hub image

Both of these microservices are pretty basic and sends out “Hello <Username>” to the console when <Username> is passed as a query parameter to a http Get Rest endpoint.

Both of these run fine when run as docker containers and works as expected when tested with either curl or postman. Now, I want to manage the deployment and scaling of these microservices using Kubernetes. That’s exactly what Kubernetes is meant for.

To run the hello-boot microservice:

kubectl run hello-boot — image=chakrar27/helloboot — port=8080

where “chakrar27/helloboot” is my image name for the spring-boot microservice. The above command results in the following output

replicationcontroller “hello-boot” created

You can check the status of the pod by typing

kubectl get pods ->-> it would show something like this
NAME READY STATUS RESTARTS AGE
 hello-boot-jvdz3 1/1 Running 0 3m

Once the replication controller is created, next task is to create a “service” out of it so that it’s available to the external resources. You do that by

kubectl expose rc hello-boot — type=NodePort
 -> service “hello-boot” exposed

Here, the type=NodePort indicates that this service is accessible on a port visible to the resources external to the kubernetes cluster.

Once the service is created, you can check the details of the service

kubectl describe service hello-boot ->-> which emits
Name: hello-boot
Namespace: default
Labels: run=hello-boot
Selector: run=hello-boot
Type: NodePort
IP: 10.0.0.29
Port: <unnamed> 8080/TCP
NodePort: <unnamed> 31226/TCP
Endpoints: 172.17.0.4:8080
Session Affinity: None
No events.

To get the service url, type

minikube service — url=true hello-boot which gives something similar to this
http://192.168.99.104:31226

A curl on the above url gives the following output as expected

curl -i http://192.168.99.104:31226/greet?name=Mike
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 13
Date: Mon, 06 Mar 2017 01:09:50 GMT
Hello, Mike!

Run containers at scale

We saw how to run a single instance of a docker container using kubernetes. One significant feature of Kubernetes allows us to deploy containers at scale using something known as “replica-s”. In order to scale the hello-boot microservice to let’s say five instances, all I need to do is

kubectl run hello-boot — replicas=5 — labels=”run=load-balanced-helloboot” — image=chakrar27/helloboot — port=8080

Check on Pods

C:\minikube>kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-boot-dtnx7 1/1 Running 0 10s
hello-boot-lzvsm 1/1 Running 0 3h
hello-boot-r2xnt 1/1 Running 0 3h
hello-boot-rx75m 1/1 Running 0 3h
hello-boot-w0qsk 1/1 Running 0 3h

Create the service

kubectl expose rc hello-boot — type=NodePort

Describe the service

C:\minikube>kubectl describe service hello-boot
Name: hello-boot
Namespace: default
Labels: run=load-balanced-helloboot
Selector: run=load-balanced-helloboot
Type: NodePort
IP: 10.0.0.185
Port: <unnamed> 8080/TCP
NodePort: <unnamed> 32447/TCP
Endpoints: 172.17.0.14:8080,172.17.0.4:8080,172.17.0.5:8080 + 2 mor
e…
Session Affinity: None
No events.

Since there are 5 pods, it’s showing 5 different ip endpoints. But these ip addresses are all internal within the cluster.

To get the external service url, do

C:\minikube>minikube service — url=true hello-boot
http://192.168.99.104:32447

Now, test with curl using the above url:

C:\>curl -i http://192.168.99.104:32447/greet?name=Peter
Output:
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 13
Date: Mon, 06 Mar 2017 09:14:16 GMT
Hello, Peter!

Now, you have successfully scaled and deployed a spring-boot microservice and is serving traffic using 5 instances of the service.

In a similar fashion, you can do scale and deploy the node js microservice as well. The commands for my example are furnished below (It runs on port 8081)

kubectl run hello-node — replicas=5 — labels=”run=load-balanced-hellonode” — image=chakrar27/hellonode:hello_nodejs — port=8081
kubectl expose rc hello-node — type=NodePort
minikube service — url=true hello-node

Kubectl get pods give the following:

C:\minikube>kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-boot-dtnx7 1/1 Running 0 2h
hello-boot-lzvsm 1/1 Running 0 6h
hello-boot-r2xnt 1/1 Running 0 6h
hello-boot-rx75m 1/1 Running 0 6h
hello-boot-w0qsk 1/1 Running 0 6h
hello-node-n6wcr 1/1 Running 0 3h
hello-node-pncfm 1/1 Running 0 3h
hello-node-snnft 1/1 Running 0 3h
hello-node-wxz1h 1/1 Running 0 3h
hello-node-xs6vp 1/1 Running 0 3h

We can see set of 5 pods for each of the micro services.

You can also view the Kubernetes dashboard. To launch the dashboard, type

minikube.exe dashboard — url=true

>->

http://192.168.99.104:30000

Navigate to the above url in the browser. You can check the Replication Controllers

and the Services


Troubleshooting

While setting up the minikube cluster, if you encounter an error as this

minikube.exe start
Starting local Kubernetes cluster...
Kubernetes is available at https://192.168.99.100:8443.
Kubectl is now configured to use the cluster.
PS C:\Users\aprindle\Downloads> kubectl.exe get po
Unable to connect to the server: dial tcp 127.0.0.1:8080: ConnectEx tcp: No connection could be made because the target
machine actively refused it.

then do the following steps. I actually faced this error myself.

set KUBECONFIG=”C:\Users\<username>\.kube\config
kubectl config set-context minikube

Basically, you are resetting the context here. This should solve the problem.