How to Install a Kubernetes Cluster on CentOS 7

Donated by Google to the Opensource community, Kubernetes has now become the container management tool of choice. It can manage and orchestrate not just docker runtimes but also Containers and Rkt runtimes.

A typical Kubernetes cluster would generally have a master node and several worker-nodes or Minions. The worker-nodes are then managed from the master node, thus ensuring that the cluster is managed from a central point.

It’s also important to mention that you can also deploy a single-node Kubernetes cluster which is generally recommended for very light, non-production workloads. For this, you can use Minikube, which is a tool that runs a single-node Kubernetes cluster in a virtual machine on your node.

Recommended Read: How to Install a Kubernetes Cluster on CentOS 8

For this tutorial, we will walk-through a multi-node Kubernetes cluster installation on CentOS 7 Linux. This tutorial is command-line based so you will need access to your terminal window.

Prerequisites

  1. Multiple servers running Centos 7 (1 Master Node, 2 Worker Nodes). It is recommended that your Master Node have at least 2 CPUs, though this is not a strict requirement.
  2. Internet connectivity on all your nodes. We will be fetching Kubernetes and docker packages from the repository. Equally, you will need to make sure that the yum package manager is installed by default and can fetch packages remotely.
  3. You will also need access to an account with sudo or root privileges. In this tutorial, I will be using my root account.

Our 3-node cluster will look something like this:

Kubernetes Cluster Diagram

Kubernetes Cluster Diagram

Installation of Kubernetes Cluster on Master-Node

For Kubernetes to work, you will need a containerization engine. For this installation, we will use docker as it is the most popular.

The following steps will run on the Master-Node.

Step 1: Prepare Hostname, Firewall and SELinux

On your master node, set the hostname and if you don’t have a DNS server, then also update your /etc/hosts file.

# hostnamectl set-hostname master-node
# cat <<EOF>> /etc/hosts
10.128.0.27 master-node
10.128.0.29 node-1 worker-node-1
10.128.0.30 node-2 worker-node-2
EOF

You can ping worker-node-1 and worker-node-2 to test if your updated hostfile is fine using ping command.

# ping 10.128.0.29
# ping 10.128.0.30

Next, disable SElinux and update your firewall rules.

# setenforce 0
# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
# reboot

Set the following firewall rules on ports. Make sure that each firewall-cmd command, returns a success.

# firewall-cmd --permanent --add-port=6443/tcp
# firewall-cmd --permanent --add-port=2379-2380/tcp
# firewall-cmd --permanent --add-port=10250/tcp
# firewall-cmd --permanent --add-port=10251/tcp
# firewall-cmd --permanent --add-port=10252/tcp
# firewall-cmd --permanent --add-port=10255/tcp
# firewall-cmd –reload
# modprobe br_netfilter
# echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

Step 2: Setup the Kubernetes Repo

You will need to add Kubernetes repositories manually as they do not come installed by default on CentOS 7.

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

Step 3: Install Kubeadm and Docker

With the package repo now ready, you can go ahead and install kubeadm and docker packages.

# yum install kubeadm docker -y 

When the installation completes successfully, enable and start both services.

# systemctl enable kubelet
# systemctl start kubelet
# systemctl enable docker
# systemctl start docker

Step 4: Initialize Kubernetes Master and Setup Default User

Now we are ready to initialize kubernetes master, but before that you need to disable swap in order to run “kubeadm init“ command.

# swapoff -a

Initializing Kubernetes master is a fully automated process that is managed by the “kubeadm init“ command which you will run.

# kubeadm init
Initialize Kubernetes Master

Initialize Kubernetes Master

You may want to copy the last line and save it somewhere because you will need to run it on the worker nodes.

kubeadm join 10.128.0.27:6443 --token nu06lu.xrsux0ss0ixtnms5  \ --discovery-token-ca-cert-hash sha256:f996ea3564e6a07fdea2997a1cf8caeddafd6d4360d606dbc82314688425cd41 

Tip: Sometimes this command might complain about the arguments (args) passed, so edit it to avoid any errors. So, you will delete the ‘\’ character accompanying the --token and your final command will look like this.

kubeadm join 10.128.0.27:6443 --token nu06lu.xrsux0ss0ixtnms5  --discovery-token-ca-cert-hash sha256:f996ea3564e6a07fdea2997a1cf8caeddafd6d4360d606dbc82314688425cd41

Having initialized Kubernetes successfully, you will need to allow your user to start using the cluster. In our case, we want to run this installation as root user, therefore we will go ahead and run these commands as root. You can change to a sudo enabled user you prefer and run the below using sudo.

To use root, run:

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

