Thursday, 9 July 2020

Kubernetes: Hello World Application

This is a simple hello world application. Do not worry about the technical terms that come in this tutorial, I will explain all these things in detail later.

 

Step 1: Start the Virtualbox.

 

Step 2: Execute the command ‘minikube start’ to start minikube service.


Once minikube service started, execute the command ‘virtualbox’ to confirm that minikube is up and running.

$minikube start
😄  minikube v1.9.2 on Darwin 10.14.6
✨  Using the virtualbox driver based on existing profile
👍  Starting control plane node m01 in cluster minikube
🔄  Restarting existing virtualbox VM for "minikube" ...
🐳  Preparing Kubernetes v1.18.0 on Docker 19.03.8 ...
🌟  Enabling addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube"

Execute the command ‘virtualbox’ to confirm that minikube is up and running.

How to confirm minikube and VirtualBox are talking to each other?

Execute the command ‘kubectl get nodes’.

$kubectl get nodes
NAME       STATUS   ROLES    AGE     VERSION
minikube   Ready    master   2m36s   v1.18.0

Now everything is set up, lets run Hello World Application.

 

Create a deployment

Use the command 'kubectl create deployment' to create a deployment that manages a pod.

 

Example

kubectl create deployment hello-node --image=jboss/wildfly

 

Above statement pulls the image ‘jboss/wildfly’ from an image registry like Dokcerhub and creates a deployment.

$kubectl create deployment hello-node --image=jboss/wildfly
deployment.apps/hello-node created

View the deployment by executing the below command.

kubectl get deployments

$kubectl get deployments
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
hello-node   1/1     1            1           17s

View the pods by executing the below command.

$kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
hello-node-59fddbb58-rv8wt   1/1     Running   0          35s

Create a Service

By default, pod is accessible by its internal IP address within Kubernetes cluster. To make the pod accessible outside of the Kubernetes network, we need to expose pod as a Kubernetes service.

$kubectl expose deployment hello-node --type=LoadBalancer --port=8080
service/hello-node exposed

View the created service.

$kubectl get services
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.98.157.76   <pending>     8080:31007/TCP   22s
kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          76m

As you see hello-node service is available on port 31007.

 

Execute the command ‘minikube service hello-node’ to open hello-node service in the browser.

$minikube service hello-node
|-----------|------------|-------------|-----------------------------|
| NAMESPACE |    NAME    | TARGET PORT |             URL             |
|-----------|------------|-------------|-----------------------------|
| default   | hello-node |        8080 | http://192.168.99.100:31007 |
|-----------|------------|-------------|-----------------------------|
🎉  Opening service default/hello-node in default browser...

How to see everything that is deployed?

Execute the command ‘kubectl get all’ to see all that is deployed.

$kubectl get all
NAME                             READY   STATUS    RESTARTS   AGE
pod/hello-node-59fddbb58-rv8wt   1/1     Running   0          9m57s

NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/hello-node   LoadBalancer   10.98.157.76   <pending>     8080:31007/TCP   8m23s
service/kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          84m

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-node   1/1     1            1           9m57s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-node-59fddbb58   1         1         1       9m57s

From the output, you can confirm that pods, services, deployments, and replicaset are created.

 

Get yaml of the deployment

Execute the below command to see the yaml of the deployment.

 

kubectl get deploy hello-node -o yaml

