博客 / 詳情

返回

Kusica如何多機部署中心化進羣【隱語Secretflow】

打開鏈接即可點亮社區Star,照亮技術的前進之路。

Github 地址:https://github.com/secretflow/kuscia

前言

本教程幫助您在多台機器上使用 中心化組網模式 來部署 Kuscia 集羣。

前置準備

在部署 Kuscia 之前,請確保環境準備齊全,包括所有必要的軟件、資源、操作系統版本和網絡環境等滿足要求,以確保部署過程順暢進行,詳情參考部署要求。

部署流程(基於TOKEN認證)

部署 master 節點

登錄到安裝 master 的機器上,假設對外 IP 是 1.1.1.1。

指定 Kuscia 版本:

# The Kuscia image used, here using version 1.1.0b0 image
export KUSCIA_IMAGE=secretflow-registry.cn-hangzhou.cr.aliyuncs.com/secretflow/kuscia:1.1.0b0

指定 SecretFlow 版本:

# The Secretflow image used, here using version 1.11.0b1 image
export SECRETFLOW_IMAGE=secretflow-registry.cn-hangzhou.cr.aliyuncs.com/secretflow/secretflow-lite-anolis8:1.11.0b1

獲取部署腳本,部署腳本會下載到當前目錄:

docker pull ${KUSCIA_IMAGE} && docker run --rm ${KUSCIA_IMAGE} cat /home/kuscia/scripts/deploy/kuscia.sh > kuscia.sh && chmod u+x kuscia.sh

生成 master 節點的配置文件,kuscia init 參數請參考 Kuscia 配置文件:

# The --domain parameter passes the master node ID, DomainID must be globally unique and conform to the RFC 1123 label name specification. For details, refer to: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names. In a production environment, it is recommended to use the format: company-name-department-name-node-name, e.g., mycompany-secretflow-master
docker run -it --rm ${KUSCIA_IMAGE} kuscia init --mode master --domain "mycompany-secretflow-master" > kuscia_master.yaml 2>&1 || cat kuscia_master.yaml

建議檢查生成的文件,避免配置文件錯誤導致的部署啓動問題。

啓動 master,默認會在當前目錄下創建 ${USER}-kuscia-master/{data、logs} 用來存儲 master 的數據、日誌:

# The -p parameter passes the port that the master container maps to the host, ensuring it does not conflict with existing ports on the host.
# The -k parameter passes the HTTP port that the master container's KusciaAPI maps to the host, ensuring it does not conflict with existing ports on the host.
# The -a parameter specifies the engine image to be automatically imported. -a none: do not automatically import the engine image, -a secretflow (default): automatically import the secretflow engine image.
# The -m or --memory-limit parameter sets an appropriate memory limit for the node container. For example, '-m 4GiB or --memory-limit=4GiB' indicates a maximum memory limit of 4GiB, '-m -1 or --memory-limit=-1' indicates no limit. If not set, the default is 2GiB for the master node, 4GiB for the lite node, and 6GiB for the autonomy node.
./kuscia.sh start -c kuscia_master.yaml -p 18080 -k 18081

:::{tip}

  • 節點 ID 需要全局唯一併且符合 RFC 1123 標籤名規則要求,詳情請參考這裏。defaultkube-systemkube-publickube-node-leasemaster 以及 cross-domain 為 Kuscia 預定義的節點 ID,不能被使用。
  • 目前 kuscia.sh 腳本僅支持導入 Secretflow 鏡像,scql、serving 以及其他自定義鏡像請移步至註冊自定義算法鏡像
  • 如果 master 的入口網絡存在網關時,為了確保節點與 master 之間通信正常,需要網關符合一些要求,詳情請參考這裏。
  • master 節點默認使用 SQLite 作為存儲,如果生產部署,需要配置鏈接到 MySQL 數據庫的連接串,具體配置可以參考這裏
  • 需要對合作方暴露的 Kuscia 端口,可參考 Kuscia 端口介紹
  • 非 root 用户部署請參考這裏
  • 升級引擎鏡像請參考指南
    :::

建議使用 curl -kvvv https://ip:port 檢查一下是否訪問能通,正常情況下返回的 HTTP 錯誤碼是 401,內容是:unauthorized。
示例如下:

*   Trying 127.0.0.1:18080...
* Connected to 127.0.0.1 (127.0.0.1) port 18080 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=kuscia-system_ENVOY_EXTERNAL
*  start date: Sep 14 07:42:47 2023 GMT
*  expire date: Jan 30 07:42:47 2051 GMT
*  issuer: CN=Kuscia
*  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
> GET / HTTP/1.1
> Host: 127.0.0.1:18080
> User-Agent: curl/7.82.0
> Accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< x-accel-buffering: no
< content-length: 13
< content-type: text/plain
< kuscia-error-message: Domain kuscia-system.root-kuscia-master<--127.0.0.1 return http code 401.
< date: Fri, 15 Sep 2023 02:50:39 GMT
< server: kuscia-gateway
<
* Connection #0 to host 127.0.0.1 left intact
{"domain":"alice","instance":"xyz","kuscia":"v0.1","reason":"unauthorized."}

