Ray Train XGBoostTrainer 在 Kubernetes 上#
注意
要学习 Ray 在 Kubernetes 上的基础知识,建议先查看入门指南。
在本指南中,我们将向您展示如何在 Kubernetes 基础设施上运行一个 Ray 机器学习工作负载示例。
我们将使用一个 100 GB 的训练集运行 Ray 的XGBoost 训练基准测试。要了解更多关于使用 Ray 的 XGBoostTrainer 的信息,请查看XGBoostTrainer 文档。
在 GCP 上设置 Kubernetes 基础设施#
本文档提供了在 GCP 上创建 Kubernetes 集群的说明,但类似设置也适用于任何主要的云提供商。如果您想在其他云提供商上设置 Kubernetes 集群,请查看入门指南。如果您已有 Kubernetes 集群,可以忽略此步骤。
# Set up a cluster on Google Kubernetes Engine (GKE)
gcloud container clusters create autoscaler-ray-cluster \
--num-nodes=10 --zone=us-central1-c --machine-type e2-standard-16 --disk-size 1000GB
# (Optional) Set up a cluster with autopilot on Google Kubernetes Engine (GKE).
# The following command creates an autoscaling node pool with a 1 node minimum and a 10 node maximum.
# The 1 static node will be used to run the Ray head pod. This node may also host the KubeRay
# operator and Kubernetes system components. After the workload is submitted, 9 additional nodes will
# scale up to accommodate Ray worker pods. These nodes will scale back down after the workload is complete.
gcloud container clusters create autoscaler-ray-cluster \
--num-nodes=1 --min-nodes 1 --max-nodes 10 --enable-autoscaling \
--zone=us-central1-c --machine-type e2-standard-16 --disk-size 1000GB
请确保您已连接到您的 Kubernetes 集群。对于 GCP,您可以通过以下方式操作
导航到您的 GKE 集群页面,然后点击“CONNECT”按钮。然后复制“Command-line access”。
gcloud container clusters get-credentials <your-cluster-name> --region <your-region> --project <your-project>
(链接)kubectl config use-context
(链接)
对于本指南中的工作负载,建议使用具有以下属性的 Kubernetes 节点池或节点组
总共 10 个节点
每个节点容量为 16 个 CPU 和 64 Gi 内存。对于主要的云提供商,适用的实例类型包括
m5.4xlarge (Amazon Web Services)
Standard_D5_v2 (Azure)
e2-standard-16 (Google Cloud)
每个节点应配置 1000 GB 磁盘空间(用于存储训练集)。
部署 KubeRay operator#
设置好 Kubernetes 集群后,部署 KubeRay operator。有关此步骤的说明,请参阅入门指南。
部署 Ray 集群#
现在我们准备部署将执行工作负载的 Ray 集群。
提示
我们将部署的 Ray 集群配置为每个 16-CPU 的 Kubernetes 节点调度一个 Ray pod。鼓励采用每个 Kubernetes 节点一个 Ray pod 的模式,但这并非强制要求。总的来说,使用少量大型 Ray pod 比使用大量小型 Ray pod 更高效。
我们建议查看以下命令中应用的配置文件。
kubectl apply -f https://raw.githubusercontent.com/ray-project/ray/releases/2.0.0/doc/source/cluster/kubernetes/configs/xgboost-benchmark.yaml
将创建一个 Ray head pod 和 9 个 Ray worker pod。
运行工作负载#
要观察 Ray head pod 的启动进度,请运行以下命令。
# If you're on MacOS, first `brew install watch`.
watch -n 1 kubectl get pod
一旦 Ray head pod 进入 Running
状态,我们就可以执行 XGBoost 工作负载了。我们将使用Ray Job Submission 来启动工作负载。
连接到集群。#
本指南使用端口转发作为一种简单的方式来实验 Ray 集群的服务。有关生产用例,请参阅网络说明。
# Run the following blocking command in a separate shell.
kubectl port-forward service/raycluster-xgboost-benchmark-head-svc 8265:8265
提交工作负载。#
我们将使用Ray Job Python SDK 提交 XGBoost 工作负载。
from ray.job_submission import JobSubmissionClient
client = JobSubmissionClient("http://127.0.0.1:8265")
kick_off_xgboost_benchmark = (
# Clone ray. If ray is already present, don't clone again.
"git clone https://github.com/ray-project/ray || true; "
# Run the benchmark.
"python ray/release/train_tests/xgboost_lightgbm/train_batch_inference_benchmark.py"
" xgboost --size=100G --disable-check"
)
submission_id = client.submit_job(
entrypoint=kick_off_xgboost_benchmark,
)
print("Use the following command to follow this Job's logs:")
print(f"ray job logs '{submission_id}' --follow")
要提交工作负载,运行上面的 Python 脚本。该脚本可在Ray 仓库中找到。
# Download the above script.
curl https://raw.githubusercontent.com/ray-project/ray/releases/2.0.0/doc/source/cluster/doc_code/xgboost_submit.py -o xgboost_submit.py
# Run the script.
python xgboost_submit.py
观察进度。#
基准测试可能需要长达 60 分钟才能运行完成。使用以下工具观察其进度。
作业日志#
要跟踪作业日志,使用上述提交脚本打印的命令。
# Substitute the Ray Job's submission id.
ray job logs 'raysubmit_xxxxxxxxxxxxxxxx' --follow --address http://127.0.0.1:8265
Kubectl#
使用以下命令观察集群中的 pod
# If you're on MacOS, first `brew install watch`.
watch -n 1 kubectl get pod
Ray Dashboard#
在浏览器中访问 localhost:8265
查看 Ray Dashboard。
Ray Status#
使用以下命令观察自动扩缩容状态和 Ray 资源使用情况
# Substitute the name of your Ray cluster's head pod.
watch -n 1 kubectl exec -it raycluster-xgboost-benchmark-head-xxxxx -- ray status
注意
在某些情况下和某些云提供商中,K8s API 服务器在 Kubernetes 集群调整大小时可能会短暂不可用。
如果发生这种情况,请不要担心——Ray 工作负载应该不会中断。对于本指南中的示例,请等待 API 服务器恢复正常,重新启动端口转发进程,然后重新运行作业日志命令。
作业完成#
基准测试结果#
基准测试完成后,作业日志将显示结果
Results: {'training_time': 1338.488839321999, 'prediction_time': 403.36653568099973}
基准测试的性能对底层云基础设施敏感——您可能无法达到基准测试文档中引用的数字。
模型参数#
Ray head pod 中的文件 model.json
包含训练模型的参数。其他结果数据可在 head pod 的目录 ray_results
中找到。有关详细信息,请参阅XGBoostTrainer 文档。
缩减
如果启用了自动扩缩容,Ray worker pod 将在 60 秒后缩减。Ray worker pod 消失后,您的 Kubernetes 基础设施应缩减托管这些 pod 的节点。
清理#
使用以下命令删除您的 Ray 集群
kubectl delete raycluster raycluster-xgboost-benchmark
如果您在使用公共云,请不要忘记清理底层的节点组和/或 Kubernetes 集群。