Files
doc/rke2集群搭建指南.md
2025-10-16 17:32:48 +08:00

95 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# rke2集群搭建指南
rke2仅支持单宿主机单节点否则会出现一堆问题
**前期准备**
根据官方文档进行kubectl工具安装
依次执行:
```
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check //验证输出是否为ok判断文件下载是否有误
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl //安装kubectl
```
安装完成后可以执行kubectl version --client 查看当前版本
根据官方文档进行rke2的安装
执行:
```
curl -sfL https://get.rke2.io | sh -
```
国外链接安装需要挂梯子,建议使用国内下载链接
```
curl -sfL https://rancher-mirror.rancher.cn/rke2/install.sh | INSTALL_RKE2_MIRROR=cn sh -
```
#### 关键
**如果你需要使用非localhost访问集群kubernetes api需要添加--tls-san {machine.ip}**
我是这样操作的:
首先运行
```
systemctl enable rke2-server
```
不出意外应该会输出service文件位置例如我的
![alt text](./images/image1.png)
使用nano打开找到execstart命令组的rke2 server
追加内容 --tls-san {machine.ip}
例如这样:
![alt text](./images/image2.png)
保存退出
**关于镜像拉取**
个人认为躲不掉,配置以下环境变量的事情
根据.service文件可以得知systemd启动rke2-server时会读取以下目录的环境变量
EnvironmentFile=-/etc/default/%N
EnvironmentFile=-/etc/sysconfig/%N
EnvironmentFile=-/usr/local/lib/systemd/system/%N.env
%N为服务名
于是可以直接创建环境变量文件这里我选择了nano /etc/sysconfig/rke2-server文件夹可能提示不存在创建一个即可
添加如下内容:
```
HTTP_PROXY=http://127.0.0.1:7897
HTTPS_PROXY=http://127.0.0.1:7897
NO_PROXY=127.0.0.1,localhost,10.0.0.0/8,192.168.0.0/16
```
10.0.0.0/8的设置是因为我的隧道也是10.0.0.0/24所以放宽了规则避免隧道流量走代理
到这里,配置就基本上完成了,最后记得执行
```
sudo systemctl daemon-reload
```
**运行**
执行命令
```
systemctl start rke2-server
```
等待一段时间,成功后在/etc/rancher/rke2目录下有rke2.yaml,这个文件相当于集群最高管理员的密钥
可以直接使用rke2自带的kubectl也可以用全局的
全局的话有两个办法:一个是全局变量的形式,另一个是直接创建或追加集群、用户内容到~/.kube/config
至此rke2集群便可以正常运行了