When you release a new version of a Knative Service, routing all traffic to the new version at once risks downtime if the version has issues. Traffic splitting lets you route a small percentage of requests to the new Revision first, verify stability, and then gradually increase its share. If problems surface at any stage, shift traffic back to the previous Revision immediately.
How it works
In Knative on Alibaba Cloud Service Mesh (ASM), every Knative Service has one or more Revisions:
When you create a Knative Service, Knative automatically creates the first Revision.
Each time you update the Service configuration, Knative creates a new Revision.
You control the percentage of traffic routed to each Revision. The percentages across all Revisions must total 100%.
A typical canary release follows this progression:
Create a new Revision and assign it 0% of traffic.
Verify the new Revision works correctly.
Gradually shift traffic from the previous Revision to the new one (for example, 10% -> 50% -> 100%).
If the new Revision has issues at any stage, set its traffic back to 0% and restore the previous Revision to 100%.
Prerequisites
Before you begin, make sure that you have:
A Knative Service deployed through Knative on ASM. For details, see Use Knative on ASM to deploy a serverless application
This topic uses the default domain name example.com. To use a custom domain name, see Set a custom domain name in Knative on ASM.
Step 1: Create a new Revision with 0% traffic
Create a new Revision without sending any production traffic to it. This lets you confirm the Revision deploys successfully before exposing it to users.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left-side navigation pane, choose Applications > Knative.
Click the Services tab. Find your Service and click its name. In the upper-right corner, click Create Revision.
In the Basic Information step, click Advanced at the bottom. Click Add next to Environment Variables and configure the following settings: Click Next.
Parameter Value Type Custom Variable Key TARGETValue/ValueFrom Knative on ASMIn the Traffic Splitting Settings step, set Percent % to
0for Latest Revision and100for the previous Revision. Click Create.NoteThe traffic percentages for all Revisions must add up to 100%.
After the Revision is created, click the Services tab and click your Service name. In the Revision Information section, verify the new Revision appears.
Run the following command to confirm all traffic still goes to the previous Revision: Expected output: Every request returns
Hello World!, which confirms the previous Revision still handles 100% of traffic.curl http://helloworld-go.default.example.comHello World!
Step 2: Shift traffic to the new Revision
Increase the traffic percentage for the new Revision in stages while monitoring for issues.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left-side navigation pane, choose Applications > Knative.
Click the Services tab and click your Service name. In the upper-right corner, click Split Traffic. In the Split Traffic dialog box, set Percent % to
50for both the latest Revision and the previous Revision, then click OK.After you save the traffic split, click the Services tab and click your Service name. In the Revision Information section, verify the updated traffic percentages.
Send multiple requests to verify that traffic is distributed between both Revisions: Expected output (abbreviated): Responses alternate between
Hello World!(previous Revision) andHello Knative on ASM!(new Revision), roughly matching the 50/50 split.for i in $(seq 20); do curl http://helloworld-go.default.example.com; doneHello Knative on ASM! Hello Knative on ASM! Hello World! Hello Knative on ASM! Hello World! ...If the new Revision is stable, repeat step 3 to increase its traffic percentage. Continue until the new Revision handles 100% of traffic.
After the new Revision reaches 100%, verify the cutover: Expected output: Every request returns
Hello Knative on ASM!, which confirms the canary release is complete.for i in $(seq 20); do curl http://helloworld-go.default.example.com; doneHello Knative on ASM! Hello Knative on ASM! Hello Knative on ASM! ...
Roll back to the previous Revision
If the new Revision shows errors, high latency, or unexpected behavior at any point during the canary release, roll back immediately:
On the Service details page, click Split Traffic in the upper-right corner.
In the Split Traffic dialog box, set Percent % to
0for the new Revision and100for the previous Revision. Click OK.Verify the rollback by sending requests: All responses should return
Hello World!, which confirms traffic is fully restored to the previous Revision.for i in $(seq 20); do curl http://helloworld-go.default.example.com; done
What to do next
After a canary release, configure autoscaling so pod count adjusts automatically with traffic volume. Knative Serving adds a Queue Proxy container to each pod that reports concurrency metrics to the Knative Pod Autoscaler (KPA). KPA then scales the number of pods based on concurrent request volume. For details, see Enable autoscaling of pods based on the number of requests.