原文链接
1、安装要求
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
(1)一台或多台机器,操作系统 CentOS7.x-86_x64 (2)硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多 (3)可以访问外网,需要拉取镜像,如果服务器不能上网,需要提前下载镜像并导入节点 (4)禁止swap分区
2、准备环境
角色 IP 用途 k8s-master 192.168.1.76 master节点 k8s-node01 192.168.1.77 node节点 k8s-node02 192.168.1.78 node节点 k8s-server 192.168.1.79 rancher-server节点 以下操作在全部节点进行:
# 关闭防火墙
systemctl stop firewalld
# 禁用防火墙
systemctl disable firewalld
# 关闭selinux
setenforce 0 # 临时
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
# 关闭swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久 关闭后需要重启服务器
# 根据规划设置主机名
hostnamectl set-hostname <hostname>
# 在master添加hosts
cat >> /etc/hosts << EOF
192.168.1.76 k8s-master
192.168.1.77 k8s-node01
192.168.1.78 k8s-node02
192.168.1.79 k8s-server
EOF
# 将桥接的IPv4流量传递到iptables的链
vim /etc/sysctl.conf
cat > /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
# 时间同步
yum install ntpdate -y
ntpdate time.windows.com
3、安装rancher
(1)安装docker
(2)启动rancher(k8s-rancher-server)
mkdir -p /data/rancher2.7.9
docker run -d --privileged -p 80:80 -p 443:443 -v /data/rancher2.7.9:/var/lib/rancher --restart=always --name rancher2.7.9 rancher/rancher:v2.7.9
(3)访问https://192.168.1.79,设置admin的用户密码,最低12位
(4)创建集群
(4.1)选择自定义集群

(4.2)基本信息

(4.3)镜像仓库改为阿里云仓库:(registry.cn-hangzhou.aliyuncs.com)

(4.4)点击创建
(4.5)注册master节点(如果有多个master节点,就在多台机器执行)
角色全选

复制注册命令到master主机执行:
curl --insecure -fL https://192.168.1.79/system-agent-install.sh | sudo sh -s - --server https://192.168.1.79 --label 'cattle.io/os=linux' --token pdkmztsfkm85hp7p7nqw2972sthx2wvpj7b9fdnrzjqwbhgj7w4t2s --ca-checksum 958c7a62f60588911644492bc6d0b00691388984419144352a3d275cd708af42 --etcd --controlplane --worker
(4.6)注册worker节点,并复制脚本到worker主机执行

由于采用VMW虚拟机做集群,机器性能受限,节点一个一个加入,等待时间较长,配置日志如下:
[INFO ] waiting for at least one control plane, etcd, and worker node to be registered
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for agent to check in and apply initial plan
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, etcd, kube-apiserver, kube-controller-manager, kube-scheduler, kubelet
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, etcd, kube-apiserver, kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, kube-apiserver, kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, kube-apiserver, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for cluster agent to connect
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for cluster agent to connect
[INFO ] non-ready bootstrap machine(s) custom-d067344f626e and join url to be available on bootstrap node
[INFO ] provisioning done
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for control plane to be available
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for agent to check in and apply initial plan
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for probes: calico, kubelet
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for probes: calico
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-apiserver
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for probes: calico
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: etcd, kube-apiserver
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: etcd, kube-apiserver, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: etcd, kube-apiserver, kube-controller-manager, kube-scheduler
[INFO ] custom-d067344f626e
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for probes: calico
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-apiserver
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-apiserver, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: Node condition Ready is False., waiting for probes: kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: Node condition Ready is False., waiting for probes: kube-controller-manager, kube-scheduler, kubelet
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-controller-manager, kube-scheduler, kubelet
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-controller-manager
[INFO ] configuring worker node(s) custom-9dde9a1c00dd: waiting for probes: calico
[INFO ] provisioning done
[INFO ] configuring worker node(s) custom-35b45afe977c: waiting for agent to check in and apply initial plan
[INFO ] configuring worker node(s) custom-35b45afe977c: waiting for probes: calico, kubelet
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for cluster agent to connect
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, etcd, kube-apiserver, kube-controller-manager, kube-scheduler, kubelet
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, etcd, kube-apiserver, kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, kube-apiserver, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: calico
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for cluster agent to connect
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: etcd, kube-apiserver
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: etcd, kube-apiserver, kube-controller-manager, kube-scheduler
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-apiserver, kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for probes: kube-controller-manager
[INFO ] configuring bootstrap node(s) custom-d067344f626e: waiting for cluster agent to connect
[INFO ] configuring worker node(s) custom-35b45afe977c: waiting for probes: calico, kubelet
[INFO ] configuring worker node(s) custom-35b45afe977c,custom-9dde9a1c00dd
[INFO ] configuring worker node(s) custom-35b45afe977c: Node condition MemoryPressure is Unknown. Node condition DiskPressure is Unknown. Node condition PIDPressure is Unknown. Node condition Ready is Unknown., waiting for probes: calico, kubelet
[INFO ] configuring worker node(s) custom-35b45afe977c: waiting for probes: calico, kubelet
[INFO ] configuring worker node(s) custom-35b45afe977c: waiting for probes: calico
[INFO ] provisioning done
(4.7)搭建完成:


注意:
(1)master节点注册后一直提示:Waiting for probes: kube-controller-manager, kube-scheduler。可能是因为之前注册未成功留下的数据文件db有问题。可以:
查看RKE2服务日志:
journalctl -u rke2-server -f
通过清理并重建 etcd 数据
注意:此操作会丢失现有集群数据,仅适用于新集群或可接受重置的场景。
停止 RKE2 服务:
sudo systemctl stop rke2-server
备份并清理 etcd 数据目录:
sudo mv /var/lib/rancher/rke2/server/db /var/lib/rancher/rke2/server/db.bak
重新启动服务:
sudo systemctl start rke2-server
RKE2 会自动初始化新的 etcd 数据。
(2)worker节点注册后一直提示:Waiting for probes: calico
可以直接删除/var/lib/rancher/文件夹,然后重新进行注册。