简体   繁体   English

Prometheus - Django:连接被拒绝

[英]Prometheus - Django : connection refused

(This is my first time using Prometheus and I'm not very good with Docker/Django yet) (这是我第一次使用 Prometheus,我对 Docker/Django 还不是很好)

I'm running a Django project in a docker container, and Prometheus with docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus我正在docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus容器中运行 Django 项目,并使用docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus Prometheus

In my docker-compose.yml I have:在我docker-compose.yml我有:

...
nginx-proxy:
        build:
            context: ./dockerfiles/nginx-proxy/
            args:
                - DOMAIN_NAME=local.my.url
        ports:
            - "80:80"
        depends_on:
            - api
            - ...
        volumes:
            - ./volumes/nginx-front/log/:/var/log/nginx

api:
        build:
            context: ./dockerfiles/api/
            args:
                - GUNICORN_WORKERS=20
        restart: always
        volumes:
            - ./volumes/api/src/:/usr/src/app
...

In /tmp/prometheus.yml I have:/tmp/prometheus.yml我有:

global:
  scrape_interval:     15s
  evaluation_interval: 15s
  external_labels:
      monitor: 'my-project-monitor'

rule_files:

scrape_configs:
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'api'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['api.local.my.url']

The prometheus job seems to work ok (but those aren't the metrics I'm interested in), the api gives the following error from the Promotheus UI: Get http://api.local.my.url:80/metrics: dial tcp 127.0.0.1:80: connect: connection refused prometheus工作似乎工作正常(但那些不是我感兴趣的指标), api从 Promotheus UI 给出以下错误: Get http://api.local.my.url:80/metrics: dial tcp 127.0.0.1:80: connect: connection refused

However, when I type in http://api.local.my.url:80/metrics in my browser I can see the information correctly.但是,当我在浏览器中输入http://api.local.my.url:80/metrics ,我可以正确地看到信息。 I've tried replacing the URL with my IP address 10.25.2.192 but that doesn't change the result.我试过用我的 IP 地址10.25.2.192替换 URL,但这并没有改变结果。

I don't understand why it can't connect.我不明白为什么它无法连接。

It's because your prometheus container is on a different network and for it 'localhost' means a different thing.这是因为您的 prometheus 容器位于不同的网络上,因此 'localhost' 意味着不同的东西。 It purposefully does not have access to the host's network (by default).它故意无法访问主机的网络(默认情况下)。

You can verify that by running sudo docker network ls while both of your containers are running.您可以通过在两个容器运行时运行sudo docker network ls来验证这一点。

What you could do is make sure the two containers run on the same network.您可以做的是确保两个容器在同一网络上运行。

In your docker command, that would mean adding --network [name] and for docker-compose, it would mean adding a network: attribute.在您的--network [name]命令中,这意味着添加--network [name]而对于--network [name] -compose,则意味着添加network:属性。 That can be done for all the services in the docker-compose file if you add something like this at the bottom of your file:如果您在文件底部添加以下内容,则可以对 docker-compose 文件中的所有服务执行此操作:

networks:
  default:
    external:
      name: [name]

Source: https://docs.docker.com/compose/networking/#use-a-pre-existing-network来源: https : //docs.docker.com/compose/networking/#use-a-pre-existing-network

To actually create the network outside of docker-compose you can use要在 docker-compose 之外实际创建网络,您可以使用

sudo docker network create -d bridge [name]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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