$kubectl get deploy hello-node -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2020-05-06T11:10:02Z"
  generation: 1
  labels:
    app: hello-node
  managedFields:
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:app: {}
      f:spec:
        f:progressDeadlineSeconds: {}
        f:replicas: {}
        f:revisionHistoryLimit: {}
        f:selector:
          f:matchLabels:
            .: {}
            f:app: {}
        f:strategy:
          f:rollingUpdate:
            .: {}
            f:maxSurge: {}
            f:maxUnavailable: {}
          f:type: {}
        f:template:
          f:metadata:
            f:labels:
              .: {}
              f:app: {}
          f:spec:
            f:containers:
              k:{"name":"wildfly"}:
                .: {}
                f:image: {}
                f:imagePullPolicy: {}
                f:name: {}
                f:resources: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
            f:terminationGracePeriodSeconds: {}
    manager: kubectl
    operation: Update
    time: "2020-05-06T11:10:02Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:deployment.kubernetes.io/revision: {}
      f:status:
        f:availableReplicas: {}
        f:conditions:
          .: {}
          k:{"type":"Available"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
          k:{"type":"Progressing"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
        f:observedGeneration: {}
        f:readyReplicas: {}
        f:replicas: {}
        f:updatedReplicas: {}
    manager: kube-controller-manager
    operation: Update
    time: "2020-05-06T11:10:08Z"
  name: hello-node
  namespace: default
  resourceVersion: "7725"
  selfLink: /apis/apps/v1/namespaces/default/deployments/hello-node
  uid: 65df08ee-dfaa-4595-866b-73e31d10b876
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: hello-node
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hello-node
    spec:
      containers:
      - image: jboss/wildfly
        imagePullPolicy: Always
        name: wildfly
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-05-06T11:10:08Z"
    lastUpdateTime: "2020-05-06T11:10:08Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-05-06T11:10:02Z"
    lastUpdateTime: "2020-05-06T11:10:08Z"
    message: ReplicaSet "hello-node-59fddbb58" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
$

Get json of the deployment

Execute the below command to see the JSON of deployment.

kubectl get deploy hello-node -o json

$kubectl get deploy hello-node -o json
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        },
        "creationTimestamp": "2020-05-06T11:10:02Z",
        "generation": 1,
        "labels": {
            "app": "hello-node"
        },
        "managedFields": [
            {
                "apiVersion": "apps/v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:labels": {
                            ".": {},
                            "f:app": {}
                        }
                    },
                    "f:spec": {
                        "f:progressDeadlineSeconds": {},
                        "f:replicas": {},
                        "f:revisionHistoryLimit": {},
                        "f:selector": {
                            "f:matchLabels": {
                                ".": {},
                                "f:app": {}
                            }
                        },
                        "f:strategy": {
                            "f:rollingUpdate": {
                                ".": {},
                                "f:maxSurge": {},
                                "f:maxUnavailable": {}
                            },
                            "f:type": {}
                        },
                        "f:template": {
                            "f:metadata": {
                                "f:labels": {
                                    ".": {},
                                    "f:app": {}
                                }
                            },
                            "f:spec": {
                                "f:containers": {
                                    "k:{\"name\":\"wildfly\"}": {
                                        ".": {},
                                        "f:image": {},
                                        "f:imagePullPolicy": {},
                                        "f:name": {},
                                        "f:resources": {},
                                        "f:terminationMessagePath": {},
                                        "f:terminationMessagePolicy": {}
                                    }
                                },
                                "f:dnsPolicy": {},
                                "f:restartPolicy": {},
                                "f:schedulerName": {},
                                "f:securityContext": {},
                                "f:terminationGracePeriodSeconds": {}
                            }
                        }
                    }
                },
                "manager": "kubectl",
                "operation": "Update",
                "time": "2020-05-06T11:10:02Z"
            },
            {
                "apiVersion": "apps/v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:annotations": {
                            ".": {},
                            "f:deployment.kubernetes.io/revision": {}
                        }
                    },
                    "f:status": {
                        "f:availableReplicas": {},
                        "f:conditions": {
                            ".": {},
                            "k:{\"type\":\"Available\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:lastUpdateTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            },
                            "k:{\"type\":\"Progressing\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:lastUpdateTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            }
                        },
                        "f:observedGeneration": {},
                        "f:readyReplicas": {},
                        "f:replicas": {},
                        "f:updatedReplicas": {}
                    }
                },
                "manager": "kube-controller-manager",
                "operation": "Update",
                "time": "2020-05-06T11:10:08Z"
            }
        ],
        "name": "hello-node",
        "namespace": "default",
        "resourceVersion": "7725",
        "selfLink": "/apis/apps/v1/namespaces/default/deployments/hello-node",
        "uid": "65df08ee-dfaa-4595-866b-73e31d10b876"
    },
    "spec": {
        "progressDeadlineSeconds": 600,
        "replicas": 1,
        "revisionHistoryLimit": 10,
        "selector": {
            "matchLabels": {
                "app": "hello-node"
            }
        },
        "strategy": {
            "rollingUpdate": {
                "maxSurge": "25%",
                "maxUnavailable": "25%"
            },
            "type": "RollingUpdate"
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app": "hello-node"
                }
            },
            "spec": {
                "containers": [
                    {
                        "image": "jboss/wildfly",
                        "imagePullPolicy": "Always",
                        "name": "wildfly",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File"
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "terminationGracePeriodSeconds": 30
            }
        }
    },
    "status": {
        "availableReplicas": 1,
        "conditions": [
            {
                "lastTransitionTime": "2020-05-06T11:10:08Z",
                "lastUpdateTime": "2020-05-06T11:10:08Z",
                "message": "Deployment has minimum availability.",
                "reason": "MinimumReplicasAvailable",
                "status": "True",
                "type": "Available"
            },
            {
                "lastTransitionTime": "2020-05-06T11:10:02Z",
                "lastUpdateTime": "2020-05-06T11:10:08Z",
                "message": "ReplicaSet \"hello-node-59fddbb58\" has successfully progressed.",
                "reason": "NewReplicaSetAvailable",
                "status": "True",
                "type": "Progressing"
            }
        ],
        "observedGeneration": 1,
        "readyReplicas": 1,
        "replicas": 1,
        "updatedReplicas": 1
    }
}




