Basics of Traffic Splitting

    What are some common traffic splitting use-cases?

    Splitting traffic is useful for a number of very common modern infrastructure needs, such as blue/green deployments and . Bringing these industry standards to bear on Kubernetes is as simple as a single CLI command on Knative or YAML tweak, let’s see how!

    You may have noticed that when you created your Knative Service you assigned it a , “world”. If you used kn, when your Service was created Knative returned both a URL and a “latest revision” for your Knative Service. But what happens if you make a change to your Service?

    What exactly is a Revision?”

    You can think of a Revision as a stateless, autoscaling, snapshot-in-time of application code and configuration.

    A new Revision will get created each and every time you make changes to your Knative Service, whether you assign it a name or not. When splitting traffic, Knative splits traffic between different Revisions of your Knative Service.

    Instead of TARGET\=”World” update the environment variable TARGET on your Knative Service hello to greet “Knative” instead. Name this new revision hello-knative

    knYAML

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

    Expected output:

    1. Service hello created to latest revision 'hello-knative' is available at URL:
    2. http://hello.default.127.0.0.1.sslip.io
    1. ---
    2. apiVersion: serving.knative.dev/v1
    3. kind: Service
    4. metadata:
    5. name: hello
    6. spec:
    7. template:
    8. metadata:
    9. name: hello-knative
    10. spec:
    11. containers:
    12. - image: gcr.io/knative-samples/helloworld-go
    13. ports:
    14. - containerPort: 8080
    15. env:
    16. - name: TARGET

    Once you’ve edited your existing YAML file:

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

    Note, since we are updating an existing Knative Service hello, the URL doesn’t change, but our new Revision should have the new name hello-knative

    Let’s access our Knative Service again on your browser to see the change, or use curl in your terminal:

    1. curl http://hello.default.127.0.0.1.sslip.io

    Expected output:

    Splitting Traffic

    You may at this point be wondering, “where did ‘Hello World!’ go?” Remember, Revisions are a stateless snapshot-in-time of application code and configuration, so your “hello-world” Revision is still available to you.

    We can easily see a list of our existing revisions with the kn CLI:

    knkubectl

    1. kn revisions list

    Though the following example doesn’t cover it, you can peak under the hood to Kubernetes to see the revisions as Kubernetes sees them.

    1. kubectl get revisions

    Expected output:

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

    The column most relevant for our purposes is TRAFFIC. It looks like 100% of traffic is going to our latest Revision (“hello-knative”) and 0% of traffic is going to the Revision we configured earlier (“hello-world”).

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

    Lets split traffic between our two Revisions:

    @latest will always point to our “latest” Revision which, at the moment, is hello-knative.

    knYAML

    1. kn service update hello \
    2. --traffic hello-world=50 \
    3. --traffic @latest=50

    Add the following to the bottom of your existing YAML file:

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: hello
    5. spec:
    6. metadata:
    7. name: hello-knative
    8. spec:
    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. - revisionName: hello-world
    19. percent: 50

    Once you’ve edited your existing YAML file:

    Verify traffic split configure correctly by listing the revisions again.

    knkubectl

    1. kn revisions list

    Though the following example doesn’t cover it, you can peak under the hood to Kubernetes to see the revisions as Kubernetes sees them.

    1. kubectl get revisions

    Expected output:

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

    Access your Knative service on the browser again , and refresh multiple times to see the different output being served by each Revision.

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

    1. curl http://hello.default.127.0.0.1.sslip.io

    Expected output:

    1. curl http://hello.default.127.0.0.1.sslip.io
    2. Hello Knative!
    3. Hello World!