How to Deploy a Kubernetes Cluster with Ubuntu Server 18.04
It’s almost impossible to avoid the siren song of Kubernetes these days. And with good reason. This container orchestration tool makes expanding your operations with an unheard of agility almost too easy to believe, once you have everything up and running.
In order to actually use Kubernetes to deploy and manage your containers, you first have to unleash a cluster of Kubernetes servers. Once this is complete, you’ll have the ability to deploy, scale, and manage your containerized applications.
I’m going to walk you through the process of doing just that, with the help of Ubuntu Server 18.04. All you will need to make this work are a minimum of 2 Ubuntu Server 18.04 instances and a user account with sudo privileges. You’ll want to make sure all machines are updated (using the commands sudo apt-get update and sudo apt-get upgrade -y).
And some time: Roughly 30 or so minutes.
I’ll be demonstrating on two servers: One master and one node.
Let’s get to work.
Installing Docker
The following must be done on both master and nodes.
The first thing to be done is the installation of Docker. To do this, log into the server and issue the command:
1 |
sudo apt-get install docker.io |
Once Docker is installed, you need to add your user to the docker group (otherwise you’d have to run all docker commands with sudo, which could lead to security issues). To add your user to the docker group, issue the command:
1 |
sudo usermod -aG docker $USER |
Log out and log back in, so the changes will take effect.
Start and enable the docker daemon with the commands:
1 2 |
sudo systemctl start docker sudo systemctl enable docker |
Installing Kubernetes
Now we need to install Kubernetes on both machines. To do this, first add the Kubernetes GPG key with the command:
1 |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add |
If you find that curl isn’t installed (it should be), install it with the command:
1 |
sudo apt-get install curl -y |
Next, add the necessary repository with the command:
1 |
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" |
Install the necessary software with the command:
1 |
sudo apt-get install kubeadm kubelet kubectl -y |
The above command will pick up all the necessary dependencies and complete them without failure.
Hostnames
To make things easier, you’ll want to assign specific hostnames to each server. To change the hostname, issue the command:
1 |
sudo hostnamectl set-hostname HOSTNAME |
Where HOSTNAME is the hostname for the host.
You might opt to use hostnames such as:
- kubemaster
- node1
- node2
- node3
- Etc.
Log out and log back in. Finally map hostnames to IP addresses. For this, open the hosts file for editing with the command:
1 |
sudo nano /etc/hosts |
Append something similar to the bottom of that file (making sure to use the hostnames you’ve given the machines as well as their associated IP addresses):
192.168.1.218 kubemaster
192.168.1.219 kubenode1
192.168.1.220 kubenode2
Save and close the file.
Disable Swap
In order to run Kubernetes, you must first disable swap. To do this, issue the command:
1 |
sudo swapoff -a |
To make that permanent (otherwise, swap will re-enable at reboot), issue the command:
1 |
sudo nano /etc/fstab |
In the fstab file, comment out the swap entry, as shown in Figure 1:

Figure 1: Disabling swap from within fstab.
Initializing the Master
The next step is to initialize your master. To do this, issue the command:
1 |
sudo kubeadm init --pod-network-cidr=192.168.1.90/16 |
Make sure to switch out the IP address of your master in the above command.
When the initialization completes, you will be given the precise command used to join the nodes to the master (Figure 2). Make sure to copy that command.

Figure 2: The command to join nodes to master.
On the master only, create a directory for the cluster with the command:
1 |
mkdir -p $HOME/.kube |
Copy the config file into this directory with the command:
1 |
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config |
Give the config file the proper permissions with the command:
1 |
sudo chown $(id -u):$(id -g) $HOME/.kube/config |
Deploying a Pod Network
Before you join the nodes to the master, you must first deploy a pod network (otherwise nothing will function as expected). One such pod network is Flannel. Deploy that with the following command (run only on the MASTER):
1 |
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml |
Joining a Node to the Master
You are now ready to join your node(s) to the master. Go to each node and issue the join command displayed after you initialized the master. That command will look similar to:
1 |
sudo kubeadm join 192.168.1.190:6443 --token bzbwl4.ll5o9x3jjhqqwofa --discovery-token-ca-cert-hash sha256:ecb0223a05be3502c2d102f3e56104b10fcd105430eb723d3b3e816618323d73 |
Run the same join command on every node. Once the join has succeeded, go back to your master and issue the command:
1 |
kubectl get nodes |
You should see all of your joined nodes listed (Figure 3).

Figure 3: Our node has joined and is ready.
At this point, your Kubernetes cluster is ready for the deployment of your first containerized application or service. Don’t forget, if you want to join more nodes to the master (to increase your ability to scale), you’ll need that join command. If you forgot to save that command, you can always retrieve it with the command:
1 |
kubeadm token create --print-join-command |
The above will print out the join command, so you can copy and paste it into your new node.