Getting a Kubernetes cluster up and running isn’t as difficult as you might think.
Kubernetes has become the darling of the enterprise world. Why? Because if you need the ability to quickly scale a deployment of containers, you cannot beat the power and flexibility of this tool.
But how do you deploy such a system? It’s a process that requires some time and effort, but I’m going to try and help make it easy for you. Let’s walk through the steps of deploying such a cluster on three Ubuntu Server 18.04 machines:
- kubemaster – 192.168.1.218
- kubenode1 – 192.168.1.219
- kubenode2 – 192.168.1.220
Of course, you can add as many nodes as you like, but we’ll stick with three for this tutorial.
SEE: Choosing your Windows 7 exit strategy: Four options (TechRepublic Premium)
What you’ll need
First of all, you’ll need three Ubuntu Server 18.04 machines, updated and running. You’ll also need a user account (on each machine) with sudo privileges.
With those things at the ready, let’s deploy.
How to install Docker
The first thing to do is install Docker on all three machines. To do this, open a terminal window and issue the command:
sudo apt-get install docker.io -y
Once that completes, add your user to the docker group with the command:
sudo usermod -aG docker $USER
Log out and log back in, so the changes will take effect.
Start and enable Docker with the following commands:
sudo systemctl start docker sudo systemctl enable docker
How to install Kubernetes
Now we need to install Kubernetes. Since this tool isn’t available in the standard repositories, we must add the necessary repo with the following commands (on all machines):
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
Next we can install all of the necessary Kubernetes tools with the command:
sudo apt-get install kubeadm kubelet kubectl
Hostname and hosts file
You’ll want to assign specific hostnames to each server, such as:
kubemaster kubenode1 kubenode2
To do this, issue the command (on each server):
sudo hostnamectl set-hostname HOSTNAME
Where HOSTNAME is the hostname for the host.
Next edit the /etc/hosts file to map hostnames to IP addresses. Issue the command:
sudo nano /etc/hosts
And append the following to the bottom:
192.168.1.218 kubemaster 192.168.1.219 kubenode1 192.168.1.220 kubenode2
Make sure to substitute your IP addresses and hostnames.
Save and close the file.
How to deploy Kubernetes
Before you deploy Kubernetes, you must first turn swap off. To do this permanently, issue the command:
sudo nano /etc/fstab
Comment out the swap line (as shown in Figure A).
Save and close the file. Now issue the command:
sudo swapoff -a
Now on the master only, issue the command:
sudo kubeadm init --pod-network-cidr=192.168.1.218/16
Make sure to substitute the IP address of your master in the above command. All pre-flight checks should pass and eventually you should be given the exact command to run on all nodes so they can join the cluster (Figure B).
Copy that join command, because you still have a few more initialization steps to take on the master.
On the master, create a directory for the cluster with the command:
mkdir -p $HOME/.kube
Copy the config file into this directory with the command:
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
Give the config file the proper permissions with the command:
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Deploy a pod network (in this case we’ll use flannel) to the cluster with the command:
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
And now you can run the join commands on each of the nodes.
That’s all there is to it. Once you’ve joined your nodes, your Kubernetes cluster is ready to go.