Using HTTP/2 (services)

For Cloud Run services, Cloud Run by default downgrades HTTP/2 requests to HTTP/1 when those requests are sent to the container. If you want to explicitly set your service to use HTTP/2 end-to-end, with no such downgrading, you can configure it for HTTP/2. This page shows how to do the configuration.

For more information on invoking services using HTTP, refer to Invoking with an HTTPS Request.

Before you configure

Your Cloud Run service must handle requests in HTTP/2 cleartext (h2c) format. Google's frontend-serving infrastructure terminates TLS and then forwards the h2c traffic to Cloud Run and to your container through an encrypted channel.

To confirm that your service supports h2c requests, test the service locally using this cURL command:

curl -i --http2-prior-knowledge http://localhost:PORT

Setting and updating HTTP/2 end-to-end

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 specify the use of HTTP/2 end-to-end using the Google Cloud console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click the service, then click Edit and deploy new revision.

  3. If you are configuring a new service, fill out the initial service settings page as desired, then click Container(s), volumes, networking, security to expand the service configuration page.

  4. Click the Networking tab.

    image

    • Select the checkbox Enable http/2 connections
  5. Click Create or Deploy.

Command line

You can update a given service to use HTTP/2 by using the following command:

gcloud run services update SERVICE --use-http2

Replace SERVICE with the name of your service.

You can also set your service to use HTTP/2 during deployment using the command:

gcloud run deploy --image IMAGE_URL --use-http2

Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG

YAML

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

  1. To view and download the configuration:

    gcloud run services describe SERVICE --format export > service.yaml
  2. Update ports with the nameh2c and containerPort with the port of your choice, as shown in the following example:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          name: REVISION
        spec:
          containers:
          - image: IMAGE_URL
            ports:
            - name: h2c
              containerPort: 8080

    Replace

    • SERVICE with the name of your Cloud Run service
    • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
    • REVISION with a new revision name or delete it (if present). If you supply a new revision name, it must meet the following criteria:
      • Starts with SERVICE-
      • Contains only lowercase letters, numbers and -
      • Does not end with a -
      • Does not exceed 63 characters
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Add the following to a google_cloud_run_v2_service resource in your Terraform configuration, under template.containers. If your container listens for HTTP requests on a port other than 8080, replace 8080 with that port number.

resource "google_cloud_run_v2_service" "default" {
  name     = "cloudrun-service-h2c"
  location = "us-central1"

  template {
    containers {
      image = "us-docker.pkg.dev/cloudrun/container/hello"
      # Enable HTTP/2
      ports {
        name           = "h2c"
        container_port = 8080
      }
    }
  }
}

View http/2 settings

To view the current http/2 settings for your Cloud Run service:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click the service you are interested in to open the Service details page.

  3. Click the Revisions tab.

  4. In the details panel at the right, the http/2 setting is listed under the Networking tab.

Command line

  1. Use the following command:

    gcloud run services describe SERVICE
  2. Locate the http/2 setting in the returned configuration.