代码之家  ›  专栏  ›  技术社区  ›  Kurt Peek

Docker与PostgreSQL和Prometheus PostgreSQL Exporter合作拒绝连接?

  •  0
  • Kurt Peek  · 技术社区  · 6 年前

    我试图解决这个关于PostgreSQL导出器到普罗米修斯的问题( https://github.com/wrouesnel/postgres_exporter Prometheus PostgreSQL server exporter example not working on MacOS? 跑步 postgres postgres_exporter 容器连接到用户定义的网桥网络而不是主机网络,主机网络在Docker Desktop for Mac上似乎不起作用。我创建了以下内容 docker-compose.yml :

    version: "3"
    services:
      db:
        image: postgres
        environment:
          POSTGRES_PASSWORD: mypassword
        networks:
          - mynetwork
    
      exporter:
        image: wrouesnel/postgres_exporter
        environment:
          DATA_SOURCE_NAME: "postgresql://postgres:mypassword@db:5432/postgres?sslmode=disable"
        ports:
          - "9187:9187"
        networks:
          - mynetwork
    
    networks:
      mynetwork:
    

    然而,我有两个问题。首先,来自 exporter 服务显示此消息的错误:

    “打开与数据库的连接时出错(postgresql://postgres:PASSWORD_REMOVED@db:5432/博士后?sslmode=disable):拨打tcp 172.18.0.2:5432:连接:连接被拒绝”

    以下是完整的输出:

    > docker-compose up
    Creating network "postgres-performance-testing_mynetwork" with the default driver
    Creating postgres-performance-testing_db_1       ... done
    Creating postgres-performance-testing_exporter_1 ... done
    Attaching to postgres-performance-testing_db_1, postgres-performance-testing_exporter_1
    exporter_1  | time="2019-10-03T20:16:56Z" level=info msg="Established new database connection to \"db:5432\"." source="postgres_exporter.go:778"
    exporter_1  | time="2019-10-03T20:16:56Z" level=error msg="Error opening connection to database (postgresql://postgres:PASSWORD_REMOVED@db:5432/postgres?sslmode=disable): dial tcp 172.18.0.2:5432: connect: connection refused" source="postgres_exporter.go:1348"
    exporter_1  | time="2019-10-03T20:16:56Z" level=info msg="Starting Server: :9187" source="postgres_exporter.go:1459"
    db_1        | The files belonging to this database system will be owned by user "postgres".
    db_1        | This user must also own the server process.
    db_1        | 
    db_1        | The database cluster will be initialized with locale "en_US.utf8".
    db_1        | The default database encoding has accordingly been set to "UTF8".
    db_1        | The default text search configuration will be set to "english".
    db_1        | 
    db_1        | Data page checksums are disabled.
    db_1        | 
    db_1        | fixing permissions on existing directory /var/lib/postgresql/data ... ok
    db_1        | creating subdirectories ... ok
    db_1        | selecting default max_connections ... 100
    db_1        | selecting default shared_buffers ... 128MB
    db_1        | selecting default timezone ... Etc/UTC
    db_1        | selecting dynamic shared memory implementation ... posix
    db_1        | creating configuration files ... ok
    db_1        | running bootstrap script ... ok
    db_1        | performing post-bootstrap initialization ... ok
    db_1        | syncing data to disk ... ok
    db_1        | 
    db_1        | Success. You can now start the database server using:
    db_1        | 
    db_1        |     pg_ctl -D /var/lib/postgresql/data -l logfile start
    db_1        | 
    db_1        | 
    db_1        | WARNING: enabling "trust" authentication for local connections
    db_1        | You can change this by editing pg_hba.conf or using the option -A, or
    db_1        | --auth-local and --auth-host, the next time you run initdb.
    db_1        | waiting for server to start....2019-10-03 20:16:57.084 UTC [42] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    db_1        | 2019-10-03 20:16:57.095 UTC [43] LOG:  database system was shut down at 2019-10-03 20:16:56 UTC
    db_1        | 2019-10-03 20:16:57.101 UTC [42] LOG:  database system is ready to accept connections
    db_1        |  done
    db_1        | server started
    db_1        | 
    db_1        | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
    db_1        | 
    db_1        | waiting for server to shut down...2019-10-03 20:16:57.180 UTC [42] LOG:  received fast shutdown request
    db_1        | .2019-10-03 20:16:57.182 UTC [42] LOG:  aborting any active transactions
    db_1        | 2019-10-03 20:16:57.186 UTC [42] LOG:  background worker "logical replication launcher" (PID 49) exited with exit code 1
    db_1        | 2019-10-03 20:16:57.186 UTC [44] LOG:  shutting down
    db_1        | 2019-10-03 20:16:57.197 UTC [42] LOG:  database system is shut down
    db_1        |  done
    db_1        | server stopped
    db_1        | 
    db_1        | PostgreSQL init process complete; ready for start up.
    db_1        | 
    db_1        | 2019-10-03 20:16:57.297 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
    db_1        | 2019-10-03 20:16:57.297 UTC [1] LOG:  listening on IPv6 address "::", port 5432
    db_1        | 2019-10-03 20:16:57.299 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    db_1        | 2019-10-03 20:16:57.310 UTC [51] LOG:  database system was shut down at 2019-10-03 20:16:57 UTC
    db_1        | 2019-10-03 20:16:57.314 UTC [1] LOG:  database system is ready to accept connections
    

    这是因为Prometheus exporter试图在数据库准备接受连接之前连接到数据库吗?(从日志记录的顺序来看,情况似乎是这样的)。

    localhost:9187 ,其结果是我下载了一个包含HTML的文件,而不是实际看到指向的链接 /metrics 我可以遵循:

    enter image description here

    你知道我如何解决这些问题吗?

    0 回复  |  直到 6 年前
        1
  •  1
  •   richyen    6 年前

    在不知道您的图像是如何构建的情况下,我发现两个可能的罪魁祸首需要解决:

    1. 看来 exporter 容器首先启动,并尝试连接到 db postgres之前的容器甚至已经启动并运行。您可能需要添加 depends_on: db 出口商 部分
    2. 您可能需要编辑 pg_hba.conf 分贝 之前的容器 出口商 可以连接到它。默认情况下,只允许本地连接,因此您必须附加 出口商 的IP地址(或使用 /0 网络掩码(如果这是一个仅用于开发的环境)

    如果你解决了这两个问题 开始为你工作吧。

    披露:我是一个 EnterpriseDB (EDB) 受雇者

        2
  •  0
  •   Jackson Mourão    6 年前
    postgres-exporter:
        image: wrouesnel/postgres_exporter:v0.8.0
        restart: always
        environment:
          #- DATA_SOURCE_NAME=postgresql://postgres:password@postgres-db:5432/postgres?sslmode=disable
          - DATA_SOURCE_URI=postgres-db:5432/postgres?sslmode=disable
          - DATA_SOURCE_USER=postgres
          - DATA_SOURCE_PASS=password
        ports:
          - "9187:9187"
        networks:
          - postgres-prometheus
        depends_on:
          - postgres-db
    

    请注意,在本例中,DATA_SOURCE_NAME的值没有双引号,DATA_SOURCE_URI的值(如果您喜欢)没有“postgresql://”。它直接从主机名开始。

    推荐文章