Installing a Kubernetes Cluster from Scratch

This guide is based on having a 4 server setup (All running Ubuntu 16.04) with one Master and 3 additional Kubernetes nodes. These servers should all be on the same network, with all updates installed. In the following guide I have set up the following servers:

ksm = Kubernetes Server Manager
ks01 = Kubernetes Node 1
ks02 = Kubernetes Node 2
ks03 = Kubernetes Node 3

Prepping your servers:

Firstly, install prerequisits and Kubernetes on all 4 servers:

Install and enable the Docker Daemon:

apt-get install -y
systemctl start docker
systemctl enable docker

Now install Kubernetes:

curl -s | apt-key add 
echo "deb kubernetes-xenial main" >> /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl kubernetes-cni

The final stage is to disable the Swap partitions on your servers (If you have enabled) as Kubernetes does not support running a server with swap enabled:

swapoff -a

Setting up your Kubernetes Master Server:

Run the following commands on your Kubernetes Master Server only, once we have completed the below we can add the other nodes.

Create kubeadmin user (Change this as appropriate). I have included the final line below which will enable you to run sudo commands as your kubeadmin user without entering a password each time – You can do this as long as you are aware of the relevant security risks of doing so..

useradd kubeadmin
 usermod -aG sudo kubeadmin
 echo "kubeadmin ALL=NOPASSWD: ALL" >> /etc/sudoers.d/kubeadmin

Now su to your new user account and start up kubeadm

su kubeadmin
cd ~
sudo kubeadm init --pod-network-cidr --apiserver-advertise-address [Your Server's IP address]
sudo mkdir -p /home/kubeadmin
sudo chown -R kubeadmin:kubeadmin $HOME
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

When these commands have completed, make a note of the kubeadm add output from kubeadm init, this will be used later to add your Kubenetes nodes to your cluster.

You can now watch the various default pods starting up within Kubernetes via the following command. Do not continue with installing Flannel until all containers (With the exception of kube-dns) are running:

sudo kubectl get pods --all-namespaces

We must now install the flannel network manager into Kubernetes:

kubectl apply -f kube-flannel.yml

Adding Nodes to your Kubernetes Cluster:

Now, on the other 3 servers you have allocated as Kubernetes nodes, run the kubeadm join command which was given when you initiated your cluster.

You should now be able to see the kube-dns pod starting and your 3 nodes added to Kubernetes:

kubectl get pods --all-namespaces

kubectl get nodes

Any comments or questions? Get in touch here or Email me at [email protected]