请选择 进入手机版 | 继续访问电脑版

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 82|回复: 0

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

[复制链接]

508

主题

0

回帖

1546

积分

管理员

积分
1546
发表于 2023-12-9 19:52:13 | 显示全部楼层 |阅读模式
腾讯云轻量服务器 3 周年刚过,买买买完后,发现手里又多了好几台轻量服务器,拿来干什么还没想好,那就先来个“分布式吃灰”吧。
环境要求

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


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

使用的组件

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

购买并配置 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


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

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




上一篇:Saas 服务的技术解决方案
下一篇:saas 应用如何实现用户数据本地化部署?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|网盘猪

GMT+8, 2024-3-28 17:23 , Processed in 0.223531 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表