全部產品
Search
文件中心

Container Service for Kubernetes:在Ray Cluster中提交Job

更新時間:Jun 19, 2024

Ray支援構建可擴充的人工智慧(AI)和Python應用程式,廣泛應用於機器學習領域。您可以基於ACK叢集建立Ray Cluster,並在本地提交Job執行分布式任務,用於訓練模型、資料處理、模型評估等情境。在本地RayCluster提交Ray Job。

前提條件

基於ACK建立Ray Cluster

Ray Cluster內運行Job作業有多種方式。更多資訊,請參見how do you use the ray-clientquick start useing the ray job cli

  1. 執行以下命令,查詢Ray Cluster的Pod資訊。

    kubectl get pod -n ${RAY_CLUSTER_NS}

    預期輸出:

    NAME                                           READY   STATUS    RESTARTS   AGE
    myfirst-ray-cluster-head-v7pbw                 2/2     Running   0          39m
  2. 執行以下命令,您可以在本地終端串連到Pod內部的Bash Shell。

    請將Pod名稱替換為您實際的Pod的名稱。

    kubectl exec -it -n ${RAY_CLUSTER_NS} myfirst-ray-cluster-head-v7pbw -- bash
  3. 在Head Pod中使用echocat命令,儲存my_script.py檔案。

    import ray
    import os
    
    # 串連本地或者遠程ray cluster
    ray.init()
    
    @ray.remote(num_cpus=1)
    class Counter:
        def __init__(self):
            self.name = "test_counter"
            self.counter = 0
    
        def increment(self):
            self.counter += 1
    
        def get_counter(self):
            return "{} got {}".format(self.name, self.counter)
    
    counter = Counter.remote()
    
    for _ in range(10000):
        counter.increment.remote()
        print(ray.get(counter.get_counter.remote()))
    
  4. 運行my_script.py指令碼,執行分布式任務。

    python my_script.py
    # 預期輸出
    2024-01-24 04:25:27,286	INFO worker.py:1329 -- Using address 127.0.0.1:6379 set in the environment variable RAY_ADDRESS
    2024-01-24 04:25:27,286	INFO worker.py:1458 -- Connecting to existing Ray cluster at address: 172.16.0.236:6379...
    2024-01-24 04:25:27,295	INFO worker.py:1633 -- Connected to Ray cluster. View the dashboard at http://172.16.0.236:8265
    test_counter got 0
    test_counter got 1
    test_counter got 2
    test_counter got 3
    
    ...

相關操作