Perform in-place Kubernetes updates with a Blue/Green Deployment

Learn how to update an application with one simple command.

Prerequisites

In order to get full benefit from taking this lesson, you need to:

  • Know how to access your Developer Sandbox instance.
  • Be comfortable using the command line in a terminal.

In this lesson, you will:

  1. Log in to your Developer Sandbox at the command line.
  2. Deploy version 1 of an application.
  3. Create the route to version 1 of the application.
  4. View the results in your terminal.
  5. Deploy version 2 of the application.
  6. Alter (“patch”) the route to direct to version 2 instead of version 1.
  7. View the results in your terminal.
  8. Patch the routes to direct to version 1 instead of version 2.
  9. View the results in your terminal.

Log in to your Developer Sandbox instance at the command line by following these instructions.

To begin, deploy version 1 of an application. Run the following command to create our application:

oc new-app quay.io/rhdevelopers/gethostname:v1 --name gethostname-v1

Run the following command to create a route to the service:

oc expose service/gethostname-v1 --name gethostname-microservice

This allows the application to be reached from outside of the OpenShift cluster.

Next, run a curl command loop to view the application output.

Run the following command to see the URL of the application:

oc get routes

You will see output similar to the following:

NAME                       HOST/PORT                                                      PATH             SERVICES            PORT               TERMINATION           WILDCARD
gethostname-microservice   gethostname-microservice-rhn-engineering-foo-dev.apps.sandbox.x8i5.p1.openshiftapps.com             gethostname-v1     8080-tcp              ​​​​​​​None

We will use this URL in our curl command loop in the next step.

Run a curl loop against the URL to see the application output. Replace the URL in the following command with the result you got from the previous command (oc get routes) and run the command at a command line:

If you are using Bash:

for ((i=1;i<=10000;i++)); do curl http://gethostname-microservice-rhn-engineering-foo-dev.apps.sandbox.x8i5.p1.openshiftapps.com/hostname; echo -e; sleep .01; done;

If you are using PowerShell:

while ($true) { curl gethostname-microservice-rhn-engineering-dsch-dev.apps.rm3.7wse.p1.openshiftapps.com/hostname;echo '';start-sleep -Milliseconds 200;

Figure 1 is an example of the kind of output you can expect: 

gethostname curl bash output
Figure 1. curl loop output showing that gethostname-v1 is up and running.
Figure 1: curl loop output showing that gethostname-v1 is up and running.

Run the following command to deploy version 2 of our application:

oc new-app quay.io/rhdevelopers/gethostname:v2 --name gethostname-v2

At this point, both versions of gethostname are running in your Developer Sandbox cluster. However, the route (gethostname-microservice) is only accessing Version 1.

Now we'll roll out the Blue/Green deployment. By patching the route, you immediately switch access from Version 1 to Version 2.

Remember: Both versions are running, so switching between them happens very rapidly when you switch the route.

Run the following command to alter the route to point to version 2:

oc patch route/gethostname-microservice -p '{"spec": {"to": {"name": "gethostname-v2" }}}'

As the command in the previous section is processed, you will see the output of the curl loop change. This is a result of the rolling update.

Run the following command to alter the route to point to Version 1:

oc patch route/gethostname-microservice -p '{"spec": {"to": {"name": "gethostname-v1" }}}'

Summary

You have now put the Blue/Green Deployment pattern into action. As you've seen, you can switch between versions very rapidly. Running both versions at the same time enables you to recover immediately in case the new version is faulty.

Once you have determined which version you want to use, you can remove the other version and continue normal operations.

Other related learning paths include:

Previous resource
Overview: Perform in-place Kubernetes updates with a Blue/Green Deployment