In this guide, I’ll take you through the steps to install and set up a working 3 node Kubernetes Cluster on Ubuntu 18.04 Bionic Beaver Linux. Kubernetes is an open-source container-orchestration system used for automating deployment, management, and scaling of containerized applications.
Kubernetes on Ubuntu 18.04 – System Diagram
This setup is based on the following diagram:
Let’s configure system hostnames before we can proceed to next steps:
On Master Node:
Set hostname like below:
$ sudo hostnamectl set-hostname k8s-master
On Worker Node 01:
Set the hostname using hostamectl
command line tool.
$ sudo hostnamectl set-hostname k8s-node-01
On Worker Node 02:
Also set hostname for Kubernetes worker node 02.
$ sudo hostnamectl set-hostname k8s-node-02
Once correct hostname has been configured on each host, populate on each node with the values configured.
$ cat /etc/hosts 192.168.2.2 k8s-master 192.168.2.3 k8s-node-01 192.168.2.4 k8s-node-02
Setup Kubernetes on Ubuntu 18.04 – Prerequisites (Run on all nodes)
Before doing any Kubernetes specific configurations, let’s ensure all deps are satisfied. Here we will do a system update and create Kubernetes user.
Update system packages to the latest release on all nodes:
sudo apt-get update sudo apt-get upgrade sudo apt-get install linux-image-extra-virtual sudo reboot
Add user to manage Kubernetes cluster:
sudo useradd -s /bin/bash -m k8s-admin sudo passwd k8s-admin sudo usermod -aG sudo k8s-admin echo "k8s-admin ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/k8s-admin
If you prefer entering sudo password when running sudo commands as k8s-admin
user, then you can ignore the last line. You can test if no password prompt for sudo:
$ su - k8s-admin [email protected]:~$ sudo su - [email protected]:~#
All looks good, let’s proceed to install Docker engine.
Setup Kubernetes on Ubuntu 18.04 – Install Docker Engine
Kubernetes requires docker to run containers used for hosting applications and other Kubernetes services. We have a comprehensive Docker installation guide:
How to install Docker CE on Ubuntu / Debian / Fedora / Arch / CentOS
If you need a quick installation guide, then use the following commands to install Docker Engine on Ubuntu 18.04. Ensure any old version of Docker engine is uninstalled on your system:
sudo apt-get remove docker docker-engine docker.i
Install dependencies:
Import Docker repository GPG key:
Install docker:
sudo apt-get update sudo apt-get install docker-ce sudo usermod -aG docker k8s-admin
When docker has been installed, you can continue to configure the Kubernetes master node.
Setup Kubernetes on Ubuntu 18.04 – Install and Configure Kubernetes Master
All commands that will be executed on this section are meant to be run on the master node. Don’t execute any of the commands on Kubernetes worker nodes. Kubernetes Master components provide the cluster’s control plane – API Server, Scheduler, Controller Manager. They make global decisions about the cluster e.g scheduling and detecting and responding to cluster events.
Add Kubernetes repository
As of this writing, there is no official repository for Ubuntu 18.04, we will add a repository for Ubuntu 16.04. I tested it. All packages and dependencies should install fine. I’ll update this article when a repo for Ubuntu 18.04 is available.
Then import GPG key:
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
Update apt package index:
sudo apt update
Install Kubernetes Master Components
Install kubectl, kubelet, kubernetes-cni and kubeadm
Kubernetes master components:
sudo apt install kubectl kubelet kubeadm kubernetes-cni
Confirm that all package binaries are present on the file system.
$ which kubelet /usr/bin/kubelet $ which kubeadm /usr/bin/kubeadm
If swap is on, turn it off.
sudo swapoff -a
Initialize Kubernetes Cluster:
When all Kubernetes packages have been installed, you’re ready to initialize the cluster using kubeadm
command line tool.
Export required variables (Optional)
Then initialize the Kubernetes cluster using variables defined above:
If all goes well, you should get a success message with the instructions of what to do next:
Configure Access for k8s-admin
user on the Master server
Switch to k8s-admin
and copy Kubernetes configuration file with cluster information.
Deploy Weave Net POD Network to the Cluster ( Run as normal user)
Weave Net creates a virtual network that connects Docker containers across multiple hosts and enables their automatic discovery. Services provided by application containers on the Weave network can be exposed to the outside world, regardless of where they are running.
Weave Net can be installed onto your CNI-enabled Kubernetes cluster with a single command:
After a few seconds, a Weave Net pod should be running on each Node and any further pods you create will be automatically attached to the Weave network.
Setup Kubernetes Worker Nodes
When Kubernetes cluster has been initialized and the master node is online, start Worker Nodes configuration. A node is a worker machine in Kubernetes, it may be a VM or physical machine. Each node is managed by the master and has the services necessary to run pods – docker, kubelet, kube-proxy
Step 1: Ensure Docker is installed (covered)
Ensure docker engine is installed on all Worker nodes. Refer to docker installation section
Step 2: Add Kubernetes repository ( covered)
Ensure that repository for Kubenetes packages is added to the system. Refer ^^
Step 3: Install Kubenetes components
Once you’ve added Kubernetes repository, install components using:
sudo apt install kubelet kubeadm kubectl kubernetes-cni
Step 4: Join the Node to the Cluster:
Use the join command given after initializing Kubernetes cluster. E.g
When done, Check nodes status on the master:
On the two nodes, Weave Net should be configured.
Test Kubernetes Deployment
Let us create test pod to confirm that our cluster is running as expected.
Deploy it to the cluster
Create a test namespace:
$ kubectl create namespace test-namespace namespace/test-namespace created
After namespace is created, create a pod using deployment object defined earlier. -n is used to specify the namespace. We expect three pods to be created since our replicas
value is 3.
$ kubectl create -n test-namespace -f http-app-deployment.yml deployment.extensions/http-app created
Confirm:
You can see we have http-app
deployment live.
With the deployment created, we can use kubectl to create a service which exposes the Pods on a particular port. An alternative method is defining a Service object with YAML. Below is our service definition.
Create a service using kubectl
command:
$ kubectl -n test-namespace create -f http-app-service.yml service/http-app-svc created
This service will be available on Cluster IP and port 30080. To get cluster IP, use:
Kubernetes on Ubuntu 18.04 – Post-Installation
Enable shell autocompletion for kubectl commands. kubectl includes autocompletion support, which can save a lot of typing!. To enable shell completion in your current session, run:
source <(kubectl completion bash)
To add kubectl autocompletion to your profile, so it is automatically loaded in future shells run:
echo "source <(kubectl completion bash)" >> ~/.bashrc
If you are using zsh edit the ~/.zshrc file and add the following code to enable kubectl autocompletion:
if [ $commands[kubectl] ]; then source <(kubectl completion zsh) fi
Or when using Oh-My-Zsh, edit the ~/.zshrc
file and update the plugins= line to include the kubectl plugin.
source <(kubectl completion zsh)
Conclusion
We have successfully deployed a 3 node Kubernetes cluster on Ubuntu 18.04 LTS servers. Our next guides will cover Kubernetes HA, Kubernetes Monitoring, How to configure external storage and more cool stuff. Stay tuned!.