如何使用Docker Swarm部署ElasticSearch?

mgdq6dx1  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(6)|浏览(256)

我使用docker-machine创建了3个虚拟机,分别是:

NAME       ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
cluster    -        virtualbox   Running   tcp://192.168.99.101:2376           v18.09.5   
cluster2   -        virtualbox   Running   tcp://192.168.99.102:2376           v18.09.5   
master     -        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.5

然后我在master机器中创建了一个Docker群:

docker-machine ssh master "docker swarm init ----advertise-addr 192.168.99.100"

以及在clustercluster2中加入master

docker-machine ssh cluster "docker swarm join --advertise-addr 192.168.99.101 --token xxxx 192.168.99.100:2377"

docker-machine ssh cluster2 "docker swarm join --advertise-addr 192.168.99.102 --token xxxx 192.168.99.100:2377"

docker node ls信息:

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
r4a6y9wie4zp3pl4wi4e6wqp8     cluster             Ready               Active                                  18.09.5
sg9gq6s3k6vty7qap7co6eppn     cluster2            Ready               Active                                  18.09.5
xb6telu8cn3bfmume1kcektkt *   master              Ready               Active              Leader              18.09.5

存在已部署配置swarm.yml

version: "3.3"

services:
  elasticsearch:
    image: elasticsearch:7.0.0
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - cluster.name=elk
      - network.host=_eth1:ipv4_
      - network.bind_host=_eth1:ipv4_
      - network.publish_host=_eth1:ipv4_
      - discovery.seed_hosts=192.168.99.100,192.168.99.101
      - cluster.initial_master_nodes=192.168.99.100,192.168.99.101
      - bootstrap.memory_lock=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    networks:
      - backend
    deploy:
      mode: replicated
      replicas: 3
      #endpoint_mode: dnsrr
      restart_policy:
        condition: none
      resources:
        limits:
          cpus: "1.0"
          memory: "1024M"
        reservations:
          memory: 20M
networks:
  backend:
    # driver: overlay
    # attachable: true

我把ElasticSearch图像拉到虚拟机:

docker-machine ssh master "docker image pull elasticsearch:7.0.0"
docker-machine ssh cluster "docker image pull elasticsearch:7.0.0"
docker-machine ssh cluster2 "docker image pull elasticsearch:7.0.0"

在运行之前,我运行这个命令修复了一些elasticearch引导错误:

docker-machine ssh master "sudo sysctl -w vm.max_map_count=262144"
docker-machine ssh cluster "sudo sysctl -w vm.max_map_count=262144"
docker-machine ssh cluster2 "sudo sysctl -w vm.max_map_count=262144"

然后我运行'docker stack deploy -c swarm.yml es,ElasticSearch集群就不能工作了。

docker-machine ssh master
docker service logs es_elasticsearch -f

显示:

es_elasticsearch.1.uh1x0s9qr7mb@cluster    | {"type": "server", "timestamp": "2019-04-25T16:28:47,143+0000", "level": "WARN", "component": "o.e.c.c.ClusterFormationFailureHelper", "cluster.name": "elk", "node.name": "e8dba5562417",  "message": "master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [192.168.99.100, 192.168.99.101] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.99.100:9300, 192.168.99.101:9300] from hosts providers and [{e8dba5562417}{Jy3t0AAkSW-jY-IygOCjOQ}{z7MYIf5wTfOhCX1r25wNPg}{10.255.0.46}{10.255.0.46:9300}{ml.machine_memory=1037410304, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0"  }
es_elasticsearch.2.swswlwmle9e9@cluster2    | {"type": "server", "timestamp": "2019-04-25T16:28:47,389+0000", "level": "WARN", "component": "o.e.c.c.ClusterFormationFailureHelper", "cluster.name": "elk", "node.name": "af5d88a04b42",  "message": "master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [192.168.99.100, 192.168.99.101] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.99.100:9300, 192.168.99.101:9300] from hosts providers and [{af5d88a04b42}{zhxMeNMAQN2evKDlsA33qA}{fpYPTvJ6STmyqrgxlMkD_w}{10.255.0.47}{10.255.0.47:9300}{ml.machine_memory=1037410304, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0"  }
es_elasticsearch.3.x8ouukovhh80@master    | {"type": "server", "timestamp": "2019-04-25T16:28:48,818+0000", "level": "WARN", "component": "o.e.c.c.ClusterFormationFailureHelper", "cluster.name": "elk", "node.name": "0e7e4d96b31a",  "message": "master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [192.168.99.100, 192.168.99.101] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.99.100:9300, 192.168.99.101:9300] from hosts providers and [{0e7e4d96b31a}{Xs9966RjTEWvEbuj4-ySYA}{-eV4lvavSHq6JhoW0qWu6A}{10.255.0.48}{10.255.0.48:9300}{ml.machine_memory=1037410304, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0"  }