Previous                                                    Next                                                    Home

Kubernetes: setup minikube in local

What is Minikube?

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day.

 

Prerequisite

a.   Install Docker (https://self-learning-java-tutorial.blogspot.com/2019/05/installing-docker-in-mac.html)

b.   Install VirtualBox (https://www.virtualbox.org/wiki/Downloads)

 

Install Kubectl

Step 1: Install Kubectl.

 

Read steps in the below link and install kubectl.

https://kubernetes.io/docs/tasks/tools/install-kubectl/

 

Step 2: After successful installation, execute the command ‘kubectl version --client'.

$kubectl version --client
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.2", GitCommit:"52c56ce7a8272c798dbc29846288d7cd9fbae032", GitTreeState:"clean", BuildDate:"2020-04-16T11:56:40Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"darwin/amd64"}

Install Minikube

Read step in the below link and install minikube.

https://kubernetes.io/docs/tasks/tools/install-minikube/

 

Execute the command ‘minikube version’ to validate the installation.

$minikube version
minikube version: v1.9.2
commit: 93af9c1e43cab9618e301bc9fa720c63d5efa393





Previous                                                    Next                                                    Home

Tuesday, 7 July 2020

Kuberentes Architecture

Very Basic view of Kubernetes Architecture


In simple terms, Kubernetes system has a master node and cluster of worker nodes. Developer writes application definition (or) application descriptor file, which has information about containers, images, replicas, deployment strategies etc., and submit this file to the master node. Master node process this descriptor file and deploy the application on these cluster of worker nodes.

 

Master Node

Master Node is responsible for managing the entire Kubernetes System.

 

Worker Nodes

These are the nodes, where the application deploys.

 

Different components of Master Node

Kubernetes Master node owns ‘Kubernetes Control Plane’ which is responsible for controlling and managing of entire Kubernetes system.


Control Plane consists of the below components.

a.   API Server (Kube-apiserver)

b.   etcd

c.    kube-scheduler

d.   kube controller manager

e.   Cloud Controller Manager

 

Control Plane components can run on a single machine or can run on a cluster to achieve high availability. Control pane components talk to API Server and do not talk to each other directly.

 

API Server

It exposes the Kubernetes API and designed to scale horizontally. Using API server, you can configure data for API objects like Pods, Replication Sets, Deployments, Jobs, etc.,

 

etcd

It is a distributed key-value storage used by Kubernetes to persist the data. You can go through the below link to know more information about etcd.

https://etcd.io/docs/

 

kube-scheduler

It watches for new Pod requests and selects a node for them to run on. It considers multiple factors (individual and collective resource requirements, hardware/software/policy constraints, affinity and anti-affinity specifications, data locality, inter-workload interference, and deadlines) for scheduling decisions.

 

Kube-Controller-Manager

This component is responsible to run Controller processes.

 

Following are the controller processes.

a.   Node Controller: Notice and respond when a node goes down

b.   Replication Controller: Responsible to maintain the desired number of Pods for every Replication controller object.

c.    Endpoints Controller: Populate Endpoint object (endpoint object joins services and Pods)

d.   Service Account and Token Controller: Create a default service account and API access tokens for namespaces.

 

Cloud Controller Manager

This component embeds cloud-specific control logic. It is used to link Kubernetes cluster into a specific Cloud Provider’s API.

 

Following controllers have Cloud Provider dependencies.

 

a.   Node controller: For checking the cloud provider to determine if a node has been deleted in the cloud after it stops responding

b.   Route controller: For setting up routes in the underlying cloud infrastructure

c.    Service controller: For creating, updating and deleting cloud provider load balancers

 

How to check the status of Control Pane Components?

'kubectl get componentstatuses' command is used to get the status of Control Pane Components.

$kubectl get componentstatuses
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}

