Configuring CPU allocation

Learn how to specify the number of CPUs to allocate for each Knative serving container instance. By default, Knative serving does not specify a minimum or maximum number of CPUs. Instead, your services are limited by the amount of resources available. Learn more about CPU resources in the container runtime contract.

Setting and updating CPU allocation

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

You can set CPU allocation using the Google Cloud console, the Google Cloud CLI, or a YAML file when you deploy a new service or update an existing service and deploy a revision:

Console

  1. Go to Knative serving in the Google Cloud console:

    Go to Knative serving

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit & Deploy New Revision.

  3. Under Advanced settings, click Container.

    image

  4. Select the desired CPU allocation from the dropdown list. 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 Next to continue to the next section.

  6. In the Configure how this service is triggered section, select which connectivity you would like to use to invoke the service.

  7. Click Create to deploy the image to Knative serving and wait for the deployment to finish.

Command line

  • For existing services, set the number of CPUs to allocate by running the gcloud run services update command with the --cpu parameter:

    gcloud run services update SERVICE --cpu CPU
    

    Replace:

    • SERVICE with the name of your service.
    • CPU with the desired CPU allocation. Specify a value in Kubernetes CPU units. For example, specify 1 for 1 CPU, 400m for 0.4 CPU, and so forth.
  • For new services, set the number of CPUs to allocate by running the gcloud run deploy command with the --cpu parameter:

    gcloud run deploy SERVICE --image=IMAGE_URL --cpu CPU
    

    Replace:

    • SERVICE with the name of your service.
    • IMAGE_URL with a reference to the container image, for example, gcr.io/myproject/my-image:latest.
    • CPU with the desired CPU allocation. Specify a value in Kubernetes CPU units. For example, specify 1 for 1 CPU, 400m for 0.4 CPU, and so forth.

YAML

You can download the configuration of an existing service into a YAML file with the gcloud run services describe command by using the --format=export flag. You can then modify that YAML file and deploy those changes with the gcloud beta run services replace command. You must ensure that you modify only the specified attributes.

  1. Download the configuration of your service into a file named service.yaml on local workspace:

    gcloud run services describe SERVICE --format export > service.yaml

    Replace SERVICE with the name of your Knative serving service.

  2. In your local file, 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 Knative serving service
    • IMAGE with the URL of your container image.
    • CPU with the desired CPU value. 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