我想可能是网络配置错误导致的集群形成失败。我不知道如何修复它,我尝试了很多次修改配置,失败了又失败了。

ffvjumwh

ffvjumwh1#

尝试,这是工作:)docker-compose.yml

version: "3.7"
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
    hostname: "{{.Node.Hostname}}"
    environment:
      - node.name={{.Node.Hostname}}
      - cluster.name=my-cluster
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
      - discovery.seed_hosts=elasticsearch
      - cluster.initial_master_nodes=node1,node2,node3
      - node.ml=false
      - xpack.ml.enabled=false
      - xpack.monitoring.enabled=false
      - xpack.security.enabled=false
      - xpack.watcher.enabled=false
      - bootstrap.memory_lock=false
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
    deploy:
      mode: global
      endpoint_mode: dnsrr
      resources:
        limits:
          memory: 4G
  nginx:
    image: nginx:1.17.1-alpine
    ports:
      - 9200:9200
    deploy:
      mode: global
    command: |
      /bin/sh -c "echo '
      user nobody nogroup;
      worker_processes auto;
      events {
        worker_connections 1024;
      }
      http {
        client_max_body_size 4g;
        resolver 127.0.0.11 ipv6=off;
        server {
          listen *:9200;
          location / {
            proxy_set_header Connection keep-alive;
            set $$url http://elasticsearch:9200;
            proxy_pass $$url;
            proxy_set_header  Host $$http_host;
            proxy_set_header  X-Real-IP $$remote_addr;
            proxy_set_header  X-Forwarded-For $$proxy_add_x_forwarded_for;
          }
        }
      }' | tee /etc/nginx/nginx.conf && nginx -t && nginx -g 'daemon off;'"

volumes:
  elasticsearch-data:
n1bvdmb6

n1bvdmb62#

尝试手动指定所有特定的IP和绑定是棘手的,因为群覆盖网络。相反,只需使您的ES节点可发现,并让Swarm处理节点发现和通信。为了使它们可发现,我们可以使用一个可预测的名称,如Swarm节点主机名。
尝试按如下所示更改swarm.yml文件中的环境设置:

environment:
    - network.host=0.0.0.0
    - discovery.seed_hosts=elasticsearch #Service name, to let Swarm handle discovery
    - cluster.initial_master_nodes=master,cluster,cluster2 #Swarm nodes host names
    - node.name={{.Node.Hostname}} #To create a predictable node name

当然,这是假设我们已经知道了群集主机名,正如您在上面的屏幕截图中指出的那样。如果不知道这些值,我们将无法获得一组可预测的节点名来查找。在这种情况下,您可以创建一个具有特定节点名的ES节点条目,然后创建另一个引用第一个条目的节点名为cluster.initial_master_nodes的条目。

l0oc07j2

l0oc07j23#

使用无端口的dnsrr模式。使用nginx暴露elasticsearch;)查看我的docker-compose.yml

nwwlzxa7

nwwlzxa74#

