天天看點

實戰案例丨用 Docker Compose 快速運作一個 3 節點 Elasticsearch 叢集

實戰案例丨用 Docker Compose 快速運作一個 3 節點 Elasticsearch 叢集

出品丨Docker公司(ID:docker-cn)

編譯丨小東

每周一、三、五,與您不見不散!

使用 Docker 在筆記本電腦上安裝 Elasticsearch 叢集進行測試是非常友善的。在這篇文章中,我将向您展示如何快速、簡便地在 docker 上運作一個3節點的 elasticsearch 叢集進行測試。

先決條件

我們需要設定“vm.max_map_count”核心參數:

$ sudo sysctl -w vm.max_map_count=262144
           

若要永久設定此值,請将其添加到“/etc/sysctl.conf”并使用“sudo sysctl -p”指令重新加載。

Docker Compose

我們将引用的 docker compose 檔案:

version: '2.2'
services:
 elasticsearch:
   image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
   container_name: elasticsearch
   environment:
     - cluster.name=docker-cluster
     - bootstrap.memory_lock=true
     - http.cors.enabled=true
     - http.cors.allow-origin=*
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
   ulimits:
     memlock:
       soft: -1
       hard: -1
   volumes:
     - esdata1:/home/ruan/workspace/docker/elasticsearch/data
   ports:
     - 9200:9200
   networks:
     - esnet
 elasticsearch2:
   image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
   container_name: elasticsearch2
   environment:
     - cluster.name=docker-cluster
     - bootstrap.memory_lock=true
     - http.cors.enabled=true
     - http.cors.allow-origin=*
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
     - "discovery.zen.ping.unicast.hosts=elasticsearch"
   ulimits:
     memlock:
       soft: -1
       hard: -1
   volumes:
     - esdata2:/home/ruan/workspace/docker/elasticsearch/data
   networks:
     - esnet
 elasticsearch3:
   image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
   container_name: elasticsearch3
   environment:
     - cluster.name=docker-cluster
     - bootstrap.memory_lock=true
     - http.cors.enabled=true
     - http.cors.allow-origin=*
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
     - "discovery.zen.ping.unicast.hosts=elasticsearch"
   ulimits:
     memlock:
       soft: -1
       hard: -1
   volumes:
     - esdata3:/home/ruan/workspace/docker/elasticsearch/data
   networks:
     - esnet
 
 kibana:
   image: 'docker.elastic.co/kibana/kibana:6.3.2'
   container_name: kibana
   environment:
     SERVER_NAME: kibana.local
     ELASTICSEARCH_URL: http://elasticsearch:9200
   ports:
     - '5601:5601'
   networks:
     - esnet
 
 headPlugin:
   image: 'mobz/elasticsearch-head:5'
   container_name: head
   ports:
     - '9100:9100'
   networks:
     - esnet
 
volumes:
 esdata1:
   driver: local
 esdata2:
   driver: local
 esdata3:
   driver: local
 
networks:
 esnet:
           

現在,隻需確定我們在 compose 檔案中所引用的路徑是真實存在的,在我展示的執行個體中,其路徑是“/home/ruan/workspace/docker/elasticsearch/data”。

部 署

使用 docker compose 來部署 elasticsearch 叢集:

$ docker-compose up
           

它将在前台運作,您可以在控制台看到其輸出結果。

測試 Elasticsearch

接下來,讓我們運作幾個查詢。首先,檢查群集運作狀況 api:

$ curl http://127.0.0.1:9200/_cluster/health?pretty
{
 "cluster_name" : "docker-cluster",
 "status" : "green",
 "timed_out" : false,
 "number_of_nodes" : 3,
 "number_of_data_nodes" : 3,
 "active_primary_shards" : 1,
 "active_shards" : 2,
 "relocating_shards" : 0,
 "initializing_shards" : 0,
 "unassigned_shards" : 0,
 "delayed_unassigned_shards" : 0,
 "number_of_pending_tasks" : 0,
 "number_of_in_flight_fetch" : 0,
 "task_max_waiting_in_queue_millis" : 0,
 "active_shards_percent_as_number" : 100.0
}
           

建立一個複制計數為2的索引:

$ curl -H "Content-Type: application/json" -XPUT http://127.0.0.1:9200/test -d '{"number_of_replicas": 2}'
           

将文檔提取到 elasticsearch:

$ curl -H "Content-Type: application/json" -XPUT http://127.0.0.1:9200/test/docs/1 -d '{"name": "ruan"}'
{"_index":"test","_type":"docs","_id":"1","_version":1,"result":"created","_shards":{"total":3,"successful":3,"failed":0},"_seq_no":0,"_primary_term":1}
           

檢視索引:

$ curl http://127.0.0.1:9200/_cat/indices?v
health status index                      uuid                  pri rep docs.count docs.deleted store.size pri.store.size
green open  test                       w4p2Q3fTR4uMSYBfpNVPqw  5  2         1           0     3.3kb         1.1kb
green open  .monitoring-es-6-2018.04.29 W69lql-rSbORVfHZrj4vug  1  1      1601          38       4mb           2mb
           

Kibana

Kibana 也包含在應用棧中,可以通過“

http://localhost:5601/

”通路它,如下圖所示:

實戰案例丨用 Docker Compose 快速運作一個 3 節點 Elasticsearch 叢集

Elasticsearch Head UI

我喜歡直接使用 RESTFul API,但是如果您想使用 UI 與 Elasticsearch 進行互動,你可以通過“http:// localhost:9100/”通路它,如下圖所示:

實戰案例丨用 Docker Compose 快速運作一個 3 節點 Elasticsearch 叢集

删除群集

因為它在前台運作,是以您隻需按下“ctrl + c”就可以持續地将資料儲存在我們的 compose 檔案中。當您再次啟動叢集時,資料仍然會存在。

浏覽

https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

了解更多Elasticsearch 的相關資訊!