Tips

本文後續還會經常使用到 docker exec -it ${USER}-kuscia-master xxxxx 類似的命令。建議以如下方式簡化輸入。

alias km="docker exec -it ${USER}-kuscia-master"

後續相關命令可以簡化為 km xxxxx

部署 lite 節點

您可以選擇在任一台機器上部署 lite 節點(下文以 alice、bob 為例)。

部署 lite 節點 Alice

在部署 Alice 節點之前,我們需要在 master 註冊 Alice 節點,並獲取到部署時需要用到的 Token 並存入環境變量。

執行以下命令,完成節點註冊並從返回中得到 Token 並存入環境變量:

export ALICE_TOKEN=$(docker exec -i ${USER}-kuscia-master sh scripts/deploy/add_domain_lite.sh alice)
echo "Alice 的部署 Token: ${ALICE_TOKEN}"

輸出示例:

Alice 的部署 Token: abcdefg

如果 Token 遺忘了,可以通過該命令重新獲取

export ALICE_TOKEN=$(docker exec -it ${USER}-kuscia-master kubectl get domain alice -o=jsonpath='{.status.deployTokenStatuses[?(@.state=="unused")].token}')
echo "Alice 的部署 Token: ${ALICE_TOKEN}"

輸出示例:

Alice 的部署 Token: abcdefg

接下來,登錄到安裝 Alice 的機器上,假設對外暴露的 IP 是 2.2.2.2。

指定 Kuscia 版本:

# The Kuscia image used, here using version 1.1.0b0 image
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

生成 Alice 節點的配置文件:

# The --domain parameter passes the node ID.
# The --lite-deploy-token parameter passes the Token for node deployment.
# The --master-endpoint parameter passes the master container's exposed https://IP:PORT. For example, if the master is exposed at IP 1.1.1.1 and port 18080, it would be https://1.1.1.1:18080.
docker run -it --rm ${KUSCIA_IMAGE} kuscia init --mode lite --domain "alice" --master-endpoint "https://1.1.1.1:18080" --lite-deploy-token "${ALICE_TOKEN}" > lite_alice.yaml 2>&1 || cat lite_alice.yaml

啓動 Alice,默認會在當前目錄下創建 ${USER}-kuscia-lite-alice/data 目錄用來存放 alice 的數據:

# The -p parameter passes the port that the node container maps to the host, ensuring it does not conflict with existing ports on the host.
# The -k parameter passes the HTTP port that the lite container's KusciaAPI maps to the host, ensuring it does not conflict with existing ports on the host.
./kuscia.sh start -c lite_alice.yaml -p 28080 -k 28081
如果 master 與多個 lite 節點部署在同一個物理機上,可以用 -p -k -g -q -x 參數指定下端口號(例如:./kuscia.sh start -c lite_alice.yaml -p 28080 -k 28081 -g 28082 -q 28083 -x 28084),防止出現端口衝突。

部署 lite 節點 bob

在部署 Bob 節點之前,我們需要在 master 註冊 Bob 節點,並獲取到部署時需要用到的 Token 並存入環境變量。

執行以下命令,完成節點註冊並從返回中得到 Token:

export BOB_TOKEN=$(docker exec -i ${USER}-kuscia-master sh scripts/deploy/add_domain_lite.sh bob)
echo "Bob 的部署 Token: ${BOB_TOKEN}"

輸出示例:

Bob 的部署 Token: hijklmn

如果 Token 遺忘了,可以通過該命令重新獲取

export BOB_TOKEN=$(docker exec -it ${USER}-kuscia-master kubectl get domain bob -o=jsonpath='{.status.deployTokenStatuses[?(@.state=="unused")].token}')
echo "Bob 的部署 Token: ${BOB_TOKEN}"

輸出示例:

Bob 的部署 Token: hijklmn

接下來,登錄到安裝 Bob 的機器上,假設對暴露的 IP 是 3.3.3.3。

指定 Kuscia 版本:

# The Kuscia image used, here using version 1.1.0b0 image
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

生成 Bob 節點的配置文件:

# The --domain parameter passes the node ID.
# The --lite-deploy-token parameter passes the Token for node deployment.
# The --master-endpoint parameter passes the master container's exposed https://IP:PORT. For example, if the master is exposed at IP 1.1.1.1 and port 18080, it would be https://1.1.1.1:18080.
docker run -it --rm ${KUSCIA_IMAGE} kuscia init --mode lite --domain "bob" --master-endpoint "https://1.1.1.1:18080" --lite-deploy-token "${BOB_TOKEN}" > lite_bob.yaml 2>&1 || cat lite_bob.yaml

啓動 Bob,默認會在當前目錄下創建 ${USER}-kuscia-lite-bob/data 目錄用來存放 bob 的數據:

