Kubernetes Basics

Architecture


Source: https://x-team.com/blog/introduction-kubernetes-architecture/

Install Minikube

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

Install Kubectl

https://docs.docker.com/ee/ucp/user-access/kubectl/



Start minikube server. It acts like a single node kubernetes server.

$ minikube start
Starting local Kubernetes v1.10.0 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.


If you are running minikube inside the VM, use the following command.

$ minikube start --vm-driver=none


Set docker environment variables

$ minikube docker-env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/abulbasar/.minikube/certs"
export DOCKER_API_VERSION="1.35"
# Run this command to configure your shell:
# eval $(minikube docker-env)
$ eval $(minikube docker-env)


You can ssh into the VM by finding the IP (from kubectl config view) and using username "docker" password "tcuser":

$ minikube ip
192.168.XX.XX
$ ssh docker@192.168.XX.XX


Launch minikube dashboard

$ minikube dashboard


If you want to ssh into minikube,

$ minikube ssh


Otherwise, you can use username and password to login username "docker" password "tcuser" using IP address.

$ minikube ip


Test kubernetes with hello-minikube


Install the hello-minikube pod

The minikube project on GitHub offers a quick start demo which uses a pre-built Docker image hello-minikube. Since we started the minikube cluster already, we can skip the first step:


Let’s run the built-in hello-minikube pod. This will create a deployment for the pod:


$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
deployment "hello-minikube" created

We can inspect the pods and the deployments to verify these have been updated with the following commands:


$ kubectl get pods
NAME                             READY     STATUS              RESTARTS   AGE
hello-minikube-c8b6b4fdc-p2rzh   0/1       ContainerCreating   0          6s

$ kubectl get deployments
NAME             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-minikube   1         1         1            1           28s



In order to access the hello-minikube service, we must first expose the deployment to an external IP via the command:

$ kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed


Note we must use the type=NodePort because minikube doesn’t support the LoadBalancer service. We can check if the service was exposed by listing services:


$ kubectl get services
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-minikube   NodePort    10.107.144.113   <none>        8080:31206/TCP   23s
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          4h





Find minikube service url

$ minikube service hello-minikube --url
http://192.168.99.100:31206


We could get the IP of minikube

$ minikube ip
192.168.99.100


Now we can either curl the service from the CLI, or hit it via the browser.

$ curl $(minikube service hello-minikube --url)
CLIENT VALUES:
client_address=172.17.0.1
command=GET
real path=/
query=nil
request_version=1.1
request_uri=http://192.168.99.100:8080/


This verifies that we could successfully test kubenetes.


Delete the deployment and services.

$ kubectl delete deployment,service hello-minikube
deployment.extensions "hello-minikube" deleted
service "hello-minikube" deleted




Test with custom build docker image

Look at exist docker images

$ docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy-amd64                v1.10.0             bfc21aadc7d3        4 months ago        97MB
k8s.gcr.io/kube-apiserver-amd64            v1.10.0             af20925d51a3        4 months ago        225MB
k8s.gcr.io/kube-controller-manager-amd64   v1.10.0             ad86dbed1555        4 months ago        148MB
k8s.gcr.io/kube-scheduler-amd64            v1.10.0             704ba848e69a        4 months ago        50.4MB
k8s.gcr.io/etcd-amd64                      3.1.12              52920ad46f5b        5 months ago        193MB
k8s.gcr.io/kube-addon-manager              v8.6                9c16409588eb        5 months ago        78.4MB
k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64     1.14.8              c2ce1ffb51ed        7 months ago        41MB
k8s.gcr.io/k8s-dns-sidecar-amd64           1.14.8              6f7f2dc7fab5        7 months ago        42.2MB
k8s.gcr.io/k8s-dns-kube-dns-amd64          1.14.8              80cc5ea4b547        7 months ago        50.5MB
k8s.gcr.io/pause-amd64                     3.1                 da86e6ba6ca1        7 months ago        742kB
k8s.gcr.io/kubernetes-dashboard-amd64      v1.8.1              e94d2f21bc0c        8 months ago        121MB
gcr.io/k8s-minikube/storage-provisioner    v1.8.1              4689081edb10        9 months ago        80.8MB
gcr.io/google_containers/echoserver        1.4                 a90209bb39e3        2 years ago         140MB



Copy the directory - web-service.

