Create a Kubernetes Cluster With Centos
Let's stand up some clusters.
Join the DZone community and get the full member experience.Join For Free
It's yet another blog regarding the installation of a Kubernetes cluster. However, in this article, I want to start everything from scratch to explain the overall process to create a workable Kubernetes cluster using kubeadm tool.
1. Virtual Box
2. CentOS - 7 (https://wiki.centos.org/Download)
We will have a Master node and 2 worker nodes as shown in the diagram below. All the machines are CentOS-7 Virtual Machines.
In this part, I will try to explain the step-by-step process to create VMs for the K8s cluster.
While defining the VM, make sure to allocate a minimum of 2CPUs to the Master VM. Otherwise, you won't be able to run kubeadm tool.
Define the following Network Adapter for the VM.
During the installation process make sure to enable the Network Interface and also put the hostname e.g master.
Note: You can change the hostname later using hostnamectl command later too.
Continue the installation process.
At this moment we have our Master VM ready. Now, let's assign a Static IP address to the Master VM. It's very simple. By default in CentOS, the nmcli is automatically installed.
Execute the following command to check the Network interfaces installed in the VM.
We have configured only one Network Adapter, so we will have only one network interface installed in our VM. Below will be the output.
Now execute the below commands.
Now do a ping inside the machine if internet access is available.
Done! We have our Master node ready with a static IP of 192.168.15.34.
The same process can be repeated for creating the Worker nodes too. But for simplicity, we will clone this Master VM to create the worker nodes.
Install Kubeadm in the Master
At this moment, we have our brand new Master VM ready. Let's do SSH to it. Below are the steps to install Kubeadm.
setenforce 0 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
Disable firewall is not recommended. However, this set-up is only for demo purposes and we can simply disable the firewall.
systemctl disable firewalld
Also, do the below settings.
modprobe br_netfilter echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
Set Kubernetes Repo
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-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
Install Kubeadm and Docker
yum install kubeadm docker -y
Enable and Start docker and kubelet
systemctl enable kubelet systemctl start kubelet systemctl enable docker systemctl start docker
Open this file:
Comment the line that has swap keyword.
At this moment, our Master VM has got everything to run. Just before proceeding, let's clone this VM to create the Worker nodes.
Create Worker Nodes
Stop the Master VM and create 2 clones of it.
Now start each of the Worker nodes and set hostnames as worker-1 and worker-2 using the hostnamectl command
Also, set static IP addresses to the Worker nodes as described in the previous section when we configured the Master node.
At this moment we have 3 VMs ready
Update /etc/hosts of all the 3 nodes (Master, Worker-1 and Worker-2) with the below content.
Initialize the Kubernetes Cluster
Enter in the Master node and execute the following command.
After Kubeadm is initialized you should see this information.
Keep aside the generated token. This command should be executed on each Worker node to join the cluster.
Running the cluster
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
Now check the cluster with the below command in the Master node.
You should see the below output.
The nodes are in NotReady state because we are yet to install a Pod Network. We will do it in the next section.
Installing a Pod Network
We are going to use Calico as the Pod Network. It's straightforward. Check this documentation for the installation process. Install the Pod network in the Master node.
Download the Calico networking manifest for the Kubernetes API datastore.
Apply the manifest using the following command.
Wait for some moments until all pods are up in the kube-system namespace.
Now execute the below command in the Master node.
You should get the below output.
At this moment we have a fully functional Kubernetes cluster running.
Try executing kubectl get nodes in either of the Worker nodes. You should see the below output.
To be able to execute the kubectl commands across all Worker nodes, follow the below steps.
1. Create the following directory in Worker nodes.
mkdir -p $HOME/.kube
2. Copy the config file from the Master node from the below location to the target location created in step 1.
mkdir -p $HOME/.kube/config
3. Execute the below command in Worker nodes
chown $(id -u):$(id -g) $HOME/.kube/config
Now you can use kubectl commands in worker nodes too.
In this article, I have explained how to create a Kubernetes cluster using Kubeadm. I have used the minimal configuration to set up the cluster. Please feel free to let me know if you could set up this minimal cluster.
Opinions expressed by DZone contributors are their own.