Kubernetes is a software system that allows you to deploy and manage containerized apps. It abstracts away the underlying infrastructure to simplify development, deployment and management for both dev and ops teams. There are many benefits and components provided by the Kubernetes platform with many options about installing and setting up the Kubernetes cluster such as single-node, multiple-node and cloud-based deployments. In this article, we will focus on setting up multiple-node cluster on a laptop (such as 2.7 GHz Intel Core i5, 8G RAM should be sufficient for demonstration purpose).
In the process of setting up the Kubernetes cluster, you will create one VM for the Kubernetes master and another VM for the Kubernetes worker node. Setting up more than one worker node will be similar.
Prerequisites
Please refers to the links below to download and setup the following prerequisites:
Installing the OS and required packages
You will create a VM in VirtualBox for the master node by specifying the following information during the installation process:
- Name: k8s-master
- Type: Linux
- Version: RedHat (64 bit)
- Hard disk file type: VDI
- Storage on physical hard disk: Dynamically allocated
- File location and size: k8s-master, size 8 GB
Configuring the network adapter
Now you will configure the network adapter as the Bridged Adapter mode to connect your VM to the same network as your host computer is in. You can do that by selecting the VM just created and clicking the Setting icon. You can then select Network and select Attach To: Bridged Adapter.
Installing the OS
You can select the VM and Start it. You can then select the downloaded CentOS ISO image as the start-up disk and click Start.
When the “Welcome the CentOS Linux 7” appears, you can click the Continue button to go to the main setup screen as shown here:
You can click Installation Destination and then Done button on the screen.
To enable the network adapter, you will click on Network & Host Name and click ON/OFF switch. After that, you can specify the host name as master.k8s and click Apply button.
You also have to set the correct time zone by clicking Date & Time where you can select the Region and City you are in.
Now you can start the installation by clicking Begin Installation button. During the process, you can set up your root password and create a user account. After the installation completes, Reboot the CentOS Linux 7.
Installing Docker and Kubernetes
Before installing Docker and Kubernetes, you have to disable two security features: SELinux and firewall.
You need to log into Linux as root, and disable SELinux security feature by editing /etc/selinux/config file:
SELINUX=permissive
To disable the firewall, run the following command:
systemctl disable firewalld && systemctl stop firewalld
Before installing Docker and Kubernetes components, you will make the Kubernetes RPM packages available to the yum package manager by creating a kubernetes.repo file to the /etc/yum.repos.d/ directory, shown in the following.
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://yum.kubernetes.io/repos/kubernetes-el17-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
You can now install Docker, Kubelet, kubeadm, kubectl, and Kubernetes CNI:
yum install -y docker kubelet kubeadm kubectl kubenetes-cni
After the installation, you need to enable the docker and kubelet services:
systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet
You also need to enable the net.bridge.bridge-nf-call-iptables kernel option by running the following commands:
sysctl -w net.bridge.bridge-nf-call-iptables=1
echo "net.bridge.bridge-nf-call-iptables=1" > /etc/sysctl.d/k8s.conf
And, disable swap with the following command:
swapoff -a && sed -i '/ swap / s/^/#/' /etc/fstab
Setting up the worker node
You will be setup the worker node by cloning the master VM just prepared to avoid the repetitive process. First, you need to shutdown the VM:
shutdown now
From the VirtualBox UI, select Clone and enter the name of the new machine (for example, k8s-node1). Make sure to check the Reinitialize the MAC address of all network card, Full clone, and Current machine state options during the cloning process. After cloning VM completes, you can start all VMs.
Since the cloned worker node have the same hostname as the master node, you will log into the worker node and run the following commands to change the hostname:
hostnamectl --static set-hostname node1.k8s
Next, you will edit the entries in /etc/hosts on all of master and worker nodes. You can get each node’s IP by logging into the node as root and running ip addr
The IP address in the enp0s3 network adapter will be the entries used in the /etc/hosts, such as the following:
172.30.10.6 master.k8s
172.30.10.7 node1.k8s
Setting up Kubernetes Control Plane on the master node
You can use the kubeadm tool to initialize the Kubernetes master and deploy all the Control Plane components, including etcd, the API server, Scheduler, kube-proxy and Control Manager. You will need to take a note of the last line of the output from running the following command.
kubeadm init
You can now configure kubectl and test it by listing pods in the kube-system namespace on the master node, shown in the following:
export KUBECONFIG=/etc/kubernetes/admin.confkubectl get po -n kube-system
Setting up the worker node with kubeadm
You will log into the worker node as root, and run the last part that you took note of, when you ran the kubeadm init
on the master node.
kubeadm join --token ... --discovery-token-ca-cert-hash ...
You can now confirm that the worker node is registered by running the command. The result will indicate STATUS: NotReady
since the container network plugin hasn’t been deployed in the cluster yet.
kubectl get nodes
Deploying the Weave Net container network plugin
There are several CNI addons available listed at https://kubernetes.io/docs/concepts/cluster-administration/addons/. You can deploy the Weave Net CNI plugin by running the following command.
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')
You can check again if the worker node indicates STATUS: Ready
. Also, you can check all system pods running in the cluster.
kubectl get po --all-namespaces
Configuring kubectl on your local machine
Instead of logging into the master node and using kubectl there, you will want to configure the kubectl on your local machine to talk to the master node directly, with the following command on your local machine. You should replace 172.30.10.6 with the IP of your master node.
scp root@172.30.10.6:/etc/kubernetes/admin.conf ~/.kube/config2export KUBECONFIG=~/.kube/config2
Conclusions
Even though Kubernetes provides a single-node cluster called minikube for you to experience this platform. In most testing and production environment setup will be multi-node clusters and this article shows you how to do that.
Additionally, you can also deploy the Kubernetes dashboard by referring to the links below: https://github.com/kubernetes/dashboard. You can visualize the metric and graphs about the cluster and deployed Kubernetes components.
Thanks for reading.