Discovering Kubernetes

Kubernetes is a distributed cluster management application that makes easy to deploy, manage and scale applications in a public or private cloud. It moves away the complexity of an infrastructure by abstracting the cluster into a single gigantic machine with plenty of ressources.

Core Concepts

Pod: unit of deployment

A pod is collection of one or more container images, like Docker images, that work together. A pod is deployed as a single unit. The container technology makes an application easy to deploy anywhere regardless of the operating system.

pod

A pod typically runs on a worker node. Each pod of the cluster has an unique IP in order to exchange traffic. But a pod IP is only visible inside the cluster. In addition, a pod is short lived, making their IP possibly not useful to rely on. In order to access a pod consistently and externally, we need to use an object called a service. A pod can also contain other resources shared between its container application.

Service: external endpoint

A service is a way of grouping a set of nods that have common characteristics. The service has an IP that is visible from the outside. A service will automatically select its pods based on their label. The IP address is durable during the life of the Kubernetes cluster and can be used safely by the outside.

service

A service will typically load balance traffic among its pods.

Master / Workers architecture

Kubernetes has a master slave distributed architecture. The master node is the cockpit of the cluster and interact with a client through its API service. The worker nodes are responsible for running the pods through an agent called kubelet. The application needed to run container images is also installed in each node.

master_worker

Create a scalable app with minikube

In order to understand the concepts of kubernetes, let’s use a small example. In this example, we create a python server application, and deploy it in a small kubernetes environment that runs on a single VM: minikube. Minikube is very useful because it allows to test kubernetes locally without the actual need of a real cluster. After its deployment, we will expose the app to the outside and scale it up.

Create the application simple-server.py

Our application is a simple REST server with a get method that just counts the number of requests sent to that particular server during its lifetime.

Start minikube

After installing minikube and its prerequisites (including a VM driver like virtualbox) we can start it.
minikube start

Create a docker image for the application

In order to create a container image of our app in the local repository, we create the following Dockerfile next to our .py file.

Then we need to point to minikube’s docker daemon:
eval $(minikube docker-env)

We can now build the docker image of our app (the last dot is important). We build our image on top of python:2.7.15-alpine3.7 wich is somehow lightweight.

cd /dir-with-py-file/ docker build -t simple-server:v1 .

Create a docker image for the application

We can now create a deployment for the application. This will actually deploy and run one pod containing our image.

Note: “–image-pull-policy=Never” indicates that we use the image in our local repository
kubectl run simple-server --image=simple-server:v1 --port=8080 --image-pull-policy=Never

Create a service

We create a service in order to expose our pod to the outside world. This service is of type “LoadBalancer”.

kubectl expose deployment simple-server --type=LoadBalancer

Scale up the application

We can now scale up our application by creating multiples replicas.
kubectl scale deployments/simple-server --replicas=3

And run this command to call the service.
minikube service simple-server

If we refresh the broswer multiple times, we understand that the same pod is not always called and that load balancing works.

Clean up

We can now clean up what we did.

Clean kubernetes resources:
kubectl delete service simple-server
kubectl delete deployment simple-server

Remove docker image and reset docker environment:
docker rmi simple-server:v1 -f
eval $(minikube docker-env -u)

Stop and delete minikube
minikube stop
minikube delete

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s