Python dask 使用 k8s 做分布式計算

1、前提條件:需要有 k8s 環(huán)境,這里使用的是阿里云 serverless k8s (需要安裝coredns組件)
2漓摩、dask k8s 環(huán)境配置肺稀,部署 dask k8s operator

$ helm repo add dask https://helm.dask.org
$ helm repo update
$ helm repo list
NAME       URL
dask       https://helm.dask.org

$ helm search repo dask
$ helm pull dask/dask-kubernetes-operator
$ tar xvf dask-kubernetes-operator-2023.8.1.tgz
$ cd dask-kubernetes-operator
$ vim values.yaml
image:
  # name: ghcr.io/dask/dask-kubernetes-operator  # Docker image for the operator
  # 把鏡像改成從南京大學(xué)ghcr.io鏡像源拉取,避免拉取超時
  name: ghcr.nju.edu.cn/dask/dask-kubernetes-operator  # Docker image for the operator
  tag: "2023.8.1"           # Release version
  pullPolicy: IfNotPresent  # Pull policy

//  部署 dask k8s operator                    
$ helm install dask-kubernetes-operator-2023.8.1 ./dask-kubernetes-operator --values ./dask-kubernetes-operator/values.yaml

$ kubectl get pod
NAME                                                 READY   STATUS      RESTARTS   AGE
dask-kubernetes-operator-2023.8.1-68cd86f7cc-n2fsq   1/1     Running     0          1h

3改执、DaskJob 使用


https://kubernetes.dask.org/en/latest/operator_resources.html#daskjob

a .這里通過 annotations 使用了阿里云 serverless k8s 的 eci pod 競價實例,可以節(jié)省部分成本
b. 里面的 image url 改成了南京大學(xué)的鏡像源,避免拉取超時

$ cat dask-job.yaml
apiVersion: kubernetes.dask.org/v1
kind: DaskJob
metadata:
  name: simple-job
  namespace: default
spec:
  job:
    spec:
      containers:
        - name: job
          # image: "m.daocloud.io/ghcr.io/dask/dask:latest"
          # 使用 Python Dask 做分布式計算的業(yè)務(wù)代碼窘问,應(yīng)該打包為單獨的業(yè)務(wù)鏡像來使用才對
          # 這里為了方便,直接用官方的鏡像和示例代碼來測試
          image: "ghcr.nju.edu.cn/dask/dask:latest"
          imagePullPolicy: "IfNotPresent"
          args:
            - python
            - -c
            - "from dask.distributed import Client; client = Client(); print(client) # Do some work..."

  cluster:
    spec:
      worker:
        replicas: 2
        metadata:
          annotations:
            k8s.aliyun.com/eci-spot-strategy: SpotAsPriceGo
            k8s.aliyun.com/eci-use-specs: 4-8Gi
        spec:
          containers:
            - name: worker
              # image: "m.daocloud.io/ghcr.io/dask/dask:latest"
              image: "ghcr.nju.edu.cn/dask/dask:latest"
              imagePullPolicy: "IfNotPresent"
              args:
                - dask-worker
                - --name
                - $(DASK_WORKER_NAME)
                - --dashboard
                - --dashboard-address
                - "8788"
              ports:
                - name: http-dashboard
                  containerPort: 8788
                  protocol: TCP
              env:
                - name: WORKER_ENV
                  value: hello-world # We dont test the value, just the name
      scheduler:
        metadata:
          annotations:
            k8s.aliyun.com/eci-spot-strategy: SpotAsPriceGo
            k8s.aliyun.com/eci-use-specs: 2-4Gi
        spec:
          containers:
            - name: scheduler
              # image: "m.daocloud.io/ghcr.io/dask/dask:latest"
              image: "ghcr.nju.edu.cn/dask/dask:latest"
              imagePullPolicy: "IfNotPresent"
              args:
                - dask-scheduler
              ports:
                - name: tcp-comm
                  containerPort: 8786
                  protocol: TCP
                - name: http-dashboard
                  containerPort: 8787
                  protocol: TCP
              readinessProbe:
                httpGet:
                  port: http-dashboard
                  path: /health
                initialDelaySeconds: 5
                periodSeconds: 10
              livenessProbe:
                httpGet:
                  port: http-dashboard
                  path: /health
                initialDelaySeconds: 15
                periodSeconds: 20
              env:
                - name: SCHEDULER_ENV
                  value: hello-world
        service:
          type: ClusterIP
          #type: LoadBalancer
          selector:
            dask.org/cluster-name: simple-job
            dask.org/component: scheduler
          ports:
            - name: tcp-comm
              protocol: TCP
              port: 8786
              targetPort: "tcp-comm"
            - name: http-dashboard
              protocol: TCP
              port: 8787
              targetPort: "http-dashboard"
              
