Step-by-Step Guide to Deploy a Kubernetes Cluster on CentOS 7
This tutorial walks through preparing three CentOS 7 hosts, installing Docker, configuring kubeadm, initializing a Kubernetes master, troubleshooting common errors, adding worker nodes, installing a CNI plugin, testing the cluster with an Nginx deployment, and provides essential kubectl commands for ongoing management.
1. Preparation
1.1 Host Planning
master 192.168.100.101 CentOS7 2*2 50G
node1 192.168.100.102 CentOS7 2*2 50G
node2 192.168.100.103 CentOS7 2*2 50G1.2 Upgrade Kernel Version
Reference: https://www.cnblogs.com/wangyuanguang/p/180517981.3 Set Hostname and Hosts Mapping
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2
vim /etc/hosts
192.168.100.101 master
192.168.100.102 node1
192.168.100.103 node21.4 Disable SELinux and firewalld
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 01.5 Disable Swap
swapoff -a1.6 Enable Bridge Netfilter
cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward = 1
net.ipv4.tcp_tw_recycle = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system2. Deploy Docker
2.1 Docker Daemon Configuration (example)
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://hub-mirror.c.163.com","https://registry.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"],
"data-root": "/data/docker",
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": { "max-size": "300m","max-file": "3" },
"live-restore": true
}3. Deploy Kubernetes Base Commands
3.1 Add Alibaba Cloud yum repo for k8s
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF3.2 List Available Packages
yum --disablerepo="*" --enablerepo="kubernetes" list available3.3 Install kubeadm, kubectl, kubelet
yum install -y kubelet-1.28.2 kubeadm-1.28.2 kubectl-1.28.2
systemctl start kubelet
systemctl enable kubelet4. Deploy Cluster
4.1 Initialize Master
kubeadm init \
--apiserver-advertise-address=192.168.100.101 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.28.0 \
--service-cidr=10.140.0.0/16 \
--pod-network-cidr=10.240.0.0/16Parameter notes:
--apiserver-advertise-address Specify the master interface for cluster communication.
--pod-network-cidr CIDR for the pod network (Flannel example: 10.240.0.0/16).
--service-cidr CIDR for the service network.
--image-repository Use a domestic mirror for Kubernetes images.
--kubernetes-version Pin a specific version to avoid network lookups.4.2 Error Handling
Problem 1: container runtime not running (containerd CRI not implemented).
# Edit /etc/containerd/config.toml and comment out the line:
#disabled_plugins = ["cri"]
# Then restart containerd and re‑run kubeadm init.Problem 2: Re‑initializing after a failed init leaves manifest files. kubeadm reset Problem 3: Missing bridge netfilter sysctl. modprobe br_netfilter Problem 4: kubelet timeout waiting for control‑plane.
# Ensure the pause image is available, e.g.:
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.6"
systemctl daemon-reload
systemctl restart containerd.service4.3 Configure kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# For root users:
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" > /etc/profile.d/kubeconfig.sh
source /etc/profile.d/kubeconfig.sh4.4 Join Worker Nodes
kubeadm join 192.168.100.101:6443 \
--token jcdduw.3wfskykjk7gwdf1u \
--discovery-token-ca-cert-hash sha256:dd3cb5208a4ca032e85a5a30b9b02f963aff2fece13045cf8c74d7b9ed7f60984.5 Remove a Node
# Drain the node
kubectl drain node2 --delete-local-data --force --ignore-daemonsets
# Delete the node object
kubectl delete node node2
# On the removed node, clean up:
kubeadm reset -f5. Install CNI Network Plugin
Choose one of the following (Flannel is recommended on cloud VMs).
5.1 Install Flannel
mkdir /data/k8s && cd /data/k8s
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# Adjust the pod network CIDR if needed
sed -i 's/10.244.0.0/10.240.0.0/' kube-flannel.yml
kubectl apply -f kube-flannel.ymlVerify pods are running:
kubectl get pods -n kube-flannel6. Cluster Testing
# Create a test deployment
kubectl create deployment nginx --image=nginx
# Expose it as NodePort
kubectl expose deployment nginx --port=80 --type=NodePort
# Verify services on all nodes
kubectl get svcCheck the service from the other nodes as well.
7. Common kubectl Commands
# List resources
kubectl get <component> # e.g., kubectl get pod
# Apply a yaml file or directory
kubectl apply -f xxx.yaml
# Delete resources defined in a yaml file
kubectl delete -f xxx.yaml
# Describe a pod for detailed status
kubectl describe pod <pod-name>
# View pod logs
kubectl logs <pod-name>
# Show resource usage
kubectl top node # or kubectl top pod
# Exec into a pod
kubectl exec -ti <pod-name> -- /bin/bashSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
