Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

四模 k3s: 高可用部署 #344

Open
Bpazy opened this issue Dec 15, 2024 · 2 comments
Open

四模 k3s: 高可用部署 #344

Bpazy opened this issue Dec 15, 2024 · 2 comments
Labels

Comments

@Bpazy
Copy link
Owner

Bpazy commented Dec 15, 2024

k3s 单节点部署时,采用的是 sqlite 存储,当多 master 节点部署时,会采用嵌入的 etcd 存储。

由于我不具有 etcd 运维经验,所以这里直接把 k3s 迁移到 MySQL 以实现 k3s 集群的高可用部署。

@Bpazy Bpazy added the K3S label Dec 15, 2024
@Bpazy
Copy link
Owner Author

Bpazy commented Dec 15, 2024

从 SQLite 迁移到 MySQL

注意此方案没有实践,我担心 k3s 会在 sqlite 之外写入其他信息。

k3s 内置只有 sqlite 到 etcd 迁移的功能,所以这里需要自行将 sqlite 的数据迁移到 MySQL 中。

第一步:创建 MySQL 数据库和用户

$ mysql -h ************* -u root -p

mysql> create database kubernetes;
Query OK, 1 row affected (0.02 sec)

mysql> CREATE USER 'k3s'@'%' IDENTIFIED BY 'YOUR_PASSWORD';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'k3s'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.02 sec)

这里创建了名为 kubernetes 的 schema 和名为 k3s 的用户,权限暂且不管,我还无需细致的权限管理。

第二步:利用启动新 k3s Server 节点来创建表结构

curl https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
  --datastore-endpoint="mysql://k3s:password@tcp(192.168.31.41:3306)/kubernetes"

第三步:将 SQLite 数据迁移到 MySQL 中

利用的工具是 sqlite3-to-mysql,先安装:

pip install sqlite3-to-mysql

开始迁移:

@Bpazy
Copy link
Owner Author

Bpazy commented Dec 16, 2024

全新部署 k3s 高可用集群

安装 k3s

安装 K3s:

$ curl https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
  --datastore-endpoint="mysql://username:password@tcp(*************:3306)/kubernetes"

验证集群状态:

$ sudo kubectl get no
NAME             STATUS   ROLES                  AGE   VERSION
pve-gmk-ubuntu   Ready    control-plane,master   51s   v1.31.3+k3s1

为了避免每次使用 kubectl 需要 sudo 权限,这里配置 kubeconfig 权限:

$ cat >> ~/.bashrc <<EOF
export KUBECONFIG=~/.kube/config
EOF

$ sudo k3s kubectl config view --raw > "$KUBECONFIG"
$ chmod 600 "$KUBECONFIG"

不配置的话会有以下报错:

$ kubectl get no
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode or --write-kubeconfig-group to modify kube config permissions 
error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied

安装 helm

sudo snap install helm --classic

使用 Tailscale 组网

安装 Tailscale:

curl -fsSL https://tailscale.com/install.sh | sh

用于子网内有多设备都启用了 tailscale,所以我这里需要特殊配置下:

可参考: #201 (comment)

ip rule add to 192.168.31.0/24 priority 2500 lookup main

启动 tailscale:

sudo tailscale up --accept-routes

编辑 k3s config.yaml:

$ sudo vim /etc/rancher/k3s/config.yaml
disable:
  - "traefik"
  - "servicelb"

vpn-auth: name=tailscale,joinKey=********

重启 k3s:

$ sudo systemctl restart k3s
$ kubectl get no -o wide
NAME             STATUS   ROLES                  AGE    VERSION        INTERNAL-IP      EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
pve-gmk-ubuntu   Ready    control-plane,master   162m   v1.31.3+k3s1   100.66.179.112   <none>        Ubuntu 24.04.1 LTS   6.8.0-41-generic   containerd://1.7.23-k3s2

可以看到 IP 已经变成 Tailscale 的 IP 了。

安装 csi-driver-smb

helm repo add csi-driver-smb https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/charts
helm install csi-driver-smb csi-driver-smb/csi-driver-smb --namespace kube-system --version v1.14.0

如果安装遇到了网络问题,可以参考: #324

集群加入新的 Server 节点

因为 K3s Server 节点默认是可调度的,所以 HA K3s 集群不需要 Agent 节点,这里先记录加入 Server 节点方法。

先移除老 k3s 集群的 node:

$ kubectl delete no nhan-ubuntu
node "nhan-ubuntu" deleted

然后卸载要加入 HA k3s 集群 node 的 k3s(为了升级最新版):

$ /usr/local/bin/k3s-uninstall.sh

如果是卸载 agent 节点用这个命令:

$ /usr/local/bin/k3s-agent-uninstall.sh

最后加入新集群:

$ curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
   --token=这是HA k3s Token \
   --datastore-endpoint="mysql://username:password@tcp(*************:3306)/kubernetes"

[INFO]  Finding release for channel stable
[INFO]  Using v1.31.3+k3s1 as release
[INFO]  Downloading hash rancher-mirror.rancher.cn/k3s/v1.31.3-k3s1/sha256sum-amd64.txt
[INFO]  Downloading binary rancher-mirror.rancher.cn/k3s/v1.31.3-k3s1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Skipping installation of SELinux RPM
[INFO]  Skipping /usr/local/bin/kubectl symlink to k3s, already exists
[INFO]  Skipping /usr/local/bin/crictl symlink to k3s, already exists
[INFO]  Skipping /usr/local/bin/ctr symlink to k3s, command exists in PATH at /usr/bin/ctr
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

token 可以在任意节点执行以下命令获取:

$ sudo cat cat /var/lib/rancher/k3s/server/token
K1098f789f2a35972xxxxxxxxxx81ad37a7a554b2xxxxxxxb3d2cbfaf34::server:e5779xxxxxxxxxx8b265c0c8a809

检查成果:

kubectl get no
NAME             STATUS   ROLES                  AGE    VERSION
nhan-ubuntu      Ready    control-plane,master   17s    v1.31.3+k3s1
pve-gmk-ubuntu   Ready    control-plane,master   3d2h   v1.31.3+k3s1

有几个配置标志在所有 Server 节点中必须是相同的:

网络相关标志:--cluster-dns--cluster-domain--cluster-cidr--service- cidr
控制某些组件部署的标志:--disable-helm-controller--disable-kube-proxy--disable-network-policy 和任何传递给 --disable 的组件
功能相关标志:--secrets-encryption

集群加入 Agent 节点

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - agent \
  --token=K1098f789f2a35972xxxxxxxxxx81ad37a7a554b2xxxxxxxb3d2cbfaf34::server:e5779xxxxxxxxxx8b265c0c8a809
 \
  --server https://192.168.31.41:6443

--server 指定任意 Server 节点就可以。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant