Skip to content

Commit

Permalink
Update Chinese docs
Browse files Browse the repository at this point in the history
Signed-off-by: vickyhella <vickyhella@hotmail.com>
  • Loading branch information
vickyhella authored and brandond committed Aug 14, 2023
1 parent 77e4886 commit c6ad9d0
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 40 deletions.
14 changes: 13 additions & 1 deletion i18n/zh/code.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"theme.ErrorPageContent.tryAgain": {
"message": "重试",
"description": "The label of the button to try again when the page crashed"
"description": "The label of the button to try again rendering when the React error boundary captures an error"
},
"theme.NotFound.title": {
"message": "找不到页面",
Expand Down Expand Up @@ -400,5 +400,17 @@
"theme.docs.sidebar.navAriaLabel": {
"message": "文档侧边栏",
"description": "The ARIA label for the sidebar navigation"
},
"theme.SearchPage.searchContext.everywhere": {
"message": "所有位置"
},
"theme.SearchBar.noResultsText": {
"message": "没有找到任何文档"
},
"theme.SearchBar.seeAllOutsideContext": {
"message": "查看 {context} 外的所有结果"
},
"theme.SearchBar.searchInContext": {
"message": "查看 {context} 内的所有结果"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
title: 集群负载均衡器
weight: 30
---


本节介绍如何在高可用性 (HA) K3s 集群的 Server 节点前安装外部负载均衡器。此处提供了两个示例:Nginx 和 HAProxy。

:::tip
不要混淆外部负载均衡器与嵌入式 ServiceLB,后者是一个嵌入式控制器,允许在不部署第三方负载均衡器控制器的情况下使用 Kubernetes LoadBalancer Service。有关更多详细信息,请参阅 [Service Load Balancer](../networking/networking.md#service-load-balancer)

外部负载均衡器可用于提供固定的注册地址来注册节点,或用于从外部访问 Kubernetes API Server。为了公开 LoadBalancer Service,外部负载均衡器可以与 ServiceLB 一起使用或代替 ServiceLB,但在大多数情况下,替代负载均衡器控制器(例如 MetalLB 或 Kube-VIP)是更好的选择。
:::

## 先决条件

本示例中的所有节点都运行 Ubuntu 20.04。

这两个示例假设已在 3 个节点上安装了[具有嵌入式 etcd 的 HA K3s 集群](../datastore/ha-embedded.md)

每个 K3s Server 配置有:
```yaml
# /etc/rancher/k3s/config.yaml
token: lb-cluster-gd
tls-san: 10.10.10.100
```
节点的主机名和 IP 为:
* server-1: `10.10.10.50`
* server-2: `10.10.10.51`
* server-3: `10.10.10.52`


用于负载均衡的两个节点配置了以下主机名和 IP:
* lb-1: `10.10.10.98`
* lb-2: `10.10.10.99`

存在三个附加节点,其主机名和 IP 为:
* agent-1: `10.10.10.101`
* agent-2: `10.10.10.102`
* agent-3: `10.10.10.103`

## 设置负载均衡器
<Tabs>
<TabItem value="HAProxy" default>

[HAProxy](http://www.haproxy.org/) 是一个提供 TCP 负载均衡器的开源选项。它还支持负载均衡器本身的 HA,确保各个级别的冗余。有关详细信息,请参阅 [HAProxy 文档](http://docs.haproxy.org/2.8/intro.html)。

此外,我们将使用 KeepAlived 来生成用于访问集群的虚拟 IP (VIP)。有关详细信息,请参阅 [KeepAlived 文档](https://www.keepalived.org/manpage.html)。



1) 安装 HAProxy 和 KeepAlived:

```bash
sudo apt-get install haproxy keepalived
```

2) 将以下内容添加到 lb-1 和 lb-2 上的 `/etc/haproxy/haproxy.cfg` 中:

```
frontend k3s-frontend
bind *:6443
mode tcp
option tcplog
default_backend k3s-backend

backend k3s-backend
mode tcp
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s
server server-1 10.10.10.50:6443 check
server server-2 10.10.10.51:6443 check
server server-3 10.10.10.52:6443 check
```
3) 将以下内容添加到 lb-1 和 lb-2 上的 `/etc/keepalived/keepalived.conf` 中:
```
vrrp_script chk_haproxy {
script 'killall -0 haproxy' # faster than pidof
interval 2
}

vrrp_instance haproxy-vip {
interface eth1
state <STATE> # MASTER on lb-1, BACKUP on lb-2
priority <PRIORITY> # 200 on lb-1, 100 on lb-2

virtual_router_id 51

virtual_ipaddress {
10.10.10.100/24
}

track_script {
chk_haproxy
}
}
```
6) 在 lb-1 和 lb-2 上重启 HAProxy 和 KeepAlived:
```bash
systemctl restart haproxy
systemctl restart keepalived
```

5) 在 agent-1、agent-2、agent-3 上执行以下命令来安装 K3s 并加入集群:

```bash
curl -sfL https://get.k3s.io | K3S_TOKEN=lb-cluster-gd sh -s - agent --server https://10.10.10.100:6443
```

你现在可以从 Server 节点使用 `kubectl` 与集群交互。
```bash
root@server-1 $ k3s kubectl get nodes -A
NAME STATUS ROLES AGE VERSION
agent-1 Ready <none> 32s v1.27.3+k3s1
agent-2 Ready <none> 20s v1.27.3+k3s1
agent-3 Ready <none> 9s v1.27.3+k3s1
server-1 Ready control-plane,etcd,master 4m22s v1.27.3+k3s1
server-2 Ready control-plane,etcd,master 3m58s v1.27.3+k3s1
server-3 Ready control-plane,etcd,master 3m12s v1.27.3+k3s1
```

</TabItem>

<TabItem value="Nginx">

## Nginx 负载均衡器

:::caution
Nginx 本身不支持高可用性 (HA) 配置。如果设置 HA 集群,在 K3 前面使用单个负载均衡器将重新引入单一故障点。
:::

[Nginx 开源](http://nginx.org/)提供 TCP 负载均衡器。有关详细信息,请参阅[使用 Nginx 作为 HTTP 负载均衡器](https://nginx.org/en/docs/http/load_balancing.html)

1) 在 lb-1 上创建一个包含以下内容的 `nginx.conf` 文件:

```
events {}
stream {
upstream k3s_servers {
server 10.10.10.50:6443;
server 10.10.10.51:6443;
server 10.10.10.52:6443;
}
server {
listen 6443;
proxy_pass k3s_servers;
}
}
```

2) 在 lb-1 上运行 Nginx 负载均衡器:

使用 Docker:

```bash
docker run -d --restart unless-stopped \
-v ${PWD}/nginx.conf:/etc/nginx/nginx.conf \
-p 6443:6443 \
nginx:stable
```

或者[安装 Nginx](https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/) 然后运行:

```bash
cp nginx.conf /etc/nginx/nginx.conf
systemctl start nginx
```

3) 在 agent-1、agent-2、agent-3 上执行以下命令来安装 K3s 并加入集群:

```bash
curl -sfL https://get.k3s.io | K3S_TOKEN=lb-cluster-gd sh -s - agent --server https://10.10.10.99:6443
```

你现在可以从 Server 节点使用 `kubectl` 与集群交互。
```bash
root@server1 $ k3s kubectl get nodes -A
NAME STATUS ROLES AGE VERSION
agent-1 Ready <none> 30s v1.27.3+k3s1
agent-2 Ready <none> 22s v1.27.3+k3s1
agent-3 Ready <none> 13s v1.27.3+k3s1
server-1 Ready control-plane,etcd,master 4m49s v1.27.3+k3s1
server-2 Ready control-plane,etcd,master 3m58s v1.27.3+k3s1
server-3 Ready control-plane,etcd,master 3m16s v1.27.3+k3s1
```
</TabItem>
</Tabs>
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,20 @@ HA 嵌入式 etcd 集群必须由奇数个 Server 节点组成,以便 etcd 维
* 可选:**固定注册地址**,供 Agent 节点注册到集群

