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

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

环境要求

  • 所有节点须为全新安装的Debian11/12Ubuntu22.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协议的全部端口互访。

协议端口目标描述
TCP6443子节点主节点Kubernetes API Server
TCP10250所有节点所有节点Kubelet 指标收集
UDP51820所有节点所有节点Flannel WireGuard
TCP5432-9876所有地址所有节点自定义的 Node Port
TCP80,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

集群概况

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值