Exposing a Service in Kyma with API Rules

Objective

After completing this lesson, you will be able to create API rules to expose and secure services

Expose a Service in Kyma with API Rules

Business Scenario

You have already deployed your application to a Kubernetes cluster using a deployment and created a service for it. To expose this service to the outside world, you must create an API rule.

Live Environment

In this exercise, you will perform the following tasks:

  1. Expose the service with an API Rule
  2. Test the exposed service

Prerequisites

  • You have successfully created a SAP BTP, Kyma runtime instance in the SAP BTP subaccount.
  • You have the default modules API Gateway and Istio added to your SAP BTP, Kyma runtime instance.
  • You have configured kubectl to work with your SAP BTP, Kyma runtime instance.
  • You have successfully created the hello-kyma deployment in the exercise Create and test a serverless function.
  • You have successfully created the hello-kyma-svc service in the exercise Create a service for a deployment.

Task 1: Expose the service with an API Rule

Steps

  1. Create a file named hello-kyma-api-rule.yaml with the following content:

    YAML
    1234567891011121314151617
    apiVersion: gateway.kyma-project.io/v1beta1 kind: APIRule metadata: name: hello-kyma spec: gateway: kyma-system/kyma-gateway host: hello-kyma.<hostname> # replace <hostname> with your Kyma hostname service: name: hello-kyma-svc port: 80 rules: - path: /.* methods: ["GET"] mutators: [] accessStrategies: - handler: no_auth config: {}
  2. Retrieve the hostname from your SAP BTP, Kyma runtime and adjust the host in the API Rule manifest.

    Code Snippet
    1
    kubectl get gateway kyma-gateway -n kyma-system -o=jsonpath='{.spec.servers[0].hosts[0]}'

    Note

    Please be aware that this command is returning the wildcard hostname. Add the subdomain hello-kyma to it.
  3. Apply the API Rule manifest to the cluster.

    Code Snippet
    1
    kubectl apply -f hello-kyma-api-rule.yaml
  4. Access the application in your browser.

    1. Open a web browser.

    2. Navigate to the URL http://hello-kyma.<hostname> (replace with your Kyma hostname).

    3. You should see a greeting from the hello-kyma application.

Task 2: View API Rules in the Kyma dashboard

Steps

  1. Go to the API Rules section of the Kyma dashboard.

    1. Open the Kyma dashboard in your web browser.

    2. Select the API Rules section and choose the hello-kyma API Rule.

    3. Verify the details and choose the host link to open the application in your browser.

      Exposed Service
    4. You should see a greeting from the hello-kyma application.

Result

You have successfully exposed your service with an API Rule.

Log in to track your progress & complete quizzes