Routing and managing traffic with blue/green deployment
You need:
- A Kubernetes cluster with Knative installed.
- (Optional) for use with Knative.
Note: The source code for the gcr.io/knative-samples/knative-route-demo image that is used in this sample, is located at https://github.com/mchmarny/knative-route-demo.
Deploying Revision 1 (Blue)
We’ll be deploying an image of a sample application that displays the text “App v1” on a blue background.
First, create a new file called and copy this into it:
Save the file, then deploy the configuration to your cluster:
kubectl apply --filename blue-green-demo-config.yaml
configuration "blue-green-demo" configured
This will deploy the initial revision of the sample application. Before we can route traffic to this application we need to know the name of the initial revision which was just created. Using kubectl
you can get it with the following command:
kubectl get configurations blue-green-demo -o=jsonpath='{.status.latestCreatedRevisionName}'
The command above will return the name of the revision, it will be similar to blue-green-demo-lcfrd
. In the rest of this document we will use this revision name, but yours will be different.
To route inbound traffic to it, we need to define a route. Create a new file called blue-green-demo-route.yaml
and copy the following YAML manifest into it (do not forget to edit the revision name):
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
name: blue-green-demo # The name of our route; appears in the URL to access the app
namespace: default # The namespace we're working in; also appears in the URL to access the app
spec:
traffic:
- revisionName: blue-green-demo-lcfrd
percent: 100 # All traffic goes to this revision
Save the file, then apply the route to your cluster:
route "blue-green-demo" configured
You’ll now be able to view the sample app at the URL shown by:
kubectl get route blue-green-demo
Save the file, then apply the updated configuration to your cluster:
kubectl apply --filename blue-green-demo-config.yaml
Find the name of the second revision with the following command:
kubectl get configurations blue-green-demo -o=jsonpath='{.status.latestCreatedRevisionName}'
In the rest of this document we will assume that the second revision is called blue-green-demo-m9548
, however yours will differ. Make sure to use the correct name of the second revision in the manifests that follow.
At this point, the first revision (blue-green-demo-lcfrd
) and the second revision (blue-green-demo-m9548
) will both be deployed and running. We can update our existing route to create a new (test) endpoint for the second revision while still sending all other traffic to the first revision. Edit blue-green-demo-route.yaml
:
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
name: blue-green-demo # Route name is unchanged, since we're updating an existing Route
namespace: default
spec:
traffic:
- revisionName: blue-green-demo-lcfrd
percent: 100 # All traffic still going to the first revision
- revisionName: blue-green-demo-m9548
percent: 0 # 0% of traffic routed to the second revision
tag: v2 # A named route
Save the file, then apply the updated route to your cluster:
kubectl apply --filename blue-green-demo-route.yaml
route "blue-green-demo" configured
Revision 2 of the app is staged at this point. That means:
- Knative creates a new route named v2 for testing the newly deployed version. The URL of this can be seen in the status section of your Route.
kubectl get route blue-green-demo --output jsonpath="{.status.traffic[*].url}"
This allows you to validate that the new version of the app is behaving as expected before switching any traffic over to it.
Migrating traffic to the new revision
We’ll once again update our existing route to begin shifting traffic away from the first revision and toward the second. Edit blue-green-demo-route.yaml
:
Save the file, then apply the updated route to your cluster:
route "blue-green-demo" configured
Refresh the original route (http://blue-green-demo.default.[YOUR_CUSTOM_DOMAIN].com
) a few times to see that some traffic now goes to version 2 of the app.
Lastly, we’ll update our existing route to finally shift all traffic to the second revision. Edit blue-green-demo-route.yaml
:
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
name: blue-green-demo # Updating our existing route
namespace: default
spec:
traffic:
- revisionName: blue-green-demo-lcfrd
percent: 0
tag: v1 # Adding a new named route for v1
- revisionName: blue-green-demo-m9548
percent: 100
# Named route for v2 has been removed, since we don't need it anymore
Note: You can remove the first revision blue-green-demo-lcfrd instead of 0% of traffic when you will not roll back the revision anymore. Then the non-routeable revision object will be garbage collected.
Save the file, then apply the updated route to your cluster:
kubectl apply --filename blue-green-demo-route.yaml
route "blue-green-demo" configured
Refresh the original route (http://blue-green-demo.default.[YOUR_CUSTOM_DOMAIN].com
) a few times to verify that no traffic is being routed to v1 of the app.
We added a named route to v1 of the app, so you can now access it at the URL listed in the traffic block of the status section. To get the URL, enter the following command:
kubectl get route blue-green-demo --output jsonpath="{.status.traffic[*].url}"
With all inbound traffic being directed to the second revision of the application, Knative will soon scale the first revision down to 0 running pods and the blue/green deployment can be considered complete. Using the named v1
route will reactivate a pod to serve any occasional requests intended specifically for the initial revision.
Cleaning up
To delete the sample app, enter the following commands:
kubectl delete configuration blue-green-demo
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please .