写点什么

【隐语 Secretflow】如何在 Docker 多机部署 Kuscia 点对点集群

  • 2025-12-03
    浙江
  • 本文字数:4561 字

    阅读完需:约 15 分钟


打开链接即可点亮社区 Star,照亮技术的前进之路。

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

前言

本教程帮助您在多台机器上使用 点对点组网模式 来部署 Kuscia 集群。

当前 Kuscia 节点之间只支持 Token 的身份认证方式,在跨机器部署的场景下流程较为繁琐,后续本教程会持续更新优化。

前置准备

在部署 Kuscia 之前,请确保环境准备齐全,包括所有必要的软件、资源、操作系统版本和网络环境等满足要求,以确保部署过程顺畅进行,详情参考部署要求

部署流程(基于 TOKEN 认证)

部署 alice 节点

登录到安装 alice 的机器上,本文为叙述方便,假定节点 ID 为 alice ,对外可访问的 PORT 是 11080 。

指定 Kuscia 使用的镜像版本,这里使用 1.1.0b0 版本

export KUSCIA_IMAGE=secretflow-registry.cn-hangzhou.cr.aliyuncs.com/secretflow/kuscia:1.1.0b0
复制代码

指定 Secretflow 版本:

# Using Secretflow image, version 1.11.0b1 is used hereexport 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
复制代码

生成 alice 节点配置文件,kuscia init 参数请参考 Kuscia 配置文件

# The --domain parameter specifies the node IDdocker run -it --rm ${KUSCIA_IMAGE} kuscia init --mode autonomy --domain "alice" > autonomy_alice.yaml 2>&1 || cat autonomy_alice.yaml
复制代码

建议检查生成的文件,避免配置文件错误导致的部署启动问题。

启动节点,默认会在当前目录下创建 ${USER}-kuscia-autonomy-alice/data 目录用来存放 alice 的数据。部署节点需要使用 kuscia.sh 脚本并传入节点配置文件:

# -p: Specifies the mapping of the HTTPS port from the node container to the host. Ensure this port does not conflict with existing ports on the host.# -k: Specifies the mapping of the MTLS port for the Kuscia API from the node container to the host. Ensure this port does not conflict with existing ports on the host. # -a: Specifies auto-import of engine images. Use -a none to disable auto-import. Use -a secretflow (default) to auto-import the SecretFlow engine image.# -m or --memory-limit: Sets appropriate memory limits for node containers. For example, '-m 4GiB or --memory-limit=4GiB' means limiting max memory to 4GiB, '-m -1 or --memory-limit=-1' means no limit. If not set, defaults are: master 2GiB, lite node 4GiB, autonomy node 6GiB../kuscia.sh start -c autonomy_alice.yaml -p 11080 -k 11081
复制代码

:::{tip}

  • 节点 ID 需要全局唯一并且符合 RFC 1123 标签名规则要求,详情请参考这里defaultkube-systemkube-publickube-node-leasemaster 以及 cross-domain 为 Kuscia 预定义的节点 ID,不能被使用。

  • 目前 kuscia.sh 脚本仅支持导入 SecretFlow 镜像,scql、serving 以及其他自定义镜像请移步至注册自定义算法镜像

  • 如果节点之间的入口网络存在网关时,为了确保节点与节点之间通信正常,需要网关符合一些要求,详情请参考这里

  • alice、bob 节点默认使用 SQLite 作为存储,如果生产部署,需要配置链接到 MySQL 数据库的连接串,具体配置可以参考这里

  • 需要对合作方暴露的 Kuscia 端口,可参考 Kuscia 端口介绍。如果多个 Autonomy 节点部署在同一个物理机上,可以用 -p -k -g -q -x 参数指定下端口号(例如:./kuscia.sh start -c autonomyalice.yaml -p 11080 -k 11081 -g 11082 -q 11083 -x 11084),防止出现端口冲突。

  • 非 root 用户部署请参考这里

  • 升级引擎镜像请参考指南

:::

部署 Bob 节点

您可以选择在另一台机器上部署 bob 节点,详细步骤参考上述 alice 节点部署的流程,唯一不同的是在部署前准备参数时配置 bob 节点相关的参数。假定节点 ID 为 bob ,对外可访问的 PORT 是 21080 。

配置证书

在两个 Autonomy 节点建立通信之前,您需要先给这两个节点互换证书。

Alice 颁发证书给 Bob

准备 Alice 的公钥,在 Alice 节点的机器上,可以看到包含公钥的 crt 文件:

# [alice machine] Copy domain.crt from inside the container and rename it to alice.domain.crtdocker cp ${USER}-kuscia-autonomy-alice:/home/kuscia/var/certs/domain.crt alice.domain.crt
复制代码

将 alice 的公钥 alice.domain.crt 拷贝到 bob 容器的 /home/kuscia/var/certs/ 目录中:

# [bob machine] Make sure alice.domain.crt is in the /home/kuscia/var/certs/ directory of bob containerdocker cp alice.domain.crt ${USER}-kuscia-autonomy-bob:/home/kuscia/var/certs/
复制代码

在 Bob 里添加 Alice 的证书等信息:

# [bob machine] Add alice's certificate and other informationdocker exec -it ${USER}-kuscia-autonomy-bob scripts/deploy/add_domain.sh alice p2p
复制代码

Bob 颁发证书给 Alice

准备 Bob 的公钥,在 Bob 节点的机器上,可以看到包含公钥的 crt 文件:

# [bob machine] Copy domain.crt from inside the container and rename it to bob.domain.crtdocker cp ${USER}-kuscia-autonomy-bob:/home/kuscia/var/certs/domain.crt bob.domain.crt
复制代码

