Exposing a Service in Kyma with API Rules

Objectives
After completing this lesson, you will be able to:

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.

Exercise Options

You can perform this exercise in two ways:

  1. Platform Simulation – follow the step-by-step instructions within the simulation.
  2. Live Environment - by using the instructions provided below, you can perform the steps in the SAP BTP Free Tier account.
Note

We strongly recommend first performing the steps in the Live Environment.

Platform Simulation

Choose the Start Exercise button below to open a simulation of the platform. Then follow the step-by-step instructions to expose a service in Kyma with API Rules.

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 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:

    Code snippet
    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: allow
                config: {}
    Copy code
  2. Retrieve the hostname from your SAP BTP, Kyma runtime and adjust the host in the API Rule manifest.

    Code snippet
    kubectl get gateway kyma-gateway -n kyma-system -o=jsonpath='{.spec.servers[0].hosts[0]}'
    Copy code
    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
    kubectl apply -f hello-kyma-api-rule.yaml
    Copy code
  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.

    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