Use Redis in the Kyma Runtime to Store and Retrieve Data
- How to deploy a microservice using the Kyma Console
- How to deploy functions using the Kyma Console
- How to create API Rules
- How APIs and Events are used.
Prerequisites
This sample provides a Redis deployment and two serverless functions that interact with it. The function cache-order
will be set to subscribe to an order.created
event provided by the Commerce mock application. Once triggered, the function will perform an API call to the Commerce mock to obtain additional details regarding the order and then cache the information into Redis. The function get-order
, exposed as an API, is used to then retrieve the order details from the Redis cache.
- Step 1
-
Go to the kyma-runtime-extension-samples repository. This repository contains a collection of Kyma sample applications which will be used during the tutorial.
-
Download the code by choosing the green Code button, and then choosing one of the options to download the code locally.
You can instead run the following command using your CLI at your desired folder location:
Shell/BashCopygit clone https://github.com/SAP-samples/kyma-runtime-extension-samples
-
- Step 2
In this step, you will deploy two functions into the Kyma runtime that were obtained in [Step 1](Clone the Git repository). These resources include:
cache-order-function.yaml
: This function will subscribe to an event published by the Commerce mock application. Once triggered, it will call back to the Commerce mock application to obtain thetotalPriceWithTax
and then cache the information into Redis.get-order-function.yaml
: This function will be exposed as an API allowing retrieval of data stored within the Redis cache.
In the
dev
namespace, choose the menu option Overview.Choose Overview > Upload YAML, copy the contents of the file
redis-function/k8s/cache-order-function.yaml
and then choose Submit.Repeat the steps to create the function
get-order
using the fileredis-function/k8s/get-order-function.yaml
.
- Step 3
- To expose the
get-order
function as an API, choose the menu option Discovery and Network > API Rules. Choose Create API Rule and provide the valueget-order
for the Name, Service Name and Host. Choose Create.
- To expose the
- Step 4
In this step, you will deploy Redis into the Kyma runtime that were obtained in [Step 1](Clone the Git repository). These resources include:
redis.yaml
: The deployment defining the Redis cache configuration.redis-service.yaml
: The Kubernetes service which exposes the Redis instance to the two Serverless functions.redis-secret.yaml
: The Kubernetes secret which defines the Redis password.
In the
dev
namespace, choose the menu option Overview.Choose Upload YAML. Upload or copy the contents of the file
redis-function/k8s/redis.yaml
and choose Submit. Perform the same procedure to upload the contents of the fileredis-function/k8s/redis-service.yaml
.Choose the menu option Configuration > Secrets. Choose Create Secret and choose the YAML tab. Copy the contents of the file
redis-function/k8s/redis-secret.yaml
over-writing the preexisting content found within the pane and then choose Create.After completing the deployment of the resources, the Healthy Resources panel found in Overview will report the status of the deployments and the pods they generate. The chart indicates the amount Ready vs the Total. An equal relation means everything is ready.
- Step 5
In this step, you will configure the function
cache-order
, deployed in the previous step, to run when the order.created event is fired from the Commerce Mock application.-
In the
dev
namespace, choose Workloads > Functions. -
Choose the function cache-order.
-
Choose the Configuration tab and then choose Create Subscription.
-
In the Create Subscription dialog use the following values as shown in the screenshot and then choose Create
- Name: order-created
- Service: cache-order
- Application name: mp-commerce-mock
- Event name: order.created
- Event version: v1
-
- Step 6
In this step, you will copy the URL to the commerce web services API which will be used by the function cache-order.
Choose the menu option Back to Cluster Details to go back to the Kyma home workspace.
In the Kyma home workspace, choose Integration > Applications.
Choose the
mp-commerce-mock
application by clicking on the name value shown in the list.Choose
SAP Commerce Cloud - Commerce Webservices
.Choose the copy option to copy the Central Gateway URL of the
SAP Commerce Cloud - Commerce Webservices
. This will be used in the function cache-order.
- Step 7
In this step, the URL copied in the previous step will be assigned to an environment variable of the function cache-order. This will allow the function to call the
SAP Commerce Cloud - Commerce Webservices
of the commerce mock application.In the Kyma home workspace, choose Namespace.
Choose the
dev
namespace and choose Workloads > Functions.Choose the function cache-order to open it.
Choose the Edit option and scroll down to the Environment Variables and find the Environment Variable with the Variable Name
CENTRAL_GATEWAY_URL
Paste the value copied in the previous step into the Value field making sure that the value beings with
**http://central-application...**
and choose Update.Choosing Update will cause the function to be rebuilt and deployed. The Status field will indicate that the function is Deploying and will change to Running once this process completes.
What is the status of the function after it successfully builds?
- Step 8
With the configuration steps completed, you can now test the scenario to validate that it is working as intended.
Open the mock application in the browser by choosing Discovery and Network >
API Rules
from the menu.Choose the Host entry for the commerce-mock
APIRule
to open it in the browser. This URL should be similar to:
https://commerce.*******.kyma.ondemand.com
Choose the Remote APIs tab.
Choose the SAP Commerce Cloud - Events option.
For the Event Topics, choose order.created.v1.
Modify the
orderCode
value as desired and choose Send Event. Make sure to remember theorderCode
value, it will be used in a later step.
- Step 9
In this step, we will view the logs outputted by the function to verify that the scenario is working.
In the
dev
namespace, choose Workload > Functions.Choose the function cache-order.
Scroll to the bottom of the pane to find the option Replicas of the Function and choose the value found in the table. The name will not match what is shown in the screenshot.
Under Containers choose View Logs for the container function.
If necessary search for the value
orderCode
.The output should be similar to:
- Step 10
In this step, we use the get-order function to perform a read request of the data cached in the Redis database.
Choose Discovery and Network >
API Rules
from the menu.Choose the Host entry for the get-order
APIRule
to open the application in the browser. When first opened you will received the message{"error":"No orderCode received!"}
Append the value
?orderCode=12331231
to the URL where the value is the same as used when sending the event, for examplehttps://get-order.*********.kyma.ondemand.com/?orderCode=1231231
This should output the value saved when the event was submitted.
{"orderCode":"1231231","Date":"Tue Nov 17 2020 19:28:42 GMT+0000 (Coordinated Universal Time)","Value":"100"}
Congratulations! You have successfully completed the mission.
What process exposes the Events and APIs of a connected system to a namespace?