Configuring A Multi-Node Kubernetes Cluster On AWS Cloud

In this article, I am going to show you how to configure a multi-node Kubernetes cluster on AWS cloud.

  • The first step is to launch ec2 instances on the AWS cloud on which the cluster is going to be configured. Here, I am going to launch 2 instances, one for the master node and one for the slave node.

Here I am going to use Amazon Linux 2 AMI to launch instances. The instance type is t2.micro.I have allowed all traffic on the security group of the instances.

Let’s start configuring the master node

Login into the master node. For login we can use putty or any other tools.Here I have used SSH in my Windows Command prompt to login.

1.I am going to use docker as container engine, so the first step is to install dokcer. The repo for docker is pre-configured on Amazon Linux 2 AMI and to install docker, use the above command:

$ sudo yum install docker -y

2. Start docker services

3. Get docker info

4. Here I am going to set up kubernetes cluster using kubeadm program.

From here I am login into the root account using the command

$ sudo su — root

The repo for kubeadm is not pre-configured on Amazon Linux 2 AMI. So first we have to configure yum repo for kubeadm.

For reference use this link https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

Now we can install kubeadm using command

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

Starting the Kubelet Service

As of now, the status is ‘activating’ but it will start soon, as we proceed towards configuring the cluster.

5. Kubernetes runs multiple programs behind the scene and it launched all those programs on top of docker containers. And to launch containers of the respective programs, it needs the respective images. so now we have to pull the required images

To pull the images use the command

6. Docker by default uses ‘cgroupfs’ as its Cgroup Driver. But Kubernetes does not have proper support for this driver.So, the next step is to change Cgroup driver from ‘cgroupfs’ to ‘systemd’This can be done by creating a ‘daemon.json’ file in ‘/etc/docker/’ with the following content

Restarting Docker Service. Use the above command to do

systemctl restart docker

7. The next step is to make changes in IPtables settings.Use the above commands

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

And finally, use the above command to apply the changes

sysctl — — system

8. We also need ‘iproute-tc’ package for traffic control, use the above command to install it

yum install iproute-tc -y

9. Initializing the Kubernetes Cluster using kubeadm init command.Generally, we use the above command to initialize the cluster

kubeadm init — pod-network-cidr=[Network CIDR]

Kubernetes requires a minimum of 2 CPUs and 2 GiB RAM to initialize the cluster. If you are using t2.mirco instance type (with 1 vCPU and1 Gib RAM), it might throw an error.To get rid of the error, you can use the above command:

kubeadm init — pod-network-cidr=[Network CIDR] — -ignore-preflight-errors=NumCPU — -ignore-preflight-errors=Mem

10. The next step is to set up the kubeconfig file that will help kubectl client command to connect to the cluster. Use the above commands.

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

we can also see that the kubelet service has been started:

11. We can see that we have only one node and it is not ready as of now. In the final step, we have to run the kube-flannel program.Use the above command to set up the flannel program for the cluster

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

12. The kubeadm init command gives a join token. We have to use this token in order to connect the slave node with the master node. You can also recreate this token in the master node using the above command

kubeadm token create — -print-join-command

Now we can use the above token in the slave node to connect it with the master node

Let’s start configuring the slave node

These are the operations that are same for the master as well as the slave node:

  • Installing Docker
  • Starting and Enabling Docker Service
  • Yum Repo for Kubeadm, Kubectl, and Kubelet
  • Installing Kubeadm, Kubectl, and Kubelet
  • Starting Kubelet service
  • Changing Cgroup driver and restarting docker service
  • Changing IPtables settings
  • Installing iproute-tc

After completing all these steps, the final step for the slave node is to run the join command with the token provided by the master node

And the slave has joined the cluster successfully.

And finally, run ‘kubectl get nodes’ command on the master node to check the nodes

And hence, a multi-node Kubernetes cluster with one master and one slave node has been configured.