Different Components of Worker Node


Worker Node has three components.

a.   kubelet

b.   kube-proxy

c.    Container Runtime

These components run on every worker node.

 

kubelet

Kubelet is an agent that runs on every worker node in the cluster. It talks and API Server and makes sure that containers running on a Pod.

 

kube-proxy

It is a network proxy that runs on every worker node in the cluster and load balance the network traffic between application components. It maintains network rules on the nodes, which allow network communication to your Pods from network sessions inside or outside of your cluster.

 

Container Runtime

It is responsible for running containers. It can be Docker, rkt, or any other container runtime.

 

Complete architecture looks like below.

How the process flow happen?

a. Create an Application Description file and submit it to Api Server. This description file contains  information about images, number of replicas, deployment strategy, volume mounts, secrets, how to expose these applications etc.,

 

b. Once the Api Server receives this description file, ‘Kube Scheduler’ look into these new Pod requests and select a node for them to run on.

 

c. ‘kubelet’ agent on the worker node gives an instruction to ‘Container Runtime’ to pull the image and start the containers.

 

d. When a user request for container logs, API server receives the request and send the request to kubelet which will get the logs.

 

Note

a. Will a master node run Pods?

No



Previous                                                    Next                                                    Home

Introduction to Kubernetes

What is Kubernetes?

Kubernetes (called as k8s) is one of the popular open-source container Orchestrator built by Google (It is based on Google internal system called Google Borg). Orchestration is a process of automatic configuration, coordination, and managing of computer systems and software.

 

Using Kubernetes, you can deploy and manage containerized applications. Kubernetes use Linux container technologies like Docker to provide isolation of running applications.

 

Why Kubernetes?

a.   A typical application is divided into multiple microservice and each microservice will run on one or more containers.

b.   One application may run on multiple docker containers and across multiple computing systems.

c.    Frequent upgrades of application with multiple versions possible.

d.   We may need to revert the changes and redeploy the applications if required.

e.   Based on the demand we may need to scale up or scale down the containers.

f.     How to establish communication between multiple containerized applications

g.   How will you start a new container when one of the containers went down?

h.   How to schedule the jobs that spin up containers automatically?

 

How can we address all the above points?

The Answer is Kubernetes. Kubernetes abstracts the hardware infrastructures. Nowadays, Kubernetes becomes the de-facto standard to deploy and manage distributed applications in both on-premise and Cloud.

 


Features of Container Orchestrator

a.   Provision computing systems or hosts

b.   Start a container in host

c.    Restart failing containers

d.   Establish communication link between containers.

e.   Support Autoscaling. Scale up/down the containers based on need.

 

Following are the major advantage of Kubernetes.

a.   It is open-source and highly used in the current market. Backed by a strong open source community. It is governed by the Cloud Native Computing Foundation (CNCF).

b.   You can deploy and manage containers on a cluster of virtual machines, public cloud, and private data centers using Kubernetes.

c.    Multi-host container scheduling.

d.   Kubernetes features a backward compatible for fewer versions and all the APIs are versioned.

e.   Multi-Region deployments are possible.

f.     It is highly scalable. Kubernetes 1.8 support 5000 nodes clusters and 150000 pods. A node is a worker machine in Kubernetes, It can be a Virtual or Physical machine. A pod runs on a node. One node can have multiple pods.

g.   New worker nodes can seamlessly register with Kubernetes master node to scale the environment.

h.   Service discovery is available out of the box.

i.     Kubernetes support Persistent storage. Pods can use this storage to store the data. Data stored in the persistent storage can be retained on pod restarts and failures.

j.     Rolling updates are supported. Rolling updates make the deployment possible with zero downtime.

k.    Application rollbacks are supported by Kubernetes.

l.     Built-in monitoring and health check capability

m. Secrets are managed by mounting as data volumes or Environment variables.

 

Typical Application cycle is like, you develop an application and create a docker image and you use Kubernetes to deploy the docker images.

 

Other Orchestration Engines

Apart from Kubernetes, following are the most used Orchestration Engines.

a.   Docker Swarm

b.   MESOS

c.    RANCHER

d.   Amazon EC2 container service

e.   Nomad

Previous                                                    Next                                                    Home