How to Install Kubernetes cluster on Ubuntu 18.04

System requirements

We are going to show how to create a Kubernetes cluster along with kubeadm on Ubuntu Server 18.04. Kubeadm is a command-line tool that simplifies the procedure of creating and managing a Kubernetes cluster.

All you will need to make this work are a minimum of 2 Ubuntu Server 18.04 instances.

  • Ubuntu Server 18.04 (Master)
  • Ubuntu Server 18.04 (Node)

The software dependency and network settings should be also verified before you proceed to thecd cd next step. Check the following items to prepare the environment:

Every node has a unique MAC address and product UUID

Check MAC address of your NIC

ifconfig -a

Check the product UUID on your host

sudo cat /sys/class/dmi/id/product_uuid

Every node has a different hostname

You might opt to use hostnames such as

  • kubemaster
  • node1

Docker is installed

For how to perform the Docker installation, you can follow the steps on the official website: (Ubuntu: https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/ Here we have Docker CE 19.06 installed on our machines

Disable Swap

Install Kubernetes

To install Kubernetes packages in Ubuntu 18.04 perform the following steps:

Step 1 : Some repositories are URL with HTTPS. The apt-transport-https package must be installed to access the HTTPS endpoint:

sudo apt-get update && sudo apt-get install -y apt-transport-https

Step 2 : Download the public key for accessing packages on Google Cloud, and add it as follows:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

Step 3 : Next, add a new source list for the Kubernetes packages:

sudo bash -c 'echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list'

Step 4 : Finally, it is good to install the Kubernetes packages:

sudo apt-get update && sudo apt-get install -y kubelet kubeadm kubectl

Check the version of kubernetes

kubectl version

On Kubernetes master

On Kubernetes node

Initialize Kubernetes Master

Step 1 : Now we can start the service. First enable and then start kubelet on your Kubernetes master machine:

sudo systemctl enable kubelet && sudo systemctl start kubelet

server connection failed since there is not server running

Step 2 : Go ahead and start all the master daemons via kubeadm. It is worth noting that using kubeadm requires the root permission to achieve a service level privilege. For any sudoer, each kubeadm would go after the sudo command:

sudo kubeadm init

Step 3 : And you will see the sentence Your Kubernetes master has initialized successfully! showing on the screen. Congratulations! You are almost done! Just follow the information about the user environment setup below the greeting message:

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 : The preceding commands ensure every Kubernetes instruction is fired by your account execute with the proper credentials and connects to the correct server portal:

kubectl version

Network configurations for containers

After the master of the cluster is ready to handle jobs and the services are running, for the purpose of making containers accessible to each other through networking, we need to set up the network for container communication. It is even more important initially while building up a Kubernetes cluster with kubeadm, since the master daemons are all running as containers. kubeadm supports the CNI ( https://github.com/containernetworking/cni).

There are many third-party CNI solutions that supply secured and reliable container network environments. Calico (https://www.projectcalico.org), one CNI provide stable container networking. Calico is light and simple, but still well implemented by the CNI standard and integrated with Kubernetes:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

We use kubectl to apply the configuration of Calico to our new-born Kubernetes.

Join worker nodes to master node

Let’s log in to your Kubernetes node to join the group controlled by kubeadm

Step 1 : First, enable and start the service, kubelet. Every Kubernetes machine should have kubelet running on it:

sudo systemctl enable kubelet && sudo systemctl start kubelet

Step 2 : After that, fire the kubeadm join command with an input flag token and the IP address of the master, notifying the master that it is a secured and authorized node. You can get the token on the master node via the kubeadm command:

sudo kubeadm token list

What if you call kubeadm token list to list the tokens, and see they are all expired? You can create a new one manually by this command:

kubeadm token create

Step 3 : In the preceding output, if kubeadm init succeeds, the default token will be generated. Copy the token and paste it onto the node, and then compose the following command:

sudo kubeadm join --token 41nm9a.9sqegg6u9f4tlb6n 192.168.1.92:6443 --discovery-token-unsafe-skip-ca-verification

The master IP is 192.168.1.92, token is 41nm9a.9sqegg6u9f4tlb6n, 6443 is the port of api server.

Step 4 : Once you see the words “This node has joined the cluster” showing on the screen, it is time to check with the master if the group got the new member:

kubectl get nodes

The flag discovery-token-unsafe-skip-ca-verification used while joining the cluster. Remember the kubelet log that says the certificate file is not found? That’s it, since our Kubernetes node is brand new and clean, and has never connected with the master before. There is no certificate file to find for verification. But now, because the node has shaken hands with the master, the file exists. We may join in this way (in some situation requiring rejoining the same cluster):

HASH=$(openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //')
sudo kubeadm join --token da3a90.9a119695a933a867 192.168.122.101:6443 --discovery-token-ca-cert-hash sha256:$HASH

Well done! kubeadm is installed and kubelet is running. You can easily go through the preceding steps to build your Kubernetes cluster.

Add Comment