<@ULVA73B9P> How can I limit the resources of flow...
# ask-marvin
j
@Marvin How can I limit the resources of flows created in a Kubernetes type workpool?
m
thought for 143.5 seconds
You control CPU/memory for Kubernetes runs by configuring the work pool’s base job template (defaults for all flows), then optionally overriding per-deployment or per-run. Here are the three ways: 1) Set defaults on the Kubernetes work pool (Base job template) - In the UI: Work Pools → your Kubernetes pool → edit Base job template JSON; add resources to the container spec and (optionally) expose variables to make them easy to override. - Via CLI:
Copy code
prefect work-pool get-default-base-job-template --type kubernetes > base.json
  # edit base.json, then
  prefect work-pool update --base-job-template base.json my-k8s-pool
- Example base template fragment (JSON) that adds resources and exposes variables:
Copy code
{
  "variables": {
    "cpu_request": {
      "title": "CPU Request",
      "type": "string",
      "default": null
    },
    "cpu_limit": {
      "title": "CPU Limit",
      "type": "string",
      "default": null
    },
    "memory_request": {
      "title": "Memory Request",
      "type": "string",
      "default": null
    },
    "memory_limit": {
      "title": "Memory Limit",
      "type": "string",
      "default": null
    }
  },
  "job_configuration": {
    "job_manifest": {
      "apiVersion": "batch/v1",
      "kind": "Job",
      "spec": {
        "template": {
          "spec": {
            "containers": [
              {
                "name": "prefect-job",
                "image": "{{ image }}",
                "args": "{{ command }}",
                "env": "{{ env }}",
                "resources": {
                  "requests": {
                    "cpu": "{{ cpu_request }}",
                    "memory": "{{ memory_request }}"
                  },
                  "limits": {
                    "cpu": "{{ cpu_limit }}",
                    "memory": "{{ memory_limit }}"
                  }
                }
              }
            ],
            "restartPolicy": "Never"
          }
        }
      }
    }
  }
}
- If you prefer static defaults, you can replace the
{{ ... }}
with literal values like
"500m"
or
"1Gi"
. 2) Override per deployment (prefect.yaml) - Add job variables under the deployment’s work_pool:
Copy code
deployments:
    - name: heavy-flow
      entrypoint: flows/heavy.py:flow
      work_pool:
        name: kubernetes
        job_variables:
          cpu_request: "2"
          cpu_limit: "4"
          memory_request: "2Gi"
          memory_limit: "4Gi"
- Deploy with:
Copy code
prefect deploy -n heavy-flow
3) Override per run (one-off) - CLI:
Copy code
prefect deployment run "flow-name/deployment-name" \
    --job-variable cpu_request=2 \
    --job-variable cpu_limit=4 \
    --job-variable memory_request=1Gi \
    --job-variable memory_limit=2Gi
- UI: Deployment → Run → Custom run → add the same keys in Job Variables. Notes - Precedence: run overrides > deployment job_variables > work pool base template > Kubernetes defaults. - Use Kubernetes quantity formats (CPU: 500m, 1, 2; Memory: 512Mi, 1Gi). - Make sure your Kubernetes base job template actually references the variables you plan to set. - Helpful commands we verified: - List pools:
prefect work-pool ls
- Inspect a pool:
prefect work-pool inspect my-k8s-pool
- Get default base template:
prefect work-pool get-default-base-job-template --type kubernetes
- Update base template:
prefect work-pool update --base-job-template base.json my-k8s-pool
Docs - Run flows on Kubernetes - Customize base job templates - Override job variables - Manage work pools If you tell me whether you want global defaults or per-deployment/per-run limits, I can tailor an example exactly to your setup (and include any other k8s fields like node selectors or tolerations).