Skip to main content

Kubernetes Gang Scheduling with PodGroup

Aswin Vijayan
Aswin Vijayan
7 min read

In this guide, you will learn what gang scheduling is, how it works in Kubernetes, and how to test it on a Kubernetes cluster.

Let's start with a real-world problem.

Let's say you manage a shared GPU cluster for the ML teams in your company. Two teams submit distributed training jobs at the same time. Each job needs four GPU workers, and each worker uses one GPU.

But the cluster has only six free GPUs.

The default scheduler places Pods one by one. So it might place three workers from each job. Now both jobs have three workers running and one worker in Pending.

Here is the problem.

A distributed training job waits for all its workers to join before it starts training. So neither job can start. Six GPUs are allocated, but no training happens. Both jobs stay stuck until GPUs free up or someone deletes one of the jobs.

This is where gang scheduling helps.

Why the Default Scheduler Fails Distributed Jobs

Kubernetes normally schedules Pods based on available cluster resources. It doesn't automatically check whether all Pods of a workload can be scheduled together.

This works well for most applications, but it becomes a problem when multiple Pods depend on one another to complete a task.

For example, let's say a distributed ML training job requires four GPU workers to run together. Kubernetes might schedule two workers, while the remaining two stay Pending because of insufficient resources.

The training job can't start until all four workers are running. So the two scheduled Pods hold expensive GPUs and do no work.

This is where Kubernetes Gang Scheduling helps.

💡
In computer science, gang scheduling means running a group of related processes at the same time, on different processors.

What Is Gang Scheduling?

Gang Scheduling allows Kubernetes to schedule a group of related Pods together instead of handling each Pod individually.

Instead of scheduling each Pod independently, the scheduler checks whether enough resources are available to schedule the required number of Pods together.

It follows an all-or-nothing scheduling approach. Meaning, if the cluster cannot schedule all the Pods in a job, none of the Pods in that job will be scheduled.

This prevents partially scheduled workloads from holding resources without making useful progress.

The following image shows how gang scheduling checks resource availability before scheduling related pods together.

this image shows how gang scheduling schedules related pods together

Gang Scheduling is particularly useful for distributed machine learning and parallel batch jobs where multiple workers depend on one another.

Now, let's learn about how it works.

💡
Gang scheduling is not new in Kubernetes. Before it became part of the default scheduler, teams added it with extra schedulers and plugins.

For example, the Coscheduling plugin in the kubernetes-sigs/scheduler-plugins project uses its own PodGroup custom resource

How Gang Scheduling Works in Kubernetes

To understand gang scheduling, you need to be clear about three key parts:

  • PodGroup: the object that groups related Pods.
  • minCount: the minimum number of Pods in the group.
  • Pod link: the field in each Pod that points to its PodGroup.

A PodGroup is a Kubernetes object that represents a set of Pods that must be scheduled together.

You set the scheduling policy in the PodGroup. To get gang scheduling, you use the gang policy.

minCount: The Minimum Group Size

Inside the gang policy, the minCount field sets the minimum group size. Here is how it looks:

schedulingPolicy:
  gang:
    minCount: 3

In this example, minCount: 3 means two things:

  • The scheduler doesn't start until at least 3 Pods of the group exist.
  • The scheduler places the Pods only if at least 3 of them can fit on nodes at the same time.

Many people find one point confusing here. minCount doesn't create Pods. Your workload creates them. For example, a Job with parallelism: 3 creates three Pods. minCount only sets the threshold the scheduler checks.

Linking Pods to a PodGroup

Next, each Pod needs to say which PodGroup it belongs to. You do this with the schedulingGroup field in the Pod spec. For example, in a Job Object.

apiVersion: batch/v1
kind: Job
metadata:
  name: training-job
spec:
  parallelism: 3
  completions: 3
  completionMode: Indexed
  template:
    spec:
      schedulingGroup:
        podGroupName: demo-podgroup
      restartPolicy: Never
      containers:
      - name: worker
        image: busybox:1.36
        command: ["sh", "-c", "echo Worker started; sleep 600"]

The podGroupName field holds the name of the PodGroup. Every Pod with the same podGroupName belongs to the same gang.

Here is how it works.

  • The scheduler holds the Pods until the PodGroup exists and at least minCount Pods are created.
  • The scheduler looks for nodes for all the Pods in one scheduling cycle. It checks CPU, memory, GPUs, and placement rules like nodeSelector.
  • If at least minCount Pods fit, the scheduler places them on their nodes. If not, it places none of them. The Pods wait until resources free up. Meanwhile, the scheduler can still schedule other workloads in the cluster.

The following image compares two cases. In Case 1, the node has enough resources and all Pods are placed. In Case 2, the node lacks enough resources, so no Pods are placed.

this image shows what happens to job pods if the node has enough resources for all pods and what happens if the node does not have enough resources for all pods

So how do you turn this on? Gang scheduling is not enabled by default, so let's set it up first.

Enabling Gang Scheduling in Kubernetes

⚠️
As of the time of writing this blog, Native Gang Scheduling is available in Kubernetes v1.37 in beta, but it is disabled by default. To use it, you need to enable the GenericWorkload feature gate and the scheduling.k8s.io/v1beta1 API.

The following steps were run and tested on a Kubernetes v1.37 kubeadm cluster. To use Gang Scheduling, we need to enable feature gates on the API server, controller manager, and scheduler.

