如何使用Docker将metricbeat连接到Elasticsearch和Kibana [英] How to connect metricbeat to elasticsearch and kibana with docker

查看:428
本文介绍了如何使用Docker将metricbeat连接到Elasticsearch和Kibana的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've setup elasticsearch and kibana with docker compose. elasticsearch is deployed on: localhost:9200 while kibana is deployed on localhost:5601

当尝试通过docker run部署metricbeat时,出现以下错误:

When trying to deploy metricbeat with docker run I got the following errors:

$ 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]

我的docker-compose.yml:

My 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:


推荐答案

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

First edit your docker-compose file by adding a name for default docker network:

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

执行 docker-compose up ,然后使用以下命令启动 metricbeat

Execute docker-compose up and then start metricbeat with the below command:

$ 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"]

说明:

当您尝试部署 metricbeat 时,请提供以下envar:

When you try to deploy metricbeat, you provide below envars:


  • setup.kibana.host = kibana:5601

  • output.elasticsearch.hosts = [ localhost:9200]

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

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

I will start with the second one. With docker run command, when you are starting metricbeat, you are telling the container that it can access elastic search on localhost:9200. So when the container starts, it will access localhost on port 9200 expecting to find elasticsearch running. But, as the container is a host isolated process with its own network layer, localhost resolves to the container itself, not to your docker host machine as you are expecting.

关于 kibana 主机的设置,您首先应该了解 docker-compose 有效。默认情况下,当您执行 docker-compose up 时,将创建一个docker网络,并将yml文件上定义的所有服务都添加到该网络。在仅此网络中,可以通过服务名称访问服务。对于您的情况,按照yml文件中的定义,它们的名称为 elasticsearch kibana

Regarding the setup of kibana host, you should firstly understand how docker-compose works. By default, when you execute docker-compose up, a docker network is created and all services defined on yml file are added to this network. Inside this network and only, services are accessible through their service name. For your case, as defined on yml file, their names would be elasticsearch, kibana.

因此,为了使 metricbeat 容器能够与 elasticsearch 和<$进行通信c $ c> kibana 容器,应将其添加到同一docker网络中。这可以通过在 docker run 命令上设置-network 标志来实现。

So in order metricbeat container to be able to communicate with elasticsearch and kibana containers, it should be added to the same docker network. This can be achieved with setting --network flag on docker run command.

另一种方法是通过使用网络模式主机,但我不建议这样做。

Another approach would be to share docker host's network with your containers by using network mode host, but I would not recommend that.

参考:

Docker撰写

docker run

这篇关于如何使用Docker将metricbeat连接到Elasticsearch和Kibana的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