Traffic splitting

Traffic splitting is useful for blue/green deployments and canary deployments.

A Revision is a snapshot-in-time of application code and configuration. A new Revision is created every time you make changes to the configuration of a Knative Service. When splitting traffic, Knative splits traffic between different Revisions of your Knative Service.

Creating a new Revision

Instead of TARGET=World, update the environment variable TARGET on your Knative Service to greet “Knative” instead.

knYAML

Deploy the updated version of your Knative Service by running the command:

  1. kn service update hello \
  2. --env TARGET=Knative

As before, kn prints out some helpful information to the CLI.

Expected output

  1. Service 'hello' created to latest revision 'hello-00002' is available at URL:
  2. http://hello.default.${LOADBALANCER_IP}.sslip.io
  1. Edit your existing hello.yaml file to contain the following:

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: hello
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - image: gcr.io/knative-samples/helloworld-go
    10. ports:
    11. - containerPort: 8080
    12. env:
    13. - name: TARGET
    14. value: "Knative"
  2. Deploy the updated version of your Knative Service by running the command:

    1. kubectl apply -f hello.yaml

    Expected output

    1. service.serving.knative.dev/hello configured

Because you are updating an existing Knative Service, the URL won’t change, but the new Revision has the new name hello-00002.

Accessing the new Revision

To see the change, access the Knative Service again on your browser or use curl in your terminal:

  1. echo "Accessing URL $(kn service describe hello -o url)"
  2. curl "$(kn service describe hello -o url)"

Expected output

  1. Hello Knative!

View existing Revisions

You can view a list of existing Revisions by using the Knative (kn) or kubectl CLI:

knkubectl

View a list of revisions by running the command:

  1. kn revisions list

Expected output

  1. NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
  2. hello-00002 hello 100% 2 30s 3 OK / 4 True
  3. hello-00001 hello 1 5m 3 OK / 4 True

View a list of Revisions by running the command:

  1. kubectl get revisions

Expected output

  1. NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON ACTUAL REPLICAS DESIRED REPLICAS
  2. hello-00001 hello 1 True 0 0
  3. hello-00002 hello 2 True 0 0

When running the kn command, the relevant column is TRAFFIC. You can see that 100% of traffic is going to the latest Revision, hello-00002, which is on the row with the highest GENERATION. 0% of traffic is going to the Revision hello-00001.

When you create a new Revision of a Knative Service, Knative defaults to directing 100% of traffic to this latest Revision. You can change this default behavior by specifying how much traffic you want each Revision to receive.

Splitting traffic between Revisions

Split the traffic between the two Revisions:

knYAML

Run the command:

  1. kn service update hello \
  2. --traffic hello-00001=50 \
  3. --traffic @latest=50
  1. Add the traffic section to the bottom of your existing hello.yaml file:

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: hello
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - image: gcr.io/knative-samples/helloworld-go
    10. ports:
    11. - containerPort: 8080
    12. env:
    13. - name: TARGET
    14. value: "Knative"
    15. traffic:
    16. - latestRevision: true
    17. percent: 50
    18. - latestRevision: false
    19. percent: 50
    20. revisionName: hello-00001
  2. Apply the YAML by running the command:

    1. kubectl apply -f hello.yaml

Info

@latest alwayss point to the “latest” Revision which in this case is hello-00002.

Verify the traffic split

To verify that the traffic split has configured correctly, list the Revisions again by running the command:

  1. kn revisions list

Expected output

  1. NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
  2. hello-00002 hello 50% 2 10m 3 OK / 4 True
  3. hello-00001 hello 50% 1 36m 3 OK / 4 True

Access your Knative Service multiple times in your browser to see the different output being served by each Revision.

Similarly, you can access the Service URL from the terminal multiple times to see the traffic being split between the Revisions.

  1. echo "Accessing URL $(kn service describe hello -o url)"
  2. curl "$(kn service describe hello -o url)"

Expected output

  1. Hello Knative!
  2. Hello World!
  3. Hello Knative!
  4. Hello World!