Deploy a Go PostgreSQL API Endpoint in SAP BTP, Kyma Runtime
Develop and deploy an PostgreSQL API endpoint written in Go to SAP BTP, Kyma runtime.
Overview
You will learn
- How to configure and build a Go Docker image
- How to deploy the Go Docker image to SAP BTP, Kyma runtime
Prerequisites
Steps
Intro
This tutorial expects that the tutorial Use and Seed SAP BTP PostgreSQL in SAP BTP, Kyma Runtime has been completed. The Go API will connect to the BTP-managed PostgreSQL instance using the Service Binding Secret available in the Kyma cluster.
Deploying the image includes:
- A Kubernetes ConfigMap to store the database host configuration
- A Kubernetes Service to expose the Go application to other Kubernetes resources
- A Kyma APIRule to expose the API to the Internet
- References to the PostgreSQL Service Binding Secret for credentials
In your browser, go to kyma-runtime-samples. This repository contains a collection of Kyma sample applications which will be used during the tutorial.
Use the Code button to choose one of the options to download the code locally, or simply run the following command within your CLI at your desired folder location:
Shell/Bashgit clone https://github.com/SAP-samples/kyma-runtime-samples
Open the
api-postgresql-godirectory in your desired editor.Explore the content of the sample.
Within the
cmd/apidirectory, you can findmain.go, which is the main entry point of the Go application.The
dockerdirectory contains the Dockerfile used to generate the Docker image. The image is built in two stages to create an image with a small file size.In the first stage, a Go image is used. It copies the related content of the project into the image and builds the application. The built application is then copied into the Docker
scratchimage and exposed on port 8000. Thescratchimage is an empty image containing no other tools within it, so obtaining a shell/bash session is not possible. If desired, the following lines could be commented out to build an image with more included tools, but this results in a larger image.Shell/BashFROM scratch WORKDIR /app COPY --from=builder /app/api-postgres-go /app/The
internaldirectory contains the rest of the Go application, broken down into three packages:api,config, anddb. You can explore their contents to understand the structure and functionality.Within the
k8sdirectory you can find the Kubernetes/Kyma resources you will apply to your SAP BTP, Kyma runtime.Within the root, you can find
go.modandgo.sumfiles that are used to manage the dependencies the application uses.
Run the following commands from the api-postgresql-go directory in your CLI.
Make sure to replace the value of <your-docker-id> with your Docker account ID.
If you’re using any device with a non-x86 processor (e.g. MacBook M1/M2) you need to instruct Docker to use x86 images by setting the DOCKER_DEFAULT_PLATFORM environment variable using the command
export DOCKER_DEFAULT_PLATFORM=linux/amd64. Check Environment variables for more information.
To build the Docker image, run this command:
Shell/Bashdocker build -t <your-docker-id>/api-postgresql-go -f docker/Dockerfile .To push the Docker image to your Docker repository, run this command:
Shell/Bashdocker push <your-docker-id>/api-postgresql-go
You can find the resource definitions in the k8s folder. If you performed any changes in the configuration, these files may also need to be updated. The folder contains the following files that are relevant to this tutorial:
apirule.yaml: defines the API endpoint which exposes the application to the Internet. This endpoint does not define any authentication access strategy and should be disabled when not in use.authorizationpolicy.yaml: allows internal traffic to the service api-postgresql-go in thedevnamespace.configmap.yaml: defines the name of the database.deployment.yaml: defines the deployment definition for the Go API, as well as a service used for communication. This definition references the PostgreSQL Service Binding Secret (postgres-binding) for connection details andconfigmap.yamlfor the database name.
Within the
deployment.yaml, adjust the value ofspec.template.spec.containers.image, commented with #change it to your image, to use your Docker image. Also ensure the Secret name matches your PostgreSQL Service Binding (postgres-binding). Apply the ConfigMap and Deployment:Shell/Bashkubectl -n dev apply -f ./k8s/configmap.yaml kubectl -n dev apply -f ./k8s/deployment.yamlVerify the status of the Pod by running:
Shell/Bashkubectl -n dev get poThe Pod should now be running.
Shell/BashNAME READY STATUS RESTARTS AGE api-postgresql-go-c694bc847-tkthc 2/2 Running 0 23mRun the following command to get the domain name of your Kyma cluster:
Shellkubectl get gateways.networking.istio.io -n kyma-system kyma-gateway \ -o jsonpath='{.spec.servers[0].hosts[0]}'The result looks like this:
Shell*.<xyz123>.kyma.ondemand.comCopy the result without the leading
*..In
apirule.yaml, modifyallowOrigins.regex, to match your domain, and add theexact: http://localhost:8080key-value pair. For example:Note: The command output uses
*.as a glob wildcard prefix, but in theregexfield you replace it with.*โ the equivalent regex pattern that matches any character sequence. For example,*.<xyz123>.kyma.ondemand.combecomes the regex.*xyz123.kyma.ondemand.com.YAML... allowOrigins: - regex: ".*xyz123.kyma.ondemand.com" - exact: http://localhost:8080 ...exact: http://localhost:8080is only required if you want to locally test the frontend of your application as part of the Deploy the SAPUI5 Frontend in SAP BTP, Kyma Runtime tutorial.Apply the APIRule:
Shell/Bashkubectl -n dev apply -f ./k8s/apirule.yamlApply the AuthorizationPolicy:
Shell/Bashkubectl -n dev apply -f ./k8s/authorizationpolicy.yaml
To access the API, use the APIRule you created in the previous step.
Open Kyma dashboard.
From the menu, choose Namespaces.
Choose the
devnamespace.From the menu, choose Discovery and Network > API Rules.
Choose the Host entry for the api-postgresql-go APIRule to open the application in the browser, which will produce a 404 error. Append
/ordersto the end of the URL and refresh the page to successfully access the API. The URL should be similar to:https://api-postgresql-go.<cluster>.kyma.ondemand.com/ordersYou can use a tool such as
curlto test the various HTTP methods of the API.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.