How to Install Kubernetes Cluster on Ubuntu 18
Kubernetes (k8s) is an open-source, cloud-native, container orchestration and management platform. It’s the go-to way to automate the deployment, scaling, and maintenance of containerised applications across different nodes. From service discovery to auto-restarts, and from resource allocation tracking to compute utilisation and scaling; a well-configured k8s cluster can manage a lot on its own.
Got CentOS VMs? Learn How to install Kubernetes and deploy a cluster with Docker on CentOS 7
A Kubernetes cluster consists of a Master and at least one to several worker node(s). The Master is the virtual machine (VM) that administers all activities on your cluster. A node is a VM that serves as a worker machine in your k8s cluster to host running applications. We strongly recommend you only use VMs aka Cloud Servers to run Kubernetes, not system containers aka VPS, as these can cause issues with k8s.
A node is comprised of the Kubelet, a container runtime, and the kube-proxy. The k8s installation’s three core modules: Kubelet, kubeadm, and kubectl are agents that control the node and communicate with the Kubernetes Master. Once they have been installed and other configurations done, you will be able to create your first k8s cluster. You can manage this cluster from the command line on your kubemaster node.
Every Kubernetes instance runs on top of a container runtime, which is software responsible for managing container operations. Containers in this case are not virtualised servers but rather a solution that packages code and dependencies to run a single application (service) in an isolated (containerised) environment, essentially disassociating applications from the host machine. The most popular and recommended one is Docker, and it’s the one we will use for the purpose of this guide. However, if you want to install a different underlying container runtime, you can harness the power of the Container Runtime Interface and use basically any runtime you want.
Kubernetes groups containers into pods, its most basic operational unit, which are basically just groups of containers running on the same node. Pods are connected over a network and share storage resources.
In order to connect your nodes or VMs and make them private, make sure to choose a hosting company who provides a Virtual Local Area Network (VLAN) with their VMs. We offer a VLAN add-on to our Cloud Servers for R200 per month.
• Multiple Ubuntu 18 VMs (Cloud Servers) to house the Master and worker nodes.
• Docker or any other container runtime.
• User with
root privileges on every server.
Use our guide on How to install Docker on your Ubuntu 18.04
Before we proceed with the installations, it’s a recommended practice to update and upgrade our system. Use the following command:
sudo apt-get update && sudo apt-get upgrade
Depending on your system, it may take some time before the command finishes.
Set the appropriate hostnames for your master and worker nodes:
sudo hostnamectl set-hostname "master-node" exec bash
sudo hostnamectl set-hostname "w-node1" exec bash
Now open the /etc/hosts file and edit the hostnames for your worker nodes:
sudo cat <<EOF>> /etc/hosts 10.168.10.207 master-node 10.168.10.208 node1 w-node1 10.168.10.209 node2 w-node2 EOF
For seamless communication across multiple nodes, we need to define rules in firewall. Use the following commands on your master node to do so:
sudo ufw allow 6443/tcp sudo ufw allow 2379/tcp sudo ufw allow 2380/tcp sudo ufw allow 10250/tcp sudo ufw allow 10251/tcp sudo ufw allow 10252/tcp sudo ufw allow 10255/tcp sudo ufw reload
We also need to execute these commands on each of the worker nodes:
sudo ufw allow 10251/tcp sudo ufw allow 10255/tcp sudo ufw reload
For Kubernetes APIs to work properly and securely, we need to install https and
curl on all VMs. Use the following command to do so:
sudo apt-get install apt-transport-https curl -y
We will use
curl to add the Kubernetes repository on all the nodes. See commands below to first add the key, and then the repository:
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
sudo apt-add-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
At this stage, we are ready to install the Kubernetes package. Use the following command:
(Note: The kubeadm package will also install all other necessary packages)
sudo apt-get install kubeadm -y
Verify your installation by running the
kubeadm command. You should get output similar to this:
For kubeadm to function properly, we need to turn the swap off. Use the following command on all machines:
sudo swapoff -a
Now that we are done installing Kubernetes, we can deploy our very own cluster. These are the necessary steps:
To initialize our cluster, we need to initialize kubeadm. Run the following command on the Master node:
sudo kubeadm init
This can take some time as Kubernetes allocates resources and starts necessary services. A successful execution of the command should look like this:
Go ahead and copy the text following the line
Then you can join any number of worker nodes by running the following on each as root:. This is the command we will later use to add worker nodes to our cluster.
Note: If you forgot to copy the command, or have misplaced it, don’t worry. You can retrieve it again by entering the following command:
sudo kubeadm token create --print-join-command
In order to start managing your cluster, you need to create a directory and assume ownership. Run the following commands as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Pods within a cluster are connected via the pod network. At this point, it’s not working. This can be verified by entering the following two commands:
sudo kubectl get nodes
sudo kubectl get pods --all-namespaces
As you can see, the status of
NotReady. The CoreDNS service is also not running. To fix this, run the following command:
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
You should get the following output:
And now if you verify the statuses of your node and CoreDNS service, you should get
Running like seen below:
As a final step, you need to add worker nodes to your cluster. We will use the kubeadm join auto-generated token in Step 1. here. Run your own version of the following command on all of the worker node VMs:
kubeadm join 126.96.36.199:6443 --token hpv60w.uw784fik3nqgf7fg --discovery-token-ca-cert-hash sha256:1ce91085c95535acaf1c536d5a84b54549680762fbbaf07467c6b8893a78db1a
On successful addition, you should get the following output:
Running the following command on the
master-node should show your newly added node.
sudo kubectl get nodes
To set the role for your worker node, use the following command:
sudo kubectl label node w-node1 node-role.kubernetes.io/worker=worker
Now you’re all set up.