To use a sudo enabled user, run:

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

Now check to see if the kubectl command is activated.

# kubectl get nodes
Check Status of Nodes

Check Status of Nodes

At this point, you will also notice that the status of the master-node is ‘NotReady’. This is because we are yet to deploy the pod network to the cluster.

The pod Network is the overlay network for the cluster, that is deployed on top of the present node network. It is designed to allow connectivity across the pod.

Step 5: Setup Your Pod Network

Deploying the network cluster is a highly flexible process depending on your needs and there are many options available. Since we want to keep our installation as simple as possible, we will use Weavenet plugin which does not require any configuration or extra code and it provides one IP address per pod which is great for us. If you want to see more options, please check here.

These commands will be important to get the pod network setup.

# export kubever=$(kubectl version | base64 | tr -d '\n')
# kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$kubever"
Setup Pod Network

Setup Pod Network

Now if you check the status of your master-node, it should be ‘Ready’.

# kubectl get nodes
Check Status of Master Nodes

Check Status of Master Nodes

Next, we add the worker nodes to the cluster.

Setting Up Worker Nodes to Join Kubernetes Cluster

The following steps will run on the worker nodes. These steps should be run on every worker node when joining the Kubernetes cluster.

Step 1: Prepare Hostname, Firewall and SELinux

On your worker-node-1 and worker-node-2, set the hostname and in case you don’t have a DNS server, then also update your master and worker nodes on /etc/hosts file.

# hostnamectl set-hostname 'node-1'
# cat <<EOF>> /etc/hosts
10.128.0.27 master-node
10.128.0.29 node-1 worker-node-1
10.128.0.30 node-2 worker-node-2
EOF

You can ping master-node to test if your updated hostfile is fine.

Next, disable SElinux and update your firewall rules.

# setenforce 0
# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

Set the following firewall rules on ports. Make sure that all firewall-cmd commands, return success.

# firewall-cmd --permanent --add-port=6783/tcp
# firewall-cmd --permanent --add-port=10250/tcp
# firewall-cmd --permanent --add-port=10255/tcp
# firewall-cmd --permanent --add-port=30000-32767/tcp
# firewall-cmd  --reload
# echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

Step 2: Setup the Kubernetes Repo

You will need to add Kubernetes repositories manually as they do not come pre-installed on CentOS 7.

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

Step 3: Install Kubeadm and Docker

With the package repo now ready, you can go ahead and install kubeadm and docker packages.

# yum install kubeadm docker -y 

Start and enable both the services.

# systemctl enable docker
# systemctl start docker
# systemctl enable kubelet
# systemctl start kubelet

Step 4: Join the Worker Node to the Kubernetes Cluster

We now require the token that kubeadm init generated, to join the cluster. You can copy and paste it to your node-1 and node-2 if you had copied it somewhere.

# kubeadm join 10.128.0.27:6443 --token nu06lu.xrsux0ss0ixtnms5  --discovery-token-ca-cert-hash sha256:f996ea3564e6a07fdea2997a1cf8caeddafd6d4360d606dbc82314688425cd41 
Join Nodes to Kubernets Cluster

Join Nodes to Kubernets Cluster

As suggested on the last line, go back to your master-node and check if worker node-1 and worker node-2 have joined the cluster using the following command.

# kubectl get nodes
Check All Nodes Status in Kubernetes Cluster

Check All Nodes Status in Kubernetes Cluster

If all the steps run successfully, then, you should see node-1 and node-2 in ready status on the master-node.

Recommended Read: How to Deploy Nginx on a Kubernetes Cluster

At this point, we have successfully completed an installation of a Kubernetes cluster on Centos 7 and we have successfully on-boarded two worker-nodes. You can now begin to create your pods and deploy your services.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Kioie Eddy

Work as a Cloud Architect in Nairobi, Kenya. Spend my time designing Cloud and DevOps architectures with a focus on Opensource. Also a contributor to several Opensource projects.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

