項目中用到Elasticsearch提供索引搜索服務,由於項目要部署在docker中,希望elasticsearch也能夠搭建在docker環境中,經過學習,成功在docker環境中完成elasticsearch集群環境的搭建。
特將搭建過程記錄如下:
參考文章:https://segmentfault.com/a/1190000018606414
本文將使用Docker
容器(使用docker-compose
編排)快速部署Elasticsearch 集群
,可用於開發環境(單機多實例)或生產環境部署。
注意,6.x
版本已經不能通過 -Epath.config
參數去指定配置文件的加載位置,文檔說明:
For the archive distributions, the config directory location defaults to
$ES_HOME/config
. The location of the >config directorycan be changed
via theES_PATH_CONF
environment variable as follows:ES_PATH_CONF=/path/to/my/config ./bin/elasticsearch
Alternatively, you can export the ES_PATH_CONF environment variable via the command line or via your shell profile.
即交給環境變量 ES_PATH_CONF
來設定了(官方文檔),單機部署多個實例且不使用容器的同學多多注意。
一、環境准備
安裝docker及docker-compose
docker的安裝請參考博文:https://www.cnblogs.com/guoxiangyue/p/9829221.html
docker-compose安裝:
官網安裝步驟地址:https://docs.docker.com/compose/install/#install-compose
1、執行以下命令安裝最新版本:
# curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
要安裝指定版本, 替換1.23.2為你選擇的版本
2、賦權Apply executable permissions to the binary
# chmod +x /usr/local/bin/docker-compose
3、Optionally, install command completion for the bash and zsh shell.
4、測試(查看版本)Test the installation
# docker-compose --version
打印出docker-compose的版本信息,即為安裝成功。
二、創建數據目錄
在本機中創建Elasticsearch數據的存儲目錄
1、創建數據/日志目錄/插件目錄 這里我們部署2個節點
# mkdir /usr/local/elasticsearch/data/{es01,es02} -p
# mkdir /usr/local/elasticsearch/logs/{es01,es02} -p
# mkdir /usr/local/elasticsearch/plugins/{es01,es02} -p
2、賦予目錄讀寫權限
讀寫權限這里,為了方便,直接賦予 777
# cd /usr/local/elasticsearch
# chmod 0777 data/* -R && chmod 0777 logs/* -R && chmod 0777 plugins/* -R
3、為防止jvm報錯,進行以下配置
# echo vm.max_map_count=262144 >> /etc/sysctl.conf # sysctl -p
三、docker-compse 編排服務
1、准備編排文件
# cd /usr/local/elasticsearch
# vim docker-compose.yml
2、編排文件參數說明
依賴鏡像:(這里使用6.8.6版本)
image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6
集群名稱:
- cluster.name=knowledgebase
節點名稱、是否可作為主節點、是否存儲數據
- node.name=node01
- node.master=true
- node.data=true
鎖定進程的物理內存地址避免交換(swapped)來提高性能
- bootstrap.memory_lock=true
開啟cors以便使用Head插件
- http.cors.enabled=true
- http.cors.allow-origin=*
JVM內存大小配置
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
由於5.2.1
后的版本是不支持多播的,所以需要手動指定集群各節點的tcp
數據交互地址,用於集群的節點發現
和failover
,默認缺省9300
端口,如設定了其它端口需另行指定,這里我們直接借助容器通信,也可以將各節點的9300
映射至宿主機,通過網絡端口通信。
- "discovery.zen.ping.unicast.hosts=elasticsearch_n0,elasticsearch_n1,elasticsearch_n2"
- "discovery.zen.minimum_master_nodes=2"
完整的docker-compose.yml文件:
version: '2' services: es01: image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6 container_name: es01 privileged: true restart: always environment: - cluster.name=knowledgebase - node.name=node01 - node.master=true
- node.data=true
- bootstrap.memory_lock=true
- http.cors.enabled=true
- http.cors.allow-origin=*
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=es01,es02"
- "discovery.zen.minimum_master_nodes=1" ulimits: memlock: soft: -1 hard: -1 volumes: - ./data/es01:/usr/share/elasticsearch/data - ./config/es01/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml - ./logs/es01:/usr/share/elasticsearch/logs - ./plugins/es01:/usr/share/elasticsearch/plugins ports: - 9200:9200
- 9300:9300 es02: image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6 container_name: es02 privileged: true restart: always environment: - cluster.name=knowledgebase - node.name=node02 - node.master=true
- node.data=true
- bootstrap.memory_lock=true
- http.cors.enabled=true
- http.cors.allow-origin=*
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=es01,es02"
- "discovery.zen.minimum_master_nodes=1" ulimits: memlock: soft: -1 hard: -1 volumes: - ./data/es02:/usr/share/elasticsearch/data - ./config/es02/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml - ./logs/es02:/usr/share/elasticsearch/logs - ./plugins/es02:/usr/share/elasticsearch/plugins depends_on: - es01 ports: - 9201:9200
- 9301:9300
Elasticsearch集群啟動的時候,需要主節點啟動成功后,其他節點才能啟動並join到集群里,因此在配置文件里要保證這一點,官網Control startup and shutdown order in Compose這篇文章介紹了如何控制啟動和關閉順序的問題,我這里是通過配置restart: always
和depends_on
來控制順序的,這個辦法比較簡單。
這里我們分別為node1/node2
開放宿主機的9200/9201
作為http服務端口
,各實例的tcp數據傳輸
用默認的9300
通過容器管理通信。
3、分別編寫每個節點的elasticsearch.yml配置
編寫node1的配置文件
# cd /usr/local/elasticsearch/config/es01
# vim elasticsearch.yml
在elasticsearch.yml中寫入以下內容:
cluster.name: knowledgebase node.name: es01 node.master: true node.data: false path.data: /usr/share/elasticsearch/data path.logs: /usr/share/elasticsearch/logs bootstrap.memory_lock: true network.host: 0.0.0.0 http.port: 9200 transport.tcp.port: 9300 discovery.zen.ping.unicast.hosts: ["es01", "es02"] http.cors.enabled: true http.cors.allow-origin: "*" xpack.security.enabled: false xpack.security.transport.ssl.enabled: false
編寫node2的配置文件
# cd /usr/local/elasticsearch/config/es02
# vim elasticsearch.yml
在elasticsearch.yml中寫入以下內容:
cluster.name: knowledgebase node.name: es02 node.master: true node.data: false path.data: /usr/share/elasticsearch/data path.logs: /usr/share/elasticsearch/logs bootstrap.memory_lock: true network.host: 0.0.0.0 http.port: 9200 transport.tcp.port: 9300 discovery.zen.ping.unicast.hosts: ["es01", "es02"] http.cors.enabled: true http.cors.allow-origin: "*" xpack.security.enabled: false xpack.security.transport.ssl.enabled: false
四、創建並啟動服務
# cd /usr/local/elasticsearch
# docker-compose up -d
執行完成后,查看容器狀態
# docker-compose ps
查看集群狀態
訪問 http://192.168.56.32:9200/_cat/nodes?v 即可查看集群狀態:192.168.56.32 是我的服務地址
PS. 使用Head插件觀察:
Head插件的安裝請查看博文:https://www.cnblogs.com/guoxiangyue/p/10037928.html
安裝完成后,訪問Elasticsearch集群:
成功訪問!至此docker環境下搭建Elasticsearch集群配置完成。