首先,启动一个带有 `cluster-init` 标志的 Server 节点来启用集群和一个令牌,该令牌将作为共享 secret,用于将其他 Server 加入集群。

```bash
curl -sfL https://get.k3s.io | K3S_TOKEN=SECRET sh -s - server --cluster-init
curl -sfL https://get.k3s.io | K3S_TOKEN=SECRET sh -s - server \
--cluster-init \
--tls-san=<FIXED_IP> # Optional, needed if using a fixed registration address
```

:::note
中国用户,可以使用以下方法加速安装:
```
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_TOKEN=SECRET sh -s - server --cluster-init
```
:::

启动第一台服务器后,使用共享 secret 将第二台和第三台服务器加入集群:
```bash
curl -sfL https://get.k3s.io | K3S_TOKEN=SECRET sh -s - server --server https://<ip or hostname of server1>:6443
curl -sfL https://get.k3s.io | K3S_TOKEN=SECRET sh -s - server \
--server https://<ip or hostname of server1>:6443 \
--tls-san=<FIXED_IP> # Optional, needed if using a fixed registration address
```

:::note
中国用户,可以使用以下方法加速安装:
```
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_TOKEN=SECRET sh -s - server --server https://<ip or hostname of server1>:6443
```
:::

检查第二个和第三个服务器是否已加入集群:

```bash
Expand All @@ -64,9 +55,12 @@ curl -sfL https://get.k3s.io | K3S_TOKEN=SECRET sh -s - agent --server https://<
* 功能相关标志:`--secrets-encryption`

## 现有的单节点集群

:::info 版本
[v1.22.2+k3s1](https://github.com/k3s-io/k3s/releases/tag/v1.22.2%2Bk3s1) 起可用
:::

如果你有一个使用默认嵌入式 SQLite 数据库的现有集群,你可以通过使用 `--cluster-init` 标志重新启动你的 K3s server,从而将其转换为 etcd。完成此操作后,你将能够如上所述添加其他实例。

如果由于节点已经初始化或加入了一个集群,导致在磁盘上发现一个 etcd 数据存储,那么数据存储参数(`--cluster-init``--server``--datastore-endpoint` 等)将被忽略。

> **重要提示**:K3s v1.22.2 及更高版本支持将 SQLite 迁移到 etcd。如果你将 `--cluster-init` 添加到现有 server,旧版本将创建一个新的空数据存储。
35 changes: 15 additions & 20 deletions i18n/zh/docusaurus-plugin-content-docs/current/datastore/ha.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ K3s 需要两个或更多的 Server 节点来实现 HA 配置。有关最低主
curl -sfL https://get.k3s.io | sh -s - server \
--token=SECRET \
--datastore-endpoint="mysql://username:password@tcp(hostname:3306)/database-name"
--tls-san=<FIXED_IP> # Optional, needed if using a fixed registration address
```

:::note
中国用户,可以使用以下方法加速安装:
```
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
--token=SECRET \
--datastore-endpoint="mysql://username:password@tcp(hostname:3306)/database-name"
```
:::

