Business Scenario
Exercise Options
Task Flow
- Set up prerequisites and environment.
- Execute initial blue-green deployment of the BLUE (Live) environment.
- Execute blue-green deployment of the GREEN (Idle) environment .
- Test the application.
- Make the GREEN environment the productive one.
- Clean up resources.
Prerequisites
How to Obtain Support
Platform Simulation
Task 1: Set Up Prerequisites and Environment
Steps
Ensure you have all the prerequisites installed and configured.
Perform the required steps.
Clone the SAP CAP Book Shop Application.
Enter the following code:
123
git clone https://github.tools.sap/SAP-BTP-Courses/cf400_sample_app bookshop
cd bookshop
Install Dependencies.
Enter the following code:
npm install
Log in to Cloud Foundry.
Enter the following code:
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
Build the application for production.
Enter the following code:
cds build --productionThe --production parameter ensures that the cloud deployment-related artifacts are created by cds build.
Generate the Initial MTA Archive for the Application.
Enter the following code:
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.
Deploy the initial environment (blue) using the existing .mta file.
Enter the following code:
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.
Check if the apps are running.
Enter the following code:
cf apps
Enter the route displayed for hello-cloud-foundry-srv in your browser to see the CAP start page.
Choose $metadata to explore the book entity's fields.
Task 3: Execute Blue-Green Deployment of the Idle Environment
Steps
Modify the Entity Definition.
Open the db/data-model.cds file where the Book entity is defined. It might look something like this:
1234567
namespace my.bookshop;
entity Books {
key ID : Integer;
title : String;
stock : Integer;
}
Add the new publishedDate field to the Books entity:
12345678
namespace my.bookshop;
entity Books {
key ID : Integer;
title : String;
stock : Integer;
publishedDate : Date; // New required field
}
Build and Deploy the Green Environment (new version).
Build the multitarget application archive.
mbt build -t gen --mtar hello-cloud-foundry-green.mta
Deploy the Green Environment (new version).
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
Test the Green Application(Idle).
Check if the apps are running:
cf apps
Ensure the application hello-cloud-foundry-srv-idle is functioning correctly with the new 'publishedDate' field.
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
Make the GREEN environment the productive one.
Get the Operation ID.
cf mta-ops
Make the GREEN environment the active production environment. Use the following code:
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.
Examine the result.
Verify that the old BLUE applications are deleted and the new GREEN applications are assigned to productive routes.
cf apps
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.
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
Clean up resources
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