- Wed 22 January 2025
- Neuron
EKS Cluster Setup for Trn1 Multi-Node Workloads
A multi-node job on Trn1 instances under EKS can run as an MPI job,
with one launcher process that starts the work on every node. If the job
runs a fixed command from the container image, each code change needs a
new image, a push to the registry and a new deployment. The setup below
keeps the pods running between changes. Testing a change then takes only
a file sync and an mpirun command.
Cluster layout
The cluster has three parts:
- Trn1 worker nodes run the model. Each worker pod requests the Neuron devices and EFA interfaces of its node.
- A launcher pod runs
mpirun, which starts the processes on the workers. It needs no accelerator, so it can run on a CPU node. - A shared FSx for Lustre volume holds code,
checkpoints and logs. Every pod mounts it at
/shared, and its contents survive pod restarts. The FSx for Lustre CSI driver provides the volume to the pods.
The Neuron
device plugin registers two resources.
aws.amazon.com/neuron allocates whole Neuron devices, and
aws.amazon.com/neuroncore allocates single NeuronCores. The
EFA device
plugin registers EFA interfaces as
vpc.amazonaws.com/efa. A trn1.32xlarge has 16 Neuron
devices and 8 EFA devices, and a trn1n.32xlarge has 16 EFA devices. A
worker pod that uses a whole trn1.32xlarge requests 16 and 8.
EFA traffic between nodes needs a security group that allows all traffic between its members, as the Neuron multi-node setup guide describes. The worker node group uses that security group.
The MPI operator
The Kubeflow MPI
operator runs an MPIJob as one launcher and a set of
workers. With the v2beta1 API and Open MPI, the operator
does the following:
- It creates the worker pods and a headless Service, so each worker has a stable host name.
- It writes a hostfile that lists the workers into a ConfigMap. It
mounts the hostfile in the launcher at
/etc/mpi/hostfileand setsOMPI_MCA_orte_default_hostfile, sompirunfinds the workers without a--hostfileflag. - It generates an SSH key pair, stores it in a Secret and mounts it at
/root/.sshin every pod.mpirunreaches the workers over SSH, so each worker container must run an SSH server. - With
launcherCreationPolicy: WaitForWorkersReady, it creates the launcher only after every worker is ready. The default,AtStartup, creates the launcher at once.
The parts of the spec that change between experiments are the launcher command, the worker command and the number of workers:
apiVersion: kubeflow.org/v2beta1
kind: MPIJob
metadata:
name: trn1-job
spec:
slotsPerWorker: 1
launcherCreationPolicy: WaitForWorkersReady
mpiReplicaSpecs:
Launcher:
replicas: 1
template:
spec:
containers:
- name: launcher
image: <inference image>
command: ["mpirun", "--allow-run-as-root", "-np", "2", "python3", "/shared/run.py"]
Worker:
replicas: 2
template:
spec:
containers:
- name: worker
image: <inference image>
command: ["bash", "-c", "mkdir -p /run/sshd && /usr/sbin/sshd -De"]
resources:
limits:
aws.amazon.com/neuron: 16
vpc.amazonaws.com/efa: 8The worker runs sshd in the foreground (-D)
and logs to stderr (-e). Debian and Ubuntu builds of
sshd refuse to start without the /run/sshd
directory, which a container does not create on its own. The operator’s
example
job also runs sshd -De as its worker command. The full
spec also sets the FSx volume mount and the node selectors.
Long-running pods for development
For development, the launcher command becomes
sleep infinity. The workers already run sshd,
which keeps them alive. All pods stay up until the job is deleted, and
code runs in them interactively:
metadata:
name: <user>-dev
spec:
slotsPerWorker: 1
mpiReplicaSpecs:
Launcher:
replicas: 1
template:
spec:
containers:
- name: launcher
command: ["sleep", "infinity"]
Worker:
replicas: 2
template:
spec:
containers:
- name: worker
command: ["bash", "-c", "mkdir -p /run/sshd && /usr/sbin/sshd -De"]On a shared cluster, a user-name prefix on the job name gives each developer a separate job. The operator labels every pod with the job name and its role, and those labels select the pods:
kubectl apply -f dev.yaml
kubectl get pods -l training.kubeflow.org/job-name=<user>-dev
LAUNCHER=$(kubectl get pods -o name \
-l training.kubeflow.org/job-name=<user>-dev,training.kubeflow.org/job-role=launcher)
kubectl exec -it "$LAUNCHER" -- bashRunning code across the workers
Inside the launcher, mpirun reads the hostfile and
connects to the workers over SSH. With one slot per worker,
-np 2 starts one process on each of the two workers:
mpirun --allow-run-as-root -np 2 hostnameThe code under development lives on the shared volume. The
-x option of Open MPI’s
mpirun exports an environment variable from the
launcher to every process:
export PYTHONPATH=/shared/<user>/src:$PYTHONPATH
mpirun --allow-run-as-root -np 2 -x PYTHONPATH python3 /shared/<user>/run.pyWithout -x PYTHONPATH, the worker processes use the
image’s default PYTHONPATH. They then import the package
installed in the image instead of the working copy on
/shared.
Syncing code from a laptop
rsync can use kubectl exec as its remote shell, so edits
on a laptop reach a pod without exposing an SSH server outside the
cluster. The krsync script from a Server
Fault answer wraps this. The pod needs the rsync
binary, which many images lack. On a Debian or Ubuntu image, the
launcher installs it with:
apt-get update && apt-get install -y rsyncPutting this step in the launcher command avoids repeating it after every restart. A short script syncs a directory to the shared volume through the launcher pod:
# sync.sh
pod=$(kubectl get pods -o name \
-l training.kubeflow.org/job-name=<user>-dev,training.kubeflow.org/job-role=launcher)
pod=${pod#pod/}
./krsync.sh -a ~/work/src/ "$pod":/shared/<user>/src/Running the script in a loop keeps the pod’s copy within a few seconds of the laptop:
while true; do ./sync.sh; sleep 1; done > sync.log 2>&1 &The files land on FSx, so the workers see them as soon as the launcher does.
A separate pod for the AWS CLI
Downloading a checkpoint from S3 needs the AWS CLI, and the inference
image may not include it. A small pod that runs the public
amazon/aws-cli image and mounts the same FSx volume handles
these transfers without changing the inference image:
apiVersion: v1
kind: Pod
metadata:
name: aws-cli
spec:
serviceAccountName: <account with S3 access>
containers:
- name: aws-cli
image: amazon/aws-cli
command: ["sleep", "infinity"]
volumeMounts:
- name: fsx
mountPath: /shared
volumes:
- name: fsx
persistentVolumeClaim:
claimName: <fsx claim>The service account maps to an IAM role with read access to the
bucket, through IAM
roles for service accounts. A file that this pod copies into
/shared is visible to the launcher and the workers.
The development loop
A working session with this setup has six steps:
- Apply the development job and wait for the pods to reach
Running. - Start the sync loop on the laptop.
- Open a shell in the launcher pod.
- Run the test with
mpirun ... -x PYTHONPATH. - Edit on the laptop, wait for the sync, and run the same command again.
- Delete the job with
kubectl delete mpijob <user>-devat the end of the session to release the Trn1 nodes.
A new image is needed only when the dependencies change.
References
If you found this useful, please cite this post using
Senthilkumar Gopal. (Jan 2025). EKS Cluster Setup for Trn1 Multi-Node Workloads. sengopal.me. https://sengopal.me/posts/eks-cluster-setup-for-trn1-multi-node-workloads
or
@article{gopal2025eksclustersetuptrn1,
title = {EKS Cluster Setup for Trn1 Multi-Node Workloads},
author = {Senthilkumar Gopal},
journal = {sengopal.me},
year = {2025},
month = {Jan},
url = {https://sengopal.me/posts/eks-cluster-setup-for-trn1-multi-node-workloads}
}