在 runtime_env 中认证远程 URI#

本节将帮助您

  • 避免在您的 runtime_env 中泄露远程 URI 凭证

  • 在 KubeRay 中安全地提供凭证

  • 了解认证远程 URI 的最佳实践

认证远程 URI#

您可以使用 远程 URI 将依赖项添加到您的 runtime_env 中。对于公开托管的文件,这很简单,您只需将公共 URI 粘贴到您的 runtime_env 中即可。

runtime_env = {"working_dir": (
        "https://github.com/"
        "username/repo/archive/refs/heads/master.zip"
    )
}

但是,私有托管的依赖项,例如私有 GitHub 存储库,需要进行身份验证。一种常见的身份验证方式是将凭证插入 URI 本身。

runtime_env = {"working_dir": (
        "https://username:personal_access_token@github.com/"
        "username/repo/archive/refs/heads/master.zip"
    )
}

在此示例中,personal_access_token 是用于对该 URI 进行身份验证的秘密凭证。虽然 Ray 可以使用经过身份验证的 URI 成功访问您的依赖项,但出于两个原因,**您不应在 URI 中包含秘密凭证**。

  1. Ray 可能会记录您在 runtime_env 中使用的 URI,这意味着 Ray 日志可能包含您的凭证。

  2. Ray 将您的远程依赖包存储在本地目录中,并使用解析后的远程 URI(包括您的凭证)作为目录名称。

总之,您的远程 URI 不会被视为秘密,因此不应包含秘密信息。请改用 netrc 文件。

在虚拟机上运行:netrc 文件#

netrc 文件 包含 Ray 用于自动登录远程服务器的凭证。请在此文件中设置您的凭证,而不是在远程 URI 中。

# "$HOME/.netrc"

machine github.com
login username
password personal_access_token

在此示例中,machine github.com 行指定了对 github.com 的任何访问都应使用提供的 loginpassword 进行身份验证。

注意

在 Unix 上,将 netrc 文件命名为 .netrc。在 Windows 上,将文件命名为 _netrc

netrc 文件需要所有者读/写权限,因此在创建文件后,请务必运行 chmod 命令。

chmod 600 "$HOME/.netrc"

netrc 文件添加到您的 VM 容器的主目录中,这样 Ray 就可以访问 runtime_env 的私有远程 URI,即使它们不包含凭证。

在 KubeRay 上运行:带 netrc 的 Secrets#

KubeRay 也可以从 netrc 文件中获取远程 URI 的凭证。通过 Kubernetes Secret 和 Kubernetes Volume,可以使用以下步骤提供您的 netrc 文件。

1. 启动您的 Kubernetes 集群。

2. 在本地主目录中创建 netrc 文件。

3. 将 netrc 文件的内容存储在您的集群上作为 Kubernetes Secret。

kubectl create secret generic netrc-secret --from-file=.netrc="$HOME/.netrc"

4. 使用挂载的 Volume 将 Secret 暴露给您的 KubeRay 应用程序,并将 NETRC 环境变量更新为指向 netrc 文件。在您的 KubeRay 配置中包含以下 YAML。

headGroupSpec:
    ...
    containers:
        - name: ...
          image: rayproject/ray:latest
          ...
          volumeMounts:
            - mountPath: "/home/ray/netrcvolume/"
              name: netrc-kuberay
              readOnly: true
          env:
            - name: NETRC
              value: "/home/ray/netrcvolume/.netrc"
    volumes:
        - name: netrc-kuberay
          secret:
            secretName: netrc-secret

workerGroupSpecs:
    ...
    containers:
        - name: ...
          image: rayproject/ray:latest
          ...
          volumeMounts:
            - mountPath: "/home/ray/netrcvolume/"
              name: netrc-kuberay
              readOnly: true
          env:
            - name: NETRC
              value: "/home/ray/netrcvolume/.netrc"
    volumes:
        - name: netrc-kuberay
          secret:
            secretName: netrc-secret

5. 应用您的 KubeRay 配置。

您的 KubeRay 应用程序可以使用 netrc 文件来访问私有远程 URI,即使它们不包含凭证。