Step-by-Step Guide to Setting Up a Kubernetes 1.19 Cluster on CentOS 7.9
This guide walks through preparing two CentOS 7.9 servers, installing Docker and Kubernetes 1.19 components, initializing a master node, joining a worker node, and validating the cluster with a sample Nginx deployment, including common troubleshooting tips.
This article provides a detailed, step‑by‑step tutorial for deploying a Kubernetes 1.19 cluster on two CentOS 7.9 machines (one master and one node), covering system preparation, Docker installation, Kubernetes component installation, cluster initialization, node joining, and basic validation.
Server requirements
Two CentOS 7.9 servers are needed: the master is recommended to have 2 CPU & 4 GB RAM, and the node 2 CPU & 2 GB RAM.
1. Prepare system and network
Principle: Kubernetes requires consistent hostnames, disabled firewall and SELinux, and swap turned off to avoid installation failures.
1. Configure hostname and /etc/hosts
# 在Master节点执行
hostnamectl set-hostname master
# 在Node节点执行
hostnamectl set-hostname node
# 两台机器均修改hosts文件(替换实际IP)
vi /etc/hosts
192.168.1.100 master
192.168.1.101 nodeNote: replace the IP addresses with the actual server IPs; you can view them with ifconfig .
2. Disable firewall and SELinux
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭SELinux(临时生效)
setenforce 0
# 永久关闭SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/configNote: a reboot is required for the changes to become permanent.
3. Disable swap
# 临时关闭Swap
swapoff -a
# 永久关闭Swap(注释swap行)
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstabVerify that swap is zero with free -m .
4. Configure kernel parameters and IPVS
# 加载内核模块
cat > /etc/modules-load.d/k8s.conf << EOF
br_netfilter
ip_vs
ip_vs_rr
ip_vs_sh
nf_conntrack
EOF
modprobe --all
# 配置sysctl参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --systemVerify the modules with lsmod | grep br_netfilter .
5. Synchronize server time
# 安装NTP服务
yum install -y ntp
# 启动并同步时间
systemctl start ntpd
systemctl enable ntpd
ntpdate time.windows.comCheck the time with date to ensure consistency across nodes.
2. Install Docker runtime
Docker 19.03+ is required (compatible with Kubernetes 1.19).
1. Install Docker and configure a registry mirror
# 卸载旧版本Docker
yum remove -y docker*
# 安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加阿里云Docker仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装Docker
yum install -y docker-ce-19.03.15 docker-ce-cli-19.03.15
# 配置镜像加速
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
# 启动Docker
systemctl start docker
systemctl enable dockerVerify with docker info that the cgroup driver is systemd .
3. Install Kubernetes components (kubeadm, kubelet, kubectl)
1. Add Alibaba Cloud YUM repository
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
EOF2. Install version 1.19.0
# 安装组件
yum install -y kubelet-1.19.0 kubeadm-1.19.0 kubectl-1.19.0
# 设置kubelet开机启动
systemctl enable kubeletVerify the installation with kubeadm version .
4. Initialize the master node
1. Run kubeadm init
# 替换为Master节点实际IP
kubeadm init \
--apiserver-advertise-address=192.168.1.100 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.19.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=192.168.0.0/16Save the generated kubeadm join command; it will be needed to add worker nodes.
2. Configure kubectl access
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config3. Install Calico network plugin
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yamlVerify that Calico pods are running with kubectl get pods -n kube-system .
5. Join a node to the cluster
1. Execute the saved join command on the node
# 粘贴Master初始化时输出的kubeadm join命令(示例)
kubeadm join 192.168.1.100:6443 --token abcdef.1234567890 \
--discovery-token-ca-cert-hash sha256:xxxxxx2. Check node status from the master
kubectl get nodesBoth master and node should display the Ready status.
6. Validate cluster functionality
1. Deploy a test Nginx application
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svcAccess the service at http://NodeIP:NodePort to confirm Nginx is running.
2. Common issues
Node NotReady: verify Calico installation or restart kubelet .
Image pull failures: manually pull the image with docker pull registry.aliyuncs.com/google_containers/… .
The article notes that future posts will cover storage and monitoring configurations.
IT Xianyu
We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.
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.