Deploy a Go MSSQL API Endpoint in SAP BTP, Kyma Runtime
Develop and deploy an MSSQL 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
Prerequisites
Steps
In your browser, go to kyma-runtime-extension-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-extension-samples
Open the
api-mssql-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-mssql-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.
<button class="fd-tabs__item is-selected" role="tab" data-tab-index="0">Mac and Linux</button>
<button class="fd-tabs__item" role="tab" data-tab-index="1">Windows</button>
Run the following commands from the api-mssql-go directory using your CLI.
Set the following environment variables required for the database connection. Make sure to adjust them for your environment. Review the tutorial Deploy MSSQL in SAP BTP, Kyma Runtime for the different configurations to run the database.
Shell/Bashexport MYAPP_username=sa export MYAPP_password=Yukon900 export MYAPP_database=DemoDB export MYAPP_host=localhost export MYAPP_port=1433To run the application, use the following command:
Shell/Bashgo run ./cmd/api/main.goThe application is available at http://localhost:8000/orders. You can use a tool such as curl to test the different HTTP methods, for example:
To GET data:
Shell/Bashcurl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8000/ordersTo POST data:
Shell/Bashcurl --data "{\"order_id\":\"10000003\",\"description\":\"test from curl\"}" http://localhost:8000/orders
Run the following commands from the api-mssql-go directory using your CLI.
Set the following environment variables required for the database connection. Make sure to adjust them for your environment. Review the tutorial Deploy MSSQL in SAP BTP, Kyma Runtime for the different configurations to run the database.
PowerShell$ENV:MYAPP_username='sa' $ENV:MYAPP_password='Yukon900' $ENV:MYAPP_database='DemoDB' $ENV:MYAPP_host='localhost' $ENV:MYAPP_port=1433Shellset MYAPP_username=sa set MYAPP_password=Yukon900 set MYAPP_database=DemoDB set MYAPP_host=localhost set MYAPP_port=1433To run the application, use the following command:
Shell/Bashgo run ./cmd/api/main.goThe application is available at http://localhost:8000/orders. You can use a tool such as curl to test the different HTTP methods, for example:
To GET data:
Shell/Bashcurl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8000/ordersTo POST data:
Shell/Bashcurl --data "{\"order_id\":\"10000003\",\"description\":\"test from curl\"}" http://localhost:8000/orders
Run the following commands from the api-mssql-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-mssql-go -f docker/Dockerfile .To push the Docker image to your Docker repository, run this command:
Shell/Bashdocker push <your-docker-id>/api-mssql-go
Run the following commands from the api-mssql-go directory in your CLI.
Make sure to replace the value of <your-docker-id> with your Docker account ID.
<button class="fd-tabs__item is-selected" role="tab" data-tab-index="0">Mac and Linux</button>
<button class="fd-tabs__item" role="tab" data-tab-index="1">Windows</button>
Start the image locally by running the following command:
Shell/Bashdocker run -p 8000:8000 --name api-mssql-go \ -e MYAPP_username="sa" \ -e MYAPP_password="Yukon900" \ -e MYAPP_database="DemoDB" \ -e MYAPP_host="host.docker.internal" \ -e MYAPP_port="1433" \ -d <your-docker-id>/api-mssql-go:latestThe command is expecting that the database is available at
localhost:1433on the host machine. This is denoted by thehost.docker.internalvalue. Review the tutorial Deploy MSSQL in SAP BTP, Kyma Runtime for the different configurations to run/stop the database.The application is available at http://localhost:8000/orders. You can use a tool such as curl to test the different HTTP methods, for example:
To GET data:
Shell/Bashcurl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8000/ordersTo POST data - make sure to use a unique value for the order_id:
Shell/Bashcurl --data "{\"order_id\":\"10000004\",\"description\":\"test from curl\"}" http://localhost:8000/ordersStop the Docker container by running:
Shell/Bashdocker stop api-mssql-go
Start the image locally by running the following command:
PowerShelldocker run -p 8000:8000 --name api-mssql-go ` -e MYAPP_username="sa" ` -e MYAPP_password="Yukon900" ` -e MYAPP_database="DemoDB" ` -e MYAPP_host="host.docker.internal" ` -e MYAPP_port="1433" ` -d <your-docker-id>/api-mssql-go:latestShelldocker run -p 8000:8000 --name api-mssql-go ^ -e MYAPP_username="sa" ^ -e MYAPP_password="Yukon900" ^ -e MYAPP_database="DemoDB" ^ -e MYAPP_host="host.docker.internal" ^ -e MYAPP_port="1433" ^ -d <your-docker-id>/api-mssql-go:latestThe command is expecting that the database is available at
localhost:1433on the host machine. This is denoted by thehost.docker.internalvalue. Review the tutorial Deploy MSSQL in SAP BTP, Kyma Runtime for the different configurations to run/stop the database.The application is available at http://localhost:8000/orders. You can use a tool such as curl to test the different HTTP methods, for example:
To GET data:
Shell/Bashcurl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8000/ordersTo POST data - make sure to use a unique value for the order_id:
Shell/Bashcurl --data "{\"order_id\":\"10000004\",\"description\":\"test from curl\"}" http://localhost:8000/ordersStop the Docker container by running:
Shell/Bashdocker stop api-mssql-go
You can also check the following additional commands:
To restart the Docker container, run:
Shell/Bashdocker start api-mssql-goTo remove the Docker container, run:
Shell/Bashdocker rm api-mssql-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-mssql-go in the dev namespace.configmap.yaml: defines the name of the database, host, and port. The host value assumes that the service for the database is namedmssqland is defined in thedevnamespace. Make sure to adjust this if you made any changes.deployment.yaml: defines the deployment definition for the Go API, as well as a service used for communication. This definition references bothsecret.yaml, which was defined in the previous tutorial and also included in this directory, andconfigmap.yamlby 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. Apply the Deployment which will cause an error which we will further explore:Shell/Bashkubectl -n dev apply -f ./k8s/deployment.yamlCheck the status of the Pod by running:
Shell/Bashkubectl -n dev get poThis command results in a table similar to the one below, showing a Pod with the name
api-mssql-go-ending with a random hash. Make sure to adjust this value in the subsequent commands. Notice that STATUS is reportingCreateContainerConfigError.Shell/BashNAME READY STATUS RESTARTS AGE api-mssql-go-c694bc847-tkthc 1/2 CreateContainerConfigError 0 23sYou can also see the logs that report the same STATUS
CreateContainerConfigError:Shell/Bashkubectl logs api-mssql-go-c694bc847-tkthc -c api-mssql-go -n devTo find the reason for the STATUS
CreateContainerConfigError, review the Pod definition:Shell/Bashkubectl describe pod api-mssql-go-c694bc847-tkthc -n devWithin the Events of the Pods, you should find the message
Error: configmap "api-mssql-go" not found.Apply the ConfigMap:
Shell/Bashkubectl -n dev apply -f ./k8s/configmap.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-mssql-go-c694bc847-tkthc 2/2 Running 0 23mRun the following command to get the domain name of your Kyma cluster:
Shellkubectl get gateway -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: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-mssql-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-mssql-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.