Running Pods, Replication Controllers on Kubernetes and Understanding Services

kumar rishabh
7 min readJul 31, 2020

Hands on with simple Kubernetes commands by installing minikube on your local system

So now, as we are aware of Kubernetes basics, it is the time to move ahead and implement the learning. In case you cannot recall the basics, get a quick go through at “Kubernetes in Testing

Pre-Requisites

  1. VS Code

Installing Kubernetes(Windows OS)

The first Pre-requisite is to have the Hyper-v enabled on you s system, Hyper-v is preinstalled in the windows 10 Enterprise, Pro, or Education versions. See Microsoft assistance on the same here.

Once it is up, we would be setting up another software called MiniKubes. Minikube is a tool that runs a single-node Kubernetes cluster in a virtual machine on your personal computer.

To install the software quickly, it is recommended to use chocolaty software. chocolaty provides software management automation. If you are from a linux background, chocolaty is analogous to “yum” in RHEL and “apt-get” in ubuntu world. Install chocolaty here.

Now to install minikube with chocolaty(Run As Administrator). just issue following commands :-

choco install minikube

Also, we sill be using the kubernetes cli (kubectl) to interact with kubernetes. following chocolaty command will help.

choco install kubernetes-cli

To test that the installations are ok, just type “minikube” on your command line and check for the help manual. Similarly, It should work up for “kubectl”.

Thats It!! We are done with the installation part. Lets see things in action now.

Starting Minkube and basic kubernetes commands

Now to start a single-node cluster, just start with :-

minikube start

After few minutes of wait, the following message should be seen(This message differs for different minikube versions, just be sure that there is no Errors. )

Successful Minikube start

More commands to try are :-

minikube status

minikube dashboard

See what they do, we will learn about them later.

Working With Pods

Pods, can be started through yml files, and creating a pod, service, deployment etc using kubectl.

Configuring VS Code for Kubernetes yml

For this you need some Extensions to be installed in your VS Code. Do Ctrl+Shift+X to add extension and then add following extensions:-

Extensions Needed

Now, If you are done with this, Create a yml file (Add new file and save it with .yml extension). Once done, type Pod and click enter on the suggestion:-

Kubernetes — suggestions

And see the yml template is ready.

Let us understand this by containerizing a linux machine. So first step we need to do is look for a docker container image.

If you see the Pod YML template, there are few things we need to add, They are :-

name — lets make it myfirstpod

containerPort : This is the port you want to expose. Lets make it 8090 for now.

image — This is really important that what you want your pod to be. Lets start with a small linux image. For the image lets visit docker website to fetch images. On the home page Search Linux and then select any image and read description for the tags likeubuntu:latest debian:latest or alpine:3.7.or you can google as say alpine docker image and you will find many different images.

Let us keep it ubuntu:latest

Save the file, go to Command prompt and run kubectl create -f <path with the YML file name>

Easy.. Isnt’it??

Now how can i see the Pod.. Lets try kubectl get pods

And The completed status makes your Pod setup complete. However it crashed during the run with “CrashLoopBackOff” status(The restart count kept on increasing). We may have to troubleshoot the issue. Probably this is the space issue. I tried using other images, finally got success with “nginx” image after removing the limits. And finally Pod was “Running”.

Following is the YML file used :-

Further, we may try some more commands such as “ kubectl describe pod myfirtspod” — This provides you the details of the pod including the IP and the open port. But wait can you ping this IP? Try it. You won’t be able to do that as these pods are not exposed to you directly. Now lets try to create controllers, services and deployments for selenium grid and try to use these pods. And you can delete pod as “ kubectl delete pod myfirtspod”

Replication Controller

Now Lets first start with creating a Replication Controller and see its advantage. We will use the “nginx” image. So we will create a new yml file and type Replication Controller to get template. so every thing remains same, only Replicas : 3 is added where 3 is number of parallel images yo uwant to run. This will help us create nodes later.

I used following yml :-

to get :-

Now, why use Replication Controller, if we can create multiple pods? Let us see this and try to delete a pod created by Replication Controller and then see the pods. i.e.

The replication controller assure that the 3 replicas are up and running everytime. If some pod fails or crash, a new fresh replica replaces it.

New commands to try “kubectl get ReplicationController” “kubectl describe ReplicationController myfirstrc” and “kubectl delete ReplicationController myfirstrc”

Also now if we want to change the number of replicas on run time, then we change the yml file and start “kubectl apply -f <path and name of the yml>”. this will update the setting.

Services

In Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them (sometimes this pattern is called a micro-service).

Suppose Pod A communicates with Pod B or Pod C, which are running using Replication controller RC as :-

Here, suppose if POD C crashes, then Replication controller will takeover and create a Pod D whose IP will be changed, and hence, we cannot rely on IP for communication sake, then we need a Service which keeps the record of the pods and acts as a mediator to get the work done. In this case Pod A will communicate to the Service only i.e.

Services will keep track of Pods, if they change, Service will keep track of the pods. Pod A will be connecting to the service without let the Pod A to bother about the other pods in use. Pods are indentified with the Labels(called as label selector).

Kubernetes provides following type of services :-

  • ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.
  • NodePort: Exposes the Service on each Node’s IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You’ll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort>.
  • LoadBalancer: Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.
  • ExternalName: Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record

We will be using the Node Port Service in our Selenium Grid setup.

So Since now we are aware of some basic kubectl commands, we must have a look on the Kubectl cheat sheet.

Up Next, We will be understanding Kubernetes Deployments and will create a Kubernetes Deployment to start hub and then will connect our nodes to hub via services and run our tests on Kubernetes using selenium official images.

--

--

kumar rishabh

A software test enthusiast develops, maintain and consult the testing solutions for a product. specialized in automating the tests. Firm believer of Agile.