总结摘要
本文提供了一份在离线或内网环境中,使用RKE(Rancher Kubernetes Engine)高可用(HA)部署Rancher 2.x的完整指南。内容涵盖环境准备(操作系统优化、防火墙与端口配置)、基础设施搭建(负载均衡器、DNS、私有镜像仓库如Harbor)、将Rancher所需镜像同步至私有仓库、通过RKE部署Kubernetes集群,以及最后使用Helm在集群上安装Rancher并配置自签名证书的详细步骤。
安装要求
安装概要:
1、准备节点和私有镜像仓库
2、同步镜像到私有镜像仓库
3、部署k8s集群
4、安装rancher
操作系统 和 容器运行时 要求
- rancher可以兼容当前任何流行的Linux发行版。
- 对于需要运行k3s或RKE Kubernetes 集群的节点需要使用 Docker。对于RancherD 安装来说,不需要Docker。
- rancher需要安装在支持的k8s版本上。参考
Support matrix | SUSE
了解rancher版本支持哪些Kubernetes版本。
- 为了防止客户端和服务端之间因为时间不同步而发生的证书验证错误,建议安装NTP来同步时钟
- 一些Linux发行版的默认防火墙规则可能会屏蔽掉Helm的通信。建议直接关闭防火墙,如需开启,可以尝试参考
防火墙放行端口
进行开放端口。
RKE要求
RKE可以兼容所有Docker版本。RKE 版本与 Kubernetes版本相关。
docker版本
无特殊要求,可以参考:https://www.suse.com/zh-cn/suse-rancher/support-matrix/all-supported-versions/rancher-v2-7-1/
磁盘
Rancher 的性能取决于 etcd 在集群中的性能。为了确保最佳速度,我们建议使用 SSD 磁盘来支持 Rancher 管里面的 Kubernetes 集群。在云提供商上,还需要使用允许最大 IOPS 的最小大小。在较大的集群中,请考虑使用专用存储设备存储 etcd 数据和 wal 目录。
网络要求
IP地址
无论是单节点安装还是搞可用安装rancher,每个节点都应该配置一个静态IP,如果使用DHCP,则每个节点应该具有DHCP预留,以确保该节点分配的相同的IP地址。
端口要求
注意:
- Rancher 节点可能还需要为外部认证(例如 LDAP)提供额外的出站访问。
- Kubernetes 建议 NodePort 端口使用 TCP 30000-32767。
- 对于防火墙,可能需要在集群和 pod CIDR 内启用流量。
为了正常运行,rancher需要在rancher节点和下游集群节点上开放一些端口。端口需求列出了不同集群类型的rancher和下游集群的所有必要端口。
RKE上的rancher servier节点的端口
通常情况下,rancher安装在三个RKE节点上,这些节点都有etcd、controlplane、worker角色。
下表细分了rancher节点之间流量的端口要求:
Rancher节点之间的流量规则
| 协议 | 端口 | 描述 |
|---|
| TCP | 443 | Rancher agents |
| TCP | 2379 | etcd 客户端请求 |
| TCP | 2380 | etcd 对等通信 |
| TCP | 6443 | Kubernetes apiserver |
| UDP | 8472 | Canal/Flannel VXLAN overlay 网络 |
| TCP | 9099 | Canal/Flannel livenessProbe/readinessProbe |
| TCP | 10250 | kubelet |
| TCP | 10254 | Ingress controller livenessProbe/readinessProbe |
下表细分了入站和出站流量的端口要求:
Rancher 节点的入站规则
| 协议 | 端口 | 来源 | 描述 |
|---|
| TCP | 22 | RKE CLI | RKE 通过 SSH 配置节点 |
| TCP | 80 | 负载均衡器/反向代理 | 到 Rancher UI/API 的 HTTP 流量 |
| TCP | 443 | 负载均衡器/反向代理所有集群节点和其他 API/UI 客户端的 IP | 到 Rancher UI/API 的 HTTPS 流量 |
| TCP | 6443 | Kubernetes API 客户端 | 到 Kubernetes API 的 HTTPS 流量 |
Rancher 节点的出站规则
| Protocol | Port | 目的地 | 描述 |
|---|
| TCP | 443 | 35.160.43.145,35.167.242.46,52.33.59.17 | Rancher catalog (git.rancher.io) |
| TCP | 22 | 使用 node driver 创建的任何节点 | 通过 node driver 对节点进行 SSH 配置 |
| TCP | 2376 | 使用 node driver 创建的任何节点 | node driver 使用的 Docker 守护进程 TLS 端口 |
| TCP | 6443 | 托管/导入的 Kubernetes API | Kubernetes API server |
| TCP | Provider dependent | 托管集群中 Kubernetes API endpoint 的端口 | Kubernetes API |
下游Kubernetes集群节点
下游Kubernetes集群运行应用程序和服务。本节介绍了需要在下游集群的节点上打开哪些端口,以便rancher能够与它们通信。

