In this sample, we will start from scratch / zero to deploying an CAP NodeJS application on Kyma runtime.
- You will create a sample Node.JS based CAP application (Bookshop)
- Using cds, you will create the necessary artifacts and configurations required to deploy on Kyma.
- Last, but not least, you will deploy and verify your running CAP application
Note: For simplification most of the commands have been defined using the Makefile. In case you want to understand what the actual command is, run
make <command> --just-print
- SAP BTP, Kyma runtime instance
- Docker
- make
- Kubernetes tooling
- Pack
- NodeJS 18 or higher
- SAP CAP
- SAP Hana Cloud Instance
- SAP Hana Cloud Instance mapped to Kyma
-
Set up required environment variables
-
In shell
export DOCKER_ACCOUNT=<your-docker-account> export KUBECONFIG=<your-kubeconfig> export NAMESPACE=<your-kyma-namespace> export CLUSTER_DOMAIN=$(kubectl get cm -n kube-system shoot-info -ojsonpath='{.data.domain}')
-
In Windows powershell
$ENV:DOCKER_ACCOUNT = "<your-docker-account>" $ENV:KUBECONFIG="<your-kubeconfig>" $ENV:NAMESPACE="<your-kyma-namespace>" $ENV:CLUSTER_DOMAIN=$(kubectl get cm -n kube-system shoot-info -ojsonpath='{.data.domain}')
-
-
For mac users, export the DOCKER_HOST
export DOCKER_HOST=unix://${HOME}/.docker/run/docker.sock
- Initialize the Cap Bookshop sample
make init
Let's take a minute to inspect our cap application. It is a simple Bookshop sample where you can access Book entries via API calls.
- Data model defined in ./bookshop/db/data-model.cds
- Core Data Service defined in ./bookshop/srv/cat-service.cds
Directly from CAP website, CAP promotes getting started with minimal upfront setup, based on convention over configuration, and a grow-as-you-go approach, adding settings and tools later on, only when you need them.
- Run the application locally
make run-local
- Access the CAP Srv at http://localhost:4004
- Terminate the local running app with
^C
The local run uses sqlite for storing data. For productive deployment, we would like to use Hana Cloud.
- Let's add Hana Cloud for production deployment
make add-hana
We need to add certain libraries to our NodeJs project for production
make add-required-libs
On Kyma runtime, application run as docker containers. They require a docker image to be created out of the application code / binaries.
The docker image can be stored on the public docker registry. It can be a private docker registry where access is restricted with credentials.
We will use pack to build the docker images.
- Checkout what is happening when building the docker image
make build-hana-deployer --just-print
You will notice that pack
intelligently identifies how to pack the source code and create the necessary artifacts. The same is also true for Java applications.
- Build and push the Hana deployer image
make build-hana-deployer
make push-hana-deployer
- Build and push the CAP Srv image
make build-cap-srv
make push-cap-srv
Let's do a bit of groundwork before we deploy our Helm Chart. In Kyma/Kubernetes, the workloads and required configurations will be deployed in namespaces.
We will create a namespace and enable istio-injection. You can skip this step if you already have a namespace and have enabled istio-injection.
make prepare-kyma-for-deployment
Now that we have our artifacts in place, lets shift our focus to deploying the application.
First we need the configurations to tell Kyma what and how we want to deploy.
We will use Helm Charts to define the required configurations and then deploy them on Kyma runtime.
cds
can intelligently inspect what all is defined in your cap application and generate the necessary configurations (Helm charts) to deploy it on Kyma runtime.
- Create Helm chart
make create-helm-chart
Now take a moment to understand the generated Helm chart in the chart directory.
- bookshop/chart/Chart.yaml contains the details about the chart and all its dependencies.
- bookshop/chart/values.yaml contains all the details to configure the chart deployment. You will notice that it has sections for
hana deployer
,cap application
as well as requiredservice instances
andservice bindings
- Check the make command by running
make deploy-dry-run --just-print
You will notice that we are overriding a various properties defined in chart/values.yaml
. This is standard helm feature where you can override your values by specifying them in the command line. This obviates the need to modify the values.yaml
file. Of course, you can also update the values.yaml
directly.
- Run the command to do a dry run
make deploy-dry-run
Take some time to understand what all will be deployed and how does the configuration looks like. It is interesting to notice that all these deployment configurations are auto-generated via cds.
This ensures that you as a developer does not need work with the complexities of helm charts and configurations. At the same time, these pre-shipped charts follow the best practices when it comes to deploying on Kyma.
- You can now proceed to do the actual deployment
make deploy
- Check the state of the application pods. Wait until pods are in running state.
make check-pods
- Check the hana deployer logs
make check-hana-deployer-logs
- Check the logs for the CAP application
make check-cap-srv-logs
- Access the application at the displayed URL after deploy. It will be of the form https://cap-bookshop-srv-{your-kyma-namespace}.{your-kyma-cluster-domain}
make open-deployed-app
Note: In the sample for the sake of simplicity, mocked authentication is used. If credentials are required, you can check the link.
make undeploy
This will delete the helm chart. Thereby all deployed applications, service instances and their bindings will be cleaned.
CAP supports grow-as-you-go model. Once you have familiarized yourself with CAP basics, you can go further and add capabilities such as authentication, multitenancy, extensibility, messaging and many more.
You can explore CAP further on https://cap.cloud.sap/docs/