Contact Us 1-800-596-4880

Publishing a Simple API in Local Mode

Publish a simple API running behind Flex Gateway in Local Mode, modifying YAML configuration data by one of the following methods:

  • Linux and Docker: .yaml files

  • Kubernetes: kubectl apply

The following procedures demonstrate applying a simple YAML configuration for an unsecured API with a single upstream service.

20%

25%

20%

Publish an API Running Behind Flex Gateway on Linux

Before You Begin

Before getting started, ensure that you have:

  • Flex Gateway installed and running in Local Mode. See Downloading Flex Gateway for more information about installing and running the gateway.

  • Your upstream service URL. The following example refers to a fictional orders-api service, but you can specify your own API name in metadata.name and your service details in spec.services.

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 your values where indicated:

    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: orders-api
    spec:
      address: http://0.0.0.0:8080
      services:
        orders:
          address: https://<your orders URL>:<your port>/
          routes:
            - rules:
                - path: /api/orders(/.*)
  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/animal-facts-api http://0.0.0.0:8080
    [agent][info] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/orders(/.*) methods: headerConditions:[] profile:0xc0030529f0} => {Kind:Service Name:orders-api-orders 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/orders-api-orders https://<your orders URL>:<your port>/
    [agent][debug] generating service orders-api-orders.default.svc hostname: <your orders 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)

A simple API is now running behind Flex Gateway.

Publish an API Running Behind Flex Gateway in a Docker Container

Before You Begin

Before getting started, ensure that you have:

  • Flex Gateway installed. See Downloading Flex Gateway for more information.

  • Flex Gateway registered and running in Local Mode. See Register and Run in Local Mode for more information.

  • Your upstream service URL. The following example refers to a fictional orders-api service, but you can specify your own API name in metadata.name and your service details in spec.services.

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 your values where indicated:

    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: orders-api
    spec:
      address: http://0.0.0.0:8080
      services:
        orders:
          address: https://<your orders URL>:<your port>/
          routes:
            - rules:
                - path: /api/orders(/.*)
  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/animal-facts-api http://0.0.0.0:8080
    [agent][info] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/orders(/.*) methods: headerConditions:[] profile:0xc0030529f0} => {Kind:Service Name:orders-api-orders 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/orders-api-orders https://<your orders URL>:<your port>/
    [agent][debug] generating service orders-api-orders.default.svc hostname: <your orders 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)

A simple API 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:

  • Flex Gateway installed and running in Local Mode. See Downloading Flex Gateway for more information about installing and running the gateway.

  • A Docker image stored in a Docker repository. The following example refers to a fictional orders-api application, but you can specify your own Docker repository and Docker image in the Deployment configuration resource (in spec.spec.image).

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. Using the kubectl apply command, install a Docker image named "orders-api" from a given repository.

    The following YAML snippet defines two configuration resources: Service, and Deployment. The Service resource declares the orders-api service. The Deployment resource declares the desired deployment state by specifying information such as the number of replicas to create, and the Docker image to sideload.

    cat <<EOF | kubectl apply -f -
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: orders-api
      labels:
        app: orders-api
        service: orders-api
    spec:
      ports:
      - name: http
        port: 80
        targetPort: http
      selector:
        app: orders-api
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: orders-api
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: orders-api
          version: v1
      template:
        metadata:
          labels:
            app: orders-api
            version: v1
        spec:
          containers:
          - image: <repository>/<dockerfile>
            imagePullPolicy: IfNotPresent
            name: orders-api
            ports:
            - name: http
              containerPort: 3000
            resources:
              limits:
                cpu: 100m
                memory: 64Mi
    EOF
  2. Again using kubectl apply, create the ingress to route traffic.

    The following YAML example defines one configuration resource: Ingress.

    cat <<EOF | kubectl apply -f -
    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: orders-api
    spec:
      ingressClassName: ingress-http.gateway
      rules:
      - http:
          paths:
          - path: /api/orders(/.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: orders-api
                port:
                  number: 80
    EOF

A simple API is now running behind Flex Gateway.