ElasticSearch使用教程
docker 安装es集群
运行
先用docker分别启动两个es服务,由于后面需要进行配置,这里假设es1所在的机器公网ip为:192.168.1.148
,es2所在机器ip:192.168.1.149
启动的时候指定了jvm参数,此外还可以通过编辑config/jvm.options
配置文件进行参数调整。
# 运行ES1
docker run -d --name es1 -e "ES_JAVA_OPTS=-Xms128m -Xmx128m" -d -p 9200:9200 -p 9300:9300 -p 5601:5601 -e "discovery.type=single-node" elasticsearch:7.4.1
# -e "discovery.type=single-node" 是用来标志单节点 这里需去掉
# 运行ES2
docker run -d --name es2 -e "ES_JAVA_OPTS=-Xms128m -Xmx128m" -d -p 9201:9201 -p 9301:9301 -e "discovery.type=single-node" elasticsearch:7.4.1
测试命令:curl localhost:9200 && curl localhost:19200
配置
Elasticsearch集群环境由多个节点(es实例)之间互相发现并组成,因此核心关键就是正确配置,而主要的配置文件也仅有config/elasticsearch.yml
。基于Docker启动服务的时候可以选择将配置目录(或文件)挂载出来
通过修改容器内的文件配置集群方法
es1:
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 192.168.99.100
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.99.100:9300","192.168.99.100:9301"]
discovery.zen.minimum_master_nodes: 1
es2
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.99.100
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.99.100:9300","192.168.99.100:9301"]
discovery.zen.minimum_master_nodes: 1
根据实际情况分别配置好两个es实例,最后重启实例即可:
docker restart es1 && docker restart es2
通过挂载文件的方式
1>创建数据挂在目录,以及配置ElasticSearch集群配置文件 home 目录下
mkdir /docker/ES
mkdir /docker/ES/data1
mkdir /docker/ES/data2
mkdir /docker/ES/data3
mkdir /docker/ES/config
在config文件夹下创建配置文件
touch es1.yml es2.yml es3.yml
编辑es1.yml文件
vi es1.yml
cluster.name: es-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 192.168.99.100
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["192.168.99.100:9300","192.168.99.100:9301","192.168.99.100:9302"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3"]
vi es2.yml
cluster.name: es-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.99.100
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["192.168.99.100:9300","192.168.99.100:9301","192.168.99.100:9302"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3"]
vi es3.yml
cluster.name: es-cluster
node.name: es-node3
network.bind_host: 0.0.0.0
network.publish_host: 192.168.99.100
http.port: 9202
transport.tcp.port: 9302
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["192.168.99.100:9300","192.168.99.100:9301","192.168.99.100:9302"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3"]
添加防火墙:
firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9301/tcp
firewall-cmd --add-port=9302/tcp
改变创建的文件夹权限:
chmod 777 data1 data2 data3
2>启动ElasticSearch集群容器
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v /home/docker/ES/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml --name ES01 elasticsearch:7.6.2
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 -v /home/docker/ES/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml --name ES02 elasticsearch:7.6.2
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9202:9202 -p 9302:9302 -v /home/docker/ES/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml --name ES03 elasticsearch:7.6.2
测试
以下列出的几种方式都可以查看集群状态:
- 查看日志:
docker logs -f es1
- 节点信息:
curl http://localhost:9200/_nodes?pretty
或 http://192.168.99.100:9200/_nodes?pretty ip地址可根据docker-machine ls 中查看容器ip - 确定是否成功集群,访问如下ES集群其中任何IP地址即可 http://192.168.99.100:9200/_cat/nodes?pretty
- 集群健康:
curl http://localhost:9200/_cluster/health
参考: https://www.jianshu.com/p/558e2125ef26
https://www.cnblogs.com/dalianpai/p/12701635.html
https://blog.csdn.net/art_code/article/details/99687940
https://www.jianshu.com/p/991c74a1bbf7
https://www.cnblogs.com/knownfreestyle/p/13225108.html
出现 max virtual memory areas vm.max_map_count [65530] is too low
错误内存修改
sudo sysctl -w vm.max_map_count=262144
分词器安装