Elasticsearch集群搭建
1、集群搭建
-
下载elasticsearch-7.0.0-linux-x86_64.tar.gz
-
解压文件:tar -xzvf elasticsearch-7.0.0-linux-x86_64.tar.gz,复制解压文件并重命名
cp -r elasticsearch-7.0.0 es-master
cp -r elasticsearch-7.0.0 es-slave1
cp -r elasticsearch-7.0.0 es-slave2
-
修改配置文件如下:
①主节点配置:es-master/config/elasticsearch.yml
network.host: 0.0.0.0 http.port: 9200 http.cors.enabled: true http.cors.allow-origin: "*" cluster.name: xiang node.name: master node.master: true cluster.initial_master_nodes: ["master"]
②从节点1配置:es-slave1/config/elasticsearch.yml
cluster.name: xiang node.name: slave1 network.host: 0.0.0.0 http.port: 8200 discovery.seed_hosts: ["127.0.0.1:9300"]
③从节点2配置:es-slave2/config/elasticsearch.yml
cluster.name: xiang node.name: slave2 network.host: 0.0.0.0 http.port: 7200 discovery.seed_hosts: ["127.0.0.1:9300"]
-
配置说明
配置项 配置说明 network.host 服务IP绑定(0.0.0.0即服务器所在所有IP段均可访问) http.port 集群服务端口号(集群对外通讯端口) http.cors.enabled 是否开启跨域 http.cors.allow-origin 跨域匹配规则 cluster.name 集群名称(注:同一集群必须相同) node.master 是否可以为主节点 cluster.initial_master_nodes 初始化主节点选项(初始化时主节点的选择范围) discovery.seed_hosts 集群发现(集群内部通信端口) -
启动集群
sh es-master/bin/elasticsearch -d
sh es-slave1/bin/elasticsearch -d
sh es-slave2/bin/elasticsearch -d
2、插件安装及使用
-
安装图形化插件elasticsearch-head
①下载elasticsearch-head-master(master.zip)
②解压master.zip:unzip master.zip
③需要安装node环境
④安装依赖 npm install
⑤启动:nohup npm run start > es-head.log 2>&1 &
-
脚本启动
启动脚本,使用时修改必要的路径
#! /bin/bash cd /home/es sh es-master/bin/elasticsearch -d sleep 5 echo "es-master start Success" sh es-slave/es-slave1/bin/elasticsearch -d sleep 5 echo "es-slave1 start Success" sh es-slave/es-slave2/bin/elasticsearch -d sleep 5 echo "es-slave2 start Success" cd elasticsearch-head-master nohup npm run start > es-head.log 2>&1 & echo "elasticsearch-head-master Success"
-
查看可视化界面
①访问http://127.0.0.1:9100/,es启动可能稍慢需要等待,启动后如图所示:
[外链图片转存失败(img-t1wJZxZG-1568016606543)(C:\Users\muziy\AppData\Roaming\Typora\typora-user-images\1567479030877.png)]
如上图所示,显示了三个节点表示集群启动成功,★表示主节点,●表示从节点,在连接地址栏处填写端口号为http://127.0.0.1:9200/
-
关闭对应端口程序脚本
#! /bin/bash echo $input_port array=(7200 8200 9200 9100) for port in ${array[*]} do pid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F"/" '{ print $1 }') if [ ! -n "$pid" ] then echo "端口:$port 未找到相应进程" continue fi kill $pid done
-
elasticsearch-head基本使用
3、Elasticsearch基本使用
-
数据写入
curl -X POST http://10.18.31.224:9200/lixiang-demo/testlog -d '{"data":"2019-09-06 16:32:38","user":"lixiang","msg":"哈哈哈"}' #返回结果 { "_index": "lixiang-demo", "_type": "testlog", "_id": "0p23BW0BodTikRFyJVVm", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
-
数据获取
通过第一步生成的_id获取文档信息
curl -X GET http://10.18.31.224:9200/lixiang-demo/testlog/0p23BW0BodTikRFyJVVm 或者 http://10.18.31.224:9200/lixiang-demo/testlog/0p23BW0BodTikRFyJVVm
返回结果如下:
{ "_index": "lixiang-demo", "_type": "testlog", "_id": "0p23BW0BodTikRFyJVVm", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "data": "2019-09-06 16:32:38", "user": "lixiang", "msg": "哈哈哈" } }
#添加后缀 http://10.18.31.224:9200/lixiang-demo/testlog/0p23BW0BodTikRFyJVVm/_source
返回结果如下:
#只显示了添加的内容 { "data": "2019-09-06 16:32:38", "user": "lixiang", "msg": "哈哈哈" }
-
删除
http://10.18.31.224:9200/lixiang-demo/testlog/0p23BW0BodTikRFyJVVm
返回结果如下:
{ "_index": "lixiang-demo", "_type": "testlog", "_id": "0p23BW0BodTikRFyJVVm", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
-
更新
http://10.18.31.224:9200/lixiang-demo/testlog/EJ3aBW0BodTikRFyKm3A
返回结果如下:
{ "_index": "lixiang-demo", "_type": "testlog", "_id": "EJ3aBW0BodTikRFyKm3A", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 3, "_primary_term": 1 }
4、搜索
-
全文搜索
查询当前索引全部文档:
http://10.18.31.224:9200/lixiang-demo/testlog/_search
结果如下:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 4, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "lixiang-demo", "_type": "testlog", "_id": "EJ3aBW0BodTikRFyKm3A", "_score": 1, "_source": { "data": "2019-09-06 16:32:38", "user": "lixiang", "msg": "嘿嘿嘿" } }, { "_index": "lixiang-demo", "_type": "testlog", "_id": "qZ3iBW0BodTikRFyH3Ay", "_score": 1, "_source": { "query": { "bool": { "must": [ { "match_all": {} } ], "must_not": [], "should": [] } }, "from": 0, "size": 10, "sort": [], "aggs": {} } }, { "_index": "lixiang-demo", "_type": "testlog", "_id": "u53iBW0BodTikRFyVHBf", "_score": 1, "_source": { "query": { "bool": { "must": [ { "match_all": {} } ], "must_not": [], "should": [] } }, "from": 0, "size": 10, "sort": [], "aggs": {} } }, { "_index": "lixiang-demo", "_type": "testlog", "_id": "vZ3iBW0BodTikRFyWnCu", "_score": 1, "_source": { "query": { "bool": { "must": [ { "match_all": {} } ], "must_not": [], "should": [] } }, "from": 0, "size": 10, "sort": [], "aggs": {} } } ] } }
部分查询举例:
API举例 提交方式 说明 http://10.18.31.224:9200/_search?q=张三 GET 全文匹配张三,不分索引和文档类型 http://10.18.31.224:9200/_cat/indices?v GET 查看全部索引 http://10.18.31.224:9200/_cat/nodes?v GET 集群的节点列表 http://10.18.31.224:9200/_cat/health?v GET 集群健康状态