配置 Ray 集群以使用令牌身份验证#

本指南演示如何使用 KubeRay 启用 Ray 令牌身份验证。

先决条件#

  • Kubernetes 集群。本指南使用 GKE,但这些概念适用于其他 Kubernetes 发行版。

  • kubectl 已安装并配置为与您的集群进行交互。

  • 如果使用 GKE,则已安装并配置 gcloud CLI。

  • 已安装 Helm

  • Ray 2.52.0 或更高版本。

创建或使用现有的 GKE 集群#

如果您没有 Kubernetes 集群,请使用以下命令创建一个,或者为其云提供商进行调整。

gcloud container clusters create kuberay-cluster \
    --num-nodes=2 --zone=us-west1-b --machine-type e2-standard-4

安装 KubeRay Operator#

按照 部署 KubeRay Operator 的说明,从 Helm 存储库安装最新的稳定 KubeRay Operator。

使用令牌身份验证部署 Ray 集群#

如果您使用的是 KubeRay v1.5.1 或更高版本,您可以在 RayCluster 中使用 authOptions API 来启用令牌身份验证。

kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/refs/heads/master/ray-operator/config/samples/ray-cluster.auth.yaml

启用后,KubeRay Operator 将会:

  • 创建一个包含随机生成的令牌的 Kubernetes Secret。

  • 自动为所有 Ray 容器设置 RAY_AUTH_TOKENRAY_AUTH_MODE 环境变量。

如果您使用的是低于 v1.5.1 的 KubeRay 版本,可以通过创建一个包含您的令牌的 Kubernetes Secret 并配置 RAY_AUTH_MODERAY_AUTH_TOKEN 环境变量来启用令牌身份验证。

kubectl create secret generic ray-cluster-with-auth --from-literal=auth_token=$(openssl rand -base64 32)
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/refs/heads/master/ray-operator/config/samples/ray-cluster.auth-manual.yaml

验证初始未经身份验证的访问#

尝试向集群提交 Ray 作业以验证是否需要身份验证。您应该会收到一个 401 Unauthorized 错误。

kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &
ray job submit --address https://:8265  -- python -c "import ray; ray.init(); print(ray.cluster_resources())"

您应该会看到类似以下的错误:

RuntimeError: Authentication required: Unauthorized: Missing authentication token

The Ray cluster requires authentication, but no token was provided.

Please provide an authentication token using one of these methods:
  1. Set the `RAY_AUTH_TOKEN` environment variable.
  2. Set the `RAY_AUTH_TOKEN_PATH` environment variable (pointing to a file containing the token).
  3. Create a token file at the default location: `~/.ray/auth_token`.

此错误确认 Ray 集群需要身份验证。

使用 Ray CLI 访问您的 Ray 集群#

要使用 Ray CLI 访问您的 Ray 集群,您需要配置以下环境变量:

  • RAY_AUTH_MODE:此配置使 Ray CLI 设置必要的授权头以进行令牌身份验证。

  • RAY_AUTH_TOKEN:包含用于身份验证的令牌。

  • RAY_AUTH_TOKEN_PATH:如果未设置 RAY_AUTH_TOKEN,Ray CLI 将从此路径读取令牌(默认为 ~/.ray/auth_token)。

提交一个带有已认证 Ray CLI 的作业

export RAY_AUTH_MODE=token
export RAY_AUTH_TOKEN=$(kubectl get secrets ray-cluster-with-auth --template={{.data.auth_token}} | base64 -d)
ray job submit --address https://:8265  -- python -c "import ray; ray.init(); print(ray.cluster_resources())"

作业现在应该会成功,您应该会看到类似以下的输出:

Job submission server address: https://:8265

-------------------------------------------------------
Job 'raysubmit_...' submitted successfully
-------------------------------------------------------

Next steps
  Query the logs of the job:
    ray job logs raysubmit_n2fq2Ui7cbh3p2Js
  Query the status of the job:
    ray job status raysubmit_n2fq2Ui7cbh3p2Js
  Request the job to be stopped:
    ray job stop raysubmit_n2fq2Ui7cbh3p2Js

Tailing logs until the job exits (disable with --no-wait):

...
{'node:10.112.0.52': 1.0, 'memory': ..., 'node:__internal_head__': 1.0, 'object_store_memory': ..., 'CPU': 4.0, 'node:10.112.1.49': 1.0, 'node:10.112.2.36': 1.0}

------------------------------------------
Job 'raysubmit_...' succeeded
------------------------------------------

查看 Ray Dashboard(可选)#

要从浏览器查看 Ray Dashboard,首先需要从本地机器将端口转发到集群。

kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &

然后在浏览器中打开 localhost:8265。系统将提示您提供集群的身份验证令牌,可以使用以下命令检索:

kubectl get secrets ray-cluster-with-auth --template={{.data.auth_token}} | base64 -d