# The -p parameter passes the port that the node container maps to the host, ensuring it does not conflict with existing ports on the host.
# The -k parameter passes the HTTP port that the lite container's KusciaAPI maps to the host, ensuring it does not conflict with existing ports on the host.
./kuscia.sh start -c lite_bob.yaml -p 38080 -k 38081
如果 master 與多個 lite 節點部署在同一個物理機上,可以用 -p -k -g -q -x 參數指定下端口號(例如:./kuscia.sh start -c lite_bob.yaml -p 38080 -k 38081 -g 38082 -q 38083 -x 38084),防止出現端口衝突。

配置授權

如果要發起由兩個 lite 節點參與的任務,您需要給這兩個節點之間建立授權。

創建 alice 和 bob 之間的授權

在 master 機器上執行創建授權的命令

# To reduce the cost of troubleshooting authorization errors, it is recommended to separately (using curl) access the other party's address from within the alice/bob containers to determine if connectivity is possible, before proceeding with authorization.
# Example: curl -vvv http://ip:port returns a normal HTTP error code of 401.
# The access address for the bob node is generally http://ip:port for bob. As mentioned above, bob's IP is 3.3.3.3, and the port is 38080 as stated earlier.
docker exec -it ${USER}-kuscia-master sh scripts/deploy/create_cluster_domain_route.sh alice bob http://3.3.3.3:38080
# The access address for the alice node is generally http://ip:port for alice. As mentioned above, alice's IP is 2.2.2.2, and the port is 28080 as stated earlier.
docker exec -it ${USER}-kuscia-master sh scripts/deploy/create_cluster_domain_route.sh bob alice http://2.2.2.2:28080

執行以下命令:

docker exec -it ${USER}-kuscia-master kubectl get cdr

type 為 Ready 的 condition 的 status 值為 "True" 則説明 Alice 和 Bob 之間授權建立成功。

:::{tip}

  • 如果節點之間的入口網絡存在網關時,為了確保節點與節點之間通信正常,需要網關符合一些要求,詳情請參考這裏
  • 授權失敗,請參考授權錯誤排查
    :::

運行任務

接下來,運行一個測試任務以驗證部署是否成功。

準備數據

獲取測試數據集

登錄到安裝 Alice 的機器上,將默認的測試數據拷貝到之前部署目錄的 ${USER}-kuscia-lite-alice/data 下

docker pull ${KUSCIA_IMAGE} && docker run --rm ${KUSCIA_IMAGE} cat /home/kuscia/var/storage/data/alice.csv > /tmp/alice.csv
docker cp /tmp/alice.csv ${USER}-kuscia-lite-alice:/home/kuscia/var/storage/data/
rm -rf /tmp/alice.csv

登錄到安裝 bob 的機器上,將默認的測試數據拷貝到之前部署目錄的 ${USER}-kuscia-lite-bob/data 下

docker pull ${KUSCIA_IMAGE} && docker run --rm ${KUSCIA_IMAGE} cat /home/kuscia/var/storage/data/bob.csv > /tmp/bob.csv
docker cp /tmp/bob.csv ${USER}-kuscia-lite-bob:/home/kuscia/var/storage/data/
rm -rf /tmp/bob.csv
創建測試數據表

登錄到安裝 master 的機器上,為 Alice 和 Bob 的測試數據創建 domaindata

docker exec -it ${USER}-kuscia-master scripts/deploy/create_domaindata_alice_table.sh alice
docker exec -it ${USER}-kuscia-master scripts/deploy/create_domaindata_bob_table.sh bob
創建測試數據表授權

登錄到安裝 master 的機器上,為 Alice 的測試數據創建 domaindatagrant

docker exec -it ${USER}-kuscia-master curl -X POST 'https://127.0.0.1:8082/api/v1/domaindatagrant/create' --header "Token: $(docker exec -it ${USER}-kuscia-master cat /home/kuscia/var/certs/token)" --header 'Content-Type: application/json' -d '{
 "grant_domain": "bob",
 "description": {"domaindatagrant":"alice-bob"},
 "domain_id": "alice",
 "domaindata_id": "alice-table"
}' --cacert /home/kuscia/var/certs/ca.crt --cert /home/kuscia/var/certs/ca.crt --key /home/kuscia/var/certs/ca.key

同理,登錄到安裝 master 的機器上,為 Bob 的測試數據創建 domaindatagrant

docker exec -it ${USER}-kuscia-master curl -X POST 'https://127.0.0.1:8082/api/v1/domaindatagrant/create' --header "Token: $(docker exec -it ${USER}-kuscia-master cat /home/kuscia/var/certs/token)" --header 'Content-Type: application/json' -d '{
 "grant_domain": "alice",
 "description": {"domaindatagrant":"bob-alice"},
 "domain_id": "bob",
 "domaindata_id": "bob-table"
}' --cacert /home/kuscia/var/certs/ca.crt --cert /home/kuscia/var/certs/ca.crt --key /home/kuscia/var/certs/ca.key

執行測試作業

登錄到安裝 master 的機器上

創建並啓動作業(兩方 PSI 任務)

docker exec -it ${USER}-kuscia-master scripts/user/create_example_job.sh

查看作業狀態

docker exec -it ${USER}-kuscia-master kubectl get kj -n cross-domain

任務運行遇到網絡錯誤時,可以參考這裏排查

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.