Bookmark this page

Configuring Service Discovery

Objectives

After completing this section, you should be able to discover applications deployed on OpenShift automatically.

3scale Service Discovery

With service discovery, you can import products and backends from existing Red Hat OpenShift Container Platform (RHOCP) services. Service discovery also enables you to update the ActiveDocs OpenAPI specification.

To use service discovery:

  • Enable it in the system configmap in the project where 3scale is installed.

  • Add metadata to the services to enable service discovery and configure the service information.

  • Make sure that 3scale has access to the services that you want to import.

Enabling Service Discovery

You can enable or disable service discovery globally by editing the system configmap in the 3scale project. To do this you must edit the configmap service_discovery.yml entry, setting the value production.enabled to true.

If you installed 3scale by using the operator method then service discovery is enabled by default.

You can verify that it is enabled by describing the system configmap.

[student@workstation ~]$ oc describe configmap system -n 3scale
...output omitted...
service_discovery.yml:
----
production:
  enabled: <%= cluster_token_file_exists = File.exists?(cluster_token_file_path = '/var/run/secrets/kubernetes.io/serviceaccount/token') %>
...output omitted...

Making a Service Eligible for Discovery

Service discovery needs the service details to create products and backends. For this, 3scale looks for the following meta data to know how to import and configure each service it finds.

Table 2.4. Service Discovery Metadata

TypeKeyValue
Labeldiscovery.3scale.net (Required)[true|false] to enable/disable service discovery for the service
Annotationdiscovery.3scale.net/scheme (Required)[http|https] scheme used by the service
Annotationdiscovery.3scale.net/port (Required)port number used by the service
Annotationdiscovery.3scale.net/pathbase path of the target service, if omitted the root path ("/") is used
Annotationdiscovery.3scale.net/description-pathpath to the service's OpenApi endpoint
Annotationdiscovery.3scale.net/discovery-versionversion of the 3scale discovery process

For example, to configure a service using http and listening on port 8080 with only the required configuration:

  • Add the label to enable service discovery.

[student@workstation ~]$ oc label svc/SERVICE_NAME \
  discovery.3scale.net="true"
  • Add the service schema annotation:

[student@workstation ~]$ oc annotate svc/SERVICE_NAME \
  discovery.3scale.net/scheme=http
  • Add the service port annotation:

[student@workstation ~]$ oc annotate svc/SERVICE_NAME \
  discovery.3scale.net/port="8080"

You can also achieve the same result by editing the service resource definition:

[student@workstation ~]$ oc edit service books-api

apiVersion: v1
kind: Service
metadata:
  annotations:
    discovery.3scale.net/port: "8080"
    discovery.3scale.net/scheme: http
    openshift.io/generated-by: OpenShiftNewApp
  creationTimestamp: "2022-01-07T17:15:42Z"
  labels:
    app: books-api
    app.kubernetes.io/component: books-api
    app.kubernetes.io/instance: books-api
    discovery.3scale.net: "true"
  name: books-api
...output omitted...

Discovering Services from Other Projects

To Discover a service located in a different project than the 3scale project, 3scale needs view permissions into the project where that service exists. To give 3scale access to other projects, the 3scale amp service account must have the view role.

[student@workstation ~]$ oc policy add-role-to-user \
  view system:serviceaccount:3SCALE_PROJECT:amp -n PROJECT_NAME

You can also give 3scale access to all the projects in the cluster by assigning it the view role as a cluster role.

[student@workstation ~]$ oc adm policy add-cluster-role-to-user view \
  system:serviceaccount:3SCALE_PROJECT:amp

Importing Products And Backends from Discovered Services

To make use of the discovered services, navigate to the Admin Portal to import the discovered services into 3scale products and backends.

To import a discovered service, click Products on the top pane drop-down menu to navigate to the Products page. Then click Create Product to navigate to the New Product form.

Figure 2.18: 3scale imported product

In the New Product form, select Import from OpenShift. This populates the Namespace and Name drop-down menus with the discovered services. Select the desired service, then click Create Product.

By clicking Create Product, 3scale creates the product and backend based on the 3scale metadata from the service.

Accessing the Discovered Services

Although the product import creates the product and backend resources, the product is not accessible yet.

To access the imported product and the backend associated with it, the product needs a 3scale application plan and an application.

To create an application plan named APPLICATION_PLAN you must reference the TENANT and the PRODUCT_SYSTEM_NAME resources.

[student@workstation ~]$ 3scale application-plan create \
  TENANT PRODUCT_SYSTEM_NAME APPLICATION_PLAN

To create the application you need a user account to link it to, the PRODUCT_SYSTEM_NAME, the previously created plan, and the name you want for the application.

[student@workstation ~]$ 3scale application create \
  TENANT USER_ACOUNT PRODUCT_SYSTEM_NAME APPLICATION_PLAN APPLICATION

This application provides us with a user_key to make requests in the Staging APIcast environment.

To verify the product import, navigate to the product detail page in the Admin Portal. Then click IntegrationConfiguration and scroll down to the Staging APICast section. There you can copy an example curl request to validate that your service is working properly in the staging environment.

Figure 2.19: Staging APIcast configuration

References

For more information, refer to the Service Discovery chapter in the Red Hat 3scale Api Management 2.11 Admin Portal Guide at https://access.redhat.com/documentation/en-us/red_hat_3scale_api_management/2.11/html/admin_portal_guide/service-discovery_service-discovery

Revision: do240-2.11-40390f6