Tuesday, November 26, 2024

Setup Kubernetes cluster with EC2 instance (Ubuntu 22)



Description:   In this blog We are going to setup Kubernetes Cluster with EC2 instance 


Below is the diagram for the setup 



There are many ways to setup Kubernetes Cluster 

  1.  Install Kubernetes using Minikube
  2.  Install Kubernetes using Kubeadm
  3. Install Kubernetes Using Terraform
  4.  Install Kubernetes using Kubernetes Operations (kops)

  • AWS EKS
  • Google K8s Engine
  • Azure K8s Service

In this example, we are going to setup the K8s cluster with Kubeadm [option-2]. 

Kubeadm is a tool designed to bootstrap a full-scale Kubernetes cluster. It takes care of all heavy lifting related to cluster provisioning and automates the process completely. 


In the deployment of Kubernetes clusters, two server types are used:


Master:

A Kubernetes Master is responsible for managing the Kubernetes cluster. It handles API calls related to cluster components like pods, replication controllers, services, and nodes. Key components of the master include:

  • Kube-API Server
  • Kube-Controller-Manager
  • Etcd
  • Kube-Scheduler

Node:

A Node provides the run-time environment for containers. It is a worker machine where the actual workloads run. A Kubernetes cluster typically has multiple nodes, and a collection of container pods can span across these nodes.


Server Specification

Server-typeHostnameSpecification
Masterk8s-ubuntu-master-nodet2.medium [4 GB RAM, 2 CPU, 30 GB Disk]
Worker-node-1k8s-ubuntu-worker-node-1t2.medium [4 GB RAM, 2 CPU, 30 GB Disk]
Worker-node-2k8s-ubuntu-worker-node-2t2.medium [4 GB RAM, 2 CPU, 30 GB Disk]


 In order to create K8s cluster, the following minimum requirements are needed:

Memory:

  • 2 GiB or more of RAM per instance

CPUs:

  • At least 2 CPUs on the control plane instance
Launch AWS instances: In this example, I have launch 3 instance with above specification and Ubuntu 22 image.

Below is the security group  for master and worker instance

Master:






Worker:







Install K8s cluster on Ubuntu 22


Setup Master and Worker Node:  Run below shell script in Master and Worker Node to setup the pre-requisites and kubeadm. Copy below bash script in Master and Worker machine 


Ref. Github URL:   https://github.com/harpal1990/setup-k8-Ec2


curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

 curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"

 echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check

 sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

 chmod +x kubectl
 mkdir -p ~/.local/bin
 mv ./kubectl ~/.local/bin/kubectl
 # and then append (or prepend) ~/.local/bin to $PATH

 kubectl version --client

# disable swap
sudo swapoff -a

# Create the .conf file to load the modules at bootup
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# Apply sysctl params without reboot
sudo sysctl --system

## Install CRIO Runtime
sudo apt-get update -y
sudo apt-get install -y software-properties-common curl apt-transport-https ca-certificates gpg

sudo curl -fsSL https://pkgs.k8s.io/addons:/cri-o:/prerelease:/main/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://pkgs.k8s.io/addons:/cri-o:/prerelease:/main/deb/ /" | sudo tee /etc/apt/sources.list.d/cri-o.list

sudo apt-get update -y
sudo apt-get install -y cri-o

sudo systemctl daemon-reload
sudo systemctl enable crio --now
sudo systemctl start crio.service

echo "CRI runtime installed successfully"

# Add Kubernetes APT repository and install required packages
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update -y
sudo apt-get install -y kubelet="1.29.0-*" kubectl="1.29.0-*" kubeadm="1.29.0-*"
sudo apt-get update -y
sudo apt-get install -y jq

sudo systemctl enable --now kubelet
sudo systemctl start kubelet


Setup Master Node [Only]: 

Initialise the Kubernetes Master Node, Copy the below script and run in master node 

# ./k8-master-setup.sh


sudo kubeadm config images pull sudo kubeadm init mkdir -p "$HOME"/.kube sudo cp -i /etc/kubernetes/admin.conf "$HOME"/.kube/config sudo chown "$(id -u)":"$(id -g)" "$HOME"/.kube/config # Network Plugin = calico kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.0/manifests/calico.yaml





Generate a token for worker nodes to join: Run below command in master node to get the command to join the worker node 

# kubeadm token create --print-join-command



Run same command  in both nodes to join the machines into kubernetes



Run the below command to get the node details after join in the kubernetes 
# kubectl get nodes



Congratulations, K8s is ready now you can setup the micro service infrastructure 

No comments:

Post a Comment