打開鏈接即可點亮社區Star,照亮技術的前進之路。
Github 地址:https://github.com/secretflow/kuscia
前言
本教程幫助您使用 Docker 組網模式來完成 Kuscia 集羣部署。
<span style="color: red;">注:</span>目前只支持 Kuscia 以 runp 模式以此方式組網。
前置準備
在部署 Kuscia 之前,請確保環境準備齊全,包括所有必要的軟件、資源、操作系統版本和網絡環境等滿足要求,以確保部署過程順暢進行,詳情參考部署要求。
結構圖示
work 127.0.0.1 (示例 IP 以實際為準)
manager 127.0.0.2 (示例 IP 以實際為準)
注:實際生產環境中 Alice 應該對外暴露一個統一的 LB 地址,由 LB 將請求代理至 Alice1 或 Alice2 節點實例。
部署流程
完成 Docker Swarm 組網
Docker Swarm 是 Docker 官方提供的容器編排工具,用於管理和編排多個 Docker 容器,構建和管理容器集羣。
相關描述
Docs 阿里雲
Docs Docker
初始化 swarm
選擇一台主機作為 Docker 管理節點進行初始化,IP 應指定該主機的 IP 地址,Docker 集羣將在此地址監聽。管理端口默認為 2377,也可按需配置(格式:<IP|接口>[:端口]),詳見 Docker 官方文檔。
docker swarm init --advertise-addr 127.0.0.2
執行完上述命令可得到以下描述信息,以及 Token,需要記錄該 Token 字符串,在 worker 節點宿主機執行可加入該 docker swarm
集羣。
Token 遺忘丟失也可以通過 docker swarm join-token manager 命令進行查詢
[root@node-01 ~]# docker swarm init --advertise-addr 127.0.0.2
Swarm initialized: current node (52l3w8qo6drdmvjl6t1z8bf1g) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-37xpluc9bo2sw3ez8yslcgooo8dq082pd5ao0zmtbmuqjcip51-cki6vjrdm931lnvkc5edj075s 127.0.0.2:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
初始化 Docker Network Create
[[Docs Docker] Docker Network Create](https://docs.docker.com/reference/cli/docker/network/create/)
在 docker manager 節點主機上執行:
# -d, --driver: Driver to manage the Network, default is bridge. Need to use overlay here
# --attachable: Enable manual container attachment
docker network create -d overlay --subnet 16.0.0.0/8 --attachable kuscia-exchange-cluster
--subnet 按需(最多多少個容器使用該網段)進行設置,可不設置。kuscia-exchange-cluster network 名字,需要在腳本部署時使用,必須是這個名字。
Worker 節點加入 Swarm
在 worker 節點的宿主機執行 docker swarm init 得到的 join 命令
docker swarm join --token SWMTKN-1-37xpluc9bo2sw3ez8yslcgooo8dq082pd5ao0zmtbmuqjcip51-cki6vjrdm931lnvkc5edj075s 127.0.0.2:2377
執行完 join 命令可得到以下執行結果。
[root@node-02 ~]# docker swarm join --token SWMTKN-1-37xpluc9bo2sw3ez8yslcgooo8dq082pd5ao0zmtbmuqjcip51-cki6vjrdm931lnvkc5edj075s 127.0.0.2:2377
This node joined a swarm as a worker.
也可以在 manager 節點執行 docker node ls 查詢已加入集羣的節點狀態,並且通過 STATUS 字段來判斷節點連接是否正常。
[root@node-01 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
52l3w8qo6drdmvjl6t1z8bf1g * node-01 Ready Active Leader 20.10.24
2af71qccfr8p4po7zqhimkrjr node-02 Ready Active 20.10.24
注:在實際生產中,設置多個管理節點(manager nodes)對於高可用性和容錯是非常關鍵的。詳情請參考:
- Promote or demote a node
- Administer and maintain a swarm of Docker Engines
Kuscia 部署實例
這裏使用 kuscia.sh 實現 P2P 模式中alice節點的雙機雙副本部署。
部署參考:多機部署點對點集羣
# Specify the image version for Kuscia, using version 1.1.0b0 here.
export KUSCIA_IMAGE=secretflow-registry.cn-hangzhou.cr.aliyuncs.com/secretflow/kuscia:1.1.0b0
docker pull ${KUSCIA_IMAGE} && docker run --rm ${KUSCIA_IMAGE} cat /home/kuscia/scripts/deploy/kuscia.sh > kuscia.sh && chmod u+x kuscia.sh
修改配置文件創建使用的 DB
啓動一個 MySQL 8.0+ 版本的容器,設置密碼為 password,並創建數據庫 kine,僅供測試參考。
docker run -d --name alice-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=kine mysql:8
初始化 Kuscia 配置文件
Kuscia init 參數請參考配置文件,命令示例如下:
docker run -it --rm ${KUSCIA_IMAGE} kuscia init --mode autonomy --domain "alice" --runtime "runp" --datastore-endpoint "mysql://root:password@tcp(xx.xx.xx.xx:3306)/kine" > autonomy_alice.yaml
部署 Kuscia
部署端口請參考這裏,命令示例如下:
./kuscia.sh start -c ./autonomy_alice.yaml -p 20000 -q 20001 -k 20010 -g 20011 -a none -m 8G --cluster
如果部署時報錯 kuscia-exchange-cluster 已經存在且不是預期的類型等類似錯誤,需要將 network 手動刪除後重新部署:
# Delete network
docker network rm kuscia-exchange-cluster
後續部署其它實例需要將 autonomy_alice.yaml 拷貝過去而不是重新生成
後續步驟可參考官網進行配置:多機部署點對點集羣
按照順序完成:配置證書 > 配置路由授權 > 拉起示例任務
注:使用 LB 代理時,路由授權地址使用代理服務地址建立。
LB 示例(Nginx)
以 Nginx 為例
拉起 Nginx 服務通過 8080 端口代理多副本中 alice 的宿主機地址與端口。
# Pull the latest Nginx image
docker pull nginx:latest
修改配置文件
-
從 Nginx 鏡像中拷貝配置文件至宿主機當前命令目錄
docker run --rm nginx:latest cat /etc/nginx/nginx.conf > ./nginx.conf -
修改配置文件
參考官網中 Nginx 配置示例修改配置文件中 http 代理塊,如果 Kuscia 需要使用 https 訪問,在修改的配置中
server塊中使用
https,並註釋原有 http 和打開 https 註釋部分
Nginx 代理參數配置示例
- Nginx 代理參數配置示例,詳情請參考這裏。
啓動並掛載配置
使用 Docker 拉起 Nginx 服務,並把修改的配置文件掛載至容器內
docker run -it -d --name lb-nginx -p 8080:80 -v /path/to/nginx.conf:/etc/nginx/nginx.conf nginx
驗證代理服務
多次請求代理服務,返回的 kuscia-error-message 信息是隨機的。
curl -kv http://127.0.0.1:8080
完整配置文件參考
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $http_host;
proxy_pass_request_headers on;
# To allow special characters in headers
ignore_invalid_headers off;
# Maximum number of requests through one keep-alive connection
keepalive_requests 1000;
keepalive_timeout 20m;
client_max_body_size 2m;
# To disable buffering
proxy_buffering off;
proxy_request_buffering off;
upstream backend {
# If kuscia is deployed to multiple machines, use the ip of each kuscia here
server 127.0.0.2:11080 weight=1 max_fails=5 fail_timeout=60s;
server 127.0.0.1:11080 weight=1 max_fails=5 fail_timeout=60s;
# Nginx_upstream_check_module can support upstream health check with Nginx
# Please refer to the document: https://github.com/yaoweibin/nginx_upstream_check_module/tree/master/doc
# check interval=3000 rise=2 fall=5 timeout=1000 type=http;
keepalive 32;
keepalive_timeout 600s;
keepalive_requests 1000;
}
server {
location / {
# Change the content of the comment based on the http/https mode that the proxy service needs to access
# proxy_read_timeout 10m;
# proxy_pass http://backend;
# Connect to kuscia with https
proxy_pass https://backend;
proxy_ssl_verify off;
proxy_set_header Host $host;
}
}
# This corresponds to case 3 above, kuscia needs to configure a proxy when accessing the internet
# The port must be different with the reverse proxy port
# server {
# resolver $dns_host_ip;
# location / {
# proxy_pass ${The address provided by the other organization};
# }
# }
}