First, enable the feature on the API server. Open the API server manifest,

sudo vi /etc/kubernetes/manifests/kube-apiserver.yaml

Then add these flags. This enables the feature gate and required API resources.

- --feature-gates=GenericWorkload=true
- --runtime-config=scheduling.k8s.io/v1beta1=true

Saving the file will restart the API server. Once it's restarted, verify that the feature gate is enabled and scheduling API resources are available.

$ kubectl get --raw /metrics | grep kubernetes_feature_enabled | grep GenericWorkload

kubernetes_feature_enabled{name="GenericWorkload",stage="BETA"} 1

$ kubectl api-resources --api-group=scheduling.k8s.io

NAME             SHORTNAMES  APIVERSION                  NAMESPACED   KIND
podgroups                    scheduling.k8s.io/v1beta1   true         PodGroup
priorityclasses  pc          scheduling.k8s.io/v1        false       PriorityClass
workloads                    scheduling.k8s.io/v1beta1   true         Workload

From the output, the 1 in the first command confirms that the feature gate is enabled and the availability of podgroups and workloads confirms that the required API resources are available.

Next, enable the feature on the controller manager. Open the controller manager manifest,

sudo vi /etc/kubernetes/manifests/kube-controller-manager.yaml

And add the following flag.

- --feature-gates=GenericWorkload=true

Then, do the same for the scheduler. Open the scheduler manifest,

sudo vi /etc/kubernetes/manifests/kube-scheduler.yaml

And add the following flag.

- --feature-gates=GenericWorkload=true

Enable the feature gate on all three components because the API server exposes the scheduling APIs, the scheduler enforces Gang Scheduling, and the controller manager supports the related workload-controller functionality.

Let's test gang scheduling with an example job.

Testing Gang Scheduling

Let's test Gang Scheduling using a simple Job. We will create a PodGroup that requires three Pods and a Job configured to run three Pods in parallel.

However, we will restrict the Pods to a node that doesn't have enough available CPU to run all three pods to see what happens when all pods are not able to run.

First, create a PodGroup by running the following command.

cat <<EOF | kubectl apply -f -
apiVersion: scheduling.k8s.io/v1beta1
kind: PodGroup
metadata:
  name: demo-podgroup
spec:
  schedulingPolicy:
    gang:
      minCount: 3
EOF

Then, use the following command to verify that the resource is created.

$ kubectl get podgroup

NAME            POLICY   WORKLOAD   STATUS    AGE
demo-podgroup   Gang     <none>     Pending   21s

You can see the status is Pending. Don't worry about that, it will change when a workload uses it.

Now, create a Kubernetes Job by running the following command.

In the job below, you can see the podGroupName is specified under the schedulingGroup block. We also use nodeSelector to restrict the Pods to node01 and request 600m CPU for each Pod.

💡
Make sure node01 matches a node hostname label in your cluster, or change it to match what is on your cluster. You may also need to adjust the CPU requests based on the node's available resources to reproduce the same behavior.

We did it like that to show that if all the job pods do not have enough resources to schedule, all pods stay in a pending state.

kubectl apply -f - <<EOF
apiVersion: batch/v1
kind: Job
metadata:
  name: gang-Scheduling-demo
spec:
  parallelism: 3
  completions: 3
  completionMode: Indexed
  template:
    spec:
      schedulingGroup:
        podGroupName: demo-podgroup
      nodeSelector:
        kubernetes.io/hostname: node01
      restartPolicy: Never
      containers:
      - name: worker
        image: busybox:1.36
        command: ["sh", "-c", "echo Worker started; sleep 600"]
        resources:
          requests:
            cpu: "600m"
            memory: "128Mi"
          limits:
            cpu: "600m"
            memory: "256Mi"
EOF

In this configuration, parallelism: 3 tells the Job controller to run three worker Pods concurrently, while completions: 3 requires three successful Pod completions.

The parallelism setting is important for our Gang Scheduling test because we need three Pods to exist at the same time

If you list the job pods, you can see the pods in a pending state.

$ kubectl get po

NAME                           READY   STATUS    RESTARTS   AGE

gang-Scheduling-demo-0-cbk4j   0/1     Pending   0          10s
gang-Scheduling-demo-1-2fjcc   0/1     Pending   0          10s
gang-Scheduling-demo-2-f56m9   0/1     Pending   0          10s

Now, if you check the podgroup again, you can see the status changed to Unschedulable because of insufficient resources for one pod.

$ kubectl get podgroup

NAME            POLICY   WORKLOAD   STATUS          AGE
demo-podgroup   Gang     <none>     Unschedulable   37s

Also, here's another important point. In the PodGroup, we have mentioned that a minCount of 3 is required for gang scheduling.

podgroup yaml configuration showing the gang scheduling policy with minCount set to 3

If the job is configured to run less than 3 pods, even if the cluster has enough resources to schedule the pods, they will be in a pending state.

Clean Up

Delete the Job and the PodGroup

kubectl delete job gang-scheduling-demo
kubectl delete podgroup demo-podgroup

When Should You Use Gang Scheduling?

Use gang scheduling when Pods can't do any useful work until their peers are running. For example:

  • Distributed ML training jobs, where every worker must join before training starts.
  • Parallel batch jobs, where Pods exchange data with each other.

Don't use it for workloads where each Pod works on its own. For example, the replicas of a web API can each serve requests alone. Gang scheduling would only delay them.

Subscribe to our newsletter