Installation of kubernetes on Azure virtual machines

This is a demo guide of how to install kubernetes master and worker nodes on Microsoft Azure standalone virtual machines.

This guide is not intended as a solution for production purposes.

Pre-requisites

This guide assumes that you have a Microsoft Azure account and that you installed the Azure CLI and you are logged in to the respective subscription in Azure.

First steps

To create a resource group:

az group create -n rg_k8sdemo --location eastus2 --tags Purpose=Education

To create a VNET:

az network vnet create \
--resource-group rg_k8sdemo \
--name vnet-k8s-101519 \
--address-prefix 10.0.0.0/16 \
--subnet-name subnet-k8s-101519 \
--subnet-prefix 10.0.0.0/24

To create a virtual machine (as master):

az vm create \
--location eastus2 \
--size Standard_B1ms \
--resource-group rg_k8sdemo \
--image UbuntuLTS \
--computer-name my-k8s-master-00 \
--name k8s-101519-master-00 \
--vnet-name vnet-k8s-101519 \
--subnet subnet-k8s-101519 \
--storage-sku Standard_LRS \
--ssh-key-values .ssh/id_rsa.pub \
--tags role=master

This command creates a small size virtual machine that uses “standard” storage for the OS disk. Besides, the command links the virtual machine to the previously created VNET.

To create a virtual machine (as worker node):

az vm create \
--location eastus2 \
--size Standard_B1ms \
--resource-group rg_k8sdemo \
--image UbuntuLTS \
--computer-name my-k8s-worker-00 \
--name k8s-101519-worker-00 \
--vnet-name vnet-k8s-101519 \
--subnet subnet-k8s-101519 \
--storage-sku Standard_LRS \
--ssh-key-values .ssh/id_rsa.pub \
--tags role=worker

Please repeat this command to create another VM. Adjust the values where applicable.

Kubernetes installation and configuration

GPG keys

It is required the GPG key for Docker and kubernetes, as well.

Get the Docker gpg key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

Get the Kubernetes gpg key:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add –

Repositories

Add the Docker repository:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Add the Kubernetes repository:

cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

Package management

To update the packages of the system:

sudo apt-get update

Install Docker, kubelet, kubeadm, and kubectl:

sudo apt-get install -y docker-ce=18.06.1~ce~3-0~ubuntu kubelet=1.13.5-00 kubeadm=1.13.5-00 kubectl=1.13.5-00

Hold them at the current version:

sudo apt-mark hold docker-ce kubelet kubeadm kubectl

Iptables rule

Add the iptables rule to sysctl.conf:

echo "net.bridge.bridge-nf-call-iptables=1" | sudo tee -a /etc/sysctl.conf

Enable iptables immediately:

sudo sysctl -p

Kubernetes tasks

Initialize the cluster (run only on the master):

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

I learned that it is required a minimum of 2 vCPUs for the master. Also, kubernetes complained about the kernel of the VM. I bypassed those problems as follows:

walter@my-k8s-master-00:~$ sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU,SystemVerification

Set up local kubeconfig:

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

Apply Flannel CNI network overlay:

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

Join the worker(s) node(s):

sudo kubeadm join 10.0.0.4:6443 --token <string> --discovery-token-ca-cert-hash sha256:<sha256string> --ignore-preflight-errors=SystemVerification

For the other worker node:

sudo kubeadm join 10.0.0.4:6443 --token <string> --discovery-token-ca-cert-hash sha256:<sha256string> --ignore-preflight-errors=SystemVerification

Run on the master node:

walter@my-k8s-master-00:~$ kubectl get nodes
NAME               STATUS   ROLES    AGE     VERSION
my-k8s-master-00   Ready    master   13h     v1.13.5
my-k8s-worker-00   Ready    <none>   3m18s   v1.13.5
my-k8s-worker-01   Ready    <none>   106s    v1.13.5

Closing

For demonstration purposes it was created a kubernetes cluster that runs on a single main node and couple of worker nodes. These nodes are virtual machines (VMs) created on Microsoft Azure.

Note: The standard method to run kubernetes on Microsoft Azure is using Microsoft Azure AKS service.

Leave a Comment