简体   繁体   中英

Traefik and my web app inside a container

I am trying to do something I thought was simple, but looks like I'm missing a thing.

I have a web app, which is package in a docker image I manage. It starts a server listening on port 9000. It does have an endpoint publishing metrics, on /admin/metrics . The application is deployed on a system that requires me to publish those metrics on port 9100, with the path /metrics . I could change the application, run a second server, etc, but for fun I tried something quicker (I thought): running a companion reverse proxy.

I chose traefik, and I managed to configure it properly using a file provider: when running on my machine (no container), it does redirect properly calls from /metrics on port 9100 to my app's /admin/metrics . But when inside the container, it only gives 404 errors, although the configuration is ok. I also tried to run the app only and have traefik on my machine route to the app inside the container, but it fails too.

This is my configuration:

#/app/traefik.toml
[entryPoints]
  [entryPoints.MetricsProxy]
    address = ":9100"

[providers]
  providersThrottleDuration = 42
  [providers.file]
    directory = "/app"
    watch = false

[api]
  insecure = false
  dashboard = false
  debug = false

[log]
  level = "TRACE"
#/app/metrics.toml
[http]
  [http.routers]
    [http.routers.Router0]
      entryPoints = ["MetricsProxy"]
      middlewares = ["PathConvert"]
      service = "MetricsService"
      rule = "Path(`/metrics`)"
  [http.services]
    [http.services.MetricsService]
      [http.services.MetricsService.loadbalancer]
        [[http.services.MetricsService.loadBalancer.servers]]
          url = "http://0.0.0.0:9000"
  [http.middlewares]
    [http.middlewares.PathConvert]
      [http.middlewares.PathConvert.addPrefix]
        prefix = "/admin"

Please note that I tried to replace 0.0.0.0 with 127.0.0.1 or localhost , neither works.

Finally, the Dockerfile :

FROM openjdk:8-jre-slim

WORKDIR /app

RUN \
  apt-get update -qq && apt-get install -y -qq curl && \
  curl -sSL https://github.com/containous/traefik/releases/download/v2.0.4/traefik_v2.0.4_linux_amd64.tar.gz | tar -xz

COPY bin/myapp.sh .
COPY target/universal/bluevalet-server.zip .
COPY deploy/traefik/traefik.toml .
COPY deploy/traefik/metrics.toml .
COPY deploy/nginx.conf .
COPY deploy/run.sh .

#run.sh ~~> ./traefik --configfile /app/traefik.toml & ./myapp.sh
CMD [ "/app/run.sh" ]
EXPOSE 9000
EXPOSE 9100

I guess there is something with "localhost" in the service definition, but cannot understand what.

Anyone has an idea?

Not sure why it does work this way, but I succeeded using another configuration for traefik:

[http]
  [http.routers]
    [http.routers.Router0]
      entryPoints = ["MetricsProxy"]
      middlewares = ["PathConvert"]
      service = "MetricsService"
      rule = "Path(`/metrics`)"
  [http.services]
    [http.services.MetricsService]
      [http.services.MetricsService.loadbalancer]
        [[http.services.MetricsService.loadBalancer.servers]]
          url = "http://localhost:9000/"
  [http.middlewares]
    [http.middlewares.PathConvert]
      [http.middlewares.PathConvert.replacePathRegex]
        regex = "^/metrics"
        replacement = "/admin/metrics/prometheus"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM