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

Learn how to update an application with one simple command.

Prerequisites

The working environment for this activity

  • The command line, using either Bash or PowerShell
  • A web browser

What you will do

  • Log in to your Developer Sandbox account.
  • Spin up an application.
  • Run a curl command loop.
  • Spin up Version 2 of the application.
  • Switch from Version 1 to Version 2.
  • Observe the results.
  • Switch back to Version 1.

Log in to your Developer Sandbox at the command line

Log in to your OpenShift Sandbox by following these instructions.

Spin up Version 1 of the application (gethostname-v1)

  1. Run the following command to create your application:

    oc new-app quay.io/rhdevelopers/gethostname:v1 --name gethostname-v1
  2. 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.

Run a cURL command loop to view the application output

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

    oc get routes
  2. 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
  3. We will use this URL in our curl command loop in the next step.
  4. 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:

    1. 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;
    2. If you are using PowerShell:

      while ($true) { (curl gethostname-microservice-rhn-engineering-foo-dev.apps.sandbox.x8i5.p1.openshiftapps.com/hostname).content;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.

Spin up Version 2 of the application (gethostname-v2)

Run the following command to create 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.

Patch the route to switch from Version 1 to Version 2

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:

If you are using Bash:

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

If you are using PowerShell:

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

Observe results

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.

Patch the route to switch back to Version 1

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

If you are using Bash:

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

If you are using PowerShell:

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

Conclusion

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.

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