Cloud Run lets you specify which revisions should receive traffic and to specify traffic percentages that are received by a revision. This feature lets you rollback to a previous revision, gradually deploy a revision, and split traffic between multiple revisions. This page describes how to use this feature to manage traffic to your Cloud Run revisions.
Note that traffic routing adjustments are not instantaneous. When you change traffic for revisions, all requests being processed will continue to completion. In flight requests won't be dropped and may be directed to either a new revision or a previous revision during the transition period.
Traffic splitting and session affinity
If you are splitting traffic between multiple revisions with session affinity enabled, see Session affinity and traffic splitting for details on the effect of session affinity on traffic splitting.
Lifecycle of traffic splits
If you split traffic between multiple revisions or assigned traffic to a previous revision, all subsequent deployments use that traffic split pattern going forward. To return to just using the latest revision without traffic splitting, send all traffic to the latest revision.
Required roles
To get the permissions that you need to manage Cloud Run services and revisions, ask your administrator to grant you the following IAM roles:
-
Cloud Run Developer (
roles/run.developer
) on your Cloud Run service -
Service Account User (
roles/iam.serviceAccountUser
) on the service identity
For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run service interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.
Roll back to a previous revision
To roll back to a previous revision:
Console
In the Google Cloud console, go to Cloud Run:
Locate the service in the services list, and click it.
Click the Revisions tab to show the list of current revisions for that service.
In the list of revisions, click the ellipsis icon to the right of the revision you are rolling back:
Click Manage Traffic to display the manage traffic form:
- Select the previous revision you want to roll back to in the drop-down list.
- Set that previous revision's traffic percentage to 100.
- Set the currently serving revision's percentage to 0.
- Click Save.
gcloud
Use the following command:
gcloud run services update-traffic SERVICE --to-revisions REVISION=100
- Replace SERVICE with the name of the service.
- Replace REVISION with the name of the revision you are rolling back to.
YAML
If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:
gcloud run services describe SERVICE --format export > service.yaml
Below the
spec
attribute, locate and update thetraffic
attribute to the following:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: ... traffic: - revisionName: REVISION percent: 100
Replace
- REVISION with the name of the revision you are rolling back to.
Replace the service with its new configuration using the following command:
gcloud run services replace service.yaml
Wait for the update to complete: you should see a message that the revision you are rolling back from has been deployed and is serving
0
percent of traffic.
Terraform
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
Add the following to your .tf
file:
Gradual roll out for revisions
To roll out a new revision gradually:
Console
In the Google Cloud console, go to Cloud Run:
Select a service in the services list.
Click Deploy New Revision.
Fill out the deploy form as needed, but make sure the checkbox labelled Serve this revision immediately is UNCHECKED.
Click Deploy.
Click Manage Traffic.
The new revision is listed but with a 0 percentage set: it is currently not serving any traffic. In the form:
- Set it to the desired percentage, for example, 5. Note that the currently serving version's percentage is automatically reduced by that same amount.
- Click Save.
- Repeat these Manage Traffic steps but with changed percentages, increasing the percentage as needed for the new revision. You do not need to redeploy to change the traffic percentages.
gcloud
Deploy the revision you want to roll out gradually, initially setting it to receive no traffic:
gcloud run deploy --image IMAGE --no-traffic
Replace IMAGE with the image you are deploying.
Specify the percentage of traffic you want the new revision to handle, for example, 5 percent:
gcloud run services update-traffic SERVICE --to-revisions REVISION=PERCENTAGE
- Replace SERVICE with the name of the service.
- Replace REVISION with the name of the revision you are
rolling out gradually. To specify the latest revision, you can use
LATEST
, for example,LATEST=5
. - Replace PERCENTAGE with the percentage of traffic you want
to send to the new revision, for example,
5
to send it 5% of traffic.
After the revision's performance is satisfactory, repeat the preceding
update-traffic
step, but increase the percentage value as desired.
YAML
If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:
gcloud run services describe SERVICE --format export > service.yaml
Make any desired configuration changes to the service, and specify the revision name you want for the new revision:
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: template: metadata: annotations: ... name: REVISION-NAME
Replace
- REVISION-NAME with the name you want the new revision to have.
Below the
spec
attribute, locate and update thetraffic
attribute so that the new revision serves only a small amount of traffic:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: ... traffic: - revisionName: REVISION-NEW percent: PERCENT-NEW - revisionName: REVISION-FORMER percent: PERCENT-FORMER
Note that the percentages must add up to 100. Replace
- REVISION-NEW with the name of the revision you are rolling out gradually.
- REVISION-FORMER with the name of the currently serving revision.
- PERCENT-NEW with the traffic percentage you want to send to the new revision,
for example use
10
to send 10% of the traffic to that revision. - PERCENT-FORMER with the traffic percentage you want to send to the old revision
Wait for the update to complete: you should see a message that the new revision you are gradually rolling out has been deployed and is serving the traffic percentage value you used.
Terraform
Add the following to your .tf
file and gradually update the traffic percentage from previous revision to the latest revision.
Keep in mind that every traffic change will require another terraform apply
to be executed.
Apply the changes by entering terraform apply
.
Split traffic between multiple revisions
To split traffic between two or more revisions:
Console
In the Google Cloud console, go to Cloud Run:
Locate the service in the services list, and click on it.
Click Manage Traffic.
The currently serving new revision is listed. In the form:
- Set the currently serving revision percentage to the desired split.
- Select one of the previous revisions using the dropdown list and set it to the desired percentage split.
- To split traffic between more revisions, click Add Revision, select the desired revision, and set the percentage to the desired split.
- Click Save.
gcloud
Specify the revisions and the percentage of traffic for each revision in a comma delimited list:
gcloud run services update-traffic SERVICE --to-revisions LIST
- Replace SERVICE with the name of the service.
- Replace LIST with a comma delimited list of revisions and
percentages:
REVISION1=PERCENTAGE1,REVISION2=PERCENTAGE2,REVISIONn=PERCENTAGEx
for example,hello2-00005-red=25,hello2-00001-bod=25,hello2-00002-nan=50
.
YAML
If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:
gcloud run services describe SERVICE --format export > service.yaml
Below the
spec
attribute, locate and update thetraffic
attribute so that the new revision serves only a small amount of traffic:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: ... traffic: - revisionName: REVISION-A percent: PERCENT-A - revisionName: REVISION-B percent: PERCENT-B - revisionName: REVISION-C percent: PERCENT-C
Note that percentages must add up to 100. Replace
- REVISION-A, REVISION-B, REVISION-C with the revisions you are allotting traffic to.
- PERCENT-A, PERCENT-B, PERCENT-C with the percentage for the corresponding revision.
Replace the service with its new configuration using the following command:
gcloud run services replace service.yaml
Wait for the update to complete: you should see a message that the new revision you are gradually rolling out has been deployed and is serving
5
percent (or whatever gradual value you used) of traffic.
Terraform
Add the following to your .tf
file:
Apply the changes by entering terraform apply
.
Send all traffic to the latest revision
When you deploy a new revision, you can make this revision and all future ones serve 100% of the traffic as soon as possible, overriding any established traffic split:
Console
In the Google Cloud console, go to Cloud Run:
Locate the service in the services list, and click it.
Click Deploy New Revision.
Fill out the deploy form as needed, making sure you check the checkbox labelled Serve this revision immediately. This will override any existing traffic splitting, with the new revision serving 100 percent of traffic.
Click Deploy.
gcloud
To send all traffic to the most recently deployed revision:
gcloud run services update-traffic SERVICE --to-latest
Replace SERVICE with the name of the service.
YAML
If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:
gcloud run services describe SERVICE --format export > service.yaml
Below the
spec
attribute, locate and update thetraffic
attribute to the followingapiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: ... traffic: - latestRevision: true percent: 100
Replace the service with its new configuration using the following command:
gcloud run services replace service.yaml
Wait for the update to complete: you should see a message that the (latest) revision is deployed and is serving
100
percent of traffic.
Terraform
Add the following to your .tf
file:
Apply the changes by entering terraform apply
.
Use tags for testing, traffic migration and rollbacks
To avoid incurring billing costs for tagged revisions, use service-level minimum instances or remove tags on revisions when you don't need them anymore.
A common use case for this feature is to use it for testing and vetting of a new service revision before it serves any traffic, in this typical sequence:
- Run integration tests on a container during development.
- Deploy the container to a Google Cloud project that you use only for staging, serving no traffic, and test against a tagged revision.
- Deploy it to production, without serving traffic, and test against a tagged revision in production.
- Migrate traffic to the tagged revision.
Deploy a new tagged revision
To deploy a new revision of an existing service to production:
gcloud
Deploy a new tagged revision:
gcloud run deploy myservice --image IMAGE_URL --no-traffic --tag TAG_NAME
Replace
- IMAGE_URL with the URL for your image
- TAG_NAME with your lower-case tag name
The tag allows you to directly test the new revision at a specific URL, without
serving traffic. The URL starts with the tag name you provided: for example
if you used the tag name green
on the service myservice
, you would
test the tagged revision at the URL https://green---myservice-abcdef.a.run.app
Terraform
Add the following to your .tf
file and gradually update the traffic percentage from old revision to the newest revision with the new tag.
Keep in mind that every traffic change will require another terraform apply
to be executed.
Apply the changes by entering terraform apply
.
Remove a tag
To remove a tag from a revision:
Console
In the Google Cloud console, go to Cloud Run:
Select a service in the services list.
Navigate to the Revisions section within your desired service and select the revision for which you would like to remove the existing tag.
Hold the pointer over the Revision URLs (tags) column and click the pencil icon:
In the Revision URLs dialog menu, click the Bin icon to remove the current tag used in your revision.
Click Save.
gcloud
To remove a revision tag:
gcloud run services update-traffic SERVICE --remove-tags TAG_NAME
Replace
- TAG_NAME with the name of the tag you are migrating traffic to
- SERVICE with the name of the service you are removing the tag from
YAML
If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:
gcloud run services describe SERVICE --format export > service.yaml
Make any desired configuration changes to the service.
Below the
spec
attribute, locate and remove thetag
attribute for the tagged revision:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: ... traffic: - revisionName: REVISION tag: TAG_NAME percent: PERCENT-NEW
Replace the service with its new configuration using the following command:
gcloud run services replace service.yaml
Terraform
Add the following to your .tf
file:
Apply the changes by entering terraform apply
.
Migrating traffic to a tagged revision
After confirming that the new revision works properly, you can start migrating traffic to it using the Google Cloud console, the gcloud command line, Terraform, or a YAML file:
Console
In the Google Cloud console, go to Cloud Run:
Locate the service in the services list, and click it:
Select the tagged revision you want to send traffic to:
Click Manage Traffic.
Locate the tagged revision name: it is listed but with a 0 percentage set: it is currently not serving any traffic. In the Manage traffic* form:
- Set it to the desired percentage, for example, 5. Note that the currently serving version's percentage is automatically reduced by that same amount.
- Click Save.
- Over a period of hours or days, as needed, repeat these Manage Traffic steps but with changed percentages, increasing the percentage as needed for the tagged revision. You do not need to redeploy to change the traffic percentages.
gcloud
To migrate traffic to a specific revision tag:
gcloud run services update-traffic myservice --to-tags TAG_NAME=TRAFFIC_PERCENT
Replace
- TAG_NAME with the name of the tag you are migrating traffic to
- TRAFFIC_PERCENT with the percentage of traffic you want to
the tagged revision to serve, for example,
1
.
YAML
If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:
gcloud run services describe SERVICE --format export > service.yaml
Make any chosen configuration changes to the service.
Below the
spec
attribute, locate and update thetraffic
attribute for the tagged revision so that the tagged revision serves only a small amount of traffic:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: ... traffic: - revisionName: REVISION tag: TAG_NAME percent: PERCENT-NEW - revisionName: REVISION-FORMER percent: PERCENT-FORMER
Note that the percentages must add up to 100. Replace
- REVISION with the name of the tagged revision.
- TAG_NAME with the name of the tag you are rolling out gradually.
- PERCENT-NEW with the traffic percentage you want to send to
the tagged revision, for example use
10
to send 10% of the traffic to that revision. - REVISION-FORMER with the name of the currently serving revision.
- PERCENT-FORMER with the traffic percentage you want to send to the old revision
Replace the service with its new configuration using the following command:
gcloud run services replace service.yaml
Wait for the update to complete: you should see a message that the new revision you are gradually rolling out has been deployed and is serving the traffic percentage value you used.
Terraform
Add the following to your .tf
file:
Over a period of hours or days, as needed, gradually update from one tag to the other, increasing the percentage as needed for the tagged revision.
Apply by entering terraform apply
after every change.