$ kubectl apply -f dask-job.yaml

$ kubectl get pod
NAME                                                    READY   STATUS    RESTARTS   AGE
dask-kubernetes-operator-2023.8.1-68cd86f7cc-n2fsq      1/1     Running   0          20h
simple-job-default-worker-6cc619da50-66b7647d89-k9kqd   1/1     Running   0          28s
simple-job-default-worker-e5bf0bbc1e-6ff6c877b4-d4ksj   1/1     Running   0          28s
simple-job-runner                                       1/1     Running   0          29s
simple-job-scheduler-5db7df9769-v8926                   1/1     Running   0          28s

$ kubectl get svc
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
kubernetes             ClusterIP   192.168.0.1      <none>        443/TCP             22h
simple-job-scheduler   ClusterIP   192.168.116.60   <none>        8786/TCP,8787/TCP   48s

// 查看運行結(jié)果
$ kubectl logs --tail=200 -f simple-job-runner
+ '[' '' ']'
+ '[' '' == true ']'
+ CONDA_BIN=/opt/conda/bin/conda
+ '[' -e /opt/app/environment.yml ']'
+ echo 'no environment.yml'
+ '[' '' ']'
no environment.yml
+ '[' '' ']'
+ exec python -c 'from dask.distributed import Client; client = Client(); print(client)# Do some work...'
<Client: 'tcp://172.26.219.61:8786' processes=0 threads=0, memory=0 B>

// 運行結(jié)束后宜咒,其他 pod 自動清理了
$  kubectl get pod
NAME                                                 READY   STATUS      RESTARTS   AGE
dask-kubernetes-operator-2023.8.1-68cd86f7cc-n2fsq   1/1     Running     0          20h
simple-job-runner                                    0/1     Completed   1          2m39s

參考資料:

  1. https://docs.dask.org/en/stable/deploying-kubernetes.html
  2. https://kubernetes.dask.org/en/latest/operator_resources.html
  3. https://doc.nju.edu.cn/books/35f4a/page/ghcr
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惠赫,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子故黑,更是在濱河造成了極大的恐慌儿咱,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件场晶,死亡現(xiàn)場離奇詭異,居然都是意外死亡诗轻,警方通過查閱死者的電腦和手機(jī)钳宪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扳炬,“玉大人吏颖,你說我怎么就攤上這事【媳” “怎么了侦高?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長厌杜。 經(jīng)常有香客問我奉呛,道長计螺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任瞧壮,我火速辦了婚禮登馒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘咆槽。我一直安慰自己陈轿,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布秦忿。 她就那樣靜靜地躺著麦射,像睡著了一般。 火紅的嫁衣襯著肌膚如雪灯谣。 梳的紋絲不亂的頭發(fā)上潜秋,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音胎许,去河邊找鬼峻呛。 笑死,一個胖子當(dāng)著我的面吹牛辜窑,可吹牛的內(nèi)容都是我干的钩述。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼穆碎,長吁一口氣:“原來是場噩夢啊……” “哼牙勘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起惨远,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤谜悟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后北秽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡最筒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年贺氓,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片床蜘。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡辙培,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出邢锯,到底是詐尸還是另有隱情扬蕊,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布丹擎,位于F島的核電站尾抑,受9級特大地震影響歇父,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜再愈,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一榜苫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧翎冲,春花似錦垂睬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缴渊,卻和暖如春赏壹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背疟暖。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工卡儒, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人俐巴。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓骨望,卻偏偏與公主長得像,于是被迫代替她去往敵國和親欣舵。 傳聞我的和親對象是個殘疾皇子擎鸠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,724評論 2 354