Single Node Kubernetes Cluster - Installing Docker and Kubernetes on Ubuntu Server


14 Jul 2019  Michal Fabjanski  4 mins read.

In the previous post, we have prepared an environment for work. Now we will be installing and configuring Docker and Kubernetes on our Ubuntu Server.

Installing Docker and Kubernetes on Ubuntu Server

Let’s start with turning on the VM with Ubuntu Server and connecting via SSH to the machine (I described this in the previous post). First, use the following commands to install Docker:

   
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt update && sudo apt install docker-ce

Then use the following commands to install Kubernetes:

   

sudo apt-get update && sudo apt-get install -y apt-transport-https && curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list && sudo apt-get update

sudo apt install -y kubeadm  kubelet kubernetes-cni

In the end, turn off the swap on Ubuntu Server because it is required in the changelog:

   

sudo swapoff -a

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

That’s all. Check if Docker and Kubernetes were installed using the commands:

   
docker -v

and:

   
kubectl version

You should see a similar result:

   
michal@michal:~$ docker -v
Docker version 18.09.7, build 2d0083d
michal@michal:~$ kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.0", GitCommit:"e8462b5b5dc2584fdcd18e6bcfe9f1e4d970a529", GitTreeState:"clean", BuildDate:"2019-06-19T16:40:16Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

Creating Single Node Kubernetes Cluster with Kubeadm

We will use kubeadm which helps bootstrap Kubernetes cluster. Detailed documentation is available on the kubernetes website. I will present some of the most important steps that will create a working cluster in a few minutes.

At the beginning, install kubectl using the following command:

   
sudo apt-get update && sudo apt-get install -y apt-transport-https && curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list && sudo apt-get update
sudo apt install -y kubeadm  kubelet kubernetes-cni

Now we can create a single node cluster:

   
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.0.129 --ignore-preflight-errors=NumCPU

Remember to change –apiserver-advertise-address=192.168.0.129 with your VM inet address. If you do not know how to check inet IP - read the instructions from the previous post. After a few minutes of installation, you should see the message:

 

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.0.129:6443 --token mc3oxw.8mkdwlr260ujpy6a \
    --discovery-token-ca-cert-hash sha256:fbb8705c4826ece8596ee4eb14d0375677fed231bc2a3e78326bd46f3bb1f24e

In order for Kubernetes to work for a non-root user, you must run the following commands:

   
mkdir $HOME/.k8s

sudo cp /etc/kubernetes/admin.conf $HOME/.k8s/

sudo chown $(id -u):$(id -g) $HOME/.k8s/admin.conf

export KUBECONFIG=$HOME/.k8s/admin.conf

echo "export KUBECONFIG=$HOME/.k8s/admin.conf" | tee -a ~/.bashrc

Configuring Kubernetes Cluster Networking

After kubernetes installation, we need to configure networking. This is detailed in the documentation . Execute two commands to set the networking:

   
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml

Master Node as a Worker

Finally, we have to use the kubectl taint command, so that our master node can create pods and be used as a worker node:

   
kubectl taint nodes --all node-role.kubernetes.io/master-

Summary

That’s all! We have created a working single node cluster. In the next post, I will show you how to create an admin dashboard in Kubernetes to be able to easily manage a cluster from the browser.