Use ML Diagnostics with Cluster Toolkit

Cluster Toolkit is an open-source tool that uses customizable YAML blueprints and the gcluster CLI to provision AI and machine learning (ML) clusters on Google Cloud. Cluster Toolkit integrates ML Diagnostics directly into its Google Kubernetes Engine (GKE) blueprints so that your deployed TPU clusters are ready to collect workload metrics, configurations, and profiles.

When you enable ML Diagnostics in a Cluster Toolkit blueprint, such as the gke-tpu-v6e blueprint or the gke-tpu-7x blueprint, Cluster Toolkit automates the cluster configuration and Identity and Access Management (IAM) setup steps. You don't need to manually complete the steps in IAM permissions or Configure GKE or self-managed Kubernetes.

Before you begin

Before you deploy a cluster and run workloads with ML Diagnostics, complete the following prerequisites:

Automated ML Diagnostics configuration

Cluster Toolkit automates the in-cluster components, namespace labels, IAM service accounts, and version checks required for ML Diagnostics. Setting the enable_ml_diagnostics setting to true in your blueprint is the only action required to turn on this automated setup, which prepares your GKE cluster to inject workload metadata, authenticate pods, and collect metrics and profiles without manual cluster configuration.

When you set the enable_ml_diagnostics setting to true in a Cluster Toolkit GKE blueprint, Cluster Toolkit automatically provisions and configures the following resources:

  • Managed ML Diagnostics components. Configures the managed_machine_learning_diagnostics_config block with the enabled field set to true on the GKE cluster. This configuration deploys and manages the mldiagnostics-injection-webhook and mldiagnostics-connection-operator components and the MLDiagnosticsConnection custom resource definitions without manually installing Helm charts or the cert-manager add-on.
  • Automatic namespace labeling. Creates the target workload namespace from the vars.user_namespace variable and applies the managed-mldiagnostics-gke: "true" label to the namespace. Any JobSet, LeaderWorkerSet, or RayJob workload deployed in that namespace automatically receives pod metadata injection.
  • Service accounts and Workload Identity Federation for GKE. Creates the default gke-wl-sa Google Cloud service account and associates it with the default workload-identity-k8s-sa Kubernetes service account in your workload namespace by using Workload Identity Federation for GKE. Cluster Toolkit grants the following predefined IAM roles on the project to the Google Cloud service account:

    • roles/hypercomputecluster.editor
    • roles/logging.logWriter
    • roles/monitoring.metricWriter
    • roles/monitoring.viewer
    • roles/stackdriver.resourceMetadata.writer
    • roles/storage.admin
    • roles/artifactregistry.reader
    • roles/container.admin
  • GKE version validation. Enforces a Terraform precondition check that verifies that the GKE cluster runs GKE version 1.35.0-gke.3065000 or later.

Enable ML Diagnostics in Cluster Toolkit

You can verify or customize ML Diagnostics settings in a Cluster Toolkit blueprint, deploy your GKE cluster, and confirm that the cluster resources are ready for ML workloads. ML Diagnostics is enabled by default across all standard GKE TPU blueprints in Cluster Toolkit, including the gke-tpu-v4, gke-tpu-v5e, gke-tpu-v5p, gke-tpu-v6e, and gke-tpu-7x blueprints and their -advanced blueprint variants. For other GKE blueprints, such as GPU or custom cluster blueprints, the enable_ml_diagnostics setting defaults to false in the gke-cluster module.

To enable or customize ML Diagnostics in a Cluster Toolkit blueprint, complete the following steps:

  1. Configure blueprint settings: define the workload service account roles, Workload Identity Federation for GKE binding, and ML Diagnostics cluster settings in your blueprint YAML file.
  2. Deploy the cluster: build the gcluster binary and provision the GKE cluster and supporting Google Cloud resources from your blueprint.
  3. Verify the cluster configuration: connect to the deployed cluster and confirm that the workload namespace label and Kubernetes service account are active.

Configure blueprint settings

Configure the service account and cluster modules in your blueprint so that Cluster Toolkit provisions the required IAM roles, identity binding, and GKE version. In your blueprint YAML file, such as the examples/gke-tpu-v6e/gke-tpu-v6e.yaml file, verify the following module settings:

  • In the workload_service_account module, include the hypercomputecluster.editor role in the project_roles list.
  • In the modules/scheduler/gke-cluster module, set the enable_ml_diagnostics setting to true, set the configure_workload_identity_sa setting to true, and set the version_prefix setting to "1.35.".

The following example shows the relevant blueprint configuration:

vars:
  # The namespace to configure and label for running ML workloads
  user_namespace: default

deployment_groups:
- group: primary
  modules:
  - id: workload_service_account
    source: modules/project/service-account
    settings:
      name: gke-wl-sa
      project_roles:
      - logging.logWriter
      - monitoring.metricWriter
      - monitoring.viewer
      - stackdriver.resourceMetadata.writer
      - storage.objectUser
      - artifactregistry.reader
      - hypercomputecluster.editor
  - id: gke-tpu-v6e-cluster
    source: modules/scheduler/gke-cluster
    use: [gke-tpu-v6e-net-0, workload_service_account]
    settings:
      configure_workload_identity_sa: true
      namespace: $(vars.user_namespace)
      enable_ml_diagnostics: true
      version_prefix: "1.35."
      release_channel: REGULAR

Deploy the cluster

Compile the blueprint and provision the GKE cluster, TPU node pools, service accounts, and managed ML Diagnostics components in your Google Cloud project. From the root directory of your cloned cluster-toolkit repository, build the gcluster binary if needed, and then deploy your blueprint by using the gcluster deploy command:

test -x ./gcluster || make
./gcluster deploy \
    -d examples/gke-tpu-v6e/gke-tpu-v6e-deployment.yaml \
    examples/gke-tpu-v6e/gke-tpu-v6e.yaml

