Do you want to learn how to set up Kubernetes on Ubuntu? Keep reading this article!
Kubernetes is a powerful open-source platform for automatic scaling, deployment, and management of containerized systems. It’s a robust application that groups containers into logic units making it easy to discover and manage them across multiple clusters of hosts. Kubernetes has many pieces such as network transport drivers, various system components, and CLI utilities. The system also works with numerous container tools such as Docker and includes many moving parts and enormous workloads which makes its installation intricate.
This tutorial will show you how to quickly install kubernetes on Ubuntu with minimal to no frustration.
Additional reading: if you wish to research Kubernetes and container hosting further, we recommend the following articles:
Let’s get started.
Must Haves:
For our installation to succeed, you should have the following:
- At least two Ubuntu machines; one as the master and the other for worker. If you can get etcd and Apiserver on a single machine with one core and one GB RAM for clusters with at least 10s of nodes that will be enough for this installation. On the other hand, if you intend to use more active and larger clusters consider getting more core.
Step 1: Install the dependencies
The first and the most important thing when setting up Kubernetes on Ubuntu is installing the required dependencies. The dependencies must be installed on all the machines that make your Kubernetes cluster.
One of the dependencies you need to install is the apt-transport-https. This package allows you to utilize the HTTP and HTTPS protocols in the apt repository resources. The package can be installed using the following command:
sudo apt-get update && apt-get install -y apt-transport-https
The second dependency is Docker. Docker is a reliable application that simplifies the creation and configuration of application processes in containerized systems. The Kubernetes installation will rely on Docker, and for this reason, it should be installed first. Use the following command to install Docker:
sudo apt install docker.io
Once Docker is fully installed, run the following commands to start and enable the application:
sudo systemctl start docker sudo systemctl enable docker
Step 2: Installing kubernetes components
Once the Docker is up and running, you are ready for the next step. As aforementioned, Kubernetes has many pieces and installing all the necessary components should be straightforward.
Download and incorporate the Key by running the following command at the terminal:
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add
Run the command below to install kubernetes apt repo:
$ apt-get update && apt-get install -y apt-transport-https \ && curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - $ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
Next, you need to update the package list using the following command:
apt-get update
Now, we install three components Kubeadm, kubelet, and kubernetes-cni. The kubeadm is a great utility that helps you configure the multiple components that form a working cluster; the Kubelet makes it easy to run the containers on the hosts, whereas the Kubernetes-cni defines the networking components. CNI or Container Networking Interface is a robust spec that dictates how network drivers interact with kubernetes.
Use the following command to install the three components:
$ apt-get update \ && apt-get install -y kubelet kubeadm kubernetes-cni
Run kubeadm to initialize your server. Kubeadm provides a secure cluster using mechanisms such as RBAC.
Step 3: Initialize the master
Once you have installed the three components, you can proceed to initialize your master. Go to the machine you intend to use as the master and run the following command:
sudo kubeadm init
When the initialization is complete, you will get the command below which is required to join your nodes to the master:
The command above indicates your kubernetes master is ready for the nodes. But, just before you join any node to the master, run this command:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
STEP 4: Deploy a pod network
Next, you must install and configure a pod network for everything to function properly. Here we will install a Flannel pod network. Flannel offers a reliable Software Defined Network (SDN) via ipvlan modules and kernel’s overlay. Deploying Flannel can be achieved through two commands applied on the Kubernetes master. The first command:
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
The second command is:
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml
Confirm the pod network is properly configured by running the following command: sudo kubectl get pods. If the Flannel pod network is properly installed, you will get the output below:
Step 5: Join a node
By now all the components are ready, and your kubernetes master is ready for node joining. Navigate to the terminal (of the node) and run the following command:
sudo kubeadm join --token TOKEN MASTER_IP:6443
This helps confirm the nodes have joined the master successfully;
Step 6: Deploy a service
At this point, everything is properly configured, and you can easily deploy a service on the kubernetes cluster. Run the command below from your Kubernetes master:
sudo kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster" sudo kubectl expose deployment nginx-app --port=80 --name=nginx-http
The command will help you deploy NGINX service exposed on port 80. Navigate to the node and issue the following command:
$sudo docker ps -a
The command will give you an output of the services deployed
Your Kubernetes is successfully installed on Ubuntu. The cluster comprises a single node and a master. However, it’s easy to scale the cluster by deploying more nodes using our instructions.