DEV Community

Cover image for Creating Kubernetes Cluster using Azure VMs
Deepak Patil
Deepak Patil

Posted on

Creating Kubernetes Cluster using Azure VMs

Creating Kubernetes cluster in Azure

Hello,
In this article, we will see how to set up a Kubernetes cluster in Azure. For setting cluster we will set up Azure VMs which will act as nodes for the cluster. So let's start with setup.

Start with creating two VMs one acting as master and the other as worker.

Select a D series VM with a minimum of 2 Vcpus and 8Gib of RAM and Linux OS.

SSH to both VMs and run the below commands (Both Worker and Master Node)

  • Update the registry
sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
  • upgrade the registry
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode
  • Disable the firewall
sudo ufw disable
Enter fullscreen mode Exit fullscreen mode
  • Remove swap entry
swapoff -a
sudo sed -i '/swap/d' /etc/fstab
Enter fullscreen mode Exit fullscreen mode
  • Set kernel parameters
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
sudo systemctl --system
Enter fullscreen mode Exit fullscreen mode
  • Install certificates
sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Enter fullscreen mode Exit fullscreen mode
  • Install Docker
sudo su
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update
apt install docker.io
Enter fullscreen mode Exit fullscreen mode
  • Add Kubernetes Repository
sudo mkdir -p -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Add Repository and install
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 chmod 644 /etc/apt/sources.list.d/kubernetes.list
Enter fullscreen mode Exit fullscreen mode
  • Update apt and install kubectl, kubeadm and kubelet
sudo apt-get update
sudo apt-get install -y kubeadm kubelet kubectl
Enter fullscreen mode Exit fullscreen mode

Only For Master Node :

  • Get the IP address of the master node using the below command. Copy IP address for eth0 interface.
ip addr
Enter fullscreen mode Exit fullscreen mode
  • Start kubeadm on IP address.
kubeadm init --apiserver-advertise-address=<IP_ADDRESS_OF_MASTER_NODE> --pod-network-cidr=192.168.0.0/16  --ignore-preflight-errors=all   
Enter fullscreen mode Exit fullscreen mode

Note: Copy the kubeadm join command from the previous command output.

  • Export config to KUBECONFIG variable
export KUBECONFIG=/etc/kubernetes/admin.conf
Enter fullscreen mode Exit fullscreen mode
  • Create Kubernetes configuration files.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Enter fullscreen mode Exit fullscreen mode

Run below command on each worker node.

  • Run the kubeadm join command copied so that the worker joins the cluster.
kubeadm join 10.0.0.4:6443 --token 65p8a3.ojrjbnjbwczzdxu9 --discovery-token-ca-cert-hash sha256:beee58d9efa43ee970d744b2cc2e9ae3513dc90984028047a52a25798a33710f
Enter fullscreen mode Exit fullscreen mode

—— Example Command ——

  • Enable Container Network Interface using below command.(Only For Master Node)
kubectl apply -f "https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s-1.11.yaml"
Enter fullscreen mode Exit fullscreen mode
  • Use the cluster using kubectl
kubectl get nodes
kubectl get all -A
Enter fullscreen mode Exit fullscreen mode
  • Kube join command
kubeadm token create --print-join-command
Enter fullscreen mode Exit fullscreen mode

Top comments (0)