$ git clone https://github.com/abulbasar/docker-examples
$ cd docker-examples/docker-app/web-service/
$ ls 
Dockerfile  app.py


Build a new image using specification in Dockerfile

$ docker build -t mywebappimage:1.0 .
Sending build context to Docker daemon  4.096kB
Step 1/4 : FROM  abasar/web_tier:1.0
1.0: Pulling from abasar/web_tier
c64513b74145: Already exists 
01b8b12bad90: Already exists 
c5d85cf7a05f: Already exists 
b6b268720157: Already exists 
e12192999ff1: Already exists 
f0cd933648d7: Already exists 
Digest: sha256:9c21c7d225a50ee19916e12fd2d15a91715379ebbcc804e511258e5d3bad94f8
Status: Downloaded newer image for abasar/web_tier:1.0
 ---> d6a39aa114c8
Step 2/4 : COPY . /app
 ---> a62af20ad34a
Step 3/4 : WORKDIR /app
Removing intermediate container 1a859bcb949b
 ---> e4eaea026858
Step 4/4 : CMD ["python", "app.py"]
 ---> Running in c3718aa8a0b9
Removing intermediate container c3718aa8a0b9
 ---> 27b05321d593
Successfully built 27b05321d593
Successfully tagged mywebappimage:1.0




Start kubenete deployment

$ kubectl run mywebapp --image=mywebappimage:1.0 --port=5000 --image-pull-policy=IfNotPresent
deployment.apps/mywebapp created


List the existing pods

$ kubectl get pods
NAME                        READY     STATUS    RESTARTS   AGE
mywebapp-59985486fb-hzm5k   1/1       Running   0          23s


Describe the pod

$ kubectl describe pod mywebapp-59985486fb-hzm5k
Name:           mywebapp-59985486fb-hzm5k
Namespace:      default
Node:           minikube/10.0.2.15
Start Time:     Thu, 16 Aug 2018 01:17:00 +0530
Labels:         pod-template-hash=1554104296
                run=mywebapp
Annotations:    <none>
Status:         Running
IP:             172.17.0.4
Controlled By:  ReplicaSet/mywebapp-59985486fb
Containers:
  mywebapp:
    Container ID:   docker://0833acd5c851652fa66b9d55576857c3a916bed3aed464d9425bbe4b295b69fe
    Image:          mywebappimage:1.0
    Image ID:       docker://sha256:27b05321d5935b53c2cbc097da8dc800e57354213d557f0ab2b346bac98e1bdb
    Port:           5000/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 16 Aug 2018 01:17:01 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-lvmxl (ro)
Conditions:
  Type           Status
  Initialized    True 
  Ready          True 
  PodScheduled   True 
Volumes:
  default-token-lvmxl:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-lvmxl
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason                 Age   From               Message
  ----    ------                 ----  ----               -------
  Normal  Scheduled              1m    default-scheduler  Successfully assigned mywebapp-59985486fb-hzm5k to minikube
  Normal  SuccessfulMountVolume  1m    kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-lvmxl"
  Normal  Pulled                 1m    kubelet, minikube  Container image "mywebappimage:1.0" already present on machine
  Normal  Created                1m    kubelet, minikube  Created container
  Normal  Started                1m    kubelet, minikube  Started container



Expose the deployment to an external IP address and port in order to access it via curl:

$ kubectl expose deployment mywebapp --type=NodePort
$ kubectl get services
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          4h
mywebapp     NodePort    10.105.206.4   <none>        5000:30596/TCP   9s


Find service URL

$ minikube service mywebapp --url
http://192.168.99.100:30596


Test the url using curl

$ curl http://192.168.99.100:30596
Hello world



Login to a container

$ kubectl get pod
NAME                        READY     STATUS    RESTARTS   AGE
mywebapp-59985486fb-hzm5k   1/1       Running   0          16m
$ kubectl exec -it mywebapp-59985486fb-hzm5k  -- /bin/bash


Delete the deployment and service

$ kubectl delete deployment,service mywebapp


Kubernetes Deployment Options

Installations for Windows - 10 Professional


Install docker

https://docs.docker.com/toolbox/toolbox_install_windows/#what-you-get-and-how-it-works

Install minikube

https://github.com/kubernetes/minikube/releases

Install kubectl

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.11.0/bin/windows/amd64/kubectl.exe

Add the binary in to your PATH.