Project: Volcano (Orchestration & Management / Scheduling & Orchestration)
File: how_to_use_env_plugin.md

Env Plugin is designed for business that a pod should be aware of its index in the task such as and . The indices will be registered as environment variables automatically when the Volcano job is created. For example, a tensorflow job consists of 1 ps and 2 workers. And each worker maps to a slice of raw data. In order to make the workers be aware of its target slice, they get their index in the environment variables. The index keys of the environment variables are `VKTASKINDEX` and `VCTASKINDEX`, they have the same value. The value of the indices is a number which ranges from `0` to `length - 1`. The `length` equals to the number of replicas of the task. It is also the index of the pod in the task. ```yaml apiVersion: batch.volcano.sh/v1alpha1 kind: Job metadata: name: tensorflow-dist-mnist spec: minAvailable: 3 schedulerName: volcano plugins: env: [] ## Env plugin register, note that no values are needed in the array. svc: [] policies: event: PodEvicted action: RestartJob queue: default tasks: replicas: 1 name: ps template: spec: containers: command: sh -c | PS_HOST=`cat /etc/volcano/ps.host | sed 's/$/&:2222/g' | sed 's/^/"/;s/$/"/' | tr "\n" ","`; WORKER_HOST=`cat /etc/volcano/worker.host | sed 's/$/&:2222/g' | sed 's/^/"/;s/$/"/' | tr "\n" ","`; export TFCONFIG={\"cluster\":{\"ps\":[${PSHOST}],\"worker\":[${WORKERHOST}]},\"task\":{\"type\":\"ps\",\"index\":${VKTASK_INDEX}},\"environment\":\"cloud\"}; ## Get the index from the environment variable and configure it in the TF job. python /var/tfdistmnist/dist_mnist.py image: volcanosh/dist-mnist-tf-example:0.0.1 name: tensorflow ports: containerPort: 2222 name: tfjob-port resources: {} restartPolicy: Never replicas: 2 name: worker policies: event: TaskCompleted action: CompleteJob template: spec: containers: command: sh -c | PS_HOST=`cat /etc/volcano/ps.host | sed 's/$/&:2222/g' | sed 's/^/"/;s/$/"/' | tr "\n" ","`; WORKER_HOST=`cat /etc/volcano/worker.host | sed 's/$/&:2222/g' | sed 's/^/"/;s/$/"/' | tr "\n" ","`; export TFCONFIG={\"cluster\":{\"ps\":[${PSHOST}],\"worker\":[${WORKERHOST}]},\"task\":{\"type\":\"worker\",\"index\":${VKTASK_INDEX}},\"environment\":\"cloud\"}; python /var/tfdistmnist/dist_mnist.py image: volcanosh/dist-mnist-tf-example:0.0.1 name: tensorflow ports: containerPort: 2222 name: tfjob-port resources: {} restartPolicy: Never ``` Note: When config env plugin in the tensorflow job above, all the pods will have 2 environment variables `VKTASKINDEX` and `VCTASKINDEX`. The environment variables registered in the `ps` pod are as follows. ``` [root@tensorflow-dist-mnist-ps-0 /] env | grep TASK_INDEX VKTASKINDEX=0 VCTASKINDEX=0 ``` Considering the 2 workers, you will get that their names are `tensorflow-dist-mnist-worker-0` and `tensorflow-dist-mnist-worker-1`. And the corresponding values of the index environment variables are `0` and `1`. ``` [root@tensorflow-dist-mnist-worker-0 /] env | grep TASK_INDEX VKTASKINDEX=0 VCTASKINDEX=0 ``` ``` [root@tensorflow-dist-mnist-worker-1 /] env | grep TASK_INDEX VKTASKINDEX=1 VCTASKINDEX=1 ``` Because of historical reasons, environment variables `VKTASKINDEX` and `VCTASKINDEX` both exist, `VKTASKINDEX` will be deprecated in the future releases. No value are needed when register env plugin in the volcano job.