Thursday, March 24, 2022

Blue-green deployment with IBM Cloud Code Engine and Knative

Code Engine app with green revision
From reading my past post on migrating from Cloud Foundry to Code Engine, you probably guessed that I am currently working with IBM Cloud Code Engine. One of the topics I looked into the past few days is advanced configuration of my app using the Knative CLI. Code Engine is using Knative which, again, is based on Kubernetes. All of them are involved in configuring traffic management for my app, so that I could realize blue-green deployment. It allows to gradually move traffic from the current stable to a newer app revision. Not much is needed to get it to work.

Code Engine and Knative / Kubernetes integration

Right now, it is not possible to configure all available Kubernetes and Knative options from the Code Engine UI or CLI. But it is possible to utilize both the Kubernetes CLI and the Knative CLI to work with "low level" Code Engine features. Assuming that you have the IBM Cloud CLI and Knative CLI installed and are logged in on the command line, the first step is to retrieve the Kubernetes configuration with the kubecfg parameter when selecting your Code Engine project. bluegreen is my project.

ibmcloud ce project select -n bluegreen --kubecfg

Thereafter, you can work with the kubectl (Kubernetes CLI) and kn (Knative CLI) commands. I listed the services, i.e., my apps in the project which is a Kubernetes namespace:

kn service list

Similarly, you can list the active revisions:

kn revision list

Knative traffic management

After that setup and initial test the next was to deploy a simple app. It shows the value of an environment variable which by default is default. Then, I created two new app revisions by changing that variable to GREEN and to BLUE. I could have performed it with any of the CLI commands, but opted for the Code Engine UI (just out of laziness...).

Next, I assigned the tags blue and green to the new revisions. It is not needed, but helps to remember the role of each revision. The kn service update command is the key to this and the next step.

kn service update bluegreen --tag bluegreen-00002=green --tag bluegreen-00003=blue

To split the traffic between the blue and green revisions, I again updated the service:

kn service update bluegreen --traffic blue=50 --traffic green=50

Once the update was applied, I checked the revisions again:

Knative CLI showing blue and green app revisions


With everything in place, I accessed my app with its blue and green deployments and multiple times to see if the traffic was really split. See the screenshot on top and below for the result.

The blue revision of my Code Engine app

After that success I also deployed a "yellow" revision and split the traffic 60/30/10 between the revisions. The browser-based test showed all three names, but favored BLUE over GREEN and some rare YELLOW.

Conclusions

IBM Cloud Code Engine is based on Kubernetes and utilizes Knative for offering platform to build serverless and event-driven apps. It is possible to access and work with advanced Kubernetes and Knative configuration options which are not (yet) exposed by Code Engine directly. They allow to run your app with blue-green deployment and implement advanced traffic management with few simple steps.

If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.