Contact Us 1-800-596-4880

Publishing an API with Multiple Upstream Services in Local Mode

Flex Gateway running in Local Mode supports API instances that expose multiple upstream services through a single consumer endpoint.

Flex Gateway manages traffic by using multiple routes with a defined route order and individual rulesets to direct traffic to different sets of upstream services. Each API instance supports multiple routes that can each direct traffic to multiple upstream services.

For Flex Gateways running in Local Mode, API instances with multiple upstream services are configured by using the ApiInstance and the PolicyBinding YAML configuration resources. The ApiInstance resource defines an API instance, and the route-weighted policy resource defines a single route to one or multiple upstream services. To add additional routes, add multiple route-weighted resources.

The following tutorials use a preconfigured route-weighted policy resource. For information about how to configure the resource, see Traffic Management for Multiple Upstream Services (Weighted).

20%

25%

20%

Publish an API Running Behind Flex Gateway on Linux

Before You Begin

Before getting started, ensure that you have:

Publish an API

  1. Create a configuration file with a .yaml file extension:

    1. Give the file a custom name.

    2. Save the file in the Flex Gateway configuration directory /etc/mulesoft/flex-gateway/conf.d/custom. This directory can contain multiple configuration files.

  2. Copy and paste the following YAML snippet into the file, substituting the configuration field values with your values.

    The snippet refers to the fictional products-api and products-api-beta services and a fictional json-http API instance. Specify your own API name and your own service details:

    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: json-http
    spec:
      address: http://0.0.0.0:8080/json
    ​
    ---
    ​
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: PolicyBinding
    metadata:
      name: weight-routing-test
    spec:
      targetRef:
        name: json-http
      policyRef:
        name: route-weighted
      config:
        routes:
        - weight: 75 # max_value=100, min_value=1
          destinationPath: /posts/
          destinationRef:
            name: products-api
        - weight: 25
          destinationPath: /posts/
          destinationRef:
            name: products-api-beta
      rules:
      - path: (.*)
        methods: POST
        headers:
          x-test: true
        host: test.com
      - methods: GET|POST
        headers:
          x-test: true
          y-test: false
        host: test.com
  3. Save the file. The gateway automatically refreshes the configuration.

  4. View the logs by executing the following command:

    journalctl -u flex-gateway-*

    The response looks something like this:

    [agent][info] Generating config
    [agent][info] Gateway default/18b4e890fe7d: Adding ApiInstance default/products-users-api http://0.0.0.0:8080
    [agent][info] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/products(/.*) methods: headerConditions:[] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-products Namespace:default}
    [agent][info] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/users(/.*) methods: headerConditions:[] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-users Namespace:default}
    [agent][info] Gateway default/18b4e890fe7d: Adding Policy default/envoy.filters.http.router
    [agent][info] Gateway default/18b4e890fe7d: Adding Service default/monitoring_metrics http://0.0.0.0:9881
    [agent][debug] generating service monitoring_metrics.default.svc hostname: 0.0.0.0 port: 9881
    [agent][info] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-products https://<your products URL>:<your port>/
    [agent][info] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-users https://<your users URL>:<your port>/
    [agent][debug] generating service products-users-api-products.default.svc hostname: <your products URL> port: <your port>
    [agent][debug] generating service products-users-api-users.default.svc hostname: <your users URL> port: <your port>
    [agent][info] Writing envoy bootstrap configuration to /tmp/envoy.json
    [envoy][info] cds: add 2 cluster(s), remove 2 cluster(s)
    [envoy][info] cds: added/updated 1 cluster(s), skipped 1 unmodified cluster(s)

An API with multiple upstream services now runs behind Flex Gateway.

Publish an API Running Behind Flex Gateway in a Docker Container

Before You Begin

Before getting started, ensure that you have:

Publish an API

  1. Open a terminal and navigate to the directory that will contain your Flex Gateway configuration files. This directory was specified when you started Flex Gateway.

  2. Create a configuration file with a .yaml file extension:

    1. Give the file a custom name.

    2. Save the file.

  3. Copy and paste the following YAML snippet into the file, substituting configuration fields with your values.

    The snippet refers to the fictional products-api and products-api-beta services and a fictional json-http API instance. Specify your own API name and your own service details:

    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: json-http
    spec:
      address: http://0.0.0.0:8080/json
    ​
    ---
    ​
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: PolicyBinding
    metadata:
      name: weight-routing-test
    spec:
      targetRef:
        name: json-http
      policyRef:
        name: route-weighted
      config:
        routes:
        - weight: 75 # max_value=100, min_value=1
          destinationPath: /posts/
          destinationRef:
            name: products-api
        - weight: 25
          destinationPath: /posts/
          destinationRef:
            name: products-api-beta
      rules:
      - path: (.*)
        methods: POST
        headers:
          x-test: true
        host: test.com
      - methods: GET|POST
        headers:
          x-test: true
          y-test: false
        host: test.com
  4. Save the file. The gateway automatically refreshes the configuration.

  5. View the Docker container logs, which look something like this:

    [agent][info] Generating config
    [agent][info] Gateway default/18b4e890fe7d: Adding ApiInstance default/products-users-api http://0.0.0.0:8080
    [agent][info] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/products(/.*) methods: headerConditions:[] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-products Namespace:default}
    [agent][info] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/users(/.*) methods: headerConditions:[] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-users Namespace:default}
    [agent][info] Gateway default/18b4e890fe7d: Adding Policy default/envoy.filters.http.router
    [agent][info] Gateway default/18b4e890fe7d: Adding Service default/monitoring_metrics http://0.0.0.0:9881
    [agent][debug] generating service monitoring_metrics.default.svc hostname: 0.0.0.0 port: 9881
    [agent][info] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-products https://<your products URL>:<your port>/
    [agent][info] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-users https://<your users URL>:<your port>/
    [agent][debug] generating service products-users-api-products.default.svc hostname: <your products URL> port: <your port>
    [agent][debug] generating service products-users-api-users.default.svc hostname: <your users URL> port: <your port>
    [agent][info] Writing envoy bootstrap configuration to /tmp/envoy.json
    [envoy][info] cds: add 2 cluster(s), remove 2 cluster(s)
    [envoy][info] cds: added/updated 1 cluster(s), skipped 1 unmodified cluster(s)

An API with multiple upstream services is now running behind Flex Gateway.

Publish an API Running Behind Flex Gateway in a Kubernetes Cluster

Before You Begin

Before getting started, ensure that you have:

If you use Kubernetes Service as an upstream service or you apply a policy that makes requests to a Kubernetes Service, set the following parameters in the service resource:

  • spec.ports[].protocol to TCP

  • spec.ports[].name

Your spec.ports[].name service must adhere to the <protocol>[-<suffix>] structure. [-<suffix>] is optional, and the valid options for <protocol> are:

  • HTTP

  • H2

  • HTTPS

Publish an API

  1. Use the kubectl apply command to apply the following .yaml configuration file, substituting the configuration field values with your values.

    The snippet refers to the fictional products-api and products-api-beta services and a fictional json-http API instance. Specify your own API name and your own service details:

    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: json-http
    spec:
      address: http://0.0.0.0:8080/json
    ​
    ---
    ​
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: PolicyBinding
    metadata:
      name: weight-routing-test
    spec:
      targetRef:
        name: json-http
      policyRef:
        name: route-weighted
      config:
        routes:
        - weight: 75 # max_value=100, min_value=1
          destinationPath: /posts/
          destinationRef:
            name: products-api
        - weight: 25
          destinationPath: /posts/
          destinationRef:
            name: products-api-beta
      rules:
      - path: (.*)
        methods: POST
        headers:
          x-test: true
        host: test.com
      - methods: GET|POST
        headers:
          x-test: true
          y-test: false
        host: test.com
  2. Enter the following command to view the pods logs and ensure that the API was created successfully:

kubectl logs -f <pod-name>