Kubernetes

What?

"Is a deployment automation, scaling and management framwork for containerized applications"

History?

* Kubernetes roots come from project Borg used for deployment automation at google.
* It is considered the most effective solution based on its success at Google running the search engine at a large scale.

Why?

The Problem:
* Containers (more here:https://gcallah.github.io/DevOps/deployment/dockerSwarm.html) simplied the deployment of an application or bringing up a service across various environments. When the application spans large scale or bed of users, A problem or challenge that arose is the management, recovery, scale, update of the container.
The Solution:
* The elegant solution would be to build an automation framework that handles all of these requirements and maintains the service at scale.
* The solution had to schedule, measure and allocate resources for required applications, deploy and maintain them as a cluster service.
* The solution => Kubernetes

How?

* Master - Worker Design

Components:

* Kubernetes Cluster Service - Manage the pods, create, deploy, scale, recovery from the configuration
* Kubelet Process - communication between Kubernetes cluster service and workers
* Kubernetes API - Provision to talk to the container service
* Workers - nodes or machines to run the container
* Configuration
* Pods - a very basic or smallest unit of deployment

Implementation:

Kuberenets Locally
* Using MiniKube, a local kubernetes could be setup with a virtual machine like virutal box or vmware workstation.
Basic Kubernetes Cluster Implementation as a Simple Project for the Course:
* Deploy the gcallah/DevOps static website as a cluster service with AWS or any machines in the cloud.
* Pod1 - git-sync(gcallah/devops) pulling latest updates + Pod2 - nginx container ==> Kubernetes Cluster Service ==> AWS Worker machines

Setup:

* Install minikube
* Install any hypervisor like virtualbox or vmware

Configuration:

* Docker Compose used as a base and reference to create deployment and service configuration

            
                
              # A Docker compose to create the application with two containers
              # * 1. Nginx container
              # * 2. git-sync container
              # Reference from: https://hub.docker.com/r/openweb/git-sync/
              # Run with `docker-compose up -d` once the dockerCompose
              file is created
              version: "2"
                services:
                  nginx:
                    image: nginx:latest
                    ports:
                      - "8080:80"
                    volumes:
                      - website_sources:/usr/share/nginx/html:z
                    depends_on:
                      - git-sync
                    restart: always
                  git-sync:
                    image: openweb/git-sync:0.0.1
                    environment:
                      GIT_SYNC_REPO: "https://github.com/gcallah/DevOps"
                      GIT_SYNC_DEST: "/git"
                      GIT_SYNC_BRANCH: "master"
                      GIT_SYNC_REV: "FETCH_HEAD"
                      GIT_SYNC_WAIT: "100"
                    volumes:
                      - website_sources:/git:z
                    restart: always
                volumes:
                  website_sources:
                driver: local
                
            
          
*

Used "kompose" to convert docker compose -> respective kubernetes deployment and service configuration. Use this command ./kompose convert -f ../docker-compose.yml to get the configs.


Deployment configuration

              
                
                # Deployment YAML Configuration File
                # * Pod specification of using 2 containers with a shared volume
                # References:
                # * http://paulbakker.io/kubernetes/kubernetes-static-sites/
                # * https://github.com/dysinger/learn-minikube
                # * https://github.com/kubernetes/kompose
                kind: Deployment
                apiVersion: extensions/v1beta1
                metadata:
                  name: devopswebsite
                spec:
                  replicas: 2
                  template:
                    metadata:
                       labels:
                          app: devopswebsite
                    spec:
                      containers:
                      - image: nginx
                        name: nginx
                        ports:
                        - containerPort: 80
                        volumeMounts:
                        - mountPath: "/usr/share/nginx/html"
                          name: www
                          readOnly: true
                      - image: paulbakker/git-sync
                        name: git-sync
                        imagePullPolicy: Always
                        env:
                        - name: GIT_SYNC_REPO
                          value: https://github.com/gcallah/DevOps
                        - name: GIT_SYNC_WAIT
                          value: '10'
                        volumeMounts:
                        - mountPath: "/git"
                          name: www
                      volumes:
                      - name: www
                        emptyDir: {}
                    
              
            


Service configuration

              
                
                apiVersion: v1
                kind: Service
                metadata:
                  name: devopswebsite
                spec:
                    type: NodePort
                    ports:
                      - name: http
                        port: 80
                        protocol: TCP
                    selector:
                        app: devopswebsite
                
              
            

Steps:


Minikube Start

            
              
              MacBook-Pro:devOps deployment$  minikube start
              There is a newer version of minikube available (v0.26.1).  Download it here:
              https://github.com/kubernetes/minikube/releases/tag/v0.26.1

              To disable this notification, run the following:
              minikube config set WantUpdateNotification false
              Starting local Kubernetes v1.9.4 cluster...
              Starting VM...
              Getting VM IP address...
              Moving files into cluster...
              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.

              
            
          


Status Verification

              
                
                  MacBook-Pro:devOps deployment$  kubectl get services
                  NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
                  kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   4m

                  MacBook-Pro:devOps deployment$  kubectl get pods
                  No resources found.

                
              
            


Deploy Pods and Create Service

                
                  
                    MacBook-Pro:devOps deployment$  kubectl create -f kubernetesFiles/staticWebServerDeployment.yaml
                    deployment.extensions "devopswebsite" created
                    MacBook-Pro:devOps deployment$  kubectl create -f kubernetesFiles/staticWebServerService.yaml
                    service "devopswebsite" created
                  
                
              


Status and Output Verification

                    
                      
                        MacBook-Pro:devOps deployment$ kubectl get pods
                        NAME                             READY     STATUS              RESTARTS   AGE
                        devopswebsite-646c8885d5-4k5ss   0/2       ContainerCreating   0          38s
                        devopswebsite-646c8885d5-9qxz8   0/2       ContainerCreating   0          38s
                        MacBook-Pro:devOps deployment$ kubectl get pods
                        NAME                             READY     STATUS    RESTARTS   AGE
                        devopswebsite-646c8885d5-4k5ss   2/2       Running   0          1m
                        devopswebsite-646c8885d5-9qxz8   2/2       Running   0          1m
                        MacBook-Pro:devOps deployment$ kubectl get services
                        NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
                        devopswebsite   NodePort    10.107.130.41   <none>        80:31897/TCP   2m
                        kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP        7m
                        MacBook-Pro:devOps deployment$ kubectl get services
                        NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
                        devopswebsite   NodePort    10.107.130.41   <none>        80:31897/TCP   4m
                        kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP        9m

                        MacBook-Pro:devOps deployment$ curl -g http://192.168.99.100:31897
                        <!DOCTYPE html>
                        <html>
                        
                        <head>
                          <meta charset="utf-8"/>
                          ...
                      
                    
                  

References and Source:

* Some presentation notes
* kubernetes site
* kubernetes in 5 mins
* Kubernetes Static Site reference
* Kubernetes on AWS
* DockerCompose
* DockerCompose2
* Kompose
* Kubernetes101