CPU allocation

This page describes how to specify the number of CPUs to allocate for each Cloud Run container instance.

By default Cloud Run container instances are allocated 1 CPU instance. You can change this number.

Setting and updating CPU allocation

By default, 1 CPU is allocated for each container instance.

Like any configuration change, setting CPU allocation leads to the creation of a new revision. Subsequent revisions will also automatically get this allocation unless you make explicit updates to change it.

You can set CPU allocation using the Cloud Console or the gcloud command line when you create a new service or deploy a new revision:

Console

  1. Go to Cloud Run

  2. Click CREATE SERVICE if you are setting memory limits on a new service you are deploying to. If you are setting limits on an existing service, then click on the service, then click DEPLOY NEW REVISION.

  3. Click SHOW OPTIONAL SETTINGS.

    image

  4. Select the desired CPU allocation from the dropdown list. For Cloud Run (fully managed), you can select a value of 1 or 2 vCPUs. For Cloud Run for Anthos, you can select numbers in Kubernetes CPU units. For example, specify 1 for 1 CPU, 400m for 0.4 CPU, and so forth.

  5. Click Create or Deploy.

Command line

You can update the memory allocation of a given service by using the following command:

  • If you are using Cloud Run (fully managed):

    gcloud alpha run services update SERVICE --cpu CPU
  • If you are using Cloud Run for Anthos on Google Cloud:

    gcloud run services update SERVICE --cpu CPU

Replace SERVICE with the name of your service and CPU with the desired CPU allocation. For Cloud Run (fully managed), specify the value 1 or 2. For Cloud Run for Anthos, specify a value in Kubernetes CPU units. For example, specify 1 for 1 CPU, 400m for 0.4 CPU, and so forth.

You can also set CPU allocation during deployment using the command:

  • If you are using Cloud Run (fully managed):

    gcloud alpha run deploy --image gcr.io/PROJECT-ID/IMAGE --cpu CPU
  • If you are using Cloud Run for Anthos on Google Cloud:

    gcloud run deploy --image gcr.io/PROJECT-ID/IMAGE --cpu CPU

Replace

  • PROJECT-ID with your Google Cloud project ID.
  • IMAGE with the URL of your container image.
  • CPU with the values described above.

YAML

You can download and view existing service configuration using the gcloud beta run services describe --format yaml command, which yields results in YAML format. You can then modify the fields described below and upload the modified YAML using the gcloud beta run services replace command. Make sure you only modify fields as documented.

To view and download configuration:

  1. gcloud beta run services describe SERVICE --format yaml > service.yaml
  2. Update the cpu attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        spec:
          containers:
          - image: IMAGE
            resources:
              limits:
                cpu: CPU

    Replace

    • SERVICE with the name of your Cloud Run service
    • IMAGE with the URL of your container image.
    • CPU with the desired CPU value. For Cloud Run (fully managed), specify the value 1 or 2. For Cloud Run for Anthos, specify a value in Kubernetes CPU units. For example, specify 1 for 1 CPU, 400m for 0.4 CPU, and so forth.
  3. Replace the service with its new configuration using the following command:

    gcloud beta run services replace service.yaml