如何只为prometheus容器提供节点导出器和cadvisor?

时间:2018-02-03 11:56:54

标签: docker docker-compose devops prometheus docker-container

我已经在docker容器中成功设置了Prometheus服务。我也在同一主机的不同其他端口上运行node-exporter和cadvisor等服务。

所有服务都使用docker-compose运行。 这是样本

version: '2'


volumes:
    grafana_data: {}

services:

    prometheus:
        image: prom/prometheus
        privileged: true
        volumes:
            - ./prometheus.yml:/etc/prometheus/prometheus.yml
            - ./alertmanager/alert.rules:/alertmanager/alert.rules
        command:
            - '--config.file=/etc/prometheus/prometheus.yml'
        ports:
            - '9090:9090'

    node-exporter:
        image: prom/node-exporter
        ports:
            - '9100:9100'
    cadvisor:
        image: google/cadvisor:latest 
        privileged: true
        volumes:
            - /:/rootfs:ro
            - /var/run:/var/run:rw
            - /var/lib/docker/:/var/lib/docker:ro
            - /dev/disk/:/dev/disk:ro
            - /cgroup:/sys/fs/cgroup:ro
        ports:
            - '8080:8080'
  

如何使 cadvisor 服务无法向公众开放   现在每个人都可以访问访问的cadvisor和node-exporter   主机URL与正在分配的端口。但作为prometheus   取决于它只有prometheus应该能够访问它。

2 个答案:

答案 0 :(得分:3)

如果您不需要从外部访问服务,只需发布​​该服务的端口,请从每个服务中删除端口部分。生成的撰写文件如下所示:

version: '2'
volumes:
    grafana_data: {}
services:
    prometheus:
        image: prom/prometheus
        privileged: true
        volumes:
            - ./prometheus.yml:/etc/prometheus/prometheus.yml
            - ./alertmanager/alert.rules:/alertmanager/alert.rules
        command:
            - '--config.file=/etc/prometheus/prometheus.yml'
        ports:
            - '9090:9090'

    node-exporter:
        image: prom/node-exporter
        # removed "ports" from here

    cadvisor:
        image: google/cadvisor:latest 
        privileged: true
        volumes:
            - /:/rootfs:ro
            - /var/run:/var/run:rw
            - /var/lib/docker/:/var/lib/docker:ro
            - /dev/disk/:/dev/disk:ro
            - /cgroup:/sys/fs/cgroup:ro
        # removed "ports" from here

容器通过共享网络相互通信,默认情况下,您可以使用docker compose或docker堆栈获取它。要使用容器到容器网络,请使用它的服务名称引用目标容器(在本例中为node-exportercadvisor),并使用容器端口,而不是已发布的端口,你的情况是一样的。

答案 1 :(得分:0)

此配置应按预期工作。请注意,您需要更新Prometheus配置,以便通过别名(node-exportercadvisor)而不是IP来引用节点导出器和CAdvisor。

version: '2'
volumes:
    grafana_data: {}
services:
    prometheus:
        image: prom/prometheus
        privileged: true
        volumes:
            - ./prometheus.yml:/etc/prometheus/prometheus.yml
            - ./alertmanager/alert.rules:/alertmanager/alert.rules
        command:
            - '--config.file=/etc/prometheus/prometheus.yml'
        ports:
            - '9090:9090'
        links:
            - 'node-exporter'
            - 'cadvisor'
    node-exporter:
        image: prom/node-exporter
    cadvisor:
        image: google/cadvisor:latest 
        privileged: true
        volumes:
            - /:/rootfs:ro
            - /var/run:/var/run:rw
            - /var/lib/docker/:/var/lib/docker:ro
            - /dev/disk/:/dev/disk:ro
            - /cgroup:/sys/fs/cgroup:ro