Kubernetes multi-node cluster using aws ec2 instance

Kubernetes multi-node cluster using aws ec2 instance

Setting up a multi-node Kubernetes cluster on AWS using EC2 instances involves several steps. Below is a simplified guide to help you get started. Please note that this is a basic setup, and you might need to adapt it based on your specific requirements and AWS account settings.

launching ec2 instances

  • Search ec2

  • Click on Launch instances

  • Select Amazon Linux

  • Select t2.micro

  • Number of instances = 3

  • Allow all traffic

After launching the nodes connect all the three nodes in cli.

Now install docker in all the nodes using

yum install docker -y

systemctl start docker

Now we have to install kubeadm for making the cluster so to install kubeadm we have to follow some steps.

# Set SELinux in permissive mode (effectively disabling it)

sudo setenforce 0

sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# This overwrites any existing configuration in /etc/yum.repos.d/kubernetes.repo

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/ enabled=1 gpgcheck=1 gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni EOF

Install kubelet, kubeadm and kubectl, and enable kubelet to ensure it's automatically started on startup:

sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes sudo systemctl enable --now kubelet

Now it’s time to setup the master

//to setup the master we use the below command

kubeadm init

but the command will fail with some warnings and errors, so lets check everything one by one.

  • Specify range of IP addresses for the pod network by using the option
    — pod-network-cidr

  • changing the docker cgroup driver to systemd

cd /etc/docker

vi daemon.json { "exec-opts": ["native.cgroupdriver=systemd"] }

cat daemon.json //after changing the conf file of docker we have to restart it systemctl restart docker

  • The minimum Ram and CPU requirement for this setup is 2GB and 2 CPU,
    but i am using only 1GB Ram and 1 CPU ,so you will get one error for this .
    You can either increase your Ram and CPU else ignore this error for setting up this cluster.
    Install one software called iproute-tc

yum install iproute-tc -y

So to setup masternode run these commands

kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU --ignore-preflight-errors=Mem

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

when you will run all these commands you will get joining command in the last two line of output likethis so copy it

kubeadm join 172.31.93.71:6443 --token qdachr.cmjaapxnl4nppuim --discovery-token-ca-cert-hash sha256:6573785912ba0ec0eea5cfb9a7344176cb7b5f19ed96e35aa061d8ec77a5eacd

to check the total nodes we have
kubectl get nodes

Now paste that copied code from master node to slave nodes and finally your multinode cluster will be ready.