Skip to content

Commit 56afc3f

Browse files
edoakesandrewsykimgemini-code-assist[bot]
authored
[core] Cherry pick KubeRay authentication guide (#58824)
Cherry pick: #58729 Signed-off-by: Andrew Sy Kim <[email protected]> Signed-off-by: Edward Oakes <[email protected]> Co-authored-by: Andrew Sy Kim <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 3cd2f9d commit 56afc3f

File tree

1 file changed

+38
-121
lines changed

1 file changed

+38
-121
lines changed
Lines changed: 38 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
(kuberay-auth)=
22

3-
# Configure Ray clusters with authentication and access control using KubeRay
3+
# Configure Ray clusters to use token authentication
44

5-
This guide demonstrates how to secure Ray clusters deployed with KubeRay by enabling authentication and access control using Kubernetes Role-Based Access Control (RBAC).
6-
7-
> **Note:** This guide is only supported for the RayCluster custom resource.
5+
This guide demonstrates how to enable Ray token authentication with KubeRay.
86

97
## Prerequisites
108

119
* A Kubernetes cluster. This guide uses GKE, but the concepts apply to other Kubernetes distributions.
1210
* `kubectl` installed and configured to interact with your cluster.
1311
* `gcloud` CLI installed and configured, if using GKE.
1412
* [Helm](https://helm.sh/) installed.
15-
* Ray installed locally.
13+
* Ray 2.52.0 or newer.
1614

1715
## Create or use an existing GKE Cluster
1816

@@ -27,129 +25,65 @@ gcloud container clusters create kuberay-cluster \
2725

2826
Follow [Deploy a KubeRay operator](kuberay-operator-deploy) to install the latest stable KubeRay operator from the Helm repository.
2927

30-
## Deploy a Ray cluster with authentication enabled
31-
32-
Deploy a RayCluster configured with `kube-rbac-proxy` for authentication and authorization:
28+
## Deploy a Ray cluster with token authentication
3329

30+
If you are using KubeRay v1.5.1 or newer, you can use the `authOptions` API in RayCluster to enable token authentication:
3431
```bash
3532
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/refs/heads/master/ray-operator/config/samples/ray-cluster.auth.yaml
3633
```
3734

38-
This command deploys:
39-
* A `RayCluster` resource with a `kube-rbac-proxy` sidecar container on the Head Pod. This proxy handles authentication and authorization.
40-
* A `ConfigMap` for kube-rbac-proxy, containing resource attributes required for authorization.
41-
* A `ServiceAccount`, `ClusterRole`, and `ClusterRoleBinding` that allow the `kube-rbac-proxy` to access the Kubernetes TokenReview and SubjectAccessReview APIs.
42-
43-
## Verify initial unauthorized access
35+
When enabled, the KubeRay operator will:
36+
* Create a Kubernetes Secret containing a randomly generated token.
37+
* Automatically set the `RAY_AUTH_TOKEN` and `RAY_AUTH_MODE` environment variables on all Ray containers.
4438

45-
Attempt to submit a Ray job to the cluster to verify that authentication is required. You should receive a `401 Unauthorized` error:
39+
If you are using a KubeRay version older than v1.5.1, you can enable token authentication by creating a Kubernetes Secret containing
40+
your token and configuring the `RAY_AUTH_MODE` and `RAY_AUTH_TOKEN` environment variables.
4641

4742
```bash
48-
kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &
49-
ray job submit --address http://localhost:8265 -- python -c "import ray; ray.init(); print(ray.cluster_resources())"
43+
kubectl create secret generic ray-cluster-with-auth --from-literal=auth_token=$(openssl rand -base64 32)
44+
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/refs/heads/master/ray-operator/config/samples/ray-cluster.auth-manual.yaml
5045
```
5146

52-
You may see an error similar to this:
53-
54-
```
55-
...
56-
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: http://localhost:8265/api/version
57-
```
58-
59-
This error confirms that the Ray cluster requires authentication.
60-
61-
## Configure Kubernetes RBAC for access control
62-
63-
To access the RayCluster, you need:
64-
* **Authentication:** Provide a valid authentication token (e.g., a Kubernetes service account token or a cloud IAM token) in the request headers.
65-
* **Authorization:** Your authenticated user or service account must have the necessary Kubernetes RBAC permissions to access the `RayCluster` resource.
66-
67-
This guide demonstrates granting access using a Kubernetes service account, but the same principles apply to individual Kubernetes users or cloud IAM users.
68-
69-
### Create a Kubernetes service account
70-
71-
Create a service account that represents your Ray job submitter:
72-
73-
```bash
74-
kubectl create serviceaccount ray-user
75-
```
47+
## Verify initial unauthenticated access
7648

77-
Confirm that the service account currently can't access the `RayCluster` resource:
49+
Attempt to submit a Ray job to the cluster to verify that authentication is required. You should receive a `401 Unauthorized` error:
7850

7951
```bash
80-
kubectl auth can-i get rayclusters.ray.io/ray-cluster-with-auth --as=system:serviceaccount:default:ray-user
81-
```
82-
83-
The output should be `no`.
84-
85-
### Grant access using Kubernetes RBAC
86-
87-
Create a `Role` and `RoleBinding` to grant the necessary permissions to the `ray-user` service account:
88-
89-
```yaml
90-
# ray-cluster-rbac.yaml
91-
---
92-
apiVersion: rbac.authorization.k8s.io/v1
93-
kind: Role
94-
metadata:
95-
name: ray-user
96-
namespace: default
97-
rules:
98-
- apiGroups: ["ray.io"]
99-
resources:
100-
- 'rayclusters'
101-
verbs: ["*"]
102-
---
103-
apiVersion: rbac.authorization.k8s.io/v1
104-
kind: RoleBinding
105-
metadata:
106-
name: ray-user
107-
namespace: default
108-
roleRef:
109-
apiGroup: rbac.authorization.k8s.io
110-
kind: Role
111-
name: ray-user
112-
subjects:
113-
- kind: ServiceAccount
114-
name: ray-user
115-
namespace: default
52+
kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &
53+
ray job submit --address http://localhost:8265 -- python -c "import ray; ray.init(); print(ray.cluster_resources())"
11654
```
11755

118-
Apply the RBAC configuration:
56+
You should see an error similar to this:
11957

12058
```bash
121-
kubectl apply -f ray-cluster-rbac.yaml
122-
```
123-
124-
### Verify access
59+
RuntimeError: Authentication required: Unauthorized: Missing authentication token
12560

126-
Confirm that the service account now has access to the `RayCluster` resource:
61+
The Ray cluster requires authentication, but no token was provided.
12762

128-
```bash
129-
kubectl auth can-i get rayclusters.ray.io/ray-cluster-with-auth --as=system:serviceaccount:default:ray-user
63+
Please provide an authentication token using one of these methods:
64+
1. Set the `RAY_AUTH_TOKEN` environment variable.
65+
2. Set the `RAY_AUTH_TOKEN_PATH` environment variable (pointing to a file containing the token).
66+
3. Create a token file at the default location: `~/.ray/auth_token`.
13067
```
13168

132-
The output should be `yes`.
133-
134-
## Submit a Ray job with authentication
135-
136-
Now you can submit a Ray job using the service account's authentication token.
137-
138-
Get a token for the `ray-user` service account and store it in the `RAY_JOB_HEADERS` environment variable:
69+
This error confirms that the Ray cluster requires authentication.
13970

140-
```bash
141-
export RAY_JOB_HEADERS="{\"Authorization\": \"Bearer $(kubectl create token ray-user --duration=1h)\"}"
142-
```
71+
## Accessing your Ray cluster with the Ray CLI
14372

144-
> **Note:** `kubectl create token` command is only available on Kubernetes v1.24+
73+
To access your Ray cluster using the Ray CLI, you need to configure the following environment variables:
74+
* `RAY_AUTH_MODE`: this configures the Ray CLI to set the necessary authorization headers for token authentication
75+
* `RAY_AUTH_TOKEN`: this contains the token that will be used for authentication.
76+
* `RAY_AUTH_TOKEN_PATH`: if `RAY_AUTH_TOKEN` is not set, the Ray CLI will instead read the token from this path (defaults to `~/.ray/auth_token`).
14577

146-
Submit the Ray job:
78+
Submit a job with an authenticated Ray CLI:
14779

14880
```bash
81+
export RAY_AUTH_MODE=token
82+
export RAY_AUTH_TOKEN=$(kubectl get secrets ray-cluster-with-auth --template={{.data.auth_token}} | base64 -d)
14983
ray job submit --address http://localhost:8265 -- python -c "import ray; ray.init(); print(ray.cluster_resources())"
15084
```
15185

152-
The job should now succeed, and you should see output similar to this:
86+
The job should now succeed and you should see output similar to this:
15387

15488
```bash
15589
Job submission server address: http://localhost:8265
@@ -176,32 +110,15 @@ Job 'raysubmit_...' succeeded
176110
------------------------------------------
177111
```
178112
179-
## Verify access using cloud IAM (Optional)
180-
181-
Most cloud providers allow you to authenticate to the Kubernetes cluster as your cloud IAM user. This method is a convenient way to interact with the cluster without managing separate Kubernetes credentials.
182-
183-
**Example using Google Cloud (GKE):**
184-
185-
Get an access token for your Google Cloud user:
186-
187-
```bash
188-
export RAY_JOB_HEADERS="{\"Authorization\": \"Bearer $(gcloud auth print-access-token)\"}"
189-
```
190-
191-
Submit a Ray job using the IAM token:
113+
## Viewing the Ray dashboard (optional)
114+
To view the Ray dashboard from your browser, first port forward to from your local machine to the cluster:
192115
193116
```bash
194-
ray job submit --address http://localhost:8265 -- python -c "import ray; ray.init(); print(ray.cluster_resources())"
117+
kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &
195118
```
196119
197-
The job should succeed if your cloud user has the necessary Kubernetes RBAC permissions. You may need to configure additional RBAC rules for your cloud user.
198-
199-
## View the Ray dashboard (optional)
200-
201-
To view the Ray dashboard from your browser, first configure port-forwarding:
120+
Then open `localhost:8265` in your browser. You will be prompted to provide the auth token for the cluster, which can be retrieved with:
202121
203122
```bash
204-
kubectl port-forward svc/ray-cluster-with-auth-head-svc 8265:8265 &
123+
kubectl get secrets ray-cluster-with-auth --template={{.data.auth_token}} | base64 -d
205124
```
206-
207-
Use a Chrome extension like [Requestly](https://requestly.com/) to automatically add authorization headers to requests for the dashboard endpoint `http://localhost:8265`. The authorization header format is: `Authorization: Bearer <token>`.

0 commit comments

Comments
 (0)