Verify the cluster configuration

After deployment completes, connect to the cluster to confirm that the workload namespace label and Kubernetes service account are ready for ML workloads.

Fetch your cluster credentials, and verify that the workload namespace has the managed-mldiagnostics-gke=true label and that the workload-identity-k8s-sa service account exists:

gcloud container clusters get-credentials \
    DEPLOYMENT_NAME \
    --region=REGION \
    --project=PROJECT_ID
kubectl get namespace USER_NAMESPACE --show-labels
kubectl get serviceaccount -n USER_NAMESPACE

Replace the following:

  • DEPLOYMENT_NAME: the name of your GKE cluster
  • REGION: the compute region for your cluster, such as the us-central1 region
  • PROJECT_ID: your Google Cloud project ID
  • USER_NAMESPACE: the workload namespace in the vars.user_namespace variable, such as the default namespace

Run a sample workload

Cluster Toolkit provides a sample workload in the examples/gke-tpu-v6e/ml-diagnostics-sample-workload-test directory to validate the ML Diagnostics setup on your GKE TPU cluster. The sample directory includes the following files:

Build and push the sample container image

Package the JAX test script and ML Diagnostics SDK into a container image in Artifact Registry so that your GKE cluster can pull and run the sample workload. From the examples/gke-tpu-v6e/ml-diagnostics-sample-workload-test directory in your cloned cluster-toolkit repository, create an Artifact Registry Docker repository, and then build and push the container image:

export REGION=REGION
export PROJECT_ID=PROJECT_ID
export REPO_NAME=mldiagnostics-tests
export IMAGE_URI="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/sample-workload:latest"

gcloud artifacts repositories create "${REPO_NAME}" \
    --repository-format=docker \
    --project="${PROJECT_ID}" \
    --location="${REGION}"

docker build -t "${IMAGE_URI}" .
docker push "${IMAGE_URI}"

Replace the following:

  • REGION: the region for your Artifact Registry repository and cluster, such as the us-central1 region
  • PROJECT_ID: your Google Cloud project ID

Configure the sample job manifest

Customize the Kubernetes JobSet manifest to target your TPU topology, attach the workload service account, and set your Cloud Storage profile output path. Update the sample_job.yaml manifest to match your Cluster Toolkit blueprint deployment:

apiVersion: v1
kind: Service
metadata:
  name: tpu-discovery-service
  namespace: USER_NAMESPACE
spec:
  clusterIP: None
  selector:
    jobset.sigs.k8s.io/jobset-name: sample-tpu-jobset
---
apiVersion: jobset.x-k8s.io/v1alpha2
kind: JobSet
metadata:
  name: sample-tpu-jobset
  namespace: USER_NAMESPACE
  labels:
    diagon-enabled: "true"
    kueue.x-k8s.io/queue-name: user-queue
spec:
  network:
    enableDNSHostnames: true
    subdomain: tpu-discovery-service
  replicatedJobs:
  - name: tpu-slice
    replicas: 1
    template:
      spec:
        parallelism: 1
        completions: 1
        completionMode: Indexed
        backoffLimit: 100
        template:
          spec:
            serviceAccountName: K8S_SERVICE_ACCOUNT_NAME
            automountServiceAccountToken: true
            restartPolicy: Never
            nodeSelector:
              cloud.google.com/gke-tpu-accelerator: TPU_ACCELERATOR
              cloud.google.com/gke-tpu-topology: TPU_TOPOLOGY
            containers:
            - name: jax-job
              image: WORKLOAD_IMAGE
              env:
              - name: PROJECT_ID
                value: PROJECT_ID
              - name: REGION
                value: REGION
              - name: GCS_PATH
                value: GCS_BUCKET_PATH
              ports:
              - containerPort: 8471
              - containerPort: 8431
              securityContext:
                privileged: true
              resources:
                requests:
                  google.com/tpu: TPU_CHIPS_PER_NODE
                limits:
                  google.com/tpu: TPU_CHIPS_PER_NODE

Replace the following:

  • USER_NAMESPACE: the workload namespace from the vars.user_namespace variable in your blueprint, such as the default namespace
  • K8S_SERVICE_ACCOUNT_NAME: the Kubernetes service account for Workload Identity Federation for GKE, such as the default workload-identity-k8s-sa service account
  • TPU_ACCELERATOR: the accelerator label from your blueprint, such as the tpu-v6e-slice label for TPU v6e or the tpu7x label for TPU7x
  • TPU_TOPOLOGY: the topology label from your blueprint, such as the 4x4 topology for TPU v6e or the 2x2x1 topology for TPU7x
  • WORKLOAD_IMAGE: the container image URI from the IMAGE_URI environment variable in the preceding step
  • GCS_BUCKET_PATH: an existing Cloud Storage bucket path for profiles, such as the gs://example-ml-diagnostics-bucket/tests path
  • TPU_CHIPS_PER_NODE: the number of TPU chips per node for your machine type, such as 4

Submit the job and view diagnostics

Run the sample JobSet workload on your TPU slice to generate a machine learning run, workload metrics, and XProf profiles in the Google Cloud console. Submit the JobSet manifest and inspect the pod logs to retrieve the MLRun URL for the Google Cloud console:

kubectl apply -f sample_job.yaml
kubectl get all -n USER_NAMESPACE
kubectl logs POD_NAME \
    -n USER_NAMESPACE

Replace POD_NAME with the name of a workload pod from the command output that starts with the sample-tpu-jobset-tpu-slice- prefix.

After the job starts, open the MLRun URL from the pod logs to inspect your workload configurations, metrics, and XProf profiles in the Google Cloud console. For more information, see View machine learning runs with ML Diagnostics.