Setting a maximum number of container instances

Learn how to set the maximum number of container instances that can be used for your Knative serving service. Specifying maximum instances in Knative serving allows you to limit the scaling of your service in response to incoming requests. Use this setting as a way to control your costs or to limit the number of connections to a backing service, such as to a database.

For more information on the way Knative serving autoscales container instances, refer to Instance autoscaling.

Setting and updating maximum instances

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.

By default, the number of container instances can scale infinitely. You can set an upper bound for the maximum number of instances using the Google Cloud console, or the Google Cloud CLI 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. In the field labelled Maximum number of instances, specify the desired maximum number of container instances.

  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 maximum number of container instances by running the gcloud run services update command with the --max-instances parameter:

    gcloud run services update SERVICE --max-instances MAX-VALUE

    Replace:

    • SERVICE with the name of your service.
    • MAX-VALUE with the desired maximum number of container instances. Specify default to clear any maximum instance setting.
  • For new services, set the maximum number of container instances by running the gcloud run deploy command with the --max-instances parameter:

    gcloud run deploy SERVICE --image=IMAGE_URL --max-instances MAX-VALUE

    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.
    • MAX-VALUE with the desired maximum number of container instances. Specify default to clear any maximum instance setting.

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 autoscaling.knative.dev/maxScale: attribute:

    spec:
     template:
       metadata:
         annotations:
           autoscaling.knative.dev/maxScale: 'MAX-INSTANCE' 

    Replace

    • MAX-INSTANCE with the desired maximum number.
  3. Replace the service with its new configuration using the following command:

    gcloud beta run services replace service.yaml