In this blog, I will be discussing about how to install Kubernetes on Ubuntu 18.04. Kubernetes is one of the highest trending technologies in Cloud Computing as of today. It is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem.
Prerequisites:
- Minimum 2 Ubuntu server (1 Master and 1 Worker node)
- 2 GB of RAM and 2 Core of CPU
Preparing Ubuntu servers:
1. Edit the hosts file on both the nodes using vi or vim and add the hostname and ip-address
sudo vim /etc/hosts
Make sure name resolution is working now between the kubernetes nodes
2. Disable swap space – We should disable swap as it can give issues during the installation of Kubernetes.
sudo swapoff -a
You can disable the swap permanently by editing the fstab file
sudo vim /etc/fstab
3. Reboot the server
sudo reboot
4. Install docker by running the below commands
sudo su
apt-get update
apt-get install -y docker.io
5. Run the below commands to start the docker service and enable it during every system boot.
sudo systemctl start docker
sudo systemctl enable docker
Install Kubeadm:
Kubeadm is a tool built to provide kubeadm init and kubeadm join as best practice “fast paths” for creating Kubernetes clusters. It performs the actions necessary to get a minimum viable cluster up and running.
1. Install apt-transport-https
apt-get update && apt-get install -y apt-transport-https curl
2. Adding Kubernetes key
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
3. Add the Kubernetes repository
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
> deb http://apt.kubernetes.io/ kubernetes-xenial main
> EOF
You can also add the repository by manually editing using any of the text editor available.
4. Run APT update – (Do not miss this step, otherwise the next step will fail)
apt-get update
5. Install kubeadm, kubelet and kubectl
apt-get install -y kubelet kubeadm kubectl
Initialize the Kubernetes master:
Note: Run the command on the master node only.
sudo kubeadm init --apiserver-advertise-address=MASTER_NODE_IP --pod-network-cidr=POD-Network_CIDR --kubernetes-version "KUBERNETES_VERSION"
For example: sudo kubeadm init --apiserver-advertise-address=10.128.0.9 --pod-network-cidr=172.16.0.0/16 --kubernetes-version "1.15.0"
Note: I am using 172.16.0.0/16 as my pod CIDR
–apiserver-advertise-address > The IP address the API Server will advertise it’s listening on. If not set the default network interface will be used
–pod-network-cidr > Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node.
–-kubernetes-version > Choose a specific Kubernetes version for the control plane.
For more options refer : https://kubernetes.io/
Your Kubernetes control-plane has initialized successfully. Make a note of the below 3 commands mentioned in the output of kubeadm init. To start using your cluster, you need to run the following as a regular user from the master node
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
If you do not run the above, kubectl commands will fail with the below error
root@master01:~# kubectl get nodes
The connection to the server localhost:8080 was refused – did you specify the right host or port?
You will also see kubeadm join command as below. This can be used to join worker nodes by running the following on each as root. Make sure you run this on the worker nodes.
kubeadm join 10.128.0.9:6443 --token lbrw78.uvd3slp9awct4ng2 --discovery-token-ca-cert-hash sha256:ec091cf31a37156740e0d0a4d580a7a1a0145a1c34cb43309d22704f0815eef4

Your Kubernetes cluster is now created. Go ahead and run kubectl commands to explore more.
If you run kubectl get nodes
, you will notice that the node status is not ready and there are some pods in pending status? To fix this issue, we need to install any of the Cloud Network Interface(CNI) like Calico, Weave or Flannel. In this lab, I am using Calico as my CNI.
You can download the Calico networking manifest for the Kubernetes API datastore.
curl
https://docs.projectcalico.org/v3.7/manifests/calico.yaml -O
Note: If you are using pod CIDR 192.168.0.0/16
, skip to the apply manifest step. If you are using a different pod CIDR, use the below commands to set an environment variable called POD_CIDR
containing your pod CIDR and replace 192.168.0.0/16
in the manifest with your pod CIDR.
POD_CIDR="172.16.0.0"
sed -i -e "s?192.168.0.0?$POD_CIDR?g" calico.yaml
Apply the manifest using the following command.
kubectl apply -f calico.yaml
For more details : https://docs.projectcalico.org
Run kubectl commands and confirm that the nodes are ready and pods have picked up the 172.16.0.0 network IP address.
