How to install a Kubernetes Cluster on CentOS 7, Kubernetes (k8s) is an open-source, cloud-native, container orchestration and management platform. It’s the go-to way to automate the deployment, scaling, and maintenance of containerized applications across different nodes. From service discovery to auto-restarts, and from resource allocation tracking to compute utilization and scaling; a well-configured k8s cluster can manage a lot on its own.
How to install a Kubernetes Cluster on CentOS 7 What is a Kubernetes Cluster?
How to install a Kubernetes Cluster on CentOS 7, A Kubernetes cluster consists of a Master and at least one to several worker node(s). The Master is the virtual machine (VM) that administers all activities on your cluster. A node is a VM that serves as a worker machine in your k8s cluster to host running applications. We strongly recommend you only use VMs, also known as Cloud Servers, to run Kubernetes, not system containers, or VPS, as these can cause issues with k8s.
How to install a Kubernetes Cluster on CentOS 7 A node is comprised of the Kubelet, a container runtime, and the kube-proxy. The k8s installation’s three core modules: Kubelet, kubeadm, and kubectl are agents that control the node and communicate with the Kubernetes Master. Once they have been installed and other configurations done, you will be able to create your first k8s cluster. You can manage this cluster from the command line on your kubemaster node.
Every Kubernetes instance runs on top of a container runtime, which is software responsible for managing container operations. Containers in this case are not virtualized servers but rather a solution that packages code and dependencies to run a single application (service) in an isolated (containerized) environment, essentially disassociating applications from the host machine.
How to install a Kubernetes Cluster on CentOS 7 The most popular and recommended one is Docker, and it’s the one we will use for the purpose of this guide. However, if you want to install a different underlying container runtime, you can harness the power of the Container Runtime Interface and use basically any runtime you want.
Kubernetes groups containers into pods, its most basic operational unit, which are basically just groups of containers running on the same node. Pods are connected over a network and share storage resources.
How to install a Kubernetes Cluster on CentOS 7 In order to connect your nodes or VMs and make them private, make sure to choose a hosting company that provides a Virtual Local Area Network (VLAN) with their VMs. We offer a VLAN add-on to our Cloud Servers for R200 per month.
Prerequisites
- Multiple CentOS 7 VMs (Cloud Servers) to house the Master and worker nodes.
- Docker or any other container runtime.
- User with sudo or root privileges on every server.
How to Install Kubernetes on CentOS 7
How to install a Kubernetes Cluster on CentOS 7 Installing Kubernetes on CentOS 7 involves several steps, each crucial for a successful setup. Let’s dive into the detailed process, ensuring we cover each step with necessary commands and checks.
Step 1: Install Docker on All CentOS 7 VMs
- Update the Package Database Begin by updating your package database:
Code:-
sudo yum update -y
2. Install the Dependencies Ensure you have the necessary dependencies installed:
Code:-
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
3. Add and Enable the Official Docker Repository to CentOS 7 Add the Docker repository and enable it:
Code:-
sudo yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo
4. Install the Latest Docker Version on CentOS 7 Install Docker with the following command:
Code:-
sudo yum install -y docker-ce
5. Verify Installation A successful installation output will be concluded with “Complete!”.
6. Accept the GPG Key You may be prompted to accept the GPG key to verify the fingerprint matches. The prompt will look similar to this:
Code:-
Is this ok [y/N]: y
Step 4: Manage Docker Service
Now that Docker is installed, we need to start and enable the Docker service:
- Start and Enable DockerbashCopy code
sudo systemctl start docker sudo systemctl enable docker
- Confirm Docker is Active and RunningbashCopy code
sudo systemctl status docker
Step 2: Set up the Kubernetes Repository
Since Kubernetes packages aren’t available in the official CentOS 7 repositories, we’ll add a new repository file.
- Create and Edit the Repository FilebashCopy code
sudo vi /etc/yum.repos.d/kubernetes.repo
- Enter Insert Mode and Paste Contents Press
i
to enter insert mode, then paste the following:plaintextCopy code[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
- Save and Exit the File Press
Esc
, then type:x
and pressEnter
.
Step 3: Install Kubelet on CentOS 7
The first core module to install is Kubelet.
- Install KubeletbashCopy code
sudo yum install -y kubelet
- Verify Installation Successful installation will end with “Complete!”.
Step 4: Install kubeadm and kubectl on CentOS 7
Install kubeadm, which also installs kubectl as a dependency.
- Install kubeadm bash Copy code
sudo yum install -y kubeadm
- Verify Installation Successful installation should result in the following output:plaintextCopy code
Complete!
Step 5: Set Hostnames
Update hostnames on your master node.
- Update Hostname bash Copy code
sudo hostnamectl set-hostname master-node
- Edit Hostnames for Worker Nodes Open the
/etc/hosts
file and add/edit the hostnames for your worker nodes.
Step 6: Disable SELinux
Set SELinux to permissive mode.
- Disable SELinux bash Copy code
sudo setenforce 0 sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
- Reboot for Changes to Take Effect bash Copy code
sudo reboot
Step 7: Add Firewall Rules
Add rules to the firewall on the master node to allow communication.
- Add Rules bash Copy code
sudo firewall-cmd --permanent --add-port=6443/tcp sudo firewall-cmd --permanent --add-port=2379-2380/tcp sudo firewall-cmd --permanent --add-port=10250/tcp sudo firewall-cmd --permanent --add-port=10251/tcp sudo firewall-cmd --permanent --add-port=10252/tcp sudo firewall-cmd --permanent --add-port=10255/tcp sudo firewall-cmd --reload
- Verify Output Each command should output “success”.
Step 8: Update iptables Config
Update the sysctl file for proper packet processing.
- Update Config bash Copy code
sudo modprobe br_netfilter echo '1' | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
- Verify Output Ensure the output is: plain text Copy code
1
Step 9: Disable Swap
Disable swap on all VMs.
- Disable Swap bash Copy code
sudo swapoff -a sudo sed -i '/ swap / s/^/#/' /etc/fstab
This concludes the installation and configuration of Kubernetes on CentOS 7. You can now proceed with deploying your Kubernetes cluster.
Deploying a Kubernetes Cluster on CentOS 7
Step 1: kubeadm Initialization
To launch a new Kubernetes cluster instance, you need to initialize kubeadm
. Use the following command:
bash Copy code kubeadm init
How to install a Kubernetes Cluster on CentOS 7 This command may take several minutes to execute. Upon success, you should get logs similar to those in this screenshot:
You will also get an auto-generated command at the end of the output. Copy the text following the line “Then you can join any number of worker nodes by running the following on each as root:” as highlighted in the above screenshot and save it somewhere safe. We will use this to add worker nodes to our cluster.
Note: If you forgot to copy the command, or have misplaced it, don’t worry. You can retrieve it again by entering the following command:
bash Copy code kubeadm token create --print-join-command
Step 2: Create Required Directories and Start Managing Kubernetes Cluster
In order to start managing your cluster, you need to create a directory and assume ownership. Run the following commands as a regular user:
bash Copy code mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 3: Set Up Pod Network for the Cluster
Pods within a cluster are connected via the pod network. At this point, it’s not working. This can be verified by entering the following two commands:
bash Copy code kubectl get nodes
kubectl get pods --all-namespaces
How to install a Kubernetes Cluster on CentOS 7 As you can see, the status of the master node is NotReady
. The CoreDNS service is also not running. To fix this, run the following commands:
bash Copy code kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
How to install a Kubernetes Cluster on CentOS 7 You should get the following output:
plaintext Copy code configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
...
And now if you verify the statuses of your node and CoreDNS service, you should get Ready
and Running
like seen below:
bash Copy code kubectl get nodes
kubectl get pods --all-namespaces
Step 4: Add Nodes to Your Cluster
How to install a Kubernetes Cluster on CentOS 7 As a final step, you need to add worker nodes to your cluster. We will use the kubeadm join
auto-generated token from Step 1 here. Run your own version of the following command on all of the worker node VMs:
bash Copy code kubeadm join <master-node-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
On successful addition, you should get the following output:
plaintext Copy code This node has joined the cluster:
...
Running the following command on the master node should show your newly added node:
bash Copy code kubectl get nodes
How to install a Kubernetes Cluster on CentOS 7 To set the role for your worker node, use the following command:
bash Copy code kubectl label node <node-name> node-role.kubernetes.io/worker=worker
Read More