When you release a new version of a Knative service, routing all traffic at once risks exposing users to potential bugs. Knative lets you split traffic across multiple revisions by percentage, so you can validate a new revision with a small slice of production traffic before fully rolling it out—or roll back immediately if issues appear.
Prerequisites
Before you begin, ensure that you have:
-
Knative deployed in your cluster. For more information, see Deploy and manage Knative.
How it works
A canary release with Knative Serving follows three stages:
| Stage | Action | Description |
|---|---|---|
| Deploy | Create a new revision | Deploy a new version of your Knative service as a revision with 0% of traffic, so it's live but not yet serving requests. |
| Validate | Shift traffic incrementally | Move a percentage of traffic (for example, 50%) to the new revision and verify it behaves correctly. |
| Complete or roll back | Set traffic to 100% or revert | Fully release the new revision by routing all traffic to it, or roll back by returning all traffic to the previous revision. |
Step 1: Create a Knative service
-
Log on to the ACK console. In the left navigation pane, click ACK consoleClusters.
-
On the Clusters page, find the cluster you want and click its name. In the left navigation pane, choose Applications > Knative.
-
On the Services tab of the Knative page, set Namespace to default. Click Create Service to deploy a service named
helloworld-go. After configuring the service, click Create. The following table describes the key parameters. For more details, see Deploy a Knative Service. When the Status column shows Created, the service is deployed.Parameter Description Service Name Enter a name for the service. This example uses helloworld-go.Image Name Click Select Image to choose an image and tag, then click OK. You can also enter a private registry address in domainname/namespace/imagename:tagformat. This example usesregistry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56.Access Protocol Supports HTTP and gRPC. gRPC is based on the HTTP/2 standard and Protocol Buffers (protobuf) serialization, and supports multiple programming languages. Container Port The container port to expose. Valid values: 1 to 65535. -
On the Services tab, click the
helloworld-goservice and record its domain name and gateway IP address from the Default Domain and Gateway fields. -
Run the following command to verify the service is accessible:
curl -H "Host: <default_domain_name>" http://<gateway_IP_address>Replace
<default_domain_name>and<gateway_IP_address>with the actual values. Expected output:Hello World!
Step 2: Create a revision and deploy a canary release
Create a revision
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, find the cluster you want and click its name. In the left navigation pane, choose Applications > Knative.
-
On the Services tab, select the
helloworld-goservice and click Details in the Actions column. -
Click Create Revision. On the Basic Information page, click Advanced, set the environment variable to
TARGET=Knative, and click Next.
-
On the Traffic Splitting Settings page, set the traffic Percent % for the latest revision to
0and click Create.NoteThe traffic percentages across all revisions must sum to 100.
-
After the revision is created, run the following command to confirm all traffic still goes to the old revision:
curl -H "Host: <default_domain_name>" http://<gateway_IP_address>Expected output:
Hello World!The output shows
Hello World!because the new revision has a traffic percentage of 0%, so all requests are still routed to the old revision.
Shift traffic to the new revision
-
On the Services tab, select the
helloworld-goservice and click Details in the Actions column. -
Click Split Traffic. In the Split Traffic dialog box, set the traffic Percent % to
50for both the old and the new revision, then click OK. -
Run the following loop command to observe live traffic distribution:
NoteThe command runs indefinitely. Press
Ctrl + Cto stop it.while true; do curl -H "Host: <default_domain_name>" http://<gateway_IP_address>; sleep 1; doneExpected output at a 50/50 split:
Hello World! Hello Knative! Hello World! Hello Knative! Hello World! Hello Knative! ...Requests to
helloworld-goare evenly distributed between the old and new revisions. -
Continue adjusting the traffic percentage based on your validation results. To change the split, click Split Traffic on the Services tab and update the percentage values. Set Percent % to
100for the new revision to fully release it.
Roll back to the previous revision
If you discover issues in the new revision, roll back by routing all traffic to the old revision.
-
On the Services tab, select the
helloworld-goservice and click Details in the Actions column. -
Click Split Traffic. In the Split Traffic dialog box, set the Percent % to
100for the old revision and0for the new revision, then click OK. All requests are now routed to the old revision. Verify the rollback by running the curl command—the output should show onlyHello World!.
What's next
Configure auto scaling for a Knative service based on request volume. For more information, see Enable auto scaling to withstand traffic fluctuations.