Step-by-Step Guide to Building a Kubernetes Cluster on CentOS with Docker and Flannel
This comprehensive tutorial walks you through preparing three CentOS 7.5 machines, disabling firewalls and swap, installing Docker and Kubernetes components with yum repositories, configuring the master node, joining worker nodes, deploying Flannel networking, setting up the Kubernetes dashboard, and troubleshooting common issues.
Installation Requirements (Pre‑check)
Before starting, the machines for the Kubernetes cluster must meet the following conditions:
Three machines, OS CentOS 7.5+ (minimal)
Hardware: 2 GB RAM, 2 CPUs, 30 GB disk
Installation Steps
Roles and IPs:
master – 192.168.50.128 node1 – 192.168.50.131 node2 – 192.168.50.132
2.1 Pre‑processing on All Nodes
These steps must be performed on every node (master and workers).
(1) Disable firewall and SELinux
# systemctl disable --now firewalld
# setenforce 0
# sed -i 's/enforcing/disabled/' /etc/selinux/config(3) Disable swap
# swapoff -a
# sed -i.bak 's/^.*centos-swap/#&/g' /etc/fstabTo permanently disable swap, comment out the swap line in /etc/fstab.
(4) Set hostnames
# hostnamectl set-hostname master # hostnamectl set-hostname node1 # hostnamectl set-hostname node2Run bash to load the new hostnames.
(5) Add hosts entries
# cat >>/etc/hosts <<EOF
192.168.50.128 master
192.168.50.131 node1
192.168.50.132 node2
EOF(6) Enable IPv6 forwarding
# cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# sysctl --system(7) Configure yum repositories
Use Alibaba Cloud mirrors for base and epel repositories:
# mv /etc/yum.repos.d/* /tmp
# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo(8) Set timezone and sync time
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# yum install dnf ntpdate -y
# dnf makecache
# ntpdate ntp.aliyun.com2.2 Install Docker
(1) Add Docker yum repo
# curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# cat /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
...(2) Install Docker CE
# dnf list docker-ce --showduplicatesInstall the latest version:
# dnf install -y docker-ce docker-ce-cli(3) Start Docker and enable on boot
# systemctl enable --now dockerVerify version:
# docker --version
Docker version 19.03.12, build 48a66213fea(4) Configure Docker registry mirrors
# cat > /etc/docker/daemon.json <<EOF
{"registry-mirrors": ["https://f1bhsuge.mirror.aliyuncs.com"]}
EOF
# systemctl restart docker2.3 Install Kubernetes Components
(1) Add Kubernetes yum repo
# 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
EOF(2) Install kubeadm, kubelet, kubectl (version 1.18.6)
# dnf install -y kubelet-1.18.6 kubeadm-1.18.6 kubectl-1.18.6(3) Enable kubelet at boot
# systemctl enable kubelet2.4 Deploy Kubeadm Master
(1) Generate init configuration
# kubeadm config print init-defaults > kubeadm-init.yamlTypical kubeadm-init.yaml includes API server address, pod subnet, image repository (Alibaba Cloud), and Kubernetes version.
(2) Pre‑pull images
# kubeadm config images pull --config kubeadm-init.yaml
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.18.0
... (other core images)(3) Initialize the master
# kubeadm init --config kubeadm-init.yamlAfter successful init, run as a regular user:
# mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# sudo chown $(id -u):$(id -g) $HOME/.kube/config2.5 Join Worker Nodes
Execute the kubeadm join command printed at the end of the master init on each worker:
# kubeadm join 192.168.50.128:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:05b84c41152f72ca33afe39a7ef7fa359eec3d3ed654c2692b665e2c4810af3eVerify nodes:
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 82m v1.18.6
node1 Ready <none> 60m v1.18.6
node2 Ready <none> 55m v1.18.62.6 Install Flannel Network Plugin
Flannel provides an overlay network for pods.
(1) Download manifest (adjust hosts for GitHub if needed)
# echo '199.232.28.133 raw.githubusercontent.com' >> /etc/hosts
# curl -o kube-flannel.yml https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# sed -i 's/quay.io/quay-mirror.qiniu.com/g' kube-flannel.yml
# kubectl apply -f kube-flannel.ymlVerify pods:
# kubectl get pod -n kube-system | grep kube-flannel
kube-flannel-ds-amd64-xxxx 1/1 Running ...(2) If images cannot be pulled, load a pre‑saved tarball:
# docker load -i flannel.tar2.7 Verify Cluster Health
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 82m v1.18.6
node1 Ready <none> 60m v1.18.6
node2 Ready <none> 55m v1.18.63 Test the Kubernetes Cluster
3.1 Deploy an Nginx Deployment
# kubectl create deployment nginx --image=nginx
# kubectl expose deployment nginx --port=80 --type=NodePortFind the NodePort (e.g., 32627) and access the service via the worker node IP.
3.2 Install Kubernetes Dashboard
(1) Download and modify manifest
# curl -o recommended.yaml https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
# Edit the Service to type NodePort and set nodePort (e.g., 30001)
# Apply the manifest
# kubectl apply -f recommended.yaml(2) Retrieve admin token
# kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')Access the dashboard at https://<worker‑node‑IP>:30001 using the token.
(3) Bind cluster‑admin role for full access
# kubectl create serviceaccount dashboard-admin -n kube-system
# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin4 Common Issues and Troubleshooting
Image pull failures : Use a compatible Kubernetes version in kubeadm-init.yaml or switch to a reachable mirror.
Docker cgroup driver mismatch : Set Docker to use systemd in /etc/docker/daemon.json and restart Docker.
kubectl on worker nodes : Copy /etc/kubernetes/admin.conf from the master to the worker and set KUBECONFIG environment variable.
Swap not disabled : Run swapoff -a and comment out swap entries in /etc/fstab.
Control‑plane components unhealthy : Remove --port=0 from kube‑controller‑manager.yaml and kube‑scheduler.yaml in /etc/kubernetes/manifests.
Dashboard cannot reach API server : Schedule the dashboard pod on the master node and comment out the default tolerations.
5 References
For further reading, see the blog post at https://www.cnblogs.com/FengGeBlog/p/10810632.html .
ZooKeeper is a classic distributed consistency solution; stay tuned for a dedicated session on September 1st.
Signed-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.
