/ Docker

Kubernetes Notes

Get started on your workstation

Your workstation being an Ubuntu something. This is based on https://kubernetes.io/docs/tutorials/stateless-application/hello-minikube/, plus personnal experience and so on.

Requirements installation

Install VirtualBox and curl

sudo apt install -y virtualbox curl

Install Kubectl

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x kubectl
sudo mv kubectl /usr/bin/

Install Minikube

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/bin/

Basic minikube commands

Start Minikube

minikube start --vm-driver=virtualbox

Check the dashboard

minikube dashboard

Make sure your docker commands are using minikube's VM docker daemon

eval $(minikube docker-env)

Just check this using docker images command, you'll have all the kubernetes own containers

➜  docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
gcr.io/google_containers/kubernetes-dashboard-amd64    v1.8.0              55dbc28356f2        8 weeks ago         119MB
gcr.io/k8s-minikube/storage-provisioner                v1.8.0              4689081edb10        2 months ago        80.8MB
gcr.io/k8s-minikube/storage-provisioner                v1.8.1              4689081edb10        2 months ago        80.8MB
gcr.io/google_containers/k8s-dns-sidecar-amd64         1.14.5              fed89e8b4248        3 months ago        41.8MB
gcr.io/google_containers/k8s-dns-kube-dns-amd64        1.14.5              512cd7425a73        3 months ago        49.4MB
gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64   1.14.5              459944ce8cc4        3 months ago        41.4MB
gcr.io/google_containers/kubernetes-dashboard-amd64    v1.6.3              691a82db1ecd        5 months ago        139MB
gcr.io/google-containers/kube-addon-manager            v6.4-beta.2         0a951668696f        7 months ago        79.2MB
node                                                   6.9.2               faaadb4aaf9b        13 months ago       655MB
gcr.io/google_containers/pause-amd64                   3.0                 99e59f495ffa        20 months ago       747kB

Build a (NodeJS) hello world container

So you got somehing to deploy

mkdir -p work/minikube
cd work/minikube

Create file server.js

var http = require('http');    

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello World !');
};
var www = http.createServer(handleRequest);
www.listen(8080);

Create file Dockerfile

FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js

If you did a eval $(minikube docker-env), the image will be available in Minikube straight away. Just a check

➜  docker images | grep hello
hello-node         v1      13bcae970649     2 minutes ago     655MB

Create a Deployment

kubectl run command creates a Deployment that manages a Pod. The Pod runs (ad restart if needed, etc) a Container based on your hello-node:v1 Docker image

kubectl run hello-node --image=hello-node:v1 --port=8080

Check the result

kubectl get deployments
kubectl get pods
kubectl get events

Create a Service

By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster. To make the hello-node Container accessible from outside the Kubernetes virtual network, you have to expose the Pod as a Kubernetes Service.

kubectl expose deployment hello-node --type=LoadBalancer

Check the result

kubectl get services

The --type=LoadBalancer flag indicates that you want to expose your Service outside of the cluster. On cloud providers that support load balancers, an external IP address would be provisioned to access the Service. On Minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

To access your hello-World

minikube service hello-node

Update your app

Update helloworld/server.js

var http = require('http');
var os = require('os');

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello World from ' + os.hostname() + '!');
};
var www = http.createServer(handleRequest);
www.listen(8080);

Then build version 2 : docker build -t hello-node:v2 .

Update the image for the deployment

kubectl set image deployment/hello-node hello-node=hello-node:v2

Check the result

minikube service hello-node

Scale your app

Let's say it's black friday, you wanna scale up

kubectl scale deployment hello-node --replicas=9000

Open your app via minikube service hello-node, again and again. You'll have the same Hello World from hello-node-65something55c-cbv6g!. Open another browser, or wipe your cache fully. You'll have another ID : kubernetes's Service is doing its jobs, load balancing + session affinity

Minikube plugins

Just to get a shot at some interesting stuff for Kubernetes, minikube addons list

Heapster

This will automagically collects stuff and display nice curves, via FOSS like InfluxDB, Grafana. Here are the sources

minikube addons enable heapster

Wait a minute, then explore the dashboards

minikube addons open heapster

For example,

  • clic on top left "Home", check "Cluster"
  • clic on top left "Home", check "Pods", select namespace "default", select a random hello-node "pod"

Clean up

Remove service and deployment

kubectl delete service hello-node
kubectl delete deployment hello-node

Stop and remove Minikube VM

minikube stop
eval $(minikube docker-env -u)
minikube delete

Kubernetes things

Labels

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

➜  kubectl get pods                  
NAME                          READY     STATUS    RESTARTS   AGE
hello-node-657c99955c-7jtwd   1/1       Running   0          19h
hello-node-657c99955c-7wlvx   1/1       Running   0          19h
hello-node-657c99955c-cbv6g   1/1       Running   0          19h
hello-node-657c99955c-jxglb   1/1       Running   0          19h
hello-node-657c99955c-kd852   1/1       Running   0          19h

➜  kubectl label pods hello-node-657c99955c-kd852 env=dev
pod "hello-node-657c99955c-kd852" labeled

➜  kubectl get pods -l env=dev
NAME                          READY     STATUS    RESTARTS   AGE
hello-node-657c99955c-kd852   1/1       Running   0          19h
Kubernetes Notes
Share this