Geschreven door Dennis Vermeulen

Installing a Kubernetes cluster and connecting it to Lens

DevOps4 minuten leestijd

In this tutorial you can experiment with a local Kubernetes cluster that can be installed using VirtualBox or another virtualization product. This time we will use VirtualBox and Vagrant to set it all up. The Vagrant file will create one Kubernetes master server and two Kubernetes workers. A loadbalancer will also be created and will be used in our next blog about Traefik. After the installation we use Lens to connect to the created cluster. You must have a minimum of 16 GB of RAM for setting up this cluster.

To get started, make sure you download and install Vagrant, VirtualBox and Lens if you don’t already have them.
Vagrant
VirtualBox
Lens

Install the boxes

First, we will install the boxes. In your terminal do the following:

vagrant box add ubuntu/bionic64
==> box: Loading metadata for box 'ubuntu/bionic64'
    box: URL: https://vagrantcloud.com/ubuntu/bionic64
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
 
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
 
# Choose 3) virtualbox

Getting the master, workers and loadbalancer.

git clone https://dennisvermeulen@bitbucket.org/dennisvermeulen/kubernetes-cluster.git

cd kubernetes-cluster/k8smaster
vagrant up
cd ../k8sworker1
vagrant up
cd ../k8sworker2
vagrant up
cd ../k8slb
vagrant up
cd ../k8smaster

vagrant ssh

Bootstrap

vagrant@k8smaster:~$ sudo su -
root@k8smaster:~$ kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.out

Copy config file to communicate with the cluster

root@k8smaster:~$ mkdir -p $HOME/.
kuberoot@k8smaster:~$ sudo cp -i /etc/kubernetes/admin.conf 
$HOME/.kube/config
root@k8smaster:~$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

Deploy a pod network to the cluster

root@k8smaster:~$ kubectl create -f calico.yaml

Check running pods

root@k8smaster:~# kubectl -n kube-system get pods

NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-6b9d4c8765-p9wn7   1/1     Running   0          52s
calico-node-bgklp                          1/1     Running   0          52s
coredns-5644d7b6d9-fmbsb                   1/1     Running   0          71s
coredns-5644d7b6d9-tdn9f                   1/1     Running   0          71s
etcd-k8smaster                             1/1     Running   0          13s
kube-apiserver-k8smaster                   1/1     Running   0          30s
kube-controller-manager-k8smaster          1/1     Running   0          19s
kube-proxy-kknp8                           1/1     Running   0          71s
kube-scheduler-k8smaster                   1/1     Running   0          32s

Join any number of worker nodes by running the following on each as root:
cd ../k8sworker1 
vagrant ssh
sudo -i 

Get the following information out of the kubeadm-init.out for joining the worker in the cluster.

kubeadm join k8smaster:6443 --token pzybeg.5ebv6ct0ts7q0dde \
    --discovery-token-ca-cert-hash 
sha256:b8bac2cb45f9e1e1c9db53dded9be20b3304462dc2586fe7ad5418e034013a58
  
cd ../k8slbvagrant sshsudo -idocker run -d --restart=unless-stopped --name nginx -p 
80:80 -p 443:443 -v /home/vagrant/nginx/nginx.conf:/etc/nginx/nginx.conf 
nginx:latest

Lens

Lens is the only IDE you’ll ever need to take control of your Kubernetes clusters. It is a standalone application for MacOS, Windows and Linux operating systems. It is open source and free. After you have installed Lens you can connect to your cluster using the Kubernetes config file, the only thing you have to do is change:

“server: https://k8smaster:6443” to “server: https://192.168.33.20:6443”

On k8smaster get the config file:

vagrant@k8smaster:~$ sudo cat /root/.kube/config

Start Lens and add the config file, don’t forget to change the server into https://192.168.33.20:6443.

Learning and starting with Kubernetes clusters on your local device with Lens is fun and a good starting point to experiment with Kubernetes.

Good luck and feel free to reach out if you have any questions or ideas.