Kubernetes II. Wedding Day: Installing & configuring k8s cluster

As I told you on my last blog post, I have fallen in love with Kubernetes. If you didn't read the love story yet, click here to do it for basic Kubernetes terminology and reasons why to use (or love) Kubernetes.

But now it is time to take the love affair to a next level: in this article I will show you how to create simple k8s cluster with master node and two worker nodes.

First, here are the basics you need to know before getting your hands dirty with the installation. However, if you feel like all this might be a bit too technical for you, just jump to the end of the article. Not all of us need to be Kubernetes wizards.

You might remember from my previous blog post that master node is working as a control plane for the cluster and it has components, like kube-apiserver. Kube-apiserver exposes Kubernetes API and is the front-end for receiving commands from you. Master node also has etcd which is consistent key-value store where all the cluster data is stored. Other Master componentes are kube-scheduler which watches newly created pods and selects worker node for them to run on and also kube-controller-manager and cloud-controller-manager.

Worker nodes are the machines where the workload pods (your application) are running. Worker nodes have container runtime (usually Docker), kubelet and kube-proxy.

Installation

We are going to setup Kubernetes cluster with one master node and two worker nodes. If you only want to test Kubernetes locally in your laptop, you can skip this section and scroll down to Minikube installation.

We will have Ubuntu 16.04 as an operating system. As Ubuntu 18.04 is not yet fully supported by Kubernetes, we go with older version. Note that those Ubuntu machines need to be in the same network so they can communicate with each other. You also need to have SSH access to machines.

Installation will happen with a nice kubeadm tool. With it, you don’t need to manually install every package related to k8s, as kubeadm helps you to bootstrap the cluster. Kubeadm also handles upgrading and downgrading your cluster nodes.

So the requirements are:

  • Three Ubuntu 16.04 machines (virtual or bare metal)
  • You can name them k8s-master, k8s-worker1 and k8s-worker2
  • Machines running on the same network
  • SSH access

Master setup

First we are going to setup the Master node machine. So login one of your machines via ssh and start installing magical k8s stuff!

Disable swap and update your packages and install packages we need

swapoff -a

apt-get update

apt-get install -y apt-transport-https curl

Install Docker

apt-get install -y docker.io

Check that Docker is installed with docker --version

Install kubelet, kubeadm and kubectl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

apt-get update

apt-get install -y kubelet kubeadm kubectl

apt-mark hold kubelet kubeadm kubectl

Initialize Kubernetes

Next we need to run the initialize command with kubeadm. It will install some certs, configs, etcd and other stuff related to master node.

We are going to use Flannel as our pod networking addon (https://kubernetes.io/docs/concepts/cluster-administration/networking/#flannel), so correct CIDR must be used and passed with --pod-network-cidr parameter. I am also going to ignore the preflight errors (might give warning about cpu and hostnames, we can skip those in this demo).

kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors all

Now it will take a while when installing k8s components to your master machine, just grab a cup of coffee and wait. After installation is finished, please take note of the kubeadm join command, as you will need it when configuring the worker nodes. It will be something like this:

kubeadm join <master_ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Config kubectl

We are going to use our Master as place where to run the kubectl command, so we need to create a regular user and add some configurations. You can also copy the /etc/kubernetes/admin.conf to your own computer and run kubectl from there, but we use master machine as the place where to run kubectl.

Create new user and add it to sudoers group:

adduser ubuntu --disabled-password

usermod -aG sudo ubuntu

su - ubuntu

Setup k8s config for the newly created user:

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

Install pod network

Deploy the Flannel pod network addon to the cluster:

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

Check installation

Check that all pods are running well in the kube-system namespace.

$ kubectl get pods --namespace=kube-system

NAME 							READY 	STATUS  	RESTARTS 	AGE
coredns-86c58d9df4-7642v 				1/1 	Running 	0 		11m
coredns-86c58d9df4-l9j49 				1/1 	Running 	0 		11m
etcd-k8sblog-master 					1/1 	Running 	0 		10m
kube-apiserver-k8sblog-master 			        1/1     Running 	0 		10m
kube-controller-manager-k8sblog-master 			1/1 	Running 	0 		11m
kube-flannel-ds-amd64-zlnfh 				1/1 	Running 	0 		21s
kube-proxy-7k25z 					1/1 	Running 	0 		11m
kube-scheduler-k8sblog-master 				1/1 	Running 	0 		10m

Nice! Now you have Kubernetes master up and running. Next we will setup workers.

Worker node setup

Just like with the master setup, we need to disable swap and install Docker and Kubernetes packages (kubelet, kubeadm, kubectl). You can check the instructions above, in the beginning of master setup sections.

When everything is installed in both of the worker nodes, you basically just need to run the join command you got when initializing the master with kubeadm command.

So run the command:

kubeadm join <master_ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

After running join command to both workers, check cluster status in the master (where the kubectl is configured). Should be looking something like this:

$ kubectl get nodes

NAME 			STATUS 	ROLES 	AGE 	VERSION
k8sblog-master 	Ready 	master 	24m 	v1.13.3
k8sblog-worker1 Ready 	<none> 	13s 	v1.13.3
k8sblog-worker2 Ready 	<none> 	21s 	v1.13.3

Wow! Now you have your Kubernetes cluster up and running, ready to receive some containerized workloads to run. We will see how that is going to happen in the next blog post. So stay tuned!

Minikube installation

If you don’t want to use machines to setup real cluster, you can also run a minikube in your local environment.

Installation depends on are you running macOS, Linux or Windows. Check the installation instructions here: https://kubernetes.io/docs/tasks/tools/install-minikube/

Once you got minikube installed, you can start to create the cluster:

minikube start

Let’s then deploy a sample app to see if it is working:

kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.10 --port=8080

Now you can check if the hello-minikube app is running:

kubectl get pods

Yep, it should be working nicely! Now you can start to deploy your own workloads locally to minikube and begin to study the wonders of Kubernetes.

End stuff

If this installation and configuring Kubernetes clusters seems overwhelming to you, please do not hesitate to contact Montel Intergalactic, we will be pleased to help you.

In the next part of my series of Kubernetes articles, we are going to deploy sample application to Kubernetes cluster and see how easy it is to manage it there. So next up deploying new versions with zero downtime, self-healing application, scaling up and down and all other fun and lovable things with Kubernetes.

Ville Pouhula

Ville is a wizard-level Cloud DevOps expert and a certified Kubernetes administrator. Ville is located in Joensuu, Eastern Finland, and loves fishing, hunting, and skiing in the forests.

Posts authored by ville

Contact us

We are here to help your company's technology bloom.
So do not hesitate to contact us in any matters!

Read more insight in our blog