003: Local Kubernetes with Microk8s Skip to main content

003: Local Kubernetes with Microk8s

Microk8s is a single-node Kubernetes cluster for the Ubuntu development workstation. Kubectl, Kubectx, Kubens command line tools belong to the basics.


The daily mood

I got assigned the task to analyze our release cycle in Kubernetes. So first step is to read a bit, setup a single-node cluster on my local workstation (a developer laptop) and get familiar with it. There is a comprehensive guide for installing a Kubernetes cluster from scratch using kubeadm tool, but you can do so much wrong that it is easier and safer to rely on an existing distribution. 

As an Ubuntu user I am picking microk8s that runs in a snap instead of minikube running in a virtualbox. Because it is just so much lighter and faster. I read afterwards that conjure-up should be even better.




Install MicroK8s
$ sudo snap install microk8s --classic --channel=1.15/stable # comes with exact version 2.14.3 of Tiller
$ microk8s.start
$ microk8s.enable dns dashboard registry
$ microk8s.status --wait-ready
$ microk8s.config > $HOME/.kube/microk8s.config
$ microk8s.inspect     # equivalent to kubectl get cluster-info
Note: In some cases you may need to allow priviledged containers, which is not enabled by default on Microk8s. I had that problem with ElasticSearch. A change to api-server configuration and a deactivation of sysctlInitContainer are suggested.
$ echo "--allow-privileged" >> /var/snap/microk8s/current/args/kube-apiserver
$ sudo systemctl restart snap.microk8s.daemon-apiserver.service
Next step is the kind of guy which should become your friend: kubectl.


Setup client

You may use microk8s embedded client (no config required):
sudo snap alias microk8s.kubectl kubectl
Recommended is to alternatively download and install from official release:
$ curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
$ sudo mv ./kubectl /usr/local/bin/
$ kubectl version --client
Extend bash_completion to enable command line assistance
$ sudo su -
$ kubectl completion bash > /etc/bash_completion.d/kubectl
In addition of a modern IDE, the tool kubeval can be very usefull for Kubernetes manifest developer to verify scripts before apply.v
$ wget https://github.com/instrumenta/kubeval/releases/latest/download/kubeval-linux-amd64.tar.gz
$ tar xf kubeval-linux-amd64.tar.gz && rm kubeval-linux-amd64.tar.gz $ chmod +x kubeval sudo mv kubeval /usr/local/bin/ $ kubeval --version
It is also recommended to setup kubectx/kubens and kubetail utilities. First ones allow to easily switch between different Kubernetes clusters/namespaces and therefore avoid passing namespace parameter on each kubectl call.
# kubectx
$ wget https://github.com/ahmetb/kubectx/releases/download/v0.9.0/kubectx_v0.9.0_linux_x86_64.tar.gz
$ tar xf kubectx_v0.9.0_linux_x86_64.tar.gz && rm kubectx_v0.9.0_linux_x86_64.tar.gz
$ chmod +x kubectx && sudo mv kubectx /usr/local/bin/ $ kubectx # equivalent to kubectl config get-contexts $ kubectx microk8s # equivalent to kubectl config use-context microk8s $ kubectx -c # returns microk8s
# kubens
$ wget https://github.com/ahmetb/kubectx/releases/download/v0.9.0/kubens_v0.9.0_linux_x86_64.tar.gz
$ tar xf kubens_v0.9.0_linux_x86_64.tar.gz && rm kubens_v0.9.0_linux_x86_64.tar.gz
$ chmod +x kubens && sudo mv kubens /usr/local/bin/
$ kubens               # equivalent to kubectl get ns
$ kubens kube-system   # switch to kube-system namespace
$ kubectl get all      # equivalent to kubectl get all --n kube-system
$ kubens -c            # returns kube-system
$ kubens -             # switch to previous namespace (default)
Last one allows to aggregate logs from multiple Kubernetes pods into one stream.
$ kubetail app1,app2   # equivalent to kubectl logs -f app1 & kubectl logs -f app2

Deploy something to the Cluster
# hello example
$ kubectl run hello --image=gcr.io/google_containers/echoserver:1.4 --port=8080
$ kubectl expose deployment hello --type=NodePort

# nginx server
$ kubectl run nginx --image nginx
$ kubectl expose deployment nginx --port 80 --target-port 80 --type ClusterIP\ --selector=run=nginx --name nginx

# check application
$ kubectl get pods --watch
$ sensible-browser http://$(kubectl describe pod nginx | grep IP: | head -1 | awk '{print $2}')

# clean resources
$ kubectl delete deployment,service hello,nginx

Access the Dashboard (https)
$ kubens kube-system
$ token=$(kubectl get secret | grep default-token | cut -d " " -f1)
$ kubectl describe secret $token
$ sensible-browser https://$(kubectl get svc | grep dashboard | awk '{print $3}')

List images
$ microk8s.ctr -n k8s.io images ls                 # local images

Comments

  1. FYI, Minikube does not need Virtual box on Linux. Looks like you did not try very hard using it. You can use KVM driver for great performance (the config I use) or the new docker driver to end up with Kubernetes in Docker (same as Kind tool).
    Also, Minikube now also supports multi-nodes clusters.

    ReplyDelete

Post a Comment