腾讯云 Lighthouse 组建跨地域 Kubernetes 集群

211 天前
 panisertoller

腾讯云轻量服务器 3 周年刚过,买买买完后,发现手里又多了好几台轻量服务器,拿来干什么还没想好,那就先来个“分布式吃灰”吧。

环境要求

跨云集群请参考本站另一篇文章:跨云多地域组建 Kubernetes 集群(k3s)

使用的组件

购买并配置 Lighthouse 服务器

Lighthouse 是腾讯云推出的一款轻量应用服务器产品,它提供了高性价比、高可用性、高安全性和高灵活性的云端计算服务。具体信息参阅官方文档:轻量应用服务器概述

为了部署 Kubernetes 集群,我们需要至少 2 台轻量应用服务器,并配置好相关的环境和依赖。购买步骤参见官方文档:快速创建 Linux 实例操作系统选 Debian 或 Ubuntu 最新版本

在腾讯云控制台设置防火墙

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

协议 端口 目标 描述
TCP 6443 子节点 主节点 Kubernetes API Server
TCP 10250 所有节点 所有节点 Kubelet 指标收集
UDP 51820 所有节点 所有节点 Flannel WireGuard
TCP 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

须知:本文同步自若海の技术写真,如有错漏请到原文下留言反馈。

1449 次点击
所在节点    云计算
11 条回复
julyclyde
211 天前
是从公网相连的嘛?
arloor
211 天前
@julyclyde 是的,可以看 k8 的官方文档: https://docs.k3s.io/installation/network-options#distributed-hybrid-or-multicloud-cluster

是使用 wireguard 实现的公网互联
E1n
211 天前
43.129.195.33 这个是 wireguard 的公网 IP 吗?
PluginsWorld
211 天前
有安装 dashboard 的教程吗
PluginsWorld
208 天前
是用来阿里云的 注册集群来管理。方便了很多。每个月集群的保留成本约 ¥90.25 https://discuss.plugins-world.cn/post/4E2CTwvv
panisertoller
207 天前
@E1n 主节点外网 IP ,这里只是用来演示的测试机 IP
panisertoller
207 天前
@PluginsWorld 可参考作者另一篇文章 https://www.rehiy.com/post/393/
panisertoller
207 天前
@julyclyde 从公网互联的,可以参考作者另一篇 跨云多地域组建 Kubernetes 集群(k3s)
https://www.rehiy.com/post/547/
PluginsWorld
207 天前
@panisertoller 感谢感谢。通过你的文章,我成功创建了 dashboard 。可以做到节约注册集群的费用了。https://discuss.plugins-world.cn/comment/kEc9G9yf
PluginsWorld
207 天前
跨网互联,遇到了一个多 master 无法加入的问题。目前还不知道咋解决
PluginsWorld
207 天前
@panisertoller 大佬知道怎么在 k3s 上创建 serverless 环境吗,我想学习研究一下。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/980933

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX