When implementing new machine learning algorithms, it may be useful to go through an experimentation phase during which your algorithm is made available to users for testing without necessarily requiring a production and exepensive environment with horizontal scaling on the cloud.

DALL-E Mini :AI image generated from text : <code>A photorealistic a cable-stayed bridge to the cloud in a sunny sky</code>

DALL-E Mini :AI image generated from text : A photorealistic a cable-stayed bridge to the cloud in a sunny sky-

Indeed, the very high cost of the cloud pushes to the implementation of hybrid architecture scenario for which we will use constant but cheap local resources to validate a concept, an idea or an algorithm. This is sufficient traction device for this service it is then possible to deploy it with the horizontal scaling mechanisms well known from the cloud.

โ˜๏ธ flexible with Traefix and FRP

In this article I will explain how to set up two types of proxies, in the first case we are talking about a reverse proxy and in the second a dynamic proxy.

The best known dynamic proxy is called ngrok, in this article we will use an open source version of a very similar software called FRP Fast Reverse Proxy. We will then see how to exploit it’s API to generate dynamic m’en the configuration of the dynamic proxy Traefik.

One of the main advantages of this architecture is to allow to continue to use the authentication layer in route two already realized in the traffic proxy

image-20220510152947318

In order to reduce the integration cost to its strict minimum, the solution proposed here is very simply based on the FRP API and the file provider function of traefik.

This python script placed in /usr/local/bin/frp2traefik will generarte one TOML file per service.

โš ๏ธ By default Traefik lets all HTTP messages pass through via the passHostHeader parameter which must be left at True

#!/usr/bin/env python3
"""
FPR2TRAEFIK enable to synchronize HTTP proxy declared in the reverse proxy to the dynamic proxy.

It will create dedicated file configuration for the traefik file provider
"""
import argparse
from urllib import request
import json

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--frp_ip', nargs='?', help='frp IP', default="127.0.0.1")
    args = parser.parse_args()
    url = f"http://{ args.frp_ip }:7500/api/proxy/http"
    services = json.loads(request.urlopen(url).read())
    for proxy in services['proxies']:
        conf = f"""
[http]
  [http.routers]
    [http.routers.studio_{ proxy['name'] }]
      entryPoints = ["https"]
      middlewares = ["traefik-forward-auth@docker"]
      service = "studio_{ proxy['name'] }"
      rule = "Host(`{ proxy['conf']['custom_domains'][0] }`)"
      [http.routers.studio_{ proxy['name'] }.tls]
        certResolver = "default"
        
  [http.services]
    [http.services.studio_{ proxy['name'] }]
      [http.services.studio_{ proxy['name'] }.loadBalancer]
        [[http.services.studio_{ proxy['name'] }.loadBalancer.servers]]
          url = "http://{ args.frp_ip }:8888"
    """
        with (open(proxy['name'] + ".toml", "w")) as f:
            f.writelines(conf)

Et voilร 

Find the latest version of that script in that GitHub repo frp2traefik