Creating Kubernetes Cluster using Azure VMs
Tags: kubernetes, devops, deepcodr, azure
Published: 2024-05-11 15:49:53

### 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

```jsx
sudo apt-get update
```

- upgrade the registry

```jsx
sudo apt upgrade
```

- Disable the firewall

```jsx
sudo ufw disable
```

- Remove swap entry

```jsx
swapoff -a
sudo sed -i '/swap/d' /etc/fstab
```

- Set kernel parameters

```jsx
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
```

- Install certificates

```jsx
sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
```

- Install Docker

```jsx
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
```

- Add Kubernetes Repository

```jsx
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
```

- Update apt and install kubectl, kubeadm and kubelet

```jsx
sudo apt-get update
sudo apt-get install -y kubeadm kubelet kubectl
```

#### Only For Master Node :

- Get the IP address of the master node using the below command. Copy IP address for eth0 interface.

```jsx
ip addr
```

- Start kubeadm on IP address.

```jsx
kubeadm init --apiserver-advertise-address=<IP_ADDRESS_OF_MASTER_NODE> --pod-network-cidr=192.168.0.0/16  --ignore-preflight-errors=all   
```

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

- Export config to KUBECONFIG variable

```jsx
export KUBECONFIG=/etc/kubernetes/admin.conf
```

- Create Kubernetes configuration files.

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

#### Run below command on each worker node.

- Run the kubeadm join command copied so that the worker joins the cluster.

```jsx
kubeadm join 10.0.0.4:6443 --token 65p8a3.ojrjbnjbwczzdxu9 --discovery-token-ca-cert-hash sha256:beee58d9efa43ee970d744b2cc2e9ae3513dc90984028047a52a25798a33710f
```

—— Example Command ——

- Enable Container Network Interface using below command.(Only For Master Node)

```jsx
kubectl apply -f "https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s-1.11.yaml"
```

- Use the cluster using kubectl

```jsx
kubectl get nodes
kubectl get all -A
```

- Kube join command

```jsx
kubeadm token create --print-join-command
```