Implementing Blue-Green Deployment with SAP BTP, Cloud Foundry Runtime for Zero Downtime Updates

Objective

After completing this lesson, you will be able to implement Blue-Green Deployment with SAP BTP, Cloud Foundry Runtime for Zero Downtime updates.

Implement a Blue-Green Deployment with SAP BTP, Cloud Foundry Runtime for Zero Downtime Updates

Business Scenario

Let's begin by considering the following scenario: you're responsible for maintaining the Book Shop application built using SAP CAP (Cloud Application Programming). Your task is to implement a blue-green deployment strategy to ensure minimal downtime when introducing a breaking change, such as adding a new required field to an entity. This exercise will guide you through the steps to deploy the changes safely and effectively using SAP BTP, Cloud Foundry Runtime's blue-green deployment strategy.

Exercise Options

You can perform this exercise in two ways:

  1. Live Environment by using the instructions provided below, you can perform the steps in the SAP BTP Free Tier account.
  2. Platform Simulation – follow the step-by-step instructions within the simulation by clicking the ‘Start Exercise’ button below .

Note

It's strongly recommend first performing the steps in the live environment.

Task Flow

In this exercise, you'll perform the following tasks:

  1. Set up prerequisites and environment.
  2. Execute initial blue-green deployment of the BLUE (Live) environment.
  3. Execute blue-green deployment of the GREEN (Idle) environment .
  4. Test the application.
  5. Make the GREEN environment the productive one.
  6. Clean up resources.

Prerequisites

How to Obtain Support

To get support during the exercises, please add your question in our SAP BTP Learning Group.

Platform Simulation

This exercise uses a sample multitarget application (MTA) named "bookshop" containing one application with a REST interface.

Task 1: Set Up Prerequisites and Environment

Steps

  1. Ensure you have all the prerequisites installed and configured.

    1. Perform the required steps.

  2. Clone the SAP CAP Book Shop Application.

    1. Enter the following code:

      Code Snippet
      123
      git clone https://github.tools.sap/SAP-BTP-Courses/cf400_sample_app bookshop cd bookshop
  3. Install Dependencies.

    1. Enter the following code:

      npm install
  4. Log in to Cloud Foundry.

    1. Enter the following code:

      Code Snippet
      123
      cf api <API-ENDPOINT> cf login cf target -o <ORG> -s <SPACE>

      You can find the API endpoint in the Overview section of your subaccount in the SAP BTP cockpit.

Task 2: Execute Initial Blue-Green Deployment of the Live Environment

Steps

  1. Build the application for production.

    1. Enter the following code:

      cds build --production

      The --production parameter ensures that the cloud deployment-related artifacts are created by cds build.

  2. Generate the Initial MTA Archive for the Application.

    1. Enter the following code:

      Code Snippet
      1234
      mbt build -t gen --mtar hello-cloud-foundry-blue.mta INFO generating the MTA archive... INFO the MTA archive generated at: gen\hello-cloud-foundry-blue.mta

      You can find the API endpoint in the Overview section of your subaccount in the SAP BTP cockpit.

  3. Deploy the initial environment (blue) using the existing .mta file.

    1. Enter the following code:

      Code Snippet
      1234567891011
      cf deploy .\gen\hello-cloud-foundry-blue.mta -f --strategy blue-green ... Operation ID: 1b313fd2-7a96-11ef-910c-eeee0a9ee328 ... No deployed MTA detected - this is initial deployment ... Starting application "hello-cloud-foundry-srv-idle"... Application "hello-cloud-foundry-srv-idle" started and available at "<org>-<space>-hello-cloud-foundry-srv-idle.<domain>" ... Renaming application "hello-cloud-foundry-srv-idle" to "hello-cloud-foundry-srv"... ...

      Note for Mac Users: The command provided uses a Windows-specific file path format (for example, .\mta_archives\hello-cloud-foundry-blue.mtar). On a Mac, you'll need to modify the path format to Unix-style by replacing the backslashes (\) with forward slashes ('/'). For example, use ./mta_archives/hello-cloud-foundry-blue.mtar instead.

  4. Check if the apps are running.

    1. Enter the following code:

      cf appsScreenshot, displaying the running apps.
  5. Enter the route displayed for hello-cloud-foundry-srv in your browser to see the CAP start page.

    Screenshot, displaying where to find the $metadata
  6. Choose $metadata to explore the book entity's fields.

    Screenshot from the system, displaying the entity fields of the book.

Task 3: Execute Blue-Green Deployment of the Idle Environment

Steps

  1. Modify the Entity Definition.

    1. Open the db/data-model.cds file where the Book entity is defined. It might look something like this:

      Code Snippet
      1234567
      namespace my.bookshop; entity Books { key ID : Integer; title : String; stock : Integer; }
    2. Add the new publishedDate field to the Books entity:

      Code Snippet
      12345678
      namespace my.bookshop; entity Books { key ID : Integer; title : String; stock : Integer; publishedDate : Date; // New required field }
  2. Build and Deploy the Green Environment (new version).

    1. Build the multitarget application archive.

      mbt build -t gen --mtar hello-cloud-foundry-green.mta
    2. Deploy the Green Environment (new version).

      Code Snippet
      123456789101112131415
      cf deploy .\gen\hello-cloud-foundry-green.mta -f --strategy blue-green ... Operation ID: 34099f67-7a9b-11ef-90ea-eeee0a9a9d78 ... Renaming application "hello-cloud-foundry-srv" to "hello-cloud-foundry-srv-live"... ... Starting application "hello-cloud-foundry-srv-idle"... Application "hello-cloud-foundry-srv-idle" started and available at "<org>-<space>-hello-cloud-foundry-srv-idle.<domain>" ... Process has entered testing phase. After testing your new deployment you can resume or abort the process. Use "cf deploy -i 34099f67-7a9b-11ef-90ea-eeee0a9a9d78 -a abort" to abort the process. Use "cf deploy -i 34099f67-7a9b-11ef-90ea-eeee0a9a9d78 -a resume" to resume the process. Hint: Use the "--skip-testing-phase" option of the deploy command to skip this phase. Hint: Use the '--skip-testing-phase' option of the deploy command to skip this phase. ...

Task 4: Test the Green Application(Idle)

Steps

  1. Test the Green Application(Idle).

    1. Check if the apps are running:

      cf appsScreenshot, displaying the running apps.
    2. Ensure the application hello-cloud-foundry-srv-idle is functioning correctly with the new 'publishedDate' field.

      Screenshot, displaying the running environment.

Task 5: Make the GREEN Environment the Productive One

In this task, you'll transition the GREEN environment to become the new productive environment by switching routes and verifying the deployment.

Steps

  1. Make the GREEN environment the productive one.

    1. Get the Operation ID.

      cf mta-opsScreenshot, displaying the running apps.
    2. Make the GREEN environment the active production environment. Use the following code:

      Code Snippet
      12345678910111213
      cf deploy -i 34099f67-7a9b-11ef-90ea-eeee0a9a9d78 -a resume Executing action "resume" on operation 34099f67-7a9b-11ef-90ea-eeee0a9a9d78... ... Updating application "hello-cloud-foundry-srv-idle"... Stopping application "hello-cloud-foundry-srv-idle"... Starting application "hello-cloud-foundry-srv-idle"... Application "hello-cloud-foundry-srv-idle" started and available at "<org>-<space>-hello-cl2bbcd228.<domain>" Renaming application "hello-cloud-foundry-srv-idle" to "hello-cloud-foundry-srv"... Deleting routes for application "hello-cloud-foundry-srv-live"... Stopping application "hello-cloud-foundry-srv-live"... Deleting application "hello-cloud-foundry-srv-live"... Process finished.
  2. Examine the result.

    1. Verify that the old BLUE applications are deleted and the new GREEN applications are assigned to productive routes.

      cf appsScreenshot, displaying the running apps.
    2. Verify that the GREEN environment is the productive one and serves on the productive routes, enter the route displayed for hello-cloud-foundry-srv in your browser, and check the book entity's fields.

      Screenshot, displaying the running environment.

Task 6: Clean up Resources

It's recommended to stop or undeploy your MTAs when they are no longer needed. In order to do so, run the following command.

Steps

  1. Clean up resources

    1. Use the following code:

      cf undeploy hello-cloud-foundry --delete-services --delete-service-keys -f

Result

In this exercise, you successfully implemented a blue-green deployment strategy using Cloud Foundry to ensure zero downtime updates for the Book Shop application. You began by setting up the environment, deploying the initial blue environment, and verifying its functionality. Next, you updated the entity definition in the idle (green) environment, deployed the new version, and tested it to ensure that the changes were applied correctly. Finally, you made the green environment the new productive one, ensuring a smooth transition without disrupting service. You concluded by cleaning up the resources to maintain a tidy workspace.

Further Reading / Reference Links

Log in to track your progress & complete quizzes