如何通过泊坞窗管理连接拒绝错误elasticsearc-kibana-nginx?

时间:2019-03-24 13:22:02

标签: docker elasticsearch nginx docker-compose kibana

也许我花了6个小时来寻找答案。但是我找不到任何解决方案。我想使用nginx最多增加1个节点elasticsearch +1 kibana(用于负载平衡,代理和安全性问题),但是当docker在kibana部件上创建错误时,如何使用以下代码通过nginx托管elasticsearch node-kibana?

Error:Unable to revive connection: http://elasticsearch:9200/

Elasticsearch.yml:

network.host: localhost
http.port: 9200
xpack.security.enabled: false
xpack.monitoring.enabled: true
xpack.graph.enabled: false
xpack.watcher.enabled: false

ElasticSearch Dockerfile:

FROM docker.elastic.co/elasticsearch/elasticsearch:6.6.2
COPY ./config/elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml 
RUN elasticsearch-plugin  install analysis-kuromoji

kibana.yml:

---
# Default Kibana configuration from kibana-docker.

server.name: kibana
server.host: "0"
elasticsearch.url: http://elasticsearch:9200
elasticsearch.username: elastic
elasticsearch.password: changeme
xpack.monitoring.ui.container.elasticsearch.enabled: true

Kibana Dockerfile:

FROM docker.elastic.co/kibana/kibana:6.6.2
COPY ./config/kibana.yml /opt/kibana/config/kibana.yml
RUN apt-get update && apt-get install -y netcat
COPY entrypoint.sh /tmp/entrypoint.sh
RUN chmod +x /tmp/entrypoint.sh
RUN kibana plugin --install elastic/sense
CMD ["/tmp/entrypoint.sh"]

entrypoint.sh:

#!/usr/bin/env bash

# Wait for the Elasticsearch container to be ready before starting Kibana.
echo "Stalling for Elasticsearch"
while true; do
  nc -q 1 elasticsearch 9200 2>/dev/null && break
done
echo "Starting Kibana"
exec kibana

nginx.conf:

upstream elasticsearch {
  server 38.252.127.221:9200;
  keepalive 15;
}

upstream kibana {
  server 38.252.127.221:5601;
  keepalive 15;
}

server {
  listen 9200;

  location / {
    auth_basic           "Protected Elasticsearch";
    auth_basic_user_file /etc/nginx/htpasswd.users;

    proxy_pass http://elasticsearch;
    proxy_redirect off;
    proxy_buffering off;

    proxy_http_version 1.1;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
  }
}

server {
  listen 5601;
  location / {
    auth_basic           "Protected Kibana";
    auth_basic_user_file /etc/nginx/htpasswd.users;
    proxy_pass  http://kibana;
    proxy_redirect off;
    proxy_buffering off;
    proxy_http_version 1.1;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
  }
}

docker-compose.yml

version: '2'
services:
 elasticsearch:
container_name: esc
image: esi:1.0.0
build: ./es
volumes:
  - ./data/es:/usr/share/elasticsearch/data
ports:
    - 9200:9200
expose:
    - 9300
kibana:
container_name: kibanac
image: kibanai:1.0.0
build: ./kibana
links:
  - elasticsearch
ports:
  - 5601:5601
nginx:
image: nginx:latest
restart: unless-stopped
volumes:
  - ./nginx/config:/etc/nginx/conf.d:ro,Z
  - ./nginx/htpasswd.users:/etc/nginx/htpasswd.users:ro,Z
ports:
  - "8900:5601"
  - "8901:9200"

 depends_on:
  - elasticsearch
  - kibana

1 个答案:

答案 0 :(得分:1)

elasticsearch.yml中,将第一行更改为:

network.host: 0.0.0.0

以前,您是在告诉Elasticsearch仅在localhost上侦听,因此来自其他容器的任何连接均无法按预期工作,因为Elastic Search Service不在侦听其他接口,但是将其设置为0.0.0.0时,您将使elasticsearch能够接收来自其他容器的连接,而您不应该遇到Connection Refused问题

还请注意,您无需发布9200、5601端口,因为这将使任何人都可以直接调用它们而无需通过nginx基本认证。

  

下面的下一部分不在问题范围内,但值得一提。

您可能需要替换下面添加的entrypoint.sh的这一部分:

# Wait for the Elasticsearch container to be ready before starting Kibana.
echo "Stalling for Elasticsearch"
while true; do
  nc -q 1 elasticsearch 9200 2>/dev/null && break
done

通过使用wait-for-itwait-for,您的方式和这些脚本使您能够在启动另一个容器的服务之前等待另一个连接可用。