There are a bunch of ways to install Kubernetes.
Here we’ll take a look at Minikube but see future posts for Google Container Engine (GKE), AWS and a Manual Install.
Minikube
Minikube is a tool that is used to run Kubernetes locally. It runs a single-node Kubernetes cluster inside a VM so you can try out Kubernetes or develop with it.
TLDR
Start: minikube start
Upgrade:
1 2 |
brew update brew cask reinstall minikube |
https://stackoverflow.com/questions/45002364/how-to-upgrade-minikube
Important note: the default minikube startup config for me was 4GB. With a Mac that has 8GB of memory, it will very quickly die from memory pressure as com.docker.hyperkit has 1.58GB.
To fix this use:
minikube config set memory 2048
(I’ve found I get memory pressure with less – e.g. 1024
)
https://github.com/kubernetes/minikube/issues/567
[RANT: why not just use upgrade
rather than reinstall
. And why should you have to spend several minutes Google’ing then have to go to a Stackoverflow page to find out how to upgrade a piece of software? Wouldn’t it have made sense to put some info somewhere like here: https://kubernetes.io/docs/tasks/tools/install-minikube/%5D
What is Minikube
Minikube creates a VM which has a Master and Node that can be managed from the Host (via kubectl
).
Components are:
VM: minikube
Localkube (binary): runs Master and Node
Container runtime: currently runs Docker
Host: kubectl
Install
1 2 3 4 5 6 |
brew install kubectl brew cask install minikube brew install docker-machine-driver-xhyve sudo chown root:wheel /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve sudo chmod u+s /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve minikube start --vm-driver=xhyve |
Note: this driver now seems to be deprecated. i.e.
WARNING: The xhyve driver is now deprecated and support for it will be removed in a future release.
Please consider switching to the hyperkit driver, which is intended to replace the xhyve driver.
Also, kubectl
is the same as kubernetes-cli
: https://formulae.brew.sh/formula/kubernetes-cli
Checking
Checking which cluster we’re managing:
kubectl config current-context
minikube
Note: we could use kubectl
to manage another Kubernetes cluster (e.g. Production). Just need to switch clusters.
Commands
List nodes
kubectl get nodes
If it’s not running you may get something like:
kubectl get nodes
No resources found.
Unable to connect to the server: dial tcp 192.168.64.5:8443: i/o timeout
Annoyingly, it takes around 2:30
mins to timeout!
Stop minikube
minikube stop
(keeps config)
I got an error here:
Stopping local Kubernetes cluster…
Error stopping machine: Error stopping host: minikube: unexpected EOF
and hanging occasionally at:
Starting cluster components…
Solution seemed to be:
minikube delete; rm -rf ~/.minikube
https://github.com/kubernetes/minikube/issues/227
and
https://github.com/kubernetes/minikube/issues/867
Note: be patient. On a 2018 top-end MacBook Pro with a 10Mbps network connection, this took just under 14 minutes with the major chunks of time being:
Downloading Minikube ISO
and
Starting cluster components...
If you’ve already downloaded and started the components previously then this step will only take around 1 minute.
Delete Kubernetes cluster
minikube delete
Start Minikube with a Kubernetes version
Note however that downgrading is not supported. E.g. if you’ve already run v1.10.0
then
minikube start --vm-driver=xhyve --kubernetes-version="v1.6.0"
will fail with
Kubernetes version downgrade is not supported. Using version: v1.10.0
minikube commands
minikube
by itself shows all the minikube commands.
minikube dashboard
opens the dashboard. This shows (amongst much else):
- Nodes
- Pods (note: choose
kube-system
rather than the defaultdefault
from Namespace to see these)
minikube status
shows the current status.
References:
https://kubernetes.io/docs/tutorials/hello-minikube/
Create a test application
server.js
1 2 3 4 5 6 7 8 9 |
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); |
Run with
node server.js
And test with
Package into a docker image
Create a file in the same folder called Dockerfile
1 2 3 4 |
FROM node:6.9.2 EXPOSE 8080 COPY server.js . CMD node server.js |
Note from https://kubernetes.io/docs/tutorials/hello-minikube/
Because this tutorial uses Minikube, instead of pushing your Docker image to a registry, you can simply build the image using the same Docker host as the Minikube VM, so that the images are automatically present. To do so, make sure you are using the Minikube Docker daemon:
eval $(minikube docker-env)
Note: Later, when you no longer wish to use the Minikube host, you can undo this change by runningeval $(minikube docker-env -u)
.
Build with
docker build -t hello-node:v1 .
The -t
tags it as hello-node:v1
Run with kubectl run hello-node --image=hello-node:v1 --port=8080 --image-pull-policy=Never
--image-pull-policy=Never
‘cos you’ve only built it locally (i.e. you haven’t pushed it to the Docker registry).
View deployments
kubectl get deployments
View the pod
kubectl get pods
Note: you can see this all via a GUI if you run minikube dashboard
and select Overview.
E.g. events
kubectl get events
and via Dashboard:
Workloads > Pods > <select pod> > Events
Create a Service
To make the Pod accessible from outside (it’s currently only accessibly via its internal IP address), use the expose command. i.e.
kubectl expose deployment hello-node --type=LoadBalancer
and view the Service:
kubectl get services
e.g.
minikube service hello-node
and you should see the logs for this via:
kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-57c6b66f9c-csxq5 1/1 Running 0 29m
hellonode > kubectl logs hello-node-57c6b66f9c-csxq5
Received request for URL: /
Received request for URL: /favicon.ico
Update your app
Update server.js
to return a new message
e.g. response.end('Hello World version 2!');
Build a new version (e.g. v2):
docker build -t hello-node:v2 .
Update the image of your deployment (i.e. replacing the previous one):
kubectl set image deployment/hello-node hello-node=hello-node:v2
and run with:
minikube service hello-node
Clean up
Clean up the resources created in the cluster:
kubectl delete service hello-node
kubectl delete deployment hello-node
Optionally, force removal of the Docker images created:
docker rmi hello-node:v1 hello-node:v2 -f
Optionally, stop the Minikube VM:
minikube stop
eval $(minikube docker-env -u)
Optionally, delete the Minikube VM:
minikube delete
Errors / Problems
“Starting cluster components” hangs
e.g. https://github.com/kubernetes/minikube/issues/2886
Ignore the suggestion to use --show-libmachine-logs
– that doesn’t work.
Instead:
1 2 |
minikube delete rm -rf ~/.minikube |
https://github.com/kubernetes/minikube/issues/2765
Other issues