Modal Title
Kubernetes

How to Deploy a Kubernetes Cluster with Ubuntu Server 18.04

How to install Kubernetes on your Ubuntu server.
Oct 15th, 2019 1:28pm by
Featued image for: How to Deploy a Kubernetes Cluster with Ubuntu Server 18.04
Feature image by M Ameen from Pixabay.

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:


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:


Log out and log back in, so the changes will take effect.

Start and enable the docker daemon with the commands:

Installing Kubernetes

Now we need to install Kubernetes on both machines. To do this, first add the Kubernetes GPG key with the command:


If you find that curl isn’t installed (it should be), install it with the command:


Next, add the necessary repository with the command:


Install the necessary software with the command:


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:


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:


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:


To make that permanent (otherwise, swap will re-enable at reboot), issue the command:


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:


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:


Copy the config file into this directory with the command:


Give the config file the proper permissions with the command:

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):

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:


Run the same join command on every node. Once the join has succeeded, go back to your master and issue the command:


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:


The above will print out the join command, so you can copy and paste it into your new node.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.