将 Bob 的公钥 bob.domain.crt 拷贝到 alice 容器的 /home/kuscia/var/certs/ 目录中:

# [alice machine] Make sure bob.domain.crt is in the /home/kuscia/var/certs/ directory of alice containerdocker cp bob.domain.crt ${USER}-kuscia-autonomy-alice:/home/kuscia/var/certs/
复制代码

在 Alice 里添加 Bob 的证书等信息:

# [alice machine] Add bob's certificate and other informationdocker exec -it ${USER}-kuscia-autonomy-alice scripts/deploy/add_domain.sh bob p2p
复制代码

配置授权

如果要发起由两个 Autonomy 节点参与的任务,您需要给这两个节点之间建立授权。

创建 Alice 到 Bob 的授权

# [alice machine]# Assuming bob's external IP is 2.2.2.2, 21080 is bob's exposed access port mentioned above# To reduce troubleshooting costs for authorization errors, it's recommended to test connectivity to bob's address from within the alice container (using curl) before authorizing# Example: curl -kvvv https://2.2.2.2:21080 should return HTTP error code 401 normallydocker exec -it ${USER}-kuscia-autonomy-alice scripts/deploy/join_to_host.sh alice bob https://2.2.2.2:21080
复制代码

创建 Bob 到 Alice 的授权

# [bob machine]# Assuming alice's external IP is 1.1.1.1, 11080 is alice's exposed access port mentioned above# To reduce troubleshooting costs for authorization errors, it's recommended to test connectivity to alice's address from within the bob container (using curl) before authorizing# Example: curl -kvvv https://1.1.1.1:11080 should return HTTP error code 401 normallydocker exec -it ${USER}-kuscia-autonomy-bob scripts/deploy/join_to_host.sh bob alice https://1.1.1.1:11080
复制代码

检查节点之间网络通信状态

  • 方法一:

[Alice 机器] 执行以下命令:

``bash docker exec -it ${USER}-kuscia-autonomy-alice kubectl get cdr alice-bob``

[Bob 机器] 执行以下命令:

``bash docker exec -it ${USER}-kuscia-autonomy-bob kubectl get cdr bob-alice``

当 "READR" 列为 "True" 时,说明 Alice 和 Bob 之间授权建立成功。

  • 方法二:

[alice 机器] 执行以下命令:

``bash docker exec -it ${USER}-kuscia-autonomy-alice kubectl get cdr alice-bob -o=jsonpath="{.status.tokenStatus.sourceTokens[*]}"``

[bob 机器] 执行以下命令:

``bash docker exec -it ${USER}-kuscia-autonomy-bob kubectl get cdr bob-alice -o=jsonpath="{.status.tokenStatus.sourceTokens[*]}"``

当命令执行成功得到返回结果时表示授权成功

:::{tip}

  • 如果节点之间的入口网络存在网关时,为了确保节点与节点之间通信正常,需要网关符合一些要求,详情请参考这里

  • 授权失败,请参考授权错误排查文档

:::

准备测试数据

  • Alice 节点准备测试数据

登录到安装 Alice 的机器上,将默认的测试数据拷贝到之前部署目录的 ${USER}-kuscia-autonomy-alice/data 下

``bash 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-autonomy-alice:/home/kuscia/var/storage/data/ rm -rf /tmp/alice.csv``

为 Alice 的测试数据创建 domaindata

``bash docker exec -it ${USER}-kuscia-autonomy-alice scripts/deploy/create_domaindata_alice_table.sh alice``

为 Alice 的测试数据创建 domaindatagrant

``bash docker exec -it ${USER}-kuscia-autonomy-alice curl -X POST 'https://127.0.0.1:8082/api/v1/domaindatagrant/create' --header "Token: $(docker exec -it ${USER}-kuscia-autonomy-alice 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``

  • Bob 节点准备测试数据

登录到安装 Bob 的机器上,将默认的测试数据拷贝到之前部署目录的 ${USER}-kuscia-autonomy-alice/data 下

``bash 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-autonomy-bob:/home/kuscia/var/storage/data/ rm -rf /tmp/bob.csv``

为 Bob 的测试数据创建 domaindata

``bash docker exec -it ${USER}-kuscia-autonomy-bob scripts/deploy/create_domaindata_bob_table.sh bob``

为 Bob 的测试数据创建 domaindatagrant

``bash docker exec -it ${USER}-kuscia-autonomy-bob curl -X POST 'https://127.0.0.1:8082/api/v1/domaindatagrant/create' --header "Token: $(docker exec -it ${USER}-kuscia-autonomy-bob 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``

执行作业

创建并启动作业(两方 PSI 任务), 以 Alice 节点机器上执行命令为例

docker exec -it ${USER}-kuscia-autonomy-alice scripts/user/create_example_job.sh
复制代码

查看作业状态

docker exec -it ${USER}-kuscia-autonomy-alice kubectl get kj -n cross-domain
复制代码

任务运行遇到网络错误时,可以参考这里排查

用户头像

关注微信公众号:隐语的小剧场 2022-08-01 加入

隐语SecretFlow是蚂蚁自主研发的隐私计算开源框架,内置MPC、TEE、同态等多种密态计算虚拟设备供灵活选择。同时我们专注于隐私计算领域任何前沿技术、最新动态、行业资讯,隐语期待您的加入!

评论

发布
暂无评论
【隐语Secretflow】如何在Docker多机部署Kuscia点对点集群_隐语SecretFlow_InfoQ写作社区