腾讯云多地域组建Kubernetes集群(k3s)

本文中的脚本针对多地域的腾讯云服务器组建Kubernetes集群进行优化。腾讯云多地域请参考 腾讯云单地域组建Kubernetes集群,跨云多地域请参考 跨云多地域组建Kubernetes集群(k3s)

环境要求

  • 所有节点须为全新安装的Debian11/12Ubuntu22.04操作系统
  • 脚本使用了腾讯云的metadata获取IP,所有节点部署在腾讯云上不同地域(CVM、Lighthouse均可)

非腾讯云机器请手动设置PUBLIC_IPPRIVATE_IP环境变量的值

使用的组件

  • K3s 一个轻量级的Kubernetes发行版,专为生产环境而设计
  • Flannel 一个虚拟网络层,用于为容器化工作负载创建网络隔离和通信
  • WireGuard 一种轻量级、高性能、安全的虚拟专用网络(VPN)协议,旨在提供安全的通信
  • Flannel-WireGuard Flannel网络插件的一种后端,使用WireGuard加密和隧道技术来保护网络通信

设置防火墙

参考下面的列表在腾讯云控制台设置防火墙规则。若无需精细控制的,可以设置为允许所有节点间TCP/UPD协议的全部端口互访。

本文中将k8s默认的Node Port端口30000-32767更改为了常用的5432-9876,读者朋友也可以根据自己的情况重新定义,只要保持service-node-port-range参数和防火墙放开的端口对应即可。

协议 端口 目标 描述
TCP 6443 子节点 主节点 K8s API Server
TCP 10250 所有节点 所有节点 Kubelet 指标收集
UDP 51820 所有节点 所有节点 Flannel WireGuard
TCP 5432-9876 所有地址 所有节点 自定义 Node Port,可选
UDP 5432-9876 所有地址 所有节点 自定义 Node Port,可选
TCP 80,443 所有地址 所有节点 Web 服务,可选

部署K3S主节点

下面这段代码在主节点服务器上执行,注意替换SERVER_TOKEN为一个不少于32个字母的随机字符串。

apt update
apt install -y wireguard

echo "net.ipv4.ip_forward = 1" >/etc/sysctl.d/ip_forward.conf
sysctl -p /etc/sysctl.d/ip_forward.conf

export SERVER_TOKEN=r83nui54eg8wihyiteshuo3o43gbf7u9er63o43gbf7uitujg8wihyitr6

export PUBLIC_IP=$(curl -Ls http://metadata.tencentyun.com/latest/meta-data/public-ipv4)
export PRIVATE_IP=$(curl -Ls http://metadata.tencentyun.com/latest/meta-data/local-ipv4)

export INSTALL_K3S_SKIP_DOWNLOAD=true
export DOWNLOAD_K3S_BIN_URL=https://github.com/k3s-io/k3s/releases/download/v1.28.2%2Bk3s1/k3s

if [ $(curl -Ls http://ipip.rehi.org/country_code) == "CN" ]; then
   DOWNLOAD_K3S_BIN_URL=https://ghproxy.com/${DOWNLOAD_K3S_BIN_URL}
fi

curl -Lo /usr/local/bin/k3s $DOWNLOAD_K3S_BIN_URL
chmod a+x /usr/local/bin/k3s

curl -Ls https://get.k3s.io | sh -s - server \
    --cluster-init \
    --token $SERVER_TOKEN \
    --node-ip $PRIVATE_IP \
    --node-external-ip $PUBLIC_IP \
    --advertise-address $PRIVATE_IP \
    --service-node-port-range 5432-9876 \
    --flannel-backend wireguard-native \
    --flannel-external-ip

部署K3S子节点

下面这段代码在子节点服务器上执行,注意替换SERVER_TOKEN为和主节点相同的随机字符串,SERVER_IP为主节点的公网IP地址(在主节点执行命令curl -Ls http://metadata.tencentyun.com/latest/meta-data/public-ipv4即可获取)。

apt update
apt install -y wireguard

echo "net.ipv4.ip_forward = 1" >/etc/sysctl.d/ip_forward.conf
sysctl -p /etc/sysctl.d/ip_forward.conf

export SERVER_IP=43.129.195.33
export SERVER_TOKEN=r83nui54eg8wihyiteshuo3o43gbf7u9er63o43gbf7uitujg8wihyitr6

export PUBLIC_IP=$(curl -Ls http://metadata.tencentyun.com/latest/meta-data/public-ipv4)
export PRIVATE_IP=$(curl -Ls http://metadata.tencentyun.com/latest/meta-data/local-ipv4)

export INSTALL_K3S_SKIP_DOWNLOAD=true
export DOWNLOAD_K3S_BIN_URL=https://github.com/k3s-io/k3s/releases/download/v1.28.2%2Bk3s1/k3s

if [ $(curl -Ls http://ipip.rehi.org/country_code) == "CN" ]; then
   DOWNLOAD_K3S_BIN_URL=https://ghproxy.com/${DOWNLOAD_K3S_BIN_URL}
fi

curl -Lo /usr/local/bin/k3s $DOWNLOAD_K3S_BIN_URL
chmod a+x /usr/local/bin/k3s

curl -Ls https://get.k3s.io | sh -s - agent \
    --server https://$SERVER_IP:6443 \
    --token $SERVER_TOKEN \
    --node-ip $PRIVATE_IP \
    --node-external-ip $PUBLIC_IP

验证集群

在主节点执行下面的命令,查看节点和容器状态

kubectl get node
kubectl top node

kubectl get pods -A

集群概况

文章作者: 若海; 原文链接: https://www.rehiy.com/post/548/; 转载需声明来自技术写真 - 若海

已有 8 条评论

  1. 谢谢大佬~通过你的 kubernetes-dashboard 文章,我也成功搭建了管理页面。

  2. 后来去掉了部分参数后解决了。完成了单主的集群。

    这里是我实践过程中记录的操作步骤:https://discuss.plugins-world.cn/post/2Bexcn8C 搭建跨云 k3s 服务

  3. 混合云部署的看过啦,也失败。

    这个地方获取公网IP,我是去服务器云厂商复制的。

    1. 目前错误信息太少,只能判断是子节点无法连通腾讯云上部署的主节点,排查方向还是在ip和防火墙方面,另外也可以运行下 wg show 检查是否组网成功。

  4. 通过这个方式搭建的时候。

    腾讯云 master 节点成功启动了。

    华为云 worker 节点无法连上集群。会提示无法连接 http://xxxx:6444

    Oct 12 16:15:28 mouyong-hwecs k3s[2940707]: time="2023-10-12T16:15:28+08:00" level=error msg="failed to get CA certs: Get \"https://127.0.0.1:6444/cacerts\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)"
    Oct 12 16:15:50 mouyong-hwecs k3s[2940707]: time="2023-10-12T16:15:50+08:00" level=error msg="failed to get CA certs: Get \"https://127.0.0.1:6444/cacerts\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)"
    Oct 12 16:15:51 mouyong-hwecs CRON[2941662]: pam_unix(c

    1. 可以检查下腾讯云控制台防火墙是否正确放行了所需的端口,另外这篇文章是针对腾讯云优化的,使用metadata获取ip的地方需要修改为华为云的对应信息。还可以参考这篇混合云部署的方案 https://www.rehiy.com/post/547/

  5. fooksky

    跨地域的k3s集群,对于节点间的延迟有要求吗?

    1. k3s并没有一个明确的定义,但是建议子节点和主节点的延迟控制在100ms内

添加新评论