Installing Kubernetes on CentOS: A Step-by-Step Guide
Kubernetes is a powerful container orchestration platform, and CentOS is a popular Linux distribution for hosting it. Below is a structured guide to installing Kubernetes on CentOS, covering environment preparation, component installation, cluster initialization, and verification.
Before starting, ensure your CentOS system meets the following requirements:
podman).Key Preparation Steps:
sudo setenforce 0 # Temporary disable
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config # Permanent disable
sudo swapoff -a # Disable immediately
sudo sed -i '/swap/s/^/#/' /etc/fstab # Remove swap entry from fstab (permanent)
/etc/hosts):192.168.1.100 k8s-master
192.168.1.101 k8s-worker1
192.168.1.102 k8s-worker2
ntpdate to avoid certificate validation errors:sudo yum install -y ntpdate
sudo ntpdate ntp.aliyun.com
These steps ensure compatibility with Kubernetes and prevent common issues during installation.
Kubernetes requires a container runtime to manage containers. Docker is the most widely used option.
Installation Steps:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
/etc/docker/daemon.json to avoid conflicts with Kubernetes:{
"exec-opts": ["native.cgroupdriver=systemd"]
}
Restart Docker to apply changes:sudo systemctl restart docker
Docker ensures that Kubernetes can run and manage containers efficiently.
Add the official Kubernetes YUM repository to install kubelet, kubeadm, and kubectl.
Repository Configuration:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
This repository provides stable Kubernetes packages optimized for CentOS.
Install the core components required to manage the Kubernetes cluster:
Install Commands:
sudo yum install -y kubelet kubeadm kubectl
Enable and Start kubelet:
sudo systemctl enable kubelet
sudo systemctl start kubelet
kubelet runs on every node and communicates with the control plane. kubeadm initializes the cluster, and kubectl is the command-line tool for cluster management.
The Master node manages the cluster’s control plane (API server, scheduler, controller manager).
Initialization Command:
sudo kubeadm init \
--apiserver-advertise-address=<MASTER_IP> \ # Replace with Master's IP
--pod-network-cidr=10.244.0.0/16 \ # CIDR for Pod network (matches network plugin)
--image-repository registry.aliyuncs.com/google_containers # Use Alibaba Cloud mirror for faster downloads
Output:
After successful initialization, kubeadm will output a kubeadm join command (save this—it’s required to add Worker nodes).
kubectl requires a configuration file to interact with the cluster.
Configuration Steps:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Verify the configuration:
kubectl version --client
This ensures kubectl can communicate with the Master node.
Kubernetes requires a CNI (Container Network Interface) plugin for Pod-to-Pod communication. Popular options include Flannel and Calico.
Install Flannel:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Install Calico:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Verify the network plugin is running:
kubectl get pods -n kube-system
All pods should be in the Running state before proceeding.
Worker nodes run your application containers. Use the kubeadm join command from the Master node’s initialization output to add Workers.
Example Join Command:
sudo kubeadm join 192.168.1.100:6443 \
--token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
Replace placeholders (<MASTER_IP>, token, hash) with values from the Master’s kubeadm init output.
After adding all nodes, verify the cluster is healthy:
Check Node Status:
kubectl get nodes
Expected output:
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane 10m v1.28.2
k8s-worker1 Ready <none> 5m v1.28.2
k8s-worker2 Ready <none> 3m v1.28.2
Check Pod Status:
kubectl get pods -A
All pods should be Running or Completed.
By following these steps, you’ll have a fully functional Kubernetes cluster on CentOS. Adjust parameters (e.g., --pod-network-cidr) based on your network plugin and environment requirements.