22 Responses

  1. Harper says:

    Hi,

    not sure, why I am getting the below error on node1. though follow the exact steps.

    [[email protected] ~]# kubeadm join 192.168.0.142:6443 --token nkzqbg.fkm4ulii3ub2irsi --discovery-token-ca-cert-hash sha256:795af5d43eb5f6b47df9fd39c3462f0a42ab242aa857019fd6147f0058f80b65
    W0213 20:31:56.839498 4860 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
    [preflight] Running pre-flight checks

    • Kioie Eddy says:

      Hi Harper,

      This doesn’t look much like an error. I think you are still on the right path, despite the pre-flight warnings.

  2. Eduardo Cavalcante says:

    Please add a comment on your post so that the users also need to add a flag called “fail-to-swap” to FALSE on the /etc/sysconfig/kubelet config file so that the kubelet service may start without any issues on CentOS 7 systemd.

    From:

    KUBELET_EXTRA_ARGS=
    

    TO BE:

    KUBELET_EXTRA_ARGS=--fail-swap-on=false
    

    P.S: This should be done on all servers (Master and Worker Nodes).

    • Kioie Eddy says:

      Hi Eduardo, thanks for noticing this. “Fail-swap-on=false” is a temporary measure to allow kubelet to run with swap on, bypassing swap checks. This is not an ideal option to run on, and so it is best not to go that direction.

      I looked at the documentation and tried it on one of the machines, and basically the recommendation is to simply permanently switch off swap and it should take care of all this, including during reboots.

      However, there are other situations where you would need swap on, at least temporarily. I would, therefore, give caution if you do choose to enable this option unless you are sure that is what you want.

  3. sandip says:

    I am getting below error while joining the worker node to master.

    [[email protected] yum.repos.d]# kubeadm join 10.0.3.15:6443 --token jzwvg4.bzv2b5omdcl3kosl --discovery-token-ca-cert-hash sha256:ada901bfbb4ae0e9d26aaeb54f3794cbb7bfe60f861f90efed1416a490ce041d
    W0130 06:26:26.065050 10134 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
    [preflight] Running pre-flight checks
    error execution phase preflight: couldn’t validate the identity of the API Server: abort connecting to API servers after timeout of 5m0s
    To see the stack trace of this error execute with –v=5 or higher

    • Kioie Eddy says:

      Hi Sandip, hope you managed to sort this out, if not, let me know and we can run through the installation and troubleshoot

      • Adarsh says:

        Hi Kioie, I am also facing same issue. Please let me know how to resolve this.
        Thanks

        • Kioie Eddy says:

          Hi Adarsh, DM me on @kioi_e on twitter and I can help troubleshoot.

          This error could be arising from two things:

          1. The discovery token
          2. Connectivity to the master node

          To troubleshoot,

          1. Verify that the discovery token “kubeadm join 10.0.3.15:6443 –token jzwvg4.bzv2b5omdcl3kosl –discovery-token-ca-cert-hash sha256:ada901bfbb4ae0e9d26aaeb54f3794cbb7bfe60f861f90efed1416a490ce041d” is valid. Make sure that you have the full token.
          2. Also make sure that port “10.0.3.15:6443” is accessible and is not blocked by your firewall.
          3. Also, check that your kubectl service is running and your master node is also up and running. You can use the command “kubectl get nodes” to verify.
  4. yash says:

    Wonderful Article. Saved a heck of a time figuring out the install issues.

  5. David Alfred says:

    Thank you for the article it is just about detailed enough for newbies to keep up the good work.

    I am looking forward to more articles.

  6. simon yang says:

    I am following your tutorial to install the Kubernetes cluster.

    I’ve got the error when trying to run “kubeadm init” command.

    From Kubernetes official documentation, I found out that swap must be disabled in order to run “kubeadm init“.

    Please add it to your contents.

    Thank you for sharing your good knowledge with unknowns. :)

    • Kioie Eddy says:

      Hi @Simon

      You are right, sometimes swap tends to affect the “kubeadm init“, and it is best to disable it. I will update the article to reflect this. Thanks for your feedback and thanks for reading.

  7. Pankaj kumar says:

    I have already configured this cluster but I want to configure master and slave Kubernetes servers with 2 workers node. I also want to access the graphical mode of Kubernetes. Could you please write an article on that.

    • 2 Kubernetes with 2 worker nodes.
    • Graphical GUI of Kubernetes based on IP address, not localhost IP.
    • Create Apache cluster service and how to access them.
    • Kioie Eddy says:

      Hello Pankaj, thanks for the question. I can definitely do a follow-up article.

      Just some clarification:

      • When you say 2 Kubernetes with 2 Worker nodes, do you mean 2 Master Nodes and 2 worker nodes? If so, are you talking about a high availability setup?
      • A GUI of Kubernetes based on IP address, do you mean a Kubernetes GUI running on a public IP?
      • An Apache Cluster Service, do you mean An Apache Webserver on a clustered setup?
  8. cryptoparty says:

    Why you do that if u got a good manual about kubespray?

    • Kioie Eddy says:

      @cryptoparty Kubespray is a great tool that can be used to automate the process, but there are many use-cases where a manual install would be necessary, and this tutorial is great as an option.

  9. Anabangi says:

    This is great documentation, looking forward to more articles on Kubernetes!

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.