根据我的经验,https://github.com/shazChaudhry/docker-elastic工作得很好,整个repo中的一个文件就足够了。我下载了https://github.com/shazChaudhry/docker-elastic/blob/master/docker-compose.yml并删除了logstash位,我不需要它。然后在.bashrc中添加了以下代码

export ELASTICSEARCH_HOST=$(hostname)
export ELASTICSEARCH_PASSWORD=foobar
export ELASTICSEARCH_USERNAME=elastic
export ELASTIC_VERSION=7.4.2
export INITIAL_MASTER_NODES=$ELASTICSEARCH_HOST

docker stack deploy --compose-file docker-compose.yml elastic的工作原理。

zte4gxcn

zte4gxcn5#

我从Ahmet Vehbi Olgaç的docker-compose.yml中收集到了一些想法,它对我很有用:
1.使用deployment/mode: global。这将导致群为每个这样配置的节点部署一个副本到每个群工作机。
1.使用deployment/endpoint_mode: dnsrr。这将允许群中的所有容器通过服务名称访问节点。
1.使用hostname: {{.Node.Hostname}}或类似的基于模板的表达式。这可确保每个部署的容器具有唯一的名称。
1.使用environment/node.name={{.Node.Hostname}}。同样,你可以改变模式。关键是每个es节点应该得到一个唯一的名称。
1.使用cluster.initial_master_nodes=*hostname1*,*hostname2*,...。假设你知道你的docker worker机器的主机名。使用你在#3中使用的任何模式,但是替换掉整个主机名,并包括所有的主机名。
如果您不知道自己的主机名,可以按照Andrew Cachia的答案进行操作:设置一个容器(不要复制它),使其单独充当主种子,并为其给予可预测的主机名,然后让所有其他节点将该节点作为主种子引用。但是,这会引入单点故障。

oiopk7p5

oiopk7p56#

ElasticSearch8.5.0答案。
出于我的需要,我不想添加反向代理/负载平衡器,但我确实想在运行Elasticsearch副本的swarm节点上公开端口9200(仅使用swarm),以便外部客户端可以访问Elasticsearch REST API。因此,我使用端点模式dnsrr(ref),并在运行副本的主机上公开端口9200。
如果您不需要公开端口9200(即,没有任何东西会连接到swarm之外的elasticsearch副本),请从elasticsearch服务中删除ports:配置。
我也只想让elasticsearch副本运行在我的一个swarm节点子集上(3个),我在这3个节点上创建了docker节点标签elasticsearch,然后mode: global和约束node.labels.elasticsearch==True将确保在每个节点上运行1个副本。
我也在这3个节点之一上运行Kibana:swarm可以挑选哪一个,因为矶钓5601暴露在swarm入口覆盖网络上
您可能需要编辑的行使用######标记。
第一个
部署:

# . ./elastic-stack-env
# docker stack deploy -c elastic-stack.yml elastic
# # ... after Kibana comes up, you can remove the setup service if you want:
# docker service rm elastic_setup

下面是我创建Elasticsearch CA和证书的过程:
第一个
我如何创建Kibana证书不在此问题的范围之内。
我运行了一个Fluent Bit swarm服务(mode: global,docker network elastic-net)来向elasticsearch服务发送日志。

[OUTPUT]
    name          es
    match         <whatever is appropriate for you here>
    host          elasticsearch
    port          9200
    index         my-index-default
    http_user     fluentbit
    http_passwd   ${FLUENTBIT_PASSWORD}
    tls           on
    tls.ca_file   /certs/ca/ca.crt
    tls.crt_file  /certs/elasticsearch/elasticsearch.crt
    tls.key_file  /certs/elasticsearch/elasticsearch.key
    retry_limit   false
    suppress_type_name on
#   trace_output  on

主机elasticsearch将由Docker的DNS服务器解析为elasticsearch副本的三个IP地址,因此不存在单点故障。

相关问题