常用端口
这些端口通常在你的 Kubernetes 节点上打开,无论它是什么类型的集群。
| 协议 | 端口 | 描述 |
|---|
| TCP | 22 | 使用主机驱动通过 SSH 进行节点配置 |
| TCP | 179 | Calico BGP 端口 |
| TCP | 2376 | 主机驱动与 Docker 守护进程通信的 TLS 端口 |
| TCP | 2379 | etcd 客户端请求 |
| TCP | 2380 | etcd 节点通信 |
| UDP | 8472 | Canal/Flannel VXLAN overlay 网络 |
| UDP | 4789 | Windows 集群中 Flannel VXLAN overlay 网络 |
| UDP | 8443 | Rancher webhook |
| TCP | 9099 | Canal/Flannel 健康检查 |
| TCP | 9100 | Monitoring 从 Linux node-exporters 中抓取指标所需的默认端口 |
| TCP | 9443 | Rancher webhook |
| TCP | 9796 | 集群监控拉取节点指标的默认端口(仅需要内网可达) |
| TCP | 6783 | Weave 端口 |
| UDP | 6783-6784 | Weave UDP 端口 |
| TCP | 10250 | Metrics server 与所有节点的通信 |
| TCP | 10254 | Ingress controller 健康检查 |
| TCP/UDP | 30000-32767 | NodePort 端口范围 |
其他端口要求:端口要求 | Rancher文档https://docs.rancher.cn/docs/rancher2/installation/requirements/ports/_index/
防火墙放行端口
防火墙版本:v7.x或更高版本的firewalld
查看防火墙默认规则:
应用防火墙端口规则
在Rancher高可用安装指南中,Rancher Server设置在三个节点上,三个节点具备k8s的所有角色:etcd、controlplane和worker。如果节点具备所有角色,则需要运行以下命令:
1
2
3
4
5
6
7
8
9
10
11
12
13
| firewall-cmd --permanent --add-port=22/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2379/tcp
firewall-cmd --permanent --add-port=2380/tcp
firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --permanent --add-port=9099/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10254/tcp
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --permanent --add-port=30000-32767/udp
|
如果节点具备单独的非全部角色,则需要根绝节点角色执行以下命令:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| ## 对于etcd节点,运行以下命令:
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2379/tcp
firewall-cmd --permanent --add-port=2380/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --permanent --add-port=9099/tcp
firewall-cmd --permanent --add-port=10250/tcp
## 对于control plane节点,运行以下命令:
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --permanent --add-port=9099/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10254/tcp
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --permanent --add-port=30000-32767/udp
## 对于worker nodes节点,运行以下命令:
firewall-cmd --permanent --add-port=22/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --permanent --add-port=9099/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10254/tcp
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --permanent --add-port=30000-32767/udp
|
在节点上运行firewall-cmd命令后,使用以下命令重新加载防火墙规则:
安装准备
准备rancher server所用的操作系统
| 序号 | 角色 | IP | 说明 |
|---|
| 1 | Racher-LB | | Rancher集群入口负载均衡器(nginx)节点 |
| 2 | Rancher-master01 | | Rancher管理集群master节点1 |
| 3 | Rancher-master02 | | rancher管理集群master节点2 |
| 4 | Rancher-master03 | | Rancher管理集群master节点3 |
| … | custom-master/worker | | 业务集群master/worker节点 |
环境配置(所有主机)
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭Swap分区
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
# 设置时间时区和语言
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sudo echo 'LANG="en_US.UTF-8"' >> /etc/profile
source /etc/profile
|
系统内核参数配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| echo "
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
net.ipv4.conf.all.forwarding=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
net.ipv4.neigh.default.gc_interval=60
net.ipv4.neigh.default.gc_stale_time=120
kernel.perf_event_paranoid=-1
net.ipv4.tcp_slow_start_after_idle=0
fs.inotify.max_user_watches=524288
fs.file-max=2097152
fs.inotify.max_user_instances=8192
fs.inotify.max_queued_events=16384
kernel.softlockup_all_cpu_backtrace=1
kernel.softlockup_panic=1
vm.max_map_count=262144
net.core.netdev_max_backlog=16384
net.ipv4.tcp_wmem=4096 12582912 16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 12582912 16777216
net.core.rmem_max=16777216
net.core.somaxconn=32768
net.ipv4.tcp_max_syn_backlog=8192
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
#kernel.yama.ptrace_scope=0
vm.swappiness=0
kernel.core_uses_pid=1
net.ipv4.conf.default.accept_source_route=0
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.default.promote_secondaries=1
net.ipv4.conf.all.promote_secondaries=1
fs.protected_hardlinks=1
fs.protected_symlinks=1
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
net.ipv4.tcp_max_tw_buckets=5000
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_synack_retries=2
kernel.sysrq=1
" > /etc/sysctl.d/k8s.conf
sysctl -p /etc/sysctl.d/k8s.conf
echo 30 > /proc/sys/kernel/watchdog_thresh
|
系统内核模块配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| echo 'br_netfilter
ip6_udp_tunnel
ip_set
ip_set_hash_ip
ip_set_hash_net
iptable_filter
iptable_nat
iptable_mangle
iptable_raw
nf_conntrack_netlink
nf_conntrack
nf_conntrack_ipv4
nf_defrag_ipv4
nf_nat
nf_nat_ipv4
nf_nat_masquerade_ipv4
nfnetlink
udp_tunnel
veth
vxlan
x_tables
xt_addrtype
xt_conntrack
xt_comment
xt_mark
xt_multiport
xt_nat
xt_recent
xt_set
xt_statistic' > /etc/modules-load.d/rancher_k8s.conf
|
limits配置
| 执行主机 | 说明 |
|---|
| 所有节点 | 针对k8s优化limits参数 |
1
2
3
4
| echo '* soft nofile 655350
* hard nofile 655350
* soft nproc unlimited
* hard nproc unlimited' >> /etc/security/limits.conf
|
升级内核
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| # 配置公钥
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 下载源
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
# 查看可用的系统内核包,可以看到kernel-lt与kernel-ml两个版本
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
# 安装kernel
yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml.x86_64
# 查看内核启动信息
sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
#显示结果如下:
0 : CentOS Linux (5.4.160-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1127.19.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-1127.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-20200914151306980406746494236010) 7 (Core)
# 设置默认启动内核,版本根据实际修改
grub2-set-default 0
# 查看内核版本默认启动顺序---确保第一个是我们设置的内核
awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
# 查看当前内核
grub2-editenv list
uname -r
# 重启
reboot
|
一 配置基础设置和镜像仓库
在 RKE 集群中安装 Rancher 高可用,我们建议为高可用安装配置以下基础设施:
- 3 个 Linux 节点,通常是虚拟机,您可以自行选择的基础设施提供商,例如 Amazon EC2、阿里云、腾讯云或者 vShpere。
- 1 个负载均衡器,用于将流量转发到这三个节点。
- 1 条 DNS 记录,用于将 URL 指向负载均衡器。这将成为 Rancher Server 的 URL,下游集群需要可以访问到这个地址。
- 私有 Docker 镜像仓库,用于为您的节点分发 Docker 镜像。
您可以将这些服务器放在不同的可用区里,但这些节点必须位于相同的区域/数据中心。
| 部署规模 | 集群 | 节点 | vCPUs | 内存 |
|---|
| 小 | 最多 150 个 | 最多 1500 个 | 2 | 8 GB |
| 中 | 最多 300 个 | 最多 3000 个 | 4 | 16 GB |
| 大 | 最多 500 个 | 最多 5000 个 | 8 | 32 GB |
| 特大 | 最多 1000 个 | 最多 10,000 个 | 16 | 64 GB |
| 超大 | 最多 2000 个 | 最多 20,000 个 | 32 | 128 GB |
所有服务器的规模可以按照以上表格中的配置来。
1、Linux节点
Linux节点可以与Internet断开连接,但是需要能够访问到私有镜像仓库。
2、配置负载均衡器
需要准备一台负载均衡器,将流量定向到全部节点上的rancher副本,这样可以在某个节点出现故障时,继续保障与rancher管理服务器的连接。
在后续步骤中配置 Kubernetes 时,RKE 工具将部署 NGINX Ingress 控制器。该控制器将侦听 worker 节点的 80 端口和 443 端口,以响应发送给特定主机名的流量。
在安装 Rancher 时(也是在后续步骤中),Rancher 系统将创建一个 Ingress 资源。该 Ingress 通知 NGINX Ingress 控制器侦听发往 Rancher 主机名的流量。NGINX Ingress 控制器在收到发往 Rancher 主机名的流量时,会将其转发到集群中正在运行的 Rancher Server Pod。
对于实现,请考虑是否要使用 4 层或 7 层负载均衡器:
- 4 层负载均衡器 是一种相对简单的负载均衡,它将 TCP 流量转发到您到节点。我们建议使用 4 层负载均衡器,将流量从 TCP/80 端口和 TCP/443 端口转发到 Rancher 管理面的集群节点上。集群上的 Ingress 控制器会将 HTTP 流量重定向到 HTTPS,并在 TCP/443 端口上终止 SSL/TLS。Ingress 控制器会将流量转发到 Rancher Server Pod 的 TCP/443 端口。
- 7 层负载均衡器 是一种相对复杂的负载均衡,但功能更加全面。例如,与 Rancher 本身进行 TLS 终止相反,7 层负载均衡器能够在负载均衡器处处理 TLS 终止。如果要在基础设施中进行 TLS 终止,7 层负载均衡可能会很有用。7 层负载均衡还可以为您的负载均衡器提供基于 HTTP 属性(例如 cookie 等)做出决策的能力,而 4 层负载均衡器不提供这种功能。如果决定在 7 层负载均衡器上终止 SSL/TLS 流量,则在安装 Rancher 时(后续步骤)需要使用
--set tls=external选项。
3、配置DNS记录
配置完负载均衡器后,将需要创建 DNS 记录,以将流量发送到该负载均衡器。
根据环境,DNS 记录可以是指向负载均衡器 IP 的 A 记录,也可以是指向负载均衡器主机名的 CNAME。无论哪种情况,请确保该记录是您要 Rancher 进行响应的主机名。
在安装 Rancher 时(后续步骤),您需要指定此主机名,并且在以后也无法更改它。确保您的决定是最终决定。
4、配置私有docker镜像仓库
rancher支持使用私有镜像仓库进行离线安装。必须要有自己的镜像仓库或者使用其他方式将进行发布到节点上。
二 同步镜像到私有镜像仓库
在默认情况下,rancher中所有用于创建k8s集群或者运行任何工具的镜像都是从docker hub中进行拉去,如:监控、日志、警告、流水线等镜像。在离线环境下安装rancher则需要拥有一个私有镜像仓库,这个镜像仓库应该能被rancher中的节点访问到。然后将所需要的所有镜像都导入到这个仓库中。
1、集群中仅有Linux节点
对于创建仅有Linux节点的集群的rancher server,则按照以下步骤进行推送镜像到私有镜像仓库。
- 1、查找所用rancher版本所需的所有镜像
- 2、搜集 cert-manager镜像
- 3、下载镜像到本地服务器上
- 4、将镜像推送到私有镜像仓库中
准备一台Linux服务器,存储资源最好不低于300G。
1、查找rancher版本所需资源
1、浏览版本发布页面https://github.com/rancher/rancher/releases,查找将要安装的rancher的版本。避开标记为rc或Pre-release的版本。
2、从发行版Assets部分下载以下文件,
| Release 文件 | 描述 |
|---|
rancher-images.txt | 此文件包含安装 Rancher、创建集群和运行 Rancher 工具所需的镜像列表。 |
rancher-save-images.sh | 这个脚本会从 DockerHub 中拉取在文件rancher-images.txt中描述的所有镜像,并将它们保存为文件rancher-images.tar.gz。 |
rancher-load-images.sh | 这个脚本会载入文件rancher-images.tar.gz中的镜像,并将它们推送到您自己的私有镜像库。 |
2、收集 cert-manager 镜像
如果使用自己的证书,或者要在外部负载均衡器上终止TLS,则跳过该步骤。
1、获取最新的 cert-manager Helm chart,解析模板,获取镜像详细信息:
1
2
3
4
| helm repo add jetstack https://charts.jetstack.io
helm repo update
helm fetch jetstack/cert-manager --version v0.12.0
helm template ./cert-manager-<version>.tgz | grep -oP '(?<=image: ").*(?=")' >> ./rancher-images.txt
|
2、对镜像列表进行排序,去除重复的镜像源:
1
| sort -u rancher-images.txt -o rancher-images.txt
|
3、将镜像保存到本地
1、为 rancher-save-images.sh 脚本添加可执行权限:
1
| chmod a+x rancher-save-images.sh
|
2、执行脚本rancher-save-images.sh并以--image-list ./rancher-images.txt 作为参数,创建所有需要镜像的压缩包:
1
| ./rancher-save-images.sh --image-list ./rancher-images.txt
|
国内用户,可以从
http://mirror.rancher.cn
–> rancher –> [rancher 版本] 下载 rancher-save-images.sh,该脚本支持通过参数 --from-aliyun true 来指定从阿里云镜像仓库拉去 rancher 镜像(从 rancher/rancher release 下载的 rancher-save-images.sh 不支持该参数),例如:
1
2
3
| ./rancher-save-images.sh --image-list ./rancher-images.txt --from-aliyun true
Image pull success: registry.cn-hangzhou.aliyuncs.com/rancher/busybox
Image pull success: registry.cn-hangzhou.aliyuncs.com/rancher/backup-restore-operator:v1.0.4-rc4
|
Docker 会开始拉取用于离线安装所需的镜像。这个过程会花费几分钟时间。完成时,您的当前目录会输出名为rancher-images.tar.gz的压缩包。请确认输出文件是否存在
4、推送镜像到私有仓库
下一步,您将使用脚本将文件 rancher-images.tar.gz 中的镜像上传到您自己的私有镜像库。
文件 rancher-images.txt 和 rancher-images.tar.gz 应该位于工作站中运行 rancher-load-images.sh 脚本的同一目录下。
1、登录私有镜像库:
1
| docker login <REGISTRY.YOURDOMAIN.COM:PORT>
|
2、为rancher-load-images.sh 添加可执行权限:
1
| chmod a+x rancher-load-images.sh
|
3、使用脚本 rancher-load-images.sh提取rancher-images.tar.gz文件中的镜像,根据文件rancher-images.txt中的镜像列表对提取的镜像文件重新打 tag 并推送到您的私有镜像库:
1
| ./rancher-load-images.sh --image-list ./rancher-images.txt --registry <REGISTRY.YOURDOMAIN.COM:PORT>`
|
2、集群中存在windows节点
v2.3.0 及以后版本可用
对于创建包含Linux节点和windows节点的集群的rancher server,因为一个集群会包含 Linux 和 Windows 的混合节点,所以需要同步 Linux 和 windows 镜像。、
与Linux准备镜像有所不同的是:
1、需要准备Windows Server 1809 工作站,需要能够访问网络,能够访问私有镜像仓库,资源最好能有300G。
2、必须安装 docker 18.02+ 版本,因为这个版本的 Docker 支持 manifests,这个特性是设置 Windows 集群所必须的。
3、需要下载文件如下:
| Release 文件 | 描述 |
|---|
rancher-windows-images.txt | 这个文件包含设置 Windows 集群所需的 Windows 镜像列表。 |
rancher-save-images.ps1 | 这个脚本会从 DockerHub 上拉取所有在文件rancher-windows-images.txt中列举的镜像,并将它们保存到rancher-windows-images.tar.gz文件中。 |
rancher-load-images.ps1 | 这个脚本会从压缩包rancher-windows-images.tar.gz中加载镜像,并把它们推送到您的私有镜像库 中。 |
4、将镜像保存到您的 Windows 服务工作站中
1、在 powershell中,进入上一步有下载文件的目录里。
2、运行脚本 rancher-save-images.ps1, 去创建所有必需的压缩包:
1
| ./rancher-save-images.ps1
|
结果: Docker 会开始拉取离线安装所需要的镜像。这个过程需要几分钟时间,请耐心等待。拉取镜像结束后,您的当前目录会输出名为rancher-windows-images.tar.gz的压缩包。请确认输出文件是否存在
5、准备 Docker 守护进程
将您的私有镜像库地址追加到 Docker 守护进程配置文件(C:\ProgramData\Docker\config\daemon.json)的allow-nondistributable-artifacts配置字段中。由于 Windows 镜像的基础镜像是由mcr.microsoft.com Registry 维护的,这一步是必须的,因为 Docker Hub 中缺少 Microsoft 镜像仓库层,需要将其拉入私有镜像库。
1
2
3
4
5
6
7
8
| {
...
"allow-nondistributable-artifacts": [
...
"<REGISTRY.YOURDOMAIN.COM:PORT>"
]
...
}
|
6、推送镜像到私有镜像库
将通过脚本,将文件rancher-windows-images.tar.gz中的镜像移入到您私有镜像库中。
在工作站中,文件rancher-windows-images.txt和rancher-windows-images.tar.gz要放在与运行脚本rancher-load-images.ps1的同一目录下。
如果需要,请使用 powershell,登录到您的私有镜像库:
1
| docker login <REGISTRY.YOURDOMAIN.COM:PORT>
|
在 powershell中,使用 rancher-load-images.ps1脚本,提取文件rancher-images.tar.gz中的镜像,重新打 tag 并将它们推送到您的私有镜像库中:
1
| ./rancher-load-images.ps1 --registry <REGISTRY.YOURDOMAIN.COM:PORT>
|
三 安装RKE集群
前置要求:
- 已按照安装
安装准备
Linux操作系统
- 已准备好私有镜像仓库
任务要求:
- 已安装RKE
- 已创建RKE配置文件
- 已安装docker
docker可以使用二进制安装,也可以使用yum进行安装,以下示例通过二进制安装docker
安装docker
以docker-20.10.9为例,如需其他版本,则访问
docker官方下载
自行下载。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz
tar -vxf docker-20.10.9.tgz
cp docker/* /usr/bin/
cp docker.s* /etc/usr/lib/systemd/system # 复制docker.server和docker.socket文件,如果没有特熟要求,可以百度自行下载,或使用下方示例文件。
mkdir /etc/docker/
cat > /etc/docker/daemon.json << EOF ## 如果daemon.json文件已有内容,请手动复制
{
"registry-mirrors": ["https://hub-mirror.c.163.com/"], #添加镜像加速,内网环境不需要
"exec-opts": ["native.cgroupdriver=systemd"], # 设置docker cgroupdriver为=systemd,目前看来是必须的
"insecure-registries": ["0.0.0.0"] #http协议或私有证书的https协议需要添加私有仓库信任,将0.0.0.0换成ip加端口或域名加端口,80/443除外
}
EOF
systemctl daemon-reload && systemctl enable --now docker #重新加载服务并开启docker自启动
# 另外 节点需要登录私有仓库地址,
docker login registry.exmple.com
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| cat > /etc/docker/daemon.json <<EOF
{
"oom-score-adjust": -1000,
"log-driver": "json-file",
"log-opts": {
"max-size": "500m",
"max-file": "3"
},
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 10,
"live-restore": true,
"insecure-registries": ["0.0.0.0/0"],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| # vim docker.service #将一下内容复写入docker.service文件中
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
#BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
#Requires=docker.socket
[Service]
Type=notify
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
|
1
2
3
4
5
6
7
8
9
10
| # vim docker.socket
[Unit]
Description=Docker Socket for the API
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
|
1、安装RKE
登录访问RKE版本发布页面,
Releases · rancher/rke (github.com)
,下载最新的RKE v1.x,有以下几个版本可供下载:
- macOS:
rke_darwin-amd64 - Linux (Intel/AMD):
rke_linux-amd64 - Linux (ARM 32-bit):
rke_linux-arm - Linux (ARM 64-bit):
rke_linux-arm64 - Windows (32-bit):
rke_windows-386.exe - Windows (64-bit):
rke_windows-amd64.exe
将RKE安装包转换为可执行文件,Linux操作系统需要添加一个可执行权限。
运行rke --version检查是否安装成功
2、创建RKE配置文件
在可以访问Linux主机节点22/TCP端口和6443/TCP端口的系统上,创建一个cluster.yml文件,该文件是 RKE 配置文件,用于配置你要部署 Rancher 的集群。
具体参数参考:https://docs.rancher.cn/docs/rke/config-options/_index
创建rancher用户
1
2
3
4
5
6
7
8
9
10
| # 三台rancher主机添加用户
useradd -G docker rancher
echo -n rancher | passwd --stdin rancher
# test-rancher01主机配置免密
su - rancher
ssh-keygen -t rsa
ssh-copy-id master01
ssh-copy-id master02
ssh-copy-id master03
|
RKE 选项
| 选项 | 必填 | 描述 |
|---|
address | ✓ | 离线环境中节点的 DNS 或 IP 地址 |
user | ✓ | 可运行 Docker 命令的用户 |
role | ✓ | 分配给节点的 Kubernetes 角色列表 |
internal_address | 可选1 | 用于集群内部流量的 DNS 或 IP 地址 |
ssh_key_path | | 用来验证节点的 SSH 私钥文件路径(默认值为 ~/.ssh/id_rsa) |
如果使用外部TLS终止,则需要在cluster.yml文件中添加以下内容
ingress:
provider: nginx
options:
use-forwarded-headers: ’true'
标准示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| nodes:
- address: master01
internal_address: 192.168.2.221
hostname_override: master01
user: rancher
role:
- controlplane
- etcd
- worker
- address: master02
internal_address: 192.168.2.222
hostname_override: master02
user: rancher
role:
- controlplane
- etcd
- worker
- address: master03
internal_address: 192.168.2.223
hostname_override: master03
user: rancher
role:
- controlplane
- etcd
- worker
# 集群名称
cluster_name: rancher-cluster
services:
etcd:
backup_config:
enabled: true
interval_hours: 6
retention: 8
snapshot: true
creation: 5m0s
retention: 24h
extra_args:
quota-backend-bytes: "6442450944"
auto-compaction-retention: 240 #(单位⼩时)
kube-api:
extra_args:
watch-cache: true
default-watch-cache-size: 1500
max-requests-inflight: 800
max-mutating-requests-inflight: 400
kube-controller:
extra_args:
node-cidr-mask-size: "24"
node-monitor-period: "5s"
node-monitor-grace-period: "20s"
node-startup-grace-period: "30s"
pod-eviction-timeout: "1m"
concurrent-gc-syncs: 20
concurrent-namespace-syncs: 10
concurrent-replicaset-syncs: 5
concurrent-service-syncs: 1
concurrent-serviceaccount-token-syncs: 5
deployment-controller-sync-period: 30s
pvclaimbinder-sync-period: 15s
kube-api-qps: 40
#private_registries:
# - url: 192.168.182.128 #harbor VIP地址
# user: admin
# password: Harbor12345 #harbor密码
# is_default: true
ingress:
provider: nginx
options:
use-forwarded-headers: 'true'
|
最小示例
1
2
3
4
5
6
7
| nodes:
- address: 1.2.3.4
user: ubuntu
role:
- controlplane
- etcd
- worker
|
完整文件示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
| nodes:
- address: 1.1.1.1
user: ubuntu
role:
- controlplane
- etcd
port: 2222
docker_socket: /var/run/docker.sock
- address: 2.2.2.2
user: ubuntu
role:
- worker
ssh_key_path: /home/user/.ssh/id_rsa
ssh_key: |-
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
ssh_cert_path: /home/user/.ssh/test-key-cert.pub
ssh_cert: |-
[email protected] AAAAHHNzaC1yc2EtY2VydC12MDFAb3Bl....
- address: example.com
user: ubuntu
role:
- worker
hostname_override: node3
internal_address: 192.168.1.6
labels:
app: ingress
taints:
- key: test-key
value: test-value
effect: NoSchedule
# If set to true, RKE will not fail when unsupported Docker version
# are found
ignore_docker_version: false
# Enable running cri-dockerd
# Up to Kubernetes 1.23, kubelet contained code called dockershim
# to support Docker runtime. The replacement is called cri-dockerd
# and should be enabled if you want to keep using Docker as your
# container runtime
# Only available to enable in Kubernetes 1.21 and higher
enable_cri_dockerd: true
# Cluster level SSH private key
# Used if no ssh information is set for the node
ssh_key_path: ~/.ssh/test
# Enable use of SSH agent to use SSH private keys with passphrase
# This requires the environment `SSH_AUTH_SOCK` configured pointing
#to your SSH agent which has the private key added
ssh_agent_auth: true
# List of registry credentials
# If you are using a Docker Hub registry, you can omit the `url`
# or set it to `docker.io`
# is_default set to `true` will override the system default
# registry set in the global settings
private_registries:
- url: registry.com
user: Username
password: password
is_default: true
# Bastion/Jump host configuration
bastion_host:
address: x.x.x.x
user: ubuntu
port: 22
ssh_key_path: /home/user/.ssh/bastion_rsa
# or
# ssh_key: |-
# -----BEGIN RSA PRIVATE KEY-----
#
# -----END RSA PRIVATE KEY-----
# Set the name of the Kubernetes cluster
cluster_name: mycluster
# The Kubernetes version used. The default versions of Kubernetes
# are tied to specific versions of the system images.
#
# For RKE v0.2.x and below, the map of Kubernetes versions and their system images is
# located here:
# https://github.com/rancher/types/blob/release/v2.2/apis/management.cattle.io/v3/k8s_defaults.go
#
# For RKE v0.3.0 and above, the map of Kubernetes versions and their system images is
# located here:
# https://github.com/rancher/kontainer-driver-metadata/blob/master/rke/k8s_rke_system_images.go
#
# In case the kubernetes_version and kubernetes image in
# system_images are defined, the system_images configuration
# will take precedence over kubernetes_version.
kubernetes_version: v1.10.3-rancher2
# System Images are defaulted to a tag that is mapped to a specific
# Kubernetes Version and not required in a cluster.yml.
# Each individual system image can be specified if you want to use a different tag.
#
# For RKE v0.2.x and below, the map of Kubernetes versions and their system images is
# located here:
# https://github.com/rancher/types/blob/release/v2.2/apis/management.cattle.io/v3/k8s_defaults.go
#
# For RKE v0.3.0 and above, the map of Kubernetes versions and their system images is
# located here:
# https://github.com/rancher/kontainer-driver-metadata/blob/master/rke/k8s_rke_system_images.go
#
system_images:
kubernetes: rancher/hyperkube:v1.10.3-rancher2
etcd: rancher/coreos-etcd:v3.1.12
alpine: rancher/rke-tools:v0.1.9
nginx_proxy: rancher/rke-tools:v0.1.9
cert_downloader: rancher/rke-tools:v0.1.9
kubernetes_services_sidecar: rancher/rke-tools:v0.1.9
kubedns: rancher/k8s-dns-kube-dns-amd64:1.14.8
dnsmasq: rancher/k8s-dns-dnsmasq-nanny-amd64:1.14.8
kubedns_sidecar: rancher/k8s-dns-sidecar-amd64:1.14.8
kubedns_autoscaler: rancher/cluster-proportional-autoscaler-amd64:1.0.0
pod_infra_container: rancher/pause-amd64:3.1
services:
etcd:
# Custom uid/guid for etcd directory and files
uid: 52034
gid: 52034
# if external etcd is used
# path: /etcdcluster
# external_urls:
# - https://etcd-example.com:2379
# ca_cert: |-
# -----BEGIN CERTIFICATE-----
# xxxxxxxxxx
# -----END CERTIFICATE-----
# cert: |-
# -----BEGIN CERTIFICATE-----
# xxxxxxxxxx
# -----END CERTIFICATE-----
# key: |-
# -----BEGIN PRIVATE KEY-----
# xxxxxxxxxx
# -----END PRIVATE KEY-----
# Note for Rancher v2.0.5 and v2.0.6 users: If you are configuring
# Cluster Options using a Config File when creating Rancher Launched
# Kubernetes, the names of services should contain underscores
# only: `kube_api`.
kube-api:
# IP range for any services created on Kubernetes
# This must match the service_cluster_ip_range in kube-controller
service_cluster_ip_range: 10.43.0.0/16
# Expose a different port range for NodePort services
service_node_port_range: 30000-32767
pod_security_policy: false
# Encrypt secret data at Rest
# Available as of v0.3.1
secrets_encryption_config:
enabled: true
custom_config:
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: k-fw5hn
secret: RTczRjFDODMwQzAyMDVBREU4NDJBMUZFNDhCNzM5N0I=
- identity: {}
# Enable audit logging
# Available as of v1.0.0
audit_log:
enabled: true
configuration:
max_age: 6
max_backup: 6
max_size: 110
path: /var/log/kube-audit/audit-log.json
format: json
policy:
apiVersion: audit.k8s.io/v1 # This is required.
kind: Policy
omitStages:
- "RequestReceived"
rules:
# Log pod changes at RequestResponse level
- level: RequestResponse
resources:
- group: ""
# Resource "pods" doesn't match requests to any subresource of pods,
# which is consistent with the RBAC policy.
resources: ["pods"]
# Using the EventRateLimit admission control enforces a limit on the number of events
# that the API Server will accept in a given time period
# Available as of v1.0.0
event_rate_limit:
enabled: true
configuration:
apiVersion: eventratelimit.admission.k8s.io/v1alpha1
kind: Configuration
limits:
- type: Server
qps: 6000
burst: 30000
# Enable AlwaysPullImages Admission controller plugin
# Available as of v0.2.0
always_pull_images: false
# Add additional arguments to the kubernetes API server
# This WILL OVERRIDE any existing defaults
extra_args:
# Enable audit log to stdout
audit-log-path: "-"
# Increase number of delete workers
delete-collection-workers: 3
# Set the level of log output to debug-level
v: 4
# Note for Rancher 2 users: If you are configuring Cluster Options
# using a Config File when creating Rancher Launched Kubernetes,
# the names of services should contain underscores only:
# `kube_controller`. This only applies to Rancher v2.0.5 and v2.0.6.
kube-controller:
# CIDR pool used to assign IP addresses to pods in the cluster
cluster_cidr: 10.42.0.0/16
# IP range for any services created on Kubernetes
# This must match the service_cluster_ip_range in kube-api
service_cluster_ip_range: 10.43.0.0/16
# Add additional arguments to the kubernetes API server
# This WILL OVERRIDE any existing defaults
extra_args:
# Set the level of log output to debug-level
v: 4
# Enable RotateKubeletServerCertificate feature gate
feature-gates: RotateKubeletServerCertificate=true
# Enable TLS Certificates management
# https://kubernetes.io/docs/tasks/tls/managing-tls-in-a-cluster/
cluster-signing-cert-file: "/etc/kubernetes/ssl/kube-ca.pem"
cluster-signing-key-file: "/etc/kubernetes/ssl/kube-ca-key.pem"
kubelet:
# Base domain for the cluster
cluster_domain: cluster.local
# IP address for the DNS service endpoint
cluster_dns_server: 10.43.0.10
# Fail if swap is on
fail_swap_on: false
# Configure pod-infra-container-image argument
pod-infra-container-image: "k8s.gcr.io/pause:3.2"
# Generate a certificate signed by the kube-ca Certificate Authority
# for the kubelet to use as a server certificate
# Available as of v1.0.0
generate_serving_certificate: true
extra_args:
# Set max pods to 250 instead of default 110
max-pods: 250
# Enable RotateKubeletServerCertificate feature gate
feature-gates: RotateKubeletServerCertificate=true
# Optionally define additional volume binds to a service
extra_binds:
- "/usr/libexec/kubernetes/kubelet-plugins:/usr/libexec/kubernetes/kubelet-plugins"
scheduler:
extra_args:
# Set the level of log output to debug-level
v: 4
kubeproxy:
extra_args:
# Set the level of log output to debug-level
v: 4
# Currently, only authentication strategy supported is x509.
# You can optionally create additional SANs (hostnames or IPs) to
# add to the API server PKI certificate.
# This is useful if you want to use a load balancer for the
# control plane servers.
authentication:
strategy: x509
sans:
- "10.18.160.10"
- "my-loadbalancer-1234567890.us-west-2.elb.amazonaws.com"
# Kubernetes Authorization mode
# Use `mode: rbac` to enable RBAC
# Use `mode: none` to disable authorization
authorization:
mode: rbac
# If you want to set a Kubernetes cloud provider, you specify
# the name and configuration
cloud_provider:
name: aws
# Add-ons are deployed using kubernetes jobs. RKE will give
# up on trying to get the job status after this timeout in seconds..
addon_job_timeout: 30
# Specify network plugin-in (canal, calico, flannel, weave, or none)
network:
plugin: canal
# Specify MTU
mtu: 1400
options:
# Configure interface to use for Canal
canal_iface: eth1
canal_flannel_backend_type: vxlan
# Available as of v1.2.6
canal_autoscaler_priority_class_name: system-cluster-critical
canal_priority_class_name: system-cluster-critical
# Available as of v1.2.4
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationseconds: 300
- key: "node.kubernetes.io/not-ready"
operator: "Exists"
effect: "NoExecute"
tolerationseconds: 300
# Available as of v1.1.0
update_strategy:
strategy: RollingUpdate
rollingUpdate:
maxUnavailable: 6
# Specify DNS provider (coredns or kube-dns)
dns:
provider: coredns
# Available as of v1.1.0
update_strategy:
strategy: RollingUpdate
rollingUpdate:
maxUnavailable: 20%
maxSurge: 15%
linear_autoscaler_params:
cores_per_replica: 0.34
nodes_per_replica: 4
prevent_single_point_failure: true
min: 2
max: 3
# Specify monitoring provider (metrics-server)
monitoring:
provider: metrics-server
# Available as of v1.1.0
update_strategy:
strategy: RollingUpdate
rollingUpdate:
maxUnavailable: 8
# Currently only nginx ingress provider is supported.
# To disable ingress controller, set `provider: none`
# `node_selector` controls ingress placement and is optional
ingress:
provider: nginx
node_selector:
app: ingress
# Available as of v1.1.0
update_strategy:
strategy: RollingUpdate
rollingUpdate:
maxUnavailable: 5
# All add-on manifests MUST specify a namespace
addons: |-
---
apiVersion: v1
kind: Pod
metadata:
name: my-nginx
namespace: default
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
addons_include:
- https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/rook-operator.yaml
- https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/rook-cluster.yaml
- /path/to/manifest
|
3、运行RKE
配置完cluster.yml后,启动rke进行创建集群。
1
2
| su - rancher
rke up --config ./cluster.yml
|
运行完成之后,当前文件夹下会生成额外两份文件。

将以下文件的副本保存在安全位置:
rancher-cluster.yml:RKE 集群配置文件。kube_config_cluster.yml:集群的 Kubeconfig 文件。该文件包含可完全访问集群的凭证。rancher-cluster.rkestate:Kubernetes 集群状态文件。该文件包含集群的当前状态,包括 RKE 配置以及证书
4、查看集群状态
使用kubectl命令查看集群状态,kubectl命令可以从
Rancher Releases Mirror
下载对应版本的二进制包,放到系统的/usr/bin/目录下,也可以使用docker cp命令将kubelet容器中的二进制文件拷贝到本地进行使用。命令如下:
1
| docker cp kubelet:/usr/local/bin/kubectl /usr/local/bin/
|

kubernetes配置文件
使用rke up创建k8s集群时,会生成kube_config_cluster.yml文件,将这个文件拷贝为master节点的~/kube/config文件,该master节点即可获得集群的控制权限。
1
2
3
| cp /PATH/to/kube_config_cluster.yml /root/.kube/config
# 或者从其他master节点上将文件拷贝到本地
scp master01:/root/.kube/config /root/.kube/config
|

四、安装rancher
本文档只针对于HA部署rancher,所以需要使用helm的方式进行安装。
前置要求:
1、内网仓库中已有需要安装rancher版本的容器镜像
2、已有使用rke部署的k8s集群
3、已有helm二进制文件或安装包 #可以在
https://mirror.rancher.cn/#helm/
进行下载3.x版本。
4、已有rancher对应版本的server-charts包 可以在https://mirror.rancher.cn/#server-charts/latest/ 下载对应版本charts包
| 执行主机 | 资源包 | 说明 |
|---|
| master01 | heml | 使用Helm部署rancher,下载地址参考https://mirror.rancher.cn/#helm/ |
| master01 | 自签名证书 | rancher使用自签名证书时需要,脚本参考
生成自签名 SSL 证书
|
| master01 | server chart | Rancher安装Chart |
1、自定义证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
| #!/bin/bash -e
help ()
{
echo ' ================================================================ '
echo ' --ssl-domain: 生成ssl证书需要的主域名,如不指定则默认为www.rancher.local,如果是ip访问服务,则可忽略;'
echo ' --ssl-trusted-ip: 一般ssl证书只信任域名的访问请求,有时候需要使用ip去访问server,那么需要给ssl证书添加扩展IP,多个IP用逗号隔开;'
echo ' --ssl-trusted-domain: 如果想多个域名访问,则添加扩展域名(SSL_TRUSTED_DOMAIN),多个扩展域名用逗号隔开;'
echo ' --ssl-size: ssl加密位数,默认2048;'
echo ' --ssl-cn: 国家代码(2个字母的代号),默认CN;'
echo ' 使用示例:'
echo ' ./create_self-signed-cert.sh --ssl-domain=www.test.com --ssl-trusted-domain=www.test2.com \ '
echo ' --ssl-trusted-ip=1.1.1.1,2.2.2.2,3.3.3.3 --ssl-size=2048 --ssl-date=3650'
echo ' ================================================================'
}
case "$1" in
-h|--help) help; exit;;
esac
if [[ $1 == '' ]];then
help;
exit;
fi
CMDOPTS="$*"
for OPTS in $CMDOPTS;
do
key=$(echo ${OPTS} | awk -F"=" '{print $1}' )
value=$(echo ${OPTS} | awk -F"=" '{print $2}' )
case "$key" in
--ssl-domain) SSL_DOMAIN=$value ;;
--ssl-trusted-ip) SSL_TRUSTED_IP=$value ;;
--ssl-trusted-domain) SSL_TRUSTED_DOMAIN=$value ;;
--ssl-size) SSL_SIZE=$value ;;
--ssl-date) SSL_DATE=$value ;;
--ca-date) CA_DATE=$value ;;
--ssl-cn) CN=$value ;;
esac
done
# CA相关配置
CA_DATE=${CA_DATE:-3650}
CA_KEY=${CA_KEY:-cakey.pem}
CA_CERT=${CA_CERT:-cacerts.pem}
CA_DOMAIN=cattle-ca
# ssl相关配置
SSL_CONFIG=${SSL_CONFIG:-$PWD/openssl.cnf}
SSL_DOMAIN=${SSL_DOMAIN:-'www.rancher.local'}
SSL_DATE=${SSL_DATE:-3650}
SSL_SIZE=${SSL_SIZE:-2048}
## 国家代码(2个字母的代号),默认CN;
CN=${CN:-CN}
SSL_KEY=$SSL_DOMAIN.key
SSL_CSR=$SSL_DOMAIN.csr
SSL_CERT=$SSL_DOMAIN.crt
echo -e "\033[32m ---------------------------- \033[0m"
echo -e "\033[32m | 生成 SSL Cert | \033[0m"
echo -e "\033[32m ---------------------------- \033[0m"
if [[ -e ./${CA_KEY} ]]; then
echo -e "\033[32m ====> 1. 发现已存在CA私钥,备份"${CA_KEY}"为"${CA_KEY}"-bak,然后重新创建 \033[0m"
mv ${CA_KEY} "${CA_KEY}"-bak
openssl genrsa -out ${CA_KEY} ${SSL_SIZE}
else
echo -e "\033[32m ====> 1. 生成新的CA私钥 ${CA_KEY} \033[0m"
openssl genrsa -out ${CA_KEY} ${SSL_SIZE}
fi
if [[ -e ./${CA_CERT} ]]; then
echo -e "\033[32m ====> 2. 发现已存在CA证书,先备份"${CA_CERT}"为"${CA_CERT}"-bak,然后重新创建 \033[0m"
mv ${CA_CERT} "${CA_CERT}"-bak
openssl req -x509 -sha256 -new -nodes -key ${CA_KEY} -days ${CA_DATE} -out ${CA_CERT} -subj "/C=${CN}/CN=${CA_DOMAIN}"
else
echo -e "\033[32m ====> 2. 生成新的CA证书 ${CA_CERT} \033[0m"
openssl req -x509 -sha256 -new -nodes -key ${CA_KEY} -days ${CA_DATE} -out ${CA_CERT} -subj "/C=${CN}/CN=${CA_DOMAIN}"
fi
echo -e "\033[32m ====> 3. 生成Openssl配置文件 ${SSL_CONFIG} \033[0m"
cat > ${SSL_CONFIG} <<EOM
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, serverAuth
EOM
if [[ -n ${SSL_TRUSTED_IP} || -n ${SSL_TRUSTED_DOMAIN} || -n ${SSL_DOMAIN} ]]; then
cat >> ${SSL_CONFIG} <<EOM
subjectAltName = @alt_names
[alt_names]
EOM
IFS=","
dns=(${SSL_TRUSTED_DOMAIN})
dns+=(${SSL_DOMAIN})
for i in "${!dns[@]}"; do
echo DNS.$((i+1)) = ${dns[$i]} >> ${SSL_CONFIG}
done
if [[ -n ${SSL_TRUSTED_IP} ]]; then
ip=(${SSL_TRUSTED_IP})
for i in "${!ip[@]}"; do
echo IP.$((i+1)) = ${ip[$i]} >> ${SSL_CONFIG}
done
fi
fi
echo -e "\033[32m ====> 4. 生成服务SSL KEY ${SSL_KEY} \033[0m"
openssl genrsa -out ${SSL_KEY} ${SSL_SIZE}
echo -e "\033[32m ====> 5. 生成服务SSL CSR ${SSL_CSR} \033[0m"
openssl req -sha256 -new -key ${SSL_KEY} -out ${SSL_CSR} -subj "/C=${CN}/CN=${SSL_DOMAIN}" -config ${SSL_CONFIG}
echo -e "\033[32m ====> 6. 生成服务SSL CERT ${SSL_CERT} \033[0m"
openssl x509 -sha256 -req -in ${SSL_CSR} -CA ${CA_CERT} \
-CAkey ${CA_KEY} -CAcreateserial -out ${SSL_CERT} \
-days ${SSL_DATE} -extensions v3_req \
-extfile ${SSL_CONFIG}
echo -e "\033[32m ====> 7. 证书制作完成 \033[0m"
echo
echo -e "\033[32m ====> 8. 以YAML格式输出结果 \033[0m"
echo "----------------------------------------------------------"
echo "ca_key: |"
cat $CA_KEY | sed 's/^/ /'
echo
echo "ca_cert: |"
cat $CA_CERT | sed 's/^/ /'
echo
echo "ssl_key: |"
cat $SSL_KEY | sed 's/^/ /'
echo
echo "ssl_csr: |"
cat $SSL_CSR | sed 's/^/ /'
echo
echo "ssl_cert: |"
cat $SSL_CERT | sed 's/^/ /'
echo
echo -e "\033[32m ====> 9. 附加CA证书到Cert文件 \033[0m"
cat ${CA_CERT} >> ${SSL_CERT}
echo "ssl_cert: |"
cat $SSL_CERT | sed 's/^/ /'
echo
echo -e "\033[32m ====> 10. 重命名服务证书 \033[0m"
echo "cp ${SSL_DOMAIN}.key tls.key"
cp ${SSL_DOMAIN}.key tls.key
echo "cp ${SSL_DOMAIN}.crt tls.crt"
cp ${SSL_DOMAIN}.crt tls.crt
|
1、⾃定义证书脚本,将以下脚本保存为 create_self-signed-cert.sh
2、⽣成⾃定义证书
1
2
3
4
| ./create_self-signed-cert.sh --ssl-domain=www.test.com --ssl-trusted-domain=www.test2.com \
--ssl-trusted-ip=1.1.1.1,2.2.2.2,3.3.3.3 --ssl-size=2048 --ssl-date=3650
# 验证证书
openssl verify -CAfile cacerts.pem tls.crt
|
2、Helm install
如不使⽤私有gitlab,则可增加以下参数 useBundledSystemChart = true 启⽤镜像⾃带catalog chart
1、将证书放在集群内部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| kubectl create namespace cattle-system
kubectl -n cattle-system create secret tls tls-rancher-ingress \
--cert=tls.crt \
--key=tls.key
kubectl -n cattle-system create secret generic tls-ca \
--from-file=cacerts.pem=./cacerts.pem
helm install rancher \
--namespace cattle-system \
--set service.type=NodePort \
--set service.ports.nodePort=30443 \
--set rancherImage=xxx.xxx.xxx.xxx/cnrancher/rancher \
--set rancherImageTag=v2.6.8 \
--set busyboxImage=xxx.xxx.xxx.xxx/rancher/busybox:latest \
--set ingress.tls.source=secret \
--set privateCA=true \
--set systemDefaultRegistry=xxx.xxx.xxx.xxx \
/root/rancher-2.6.8.tgz
|
2、将证书放在集群内部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| kubectl create namespace cattle-system
kubectl -n cattle-system create secret tls tls-rancher-ingress \
--cert=tls.crt \
--key=tls.key
kubectl -n cattle-system create secret generic tls-ca \
--from-file=cacerts.pem=./cacerts.pem
helm upgrade --install rancher \
--namespace cattle-system \
--set rancherImageTag=v2.6.8 \
--set hostname="rancher-chang.depsecure.com" \
--set busyboxImage=harbor.depsecure.com/rancher/busybox:latest \
--set rancherImage=harbor.depsecure.com/rancher/rancher \
--set service.type="NodePort" \
--set service.ports.nodePort="30080" \
--set tls=external \
--set privateCA=true \
--set systemDefaultRegistry=harbor.depsecure.com \
--set useBundledSystemChart=true \
/root/rancher-2.6.8.tgz
|
helm其他参数参考
常用选项
| 选项 | 默认值 | 描述 |
|---|
bootstrapPassword | " " | string - 为第一个管理员用户设置引导密码。登录后,管理员需要重置密码。如不设置,会使用随机生成的引导密码。 |
hostname | " " | string - 你的 Rancher Server 的完全限定的域名(FQDN) |
ingress.tls.source | “rancher” | string - 从哪里获取 Ingress 的证书- “rancher, letsEncrypt, secret” |
letsEncrypt.email | " " | string - 你的邮箱地址 |
letsEncrypt.environment | “production” | string - 可选项:“staging, production” |
privateCA | false | bool - 如果你的证书是由私有 CA 签发的,把这个值设置为 true |
高级选项
| 选项 | 默认值 | 描述 |
|---|
additionalTrustedCAs | false | bool - 请参见
额外的授信 CA
|
addLocal | “true” | string - 让 Rancher 检测并导入 “local” Rancher Server 集群。注意:此选项在 2.5.0 中已不可用。你可考虑使用 restrictedAdmin 选项,来避免用户修改本地集群。 |
antiAffinity | “preferred” | string - Rancher Pod 的反亲和性规则 - “preferred, required” |
auditLog.destination | “sidecar” | string - 发送审计日志到 Sidecar 容器的控制台或 hostPath 卷 - “sidecar, hostPath” |
auditLog.hostPath | “/var/log/rancher/audit” | string - 主机上的日志文件目标地址(仅当auditLog.destination 的值是 hostPath 时生效) |
auditLog.level | 0 | int - 设置
API 审计日志
等级。0 代表关闭。[0-3] |
auditLog.maxAge | 1 | int - 旧审计日志文件最多可保留的天数(仅当auditLog.destination 的值是 hostPath 时生效) |
auditLog.maxBackup | 1 | int - 审计文件最大可保留的个数(仅当 auditLog.destination 的值是 hostPath 时生效) |
auditLog.maxSize | 100 | int - 在审计日志被轮换前的最大容量,单位是 MB(仅当 auditLog.destination 的值是 hostPath 时生效) |
auditLog.image.repository | “registry.suse.com/bci/bci-micro” | string - 用于收集审计日志的镜像的位置。 |
auditLog.image.tag | “15.4.14.3” | string - 用于收集审计日志的镜像的标签。 |
auditLog.image.pullPolicy | “IfNotPresent” | string - 覆盖 auditLog 镜像的 imagePullPolicy - “Always”、“Never”、“IfNotPresent”。 |
busyboxImage | "" | string - 用于收集审计日志的 busybox 镜像位置。注意:此选项已弃用,请使用 auditLog.image.repository 来控制审计 sidecar 镜像。 |
certmanager.version | "" | string - 设置 cert-manager compatibility |
debug | false | bool - 在 Rancher Server 设置 debug 参数 |
extraEnv | [] | list - 为 Rancher 额外设置环境变量 |
imagePullSecrets | [] | list - 私有镜像仓库凭证的密文名称列表 |
ingress.configurationSnippet | "" | string - 添加额外的 Nginx 配置。可用于代理配置。 |
ingress.extraAnnotations | {} | map - 用于自定义 Ingress 的额外注释 |
ingress.enabled | true | 如果值为 false,Helm 不会安装 Rancher Ingress。你可把值设为 false 以部署你自己的 Ingress。 |
letsEncrypt.ingress.class | "" | string - cert-manager acmesolver ingress 的可选 ingress 类,用于响应 Let’s Encrypt ACME 质询。选项:traefik,nginx。 |
noProxy | | string - 不使用代理的主机名或 IP 地址的逗号分隔列表,如 "127.0.0.0/8,10.0.0.0/8, 172.16.0.0/12,192.168.0.0/16, .svc,.cluster.local,cattle-system.svc",引号内没有空格 |
proxy | "" | string - 给 Rancher 配置的 HTTP[S] 代理 |
rancherImage | “rancher/rancher” | string - Rancher 镜像源 |
rancherImagePullPolicy | “IfNotPresent” | string - 覆盖 Rancher Server 镜像的 imagePullPolicy - “Always”, “Never”, “IfNotPresent” |
rancherImageTag | 和 Chart 版本一致 | string - rancher/rancher 镜像标签 |
replicas | 3 | int - Rancher Server 副本数。如果设为 -1,会根据集群中的可用节点数自动选择 1,2或3。 |
resources | {} | map - Rancher Pod 资源请求和限制 |
restrictedAdmin | false | bool - 如果值为 true,初始的 Rancher 用户访问本地 Kubernetes 集群会受到限制,以避免权限升级。详情请参见
restricted-admin 角色
。 |
systemDefaultRegistry | "" | string - 用于所有系统容器镜像的私有仓库,例如
http://registry.example.com/
|
tls | “ingress” | string - 详情请参见
外部 TLS 终止
。- “ingress, external” |
useBundledSystemChart | false | bool - 选择 Rancher Server 打包的 system-charts。此参数用于离线环境安装。 |
其他参数请参考:https://ranchermanager.docs.rancher.com/zh/getting-started/installation-and-upgrade/installation-references/helm-chart-options#%E5%BC%95%E5%AF%BC%E5%AF%86%E7%A0%81
五、安装nginx代理
1、安装nginx
2、配置Nginx
- Rancher内部挂载证书nginx配置参考
- 将 IP_NODE1,IP_NODE2 和 IP_NODE3 替换为集群中节点的 IP 地址。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| worker_processes 4;
worker_rlimit_nofile 40000;
events {
worker_connections 8192;
}
stream {
upstream rancher_servers_https {
least_conn;
server IP_NODE_1:30443 max_fails=3 fail_timeout=5s;
server IP_NODE_2:30443 max_fails=3 fail_timeout=5s;
server IP_NODE_3:30443 max_fails=3 fail_timeout=5s;
}
server {
listen 30443;
proxy_pass rancher_servers_https;
}
}
|
- 证书挂载⾄nginx配置参考
- 将 IP_NODE1,IP_NODE2 和 IP_NODE3 替换为集群中节点的 IP 地址。
- 将两个出现的 FQDN 替换为 Rancher 的 DNS 名称。
- 将 /certs/fullchain.pem 和 /certs/privkey.pem 分别替换为服务器证书和服务器证书密钥的位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| worker_processes 4;
worker_rlimit_nofile 40000;
events {
worker_connections 8192;
}
http {
upstream rancher {
server IP_NODE_1:80;
server IP_NODE_2:80;
server IP_NODE_3:80;
}
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}
server {
listen 443 ssl http2;
server_name FQDN;
ssl_certificate /certs/fullchain.pem;
ssl_certificate_key /certs/privkey.pem;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://rancher;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# 此项允许执行的 shell 窗口保持开启,最长可达15分钟。不使用此参数的话,默认1分钟后自动关闭。
proxy_read_timeout 900s;
proxy_buffering off;
}
}
server {
listen 80;
server_name FQDN;
return 301 https://$server_name$request_uri;
}
}
|