To Setup Kubernetes cluster, I will be setting the cluster in my local environment using Hyper-V. You can use any Hypervisor you like. I recommend VirtualBox if you want to Setup a local environment. Otherwise, the below tutorial can be followed on any Virtual Machine, Physical machine, or any cloud Computing IAAS instance.
I created a virtual machine called
k8s-master-node. You can call anything you like. For the purpose of this tutorial I will stick with this name.
Before you proceed forward, ensure that swap is disabled on the system. Otherwise, Kubernetes setup will fail.
System updates and Docker installation
Let’s go ahead and first perform updates before setting up Kubernetes environment. If you have installed a fresh OS and haven’t performed below execution of the command, I highly recommend it. It will get all the security patches and updates to system tools.
sudo apt-get update
sudo apt-get upgrade -y
Once you have performed the above updates, let’s go ahead and install dependencies for Kubernetes.
apt-transport-https package will allow http & https access in apt-get repositories and sources.
sudo apt-get install apt-transport-https -y
Once the above package is installed, now we are going to install Docker which is the core engine that will run our container.
sudo apt install docker.io -y
Now we will start Docker service and enable Docker to start on system reboot. So Docker can start on reboot if reboot is performed.
sudo systemctl start docker
sudo systemctl enable docker
If you haven’t encountered any errors, that means system updates and Docker installation was successful, and we are ready to Setup Kubernetes.
Before we start Kubernetes setup, we need to download and add the key to allow Kubernetes installation.
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
If you receive
OK in return, then the key was added successfully.
Now we need to add an apt repository to install Kubernetes.
We will first create a file called
sudo vi /etc/apt/sources.list.d/kubernetes.list
Once the file is in edit mode. Go ahead and copy and paste the line below in to the file and save it.
deb http://apt.kubernetes.io/ kubernetes-xenial main
Execute the below commands to install Kubernetes and the tools required to manage Kubernetes.
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni
If no error occurred, that means all Kubernetes components are successfully installed!
Now we need to initialize a Kubernetes cluster to run as Master. Execute the following commands:
sudo kubeadm init --pod-network-cidr 10.244.0.0/16
While initializing, if you received swap-related errors as shown below. Please make sure swap is disabled in fstab file and reboot the server.
[ERROR Swap]: running with swap on is not supported. Please disable swap
[preflight] If you know what you are doing, you can make a check non-fatal with `–ignore-preflight-errors=…`
If you didn’t receive the above error, then proceed forward with additional configuration. Once the initialization is completed, you will receive a message with additional configuration to complete master node setup.
Note: Token that is shown in above end selected highlight might vary and will be different system to system as it is auto generated. Please make sure you make a note of your token to add additional nodes to join the cluster.
To complete setup. I will go ahead and execute below commands.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Pod Network Configuration
Before we start deploying pods in Kubernetes cluster. We need to ensure network configurations are applied to Kubernetes Cluster. It allows pod to pod communication, and it’s a dependency for kube-dns as well. In this case, it is the master node where you will execute the below commands. Go ahead and execute the below commands on the Master node to apply network settings.
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml
Once you have applied the above commands to setup networking. You will see that dns and flannel pods will be created. Without this, you will not be able to run dns and communication between pods in Kubernetes cluster.
Go ahead and execute the below command to see the results of all pods.
sudo kubectl get pods --all-namespaces
Once all the pods are created and up and running. You should see something like below.
Joining other Linux node to Cluster
If you would like to add another node to Kubernetes cluster, follow all the steps above provided in this tutorial under
System updates and Docker installation &
Kubernetes Installation section.
Once I have completed Kubernetes setup on other nodes. I will execute the below command to join the node to the cluster.
sudo kubeadm join --token 59f9e5.ca7acb3f48d73813 192.168.0.150:6443 --discovery-token-ca-cert-hash sha256:397f05b795e279a189e18dd9b17381c540a8d1513ca31cd40c8e3d91325a628a
Optional add-on for Kubernetes Cluster
If you like to add Kubernetes Dashboard to Setup and configure pods via UI. You can install Kubernetes Dashboard, which is free to install. Go ahead and execute the below command.
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
Once you have executed the above command. The pod will go into
ContainerCreating status. You can check the status by executing the below command.
sudo kubectl get pods --all-namespaces
Try executing the above command every 30 secs until the pod status becomes
running. Once it is in running status, you can create a proxy URL to access the dashboard from your local system where Kubernetes components are installed. I will create another tutorial soon that will show you how to Setup Kubernetes client components to manage Kubernetes cluster remotely. But if you want to know how to access the dashboard, go ahead and execute the below command.
sudo kubectl proxy
Let us know if you have any questions by dropping us a comment below.