Wednesday, February 14, 2018

Kubernetes getting started: Minikube

0. Intro: What is Kubernetes?

From wiki:
Kubernetes (commonly referred to as "K8s"[3]) is an open-source system for automating deployment, scaling and management of containerized applications[4] that was originally designed by Google and now maintained by the Cloud Native Computing Foundation. It aims to provide a "platform for automating deployment, scaling, and operations of application containers across clusters of hosts".[3] It works with a range of container tools, including Docker.

In shorts, kubernetes is a tool which allows you to upload in it many docker images, link them by services, scale and configure them.


1. Minikube:  simple image of kubernetes "to play". 

The simplest way to get familiar with kubernetes is Minikube. Let's install it.


1.1 Virtual box installation. 

It also can be running using HyperV, but I prefer Virtual box. It can be download from here.

1.2 Docker installation.

Theoretically, this step is optional, because minikube has it's own docker. So we can use public docker images without docker installation.  But it case of development, if we have to create new images - docker is needed. 

1.3 Minikube installation. 

It can be downloaded from here

1.4. Kubectl installation. 

It can be downloaded from here. Also don't miss the steps regarding putting it into system PATH variable. For example for linux it's a " sudo mv ./kubectl /usr/local/bin/kubectl"

Now let's check if everything is ok with minikube and kubectl:
$ minikube status
minikube: 
cluster: 
kubectl: 


$ kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.3", GitCommit:"d2835416544f298c919e2ead3be3d0864b52323b", GitTreeState:"clean", BuildDate:"2018-02-07T12:22:21Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?

They should produce similar output to listed above.


2. Minikube: warming up

First of all, we have to set it up: it should download needed ISO image and put it into VirtualBox.
For this, theoretically we have just to run:
$ minikube start
But if for some reason it's not working, you can try to point it to vm-driver in explicit way:
$ minikube start --vm-driver="virtualbox"

For me, this process failed with error:
The vboxdrv kernel module is not loaded. Either there is no module\n         available for the current kernel (4.9.0-5-amd64) or it failed to\n         load. Please recompile the kernel module and install it by\n\n           sudo /sbin/vboxconfig\n\n 

When I executed:
# /sbin/vboxconfig

If also failed, with complains regarding linux headers. 

So I installed headers:
# apt-get install linux-headers-amd64 linux-headers-4.9.0-5-amd64

Executed again: 
# /sbin/vboxconfig

And again:
$ minikube start
demien:minikube$ minikube start
Starting local Kubernetes v1.9.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Downloading localkube binary
 162.41 MB / 162.41 MB [============================================] 100.00% 0s
 65 B / 65 B [======================================================] 100.00% 0s
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.

Loading cached images from config file.

- finally! :)

To make sure it works we can run:
$ minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100


3. Minikube: Simple operations

3.1. Dashboard. 

To open dashboard:
$ minikube dashboard
Opening kubernetes dashboard in default browser...

Your browser should show something like this: 


From dashboard you can configure cluster pods, deployments, replicas, servers.


3.2 Minikube: terminal

We can open terminal on minikube by running: 
$ minikube ssh
In example below I checked if my home folder is mounted to kubernetes:




4. Kubectl: simple operations

More "high-level" operations, like listed above should be executed by "minikube" command, but "low-lovel" operations belongs to "kubectl". 

4.1. Context swithing

First of all it's better to make sure kubectl is connected to minikube by running: 
$ kubectl config use-context minikube
Switched to context "minikube".

4.2. Deployment

To deploy docker image to kubernetes, we have 2 options: deploy from command line, or by creation and upload of deployment descriptor file.  

First option is faster, but for only simple situations: 
$ kubectl run hello-nginx --image=nginx --port=80 --replicas=2
deployment "hello-nginx" created

For second option,  we have to create a deployment descriptor like this: 

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: hello-nginx
spec:
  selector:
    matchLabels:
      run: hello-nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      # unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is
      # generated from the deployment name
      labels:
        run: hello-nginx
    spec:
      containers:
      - name: hello-nginx
        image: nginx
        ports:
        - containerPort: 80

Most important things are at the end:
- image - name of the docker image which should be pulled from docker public registry
- containerPort - port which will be exposed outside

After creation we have to deploy this descriptor by running(deployment-demo.yml - file name):
$ kubectl apply -f ./deployment-demo.yml 
deployment "nginx-deployment" created

Now we can check in dashboard: in should be one deployment :




and it should be 2 pods, because we defined "2 replicas" in descriptor:


 

Pod in kubernetes is like a "logical host": it's a running docker image. We defined 1 image with replica=2, so kubernetes is running this image twice. 

4.3 Service

Uploaded docker image is up and running now. But  we can't use is "outside" so far. For this we need the service, which will expose running container port outside. 
For service creation we have 2 the same options: from command line and from descriptor file. 

From command line:

$ kubectl expose deployment hello-nginx --type=NodePort
service "hello-nginx" exposed
 
From descriptor file(with name service-demo.yml): 

apiVersion: v1
kind: Service
metadata:
  name: hello-nginx
  labels:
    run: hello-nginx
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080
    protocol: TCP
  selector:
    run: hello-nginx

File upload: 
$ kubectl apply -f ./service-demo.yml 
service "hello-nginx" created


Now we can check "service" menu: beside standard "kubernetes" service it should be our "hello-nginx": 


Also we can see the "outside" port here: 30080 - actually we defined it in descriptor. 
Now we can open this port using kubernetes ip and result should be: 




5. The end. 

No dubs, kubernetes is a great tool "to rule them all": to manage and configure running containers in a cluster. More tutorial can be found on official site: https://kubernetes.io/docs/tutorials/

No comments:

Post a Comment