根据数据库类型的不同,数据存储端点的格式也不同。有关详细信息,请参阅[数据存储端点格式](../datastore/datastore.md#数据存储端点格式和功能)

要在启动 server 节点时配置 TLS 证书,请参阅[数据存储配置指南](../datastore/datastore.md#外部数据库配置参数)
Expand Down Expand Up @@ -91,24 +83,27 @@ curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIR
你需要备份 token 的值,因为恢复备份和添加节点时都需要该 token。以前,K3s 在使用外部 SQL 数据存储时不强制使用 token。
:::

### 4. 可选:加入 Agent 节点

因为 K3s Server 节点默认是可调度的,所以 HA K3s 集群不需要 Agent 节点。但是,你可能希望使用专门的 Agent 节点来运行应用程序和服务。
### 4. 可选:配置固定的注册地址

在 HA 集群中加入 Agent 节点与在单个 Server 集群中加入 Agent 节点是一样的。你只需要指定 Agent 应该注册的 URL(server IP 之一或固定注册地址)和要使用的 Token 即可。

```bash
K3S_TOKEN=SECRET k3s agent --server https://server-or-fixed-registration-address:6443
```

### 5. 可选:配置固定的注册地址

Agent 节点需要一个 URL 来注册。这可以是任何 server 节点的 IP 或主机名,但在许多情况下,这些节点可能会随着时间的推移而改变。例如,如果你在支持缩放组的云中运行集群,你可能会纵向缩放 Server 节点组,导致节点被创建和销毁,从而导致 Server 节点集的 IP 发生改变。因此,你应该在 Server 节点前面有一个稳定的端点,而且它不会随时间推移而改变。你可以使用许多方法来设置此端点,例如:
Agent 节点需要一个 URL 来注册。这可以是任何 server 节点的 IP 或主机名,但在许多情况下,这些节点可能会随着时间的推移而改变。例如,如果在支持扩展组的云上运行集群,则可能会随着时间的推移创建和销毁节点,从而更改为与初始 Server 节点集不同的 IP。最好在 Server 节点前面有一个不会随时间变化的稳定端点。你可以使用许多方法来设置此端点,例如:

* 4 层 (TCP) 负载均衡器
* 轮询 DNS
* 虚拟或弹性 IP 地址

有关示例配置,请参阅[集群负载均衡器](./cluster-loadbalancer.md)

这个端点也可以用来访问 Kubernetes API。因此,你可以修改 [kubeconfig](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/) 文件来指向它,而不是特定的节点。

要避免此类配置中的证书错误,请使用 `--tls-san YOUR_IP_OR_HOSTNAME_HERE` 选项来配置 Server。这个选项在 TLS 证书中增加了一个额外的主机名或 IP 作为 Subject Alternative Name,如果你想通过 IP 和主机名访问,可以多次指定。

### 5. 可选:加入 Agent 节点

因为 K3s Server 节点默认是可调度的,所以 HA K3s 集群不需要 Agent 节点。但是,你可能希望使用专门的 Agent 节点来运行应用程序和服务。

在 HA 集群中加入 Agent 节点与在单个 Server 集群中加入 Agent 节点是一样的。你只需要指定 Agent 应该注册的 URL(server IP 之一或固定注册地址)和要使用的 Token 即可。

```bash
K3S_TOKEN=SECRET k3s agent --server https://server-or-fixed-registration-address:6443
```
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ spec:
你可以通过集群内的 Kubernetes APIServer 匿名访问 `/var/lib/rancher/k3s/server/static/` 中的内容。此 URL 可以使用 `spec.chart` 字段中的特殊变量 `%{KUBERNETES_API}%` 进行模板化。例如,打包的 Traefik 组件通过 `https://%{KUBERNETES_API}%/static/charts/traefik-12.0.000.tgz` 加载 Chart。

:::note
`name` 字段需要遵循 Helm Chart 命名约定。如需了解更多信息,请参阅 [Helm 最佳实践文档](https://helm.sh/docs/chart_best_practices/conventions/#chart-names)。
`name` 字段需要遵循 Helm Chart 命名约定。有关更多信息,请参阅 [Helm 最佳实践文档](https://helm.sh/docs/chart_best_practices/conventions/#chart-names)。
:::

### 使用 HelmChartConfig 自定义打包组件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ curl -fsSL https://tailscale.com/install.sh | sh
--vpn-auth-file=$PATH_TO_FILE
```
或者,如果你有自己的 Tailscale 服务器(例如 headscale),则可以通过将 `,controlServerURL=$URL` 附加到 vpn-auth 参数来连接它。
:::caution 警告
如果你计划使用同一个 tailscale 网络运行多个 K3s 集群,请创建适当的 [ACL](https://tailscale.com/kb/1018/acls/) 来避免 IP 冲突,或为每个集群使用不同的 podCIDR 子网。
Expand Down
4 changes: 4 additions & 0 deletions i18n/zh/docusaurus-theme-classic/navbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"item.label.GitHub": {
"message": "GitHub",
"description": "Navbar item with label GitHub"
},
"logo.alt": {
"message": "logo",
"description": "The alt text of navbar logo"
}
}

0 comments on commit c6ad9d0

Please sign in to comment.