Cloud Native 21 min read

Step‑by‑Step Guide to Deploy a Kubernetes Cluster on CentOS 7

This tutorial walks through preparing three CentOS 7 hosts, installing Docker and Kubernetes components, initializing a master node, handling common errors, joining worker nodes, installing a CNI plugin, testing the cluster, and provides essential kubectl commands for ongoing management.

Raymond Ops
Raymond Ops
Raymond Ops
Step‑by‑Step Guide to Deploy a Kubernetes Cluster on CentOS 7

1. Preparation

1.1 Host Planning

Define three hosts (master, node1, node2) with IP 192.168.100.101‑103, CentOS 7, 2 CPU × 2 GB RAM, 50 GB disk.

1.2 Upgrade Kernel

Refer to https://www.cnblogs.com/wangyuanguang/p/18051798 for kernel upgrade.

1.3 Set Hostname and /etc/hosts

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 node2

1.4 Disable SELinux and firewalld

systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

1.5 Disable swap

swapoff -a

1.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 --system

2. Install Docker

2.1 Configure daemon

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. Install Kubernetes components

3.1 Add Alibaba Cloud yum repo

cat > /etc/yum.repos.d/kubernetes.repo <<EOF
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

3.2 Install kubeadm, kubelet, kubectl

yum install -y kubelet-1.28.2 kubeadm-1.28.2 kubectl-1.28.2
systemctl start kubelet
systemctl enable kubelet

4. Initialize the cluster

4.1 Master initialization

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/16

Parameters: --apiserver-advertise-address selects the master NIC; --pod-network-cidr matches the CNI plugin (Flannel uses 10.240.0.0/16); --service-cidr defines the service network; --image-repository points to a domestic mirror; --kubernetes-version pins the version.

4.2 Common errors and fixes

Four typical pre‑flight or post‑init issues are described with solutions: (1) container runtime not running – edit /etc/containerd/config.toml to enable the cri plugin; (2) leftover manifests after a failed init – run kubeadm reset; (3) missing bridge‑nf‑call‑iptables – load br_netfilter module; (4) kubelet not starting – ensure the pause image is available and restart containerd.

4.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.sh

4.4 Join worker nodes

kubeadm join 192.168.100.101:6443 \
  --token <TOKEN> \
  --discovery-token-ca-cert-hash sha256:<HASH>

5. Install CNI plugin

5.1 Flannel

mkdir /data/k8s && cd /data/k8s
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
sed -i 's/10.244.0.0/10.240.0.0/' kube-flannel.yml
kubectl apply -f kube-flannel.yml

6. Test the cluster

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svc

7. Common kubectl commands

Examples include kubectl get pod -o wide -n namespace, kubectl apply -f xxx.yaml, kubectl delete -f xxx.yaml, kubectl describe pod pod-name, kubectl logs pod-name, kubectl top node|pod, and kubectl exec -ti pod-name /bin/bash for troubleshooting and resource inspection.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerKubernetesk8sCNICluster DeploymentCentOSFlannel
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.