如何使用 docker 将 metricbeat 连接到 elasticsearch 和 kibana

ano*_*526 4 elasticsearch docker metricbeat

我已经使用 docker compose 设置了 elasticsearch 和 kibana。localhost:9200elasticsearch部署在:而 kibana 部署在localhost:5601

尝试使用 docker run 部署 metricbeat 时,出现以下错误:

$ docker run docker.elastic.co/beats/metricbeat:6.3.2 setup -E setup.kibana.host=kibana:5601 -E output.elasticsearch.hosts=["localhost:9200"]

Exiting: Couldn't connect to any of the configured Elasticsearch hosts. Errors: [Error connection to Elasticsearch http://localhost:9200: Get http://localhost:9200: dial tcp [::1]:9200: connect: cannot assign requested address]

Exiting: Couldn't connect to any of the configured Elasticsearch hosts. Errors: [Error connection to Elasticsearch http://elasticsearch:9200: Get http://elasticsearch:9200: lookup elasticsearch on 192.168.65.1:53: no such host]
Run Code Online (Sandbox Code Playgroud)

我的 docker-compose.yml:

# ./docker-compose.yml

version: "3.7"
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
    environment:
#      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - elkdata:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
    restart: always
  kibana:
    image: docker.elastic.co/kibana/kibana:6.3.2
    volumes:
      - kibana:/usr/share/kibana/config
    ports:
      - "5601:5601"
    depends_on:
      - elasticsearch
    restart: always
volumes:
  elkdata:
  kibana:
Run Code Online (Sandbox Code Playgroud)

leo*_*pal 5

首先docker-compose通过为默认 docker 网络添加名称来编辑您的文件:

version: "3.7"
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
    environment:
#      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - elkdata:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
    networks:
      - my-network
    restart: always
  kibana:
    image: docker.elastic.co/kibana/kibana:6.3.2
    volumes:
      - kibana:/usr/share/kibana/config
    ports:
      - "5601:5601"
    networks:
      - my-network
    depends_on:
      - elasticsearch
    restart: always
volumes:
  elkdata:
  kibana:
networks:
  my-network:
    name: awesome-name
Run Code Online (Sandbox Code Playgroud)

执行docker-compose up,然后从metricbeat以下命令开始:

$ docker run docker.elastic.co/beats/metricbeat:6.3.2 --network=awesome-name setup -E setup.kibana.host=kibana:5601 -E output.elasticsearch.hosts=["elasticsearch:9200"]
Run Code Online (Sandbox Code Playgroud)

解释:

当您尝试部署时metricbeat,您提供以下环境:

  • setup.kibana.host=kibana:5601
  • output.elasticsearch.hosts=["localhost:9200"]

我将从第二个开始。使用docker run命令,当您启动时metricbeat,您是在告诉容器它可以访问 上的弹性搜索localhost:9200。所以当容器启动时,它会在 9200 端口访问 localhost 并期望找到elasticsearch运行。但是,由于容器是具有自己网络层的主机隔离进程,因此localhost解析为容器本身,而不是您期望的 docker 主机。

关于kibana主机的设置,你应该首先了解它是如何docker-compose工作的。默认情况下,当您执行 时docker-compose up,会创建一个 docker 网络,并将 yml 文件中定义的所有服务添加到该网络中。在这个网络中,只有,服务可以通过它们的服务名称访问。对于您的情况,如 yml 文件中所定义,它们的名称将是elasticsearch, kibana

因此,为了metricbeat容器能够elasticsearchkibana容器通信,应该将其添加到同一个 docker 网络中。这可以通过--networkdocker run命令上设置标志来实现。

另一种方法是使用网络模式 host与您的容器共享 docker host 的网络,但我不建议这样做。

参考:

Docker 组合

码头工人