使用 KubeRay 配置带有身份验证和访问控制的 Ray 集群#
本指南演示了如何使用 Kubernetes 基于角色的访问控制 (RBAC) 启用身份验证和访问控制,从而保护使用 KubeRay 部署的 Ray 集群。
注意:本指南仅支持 RayCluster 自定义资源。
先决条件#
一个 Kubernetes 集群。本指南使用 GKE,但这些概念也适用于其他 Kubernetes 分发版。
已安装并配置
kubectl
以与你的集群交互。如果使用 GKE,已安装并配置
gcloud
CLI。已安装 Helm。
已在本地安装 Ray。
创建或使用现有的 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 集群#
部署配置了 kube-rbac-proxy
的 RayCluster 以进行身份验证和授权
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/refs/heads/master/ray-operator/config/samples/ray-cluster.auth.yaml
此命令部署
一个带有 Head Pod 上的
kube-rbac-proxy
边车容器的RayCluster
资源。此代理负责处理身份验证和授权。一个用于 kube-rbac-proxy 的
ConfigMap
,包含授权所需的资源属性。一个
ServiceAccount
、ClusterRole
和ClusterRoleBinding
,允许kube-rbac-proxy
访问 Kubernetes TokenReview 和 SubjectAccessReview API。
配置 Kubernetes RBAC 以进行访问控制#
要访问 RayCluster,你需要
身份验证:在请求头中提供有效的身份验证令牌(例如,Kubernetes service account 令牌或云 IAM 令牌)。
授权:你的经过身份验证的用户或 service account 必须拥有必要的 Kubernetes RBAC 权限才能访问
RayCluster
资源。
本指南演示了如何使用 Kubernetes service account 授予访问权限,但同样的原则也适用于独立的 Kubernetes 用户或云 IAM 用户。
创建一个 Kubernetes service account#
创建一个表示你的 Ray job 提交者的 service account
kubectl create serviceaccount ray-user
确认此 service account 当前无法访问 RayCluster
资源
kubectl auth can-i get rayclusters.ray.io/ray-cluster-with-auth --as=system:serviceaccount:default:ray-user
输出应为 no
。
使用 Kubernetes RBAC 授予访问权限#
创建一个 Role
和 RoleBinding
以授予 ray-user
service account 必要的权限
# ray-cluster-rbac.yaml
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: ray-user
namespace: default
rules:
- apiGroups: ["ray.io"]
resources:
- 'rayclusters'
verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: ray-user
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: ray-user
subjects:
- kind: ServiceAccount
name: ray-user
namespace: default
应用 RBAC 配置
kubectl apply -f ray-cluster-rbac.yaml
验证访问权限#
确认此 service account 现在可以访问 RayCluster
资源
kubectl auth can-i get rayclusters.ray.io/ray-cluster-with-auth --as=system:serviceaccount:default:ray-user
输出应为 yes
。
提交带有身份验证的 Ray job#
现在你可以使用 service account 的身份验证令牌提交 Ray job。
获取 ray-user
service account 的令牌并将其存储在 RAY_JOB_HEADERS
环境变量中
export RAY_JOB_HEADERS="{\"Authorization\": \"Bearer $(kubectl create token ray-user --duration=1h)\"}"
注意:
kubectl create token
命令仅在 Kubernetes v1.24+ 版本可用
提交 Ray job
ray job submit --address http://localhost:8265 -- python -c "import ray; ray.init(); print(ray.cluster_resources())"
job 现在应该会成功,你应该会看到类似于此的输出
Job submission server address: http://localhost: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
------------------------------------------
使用云 IAM 验证访问权限 (可选)#
大多数云提供商允许你作为云 IAM 用户向 Kubernetes 集群进行身份验证。这种方法是一种便捷的方式来与集群交互,而无需管理单独的 Kubernetes 凭据。
使用 Google Cloud (GKE) 的示例
获取 Google Cloud 用户的访问令牌
export RAY_JOB_HEADERS="{\"Authorization\": \"Bearer $(gcloud auth print-access-token)\"}"
使用 IAM 令牌提交 Ray job
ray job submit --address http://localhost:8265 -- python -c "import ray; ray.init(); print(ray.cluster_resources())"
如果你的云用户拥有必要的 Kubernetes RBAC 权限,job 应该会成功。你可能需要为你的云用户配置额外的 RBAC 规则。
查看 Ray dashboard (可选)#
要从浏览器查看 Ray dashboard,首先配置端口转发
kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &
使用 Requestly 等 Chrome 扩展程序自动为仪表盘端点 http://localhost:8265
的请求添加授权头。授权头格式为:Authorization: Bearer
。