6. Kubernetes

6.1. Installation

To deploy to Kubernetes, we need the following components installed. On Linux, kubectl and minikube are single binary executable files.

6.2. Starting minikube cluster

Start minikube. This command effectively creates a virtual machine.

1minikube start

You should see an output like the following.

πŸ˜„  minikube v1.5.2 on Ubuntu 19.10
✨  Automatically selected the 'virtualbox' driver (alternates: [none])
πŸ’Ώ  Downloading VM boot image ...
    > minikube-v1.5.1.iso.sha256: 65 B / 65 B [--------------] 100.00% ? p/s 0s
    > minikube-v1.5.1.iso: 143.76 MiB / 143.76 MiB [] 100.00% 127.66 MiB p/s 2s
πŸ”₯  Creating virtualbox VM (CPUs=2, Memory=2000MB, Disk=20000MB) ...
🐳  Preparing Kubernetes v1.16.2 on Docker '18.09.9' ...
πŸ’Ύ  Downloading kubeadm v1.16.2
πŸ’Ύ  Downloading kubelet v1.16.2
🚜  Pulling images ...
πŸš€  Launching Kubernetes ...
βŒ›  Waiting for: apiserver
πŸ„  Done! kubectl is now configured to use "minikube"

6.3. Quicktest

6.3.1. Deploy through an image

Type in the following to deploy a service through an image.

 1# create deployment
 2kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
 3
 4# expose deployment as a service
 5kubectl expose deployment hello-minikube --type=NodePort --port=8080
 6
 7# get pod information
 8kubectl get pods
 9
10# get the service url
11minikube service hello-minikube --url
12
13# delete service
14kubectl delete services hello-minikube
15
16# delete deployment
17kubectl delete deployment hello-minikube

6.3.2. Deploy through YAML configuration

Create a file called pod.yaml.

1apiVersion: v1
2kind: Pod
3metadata:
4  name: demo
5spec:
6  containers:
7  - name: testpod
8    image: alpine:3.5
9    command: ["ping", "8.8.8.8"]

Then type in the following.

 1# create deployment
 2kubectl apply -f pod.yml
 3
 4# get pod information
 5kubectl get pods
 6
 7# get logs of pod
 8kubectl logs demo
 9
10## delete service
11kubectl delete -f pod.yml

6.3.3. Pod creation

Create a file called pod.yaml.

 1apiVersion: v1
 2kind: Pod
 3metadata:
 4  name: student
 5spec:
 6  containers:
 7  - name: db
 8    image: db-app:local
 9    ports:
10      - containerPort: 3306
11    env:
12      - name: MYSQL_ROOT_PASSWORD
13        value: "oneoffcoder"
14    volumeMounts:
15      - name: db-scripts
16        mountPath: /docker-entrypoint-initdb.d
17  - name: rest
18    image: rest-app:local
19    ports:
20      - containerPort: 5000
21  volumes:
22  - name: db-scripts
23    hostPath:
24      path: /hosthome/super/git/books/sphinx/docker/source/_static/code/compose/mysql/docker-entrypoint-initdb.d
25      type: Directory

Now try running the following commands to create a pod and more.

 1# create deployment
 2kubectl apply -f student.yml
 3
 4# get pod information
 5kubectl get pods
 6kubectl describe pod student
 7
 8# get logs of pod
 9kubectl logs student db
10kubectl logs student rest
11
12# shell access
13kubectl exec -it student --container db -- /bin/bash
14kubectl exec -it student --container rest -- /bin/bash
15
16## delete service
17kubectl delete -f student.yml

6.3.4. Deployment creation

Build the docker images.

1docker build --no-cache -t db-app:local .
2docker build --no-cache -t rest-app:local .
3docker build --no-cache -t ui-app:local .

Create student-deployment.yml with the following content.

 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: student-deployment
 5  labels:
 6    app: student
 7spec:
 8  replicas: 1
 9  selector:
10    matchLabels:
11      app: student
12  template:
13    metadata:
14      labels:
15        app: student
16    spec:
17      containers:
18      - name: db
19        image: db-app:local
20        ports:
21          - containerPort: 3306
22        env:
23          - name: MYSQL_ROOT_PASSWORD
24            value: "oneoffcoder"
25        volumeMounts:
26          - name: db-scripts
27            mountPath: /docker-entrypoint-initdb.d
28      - name: rest
29        image: rest-app:local
30        ports:
31          - containerPort: 5000
32      - name: ui
33        image: ui-app:local
34        ports:
35          - containerPort: 80
36      volumes:
37      - name: db-scripts
38        hostPath:
39          path: /hosthome/super/git/books/sphinx/docker/source/_static/code/compose/mysql/docker-entrypoint-initdb.d
40          type: Directory

Now try running the following commands to create a deployment and more.

 1# create deployment
 2kubectl create -f student-deployment.yml
 3
 4# check deployment
 5kubectl get deployment
 6
 7# check replication set
 8kubectl get rs
 9
10# get pod information
11kubectl get pods
12POD=student-deployment-75d56dc8f5-55rtt
13kubectl describe pod $POD
14
15# get logs
16kubectl logs $POD db
17kubectl logs $POD rest
18kubectl logs $POD ui
19
20# shell access
21kubectl exec -it $POD --container db -- /bin/bash
22kubectl exec -it $POD --container rest -- /bin/bash
23kubectl exec -it $POD --container ui -- /bin/sh
24
25# forward port
26kubectl port-forward $POD 3306:3306
27kubectl port-forward $POD 5000:5000
28kubectl port-forward $POD 8080:80
29
30## delete service
31kubectl delete -f student-deployment.yml

6.4. Using local images

The following command enables us to reuse Minikube’s built-in docker daemon. This feature is useful to avoid building a Docker registry and pushing images into it. When you issue docker ps you will see the containers running on Minikube.

1eval $(minikube docker-env)

6.5. Useful notes and commands

  • minikube dashboard brings up the Kubernetes dashboard

  • minikube ssh will SSH into the virutal machine

  • kubectl get deployment gets the deployments in the cluster

  • kubectl get events gets the events in the cluster

  • kubectl get svc checks the services created

  • /home (local) is mounted /hosthome (virtual machine)

  • addons in ~/.minikube/addons (local) will be moved to the virtual machine on Minikube start/restart

6.6. Removing minikube cluster

Stop minikube. This command effectively halts the virtual machine.

1minikube stop

Delete minikube. This command effectively deletes the virtual machine.

1minikube delete