1.使用rpm包安装Elasticsearch 6.6
https://www.elastic.co/cn/downloads/past-releases#elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm
yum install java-1.8.0-openjdk -y
[root@es1 ~]# rpm -ivh elasticsearch-6.6.0.rpm
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
sudo systemctl start elasticsearch.service
Created elasticsearch keystore in /etc/elasticsearch
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
[root@es1 ~]# ps aux |grep elastic
[root@es1 ~]# ss -lntup|grep 9200
[root@es1 ~]# curl 127.0.0.1:9200
{
"name" : "ZVoraSj",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "cA_BlshcRd2lJCANzUEJvA",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
[root@es1 ~]# rpm -ql elasticsearch
/etc/elasticsearch/elasticsearch.yml #es配置文件
/etc/elasticsearch/jvm.options #jvm虚拟机
/etc/init.d/elasticsearch #init启动文件
/etc/sysconfig/elasticsearch #环境变量参数
/usr/lib/sysctl.d/elasticsearch.conf #jvm打开配置
/usr/lib/systemd/system/elasticsearch.service
/var/lib/elasticsearch #默认数据目录
/var/log/elasticsearch #默认日志目录
/var/run/elasticsearch #默认pid目录
2. 配置文件
[root@es1 ~]# egrep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml
node.name: node-1 #主机名
path.data: /data/elasticsearch #数据目录
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true #内存锁定 启动就锁定设置内存 不让其他程序使用
network.host: 10.0.0.233 #监听ip
[root@es1 ~]# vim /etc/elasticsearch/jvm.options
-Xms1g #最小1G
-Xmx1g #最大1G
内存限制:官方推荐
1.不超过32G
2.最大最小内存设置一样
3.配置文件中设置锁定内存
4.最少给服务器本身空余50%的内存
[root@es1 ~]# mkdir /data/elasticsearch -p
[root@es1 ~]# chown -R elasticsearch.elasticsearch /data/elasticsearch/
3. 问题排错
https://www.elastic.co/guide/en/elasticsearch/reference/6.6/setting-system-settings.html#systemd
systemctl restart elasticsearch.service
[root@es1 ~]# tailf /var/log/elasticsearch/elasticsearch.log
[2021-02-25T15:17:49,829][INFO ][o.e.n.Node ] [node-1] starting ...
[2021-02-25T15:17:49,959][INFO ][o.e.t.TransportService ] [node-1] publish_address {10.0.0.233:9300}, bound_addresses {10.0.0.233:9300}
[2021-02-25T15:17:49,976][INFO ][o.e.b.BootstrapChecks ] [node-1] bound or publishing to a non-loopback address, enforcing bootstrap checks
[2021-02-25T15:17:49,978][ERROR][o.e.b.Bootstrap ] [node-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
vim /etc/systemd/system/elasticsearch.service.d/override.conf
或者
systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
systemctl daemon-reload
[root@es1 ~]# systemctl restart elasticsearch.service 正常
[root@es1 ~]# curl 10.0.0.233:9200
{
"name" : "node-1",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "MNeMj7vLQkG9VEbG1OAFRA",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
3.elasticsearch-head 插件
https://github.com/mobz/elasticsearch-head
方法1: 不推荐 很慢
yum install nodejs npm openssl screen git -y
npm install -g cnpm --registry=https://registry.npm.taobao.org
cd /opt
git clone https://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
cnpm install
screen -S es-head
cnpm run start
Ctrl +A +D
方法2:
谷歌浏览器中加载elasticsearch-head 插件
https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm/related
vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
systemctl restart elasticsearch.service
解压下 选择文件时候选择解压的那个文件夹 不能点进去 安装成功后连接就行了
4.elasticsearch 增删改查
添加一个索引名称vipinfo
pretty 是以友好界面显示
[root@es1 ~]# curl -XPUT 'localhost:9200/vipinfo?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "vipinfo"
}
1是索引主键
curl -XPUT http://localhost:9200/vipinfo/userinfo/1 -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
curl -X PUT http://localhost:9200/vipinfo/userinfo/2 -H 'Content-Type: application/json' -d'
{
"first_name" : "Tom",
"last_name" : "Mouse",
"age" : 35,
"about" : "I love to go QQ climbing",
"interests": [ "sports", "music" ]
}'
curl -XPUT http://localhost:9200/vipinfo/userinfo/3?pretty -H 'Content-Type: application/json' -d'
{
"first_name" : "Jack",
"last_name" : "Cat",
"age" : 35,
"about" : "I love to go mouse climbing",
"interests": [ "sports", "music" ]
}'
也可以索引(库)不存在 id不能重复
curl -XPUT http://localhost:9200/linux/test/1?pretty -H 'Content-Type: application/json' -d'
{
"first_name" : "Test",
"age" : 35,
"about" : "I love to go mouse climbing"
}'
更改 修改
curl -XPUT http://localhost:9200/linux/test/1?pretty -H 'Content-Type: application/json' -d'
{
"first_name" : "Test",
"age" : 20,
"about" : "I love to go mouse climbing"
}'
或者这种 上面需要全部写完 下面只需要写修改的 数据容易丢失 不建议
curl -XPUT http://localhost:9200/linux/test/1?pretty -H 'Content-Type: application/json' -d'
{
"first_name" : "TestName"
}'
id不能重复 使用随机id 在表中 添加sid字段 方便查询
curl -XPOST http://localhost:9200/linux/test/?pretty -H 'Content-Type: application/json' -d'
{
"sid" : "2",
"first_name" : "Test",
"age" : 35,
"about" : "I love to go mouse climbing"
}'
查询 精确查询
curl -XGET http://localhost:9200/linux/test/_search?pretty 全部查询
curl -XGET http://localhost:9200/linux/test/1?pretty 精确查询
curl -XGET http://localhost:9200/linux/test/_search?q=Test 条件查询
5.elasticsearch 集群
https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
集群状态颜色:
绿色: 所有条件都满足 数据完成 副本满足
黄色: 数据完整 副本不满足
红色: 有索引数据出现不完整了
紫色: 有分配正在同步中
es1配置文件
[root@es1 ~]# egrep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: linux #集群名称 随意
node.name: node-1 #集群中名称 不重复
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.233", "10.0.0.234"] #集群发现节点
discovery.zen.minimum_master_nodes: 2 #node节点数量/2+1 选项相关 如果网络有抖动 必设置
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@es1 ~]# tailf /var/log/elasticsearch/linux.log 日志变成跟集群名称一样名字
[2021-02-26T17:47:46,319][WARN ][o.e.d.z.ZenDiscovery ] [node-1] not enough master nodes discovered during pinging (found [[Candidate{node={node-1}{c0mppimiSguJXrl_bHb2vQ}{PstnBUaPTDyiyVcrq5c8WQ}{10.0.0.233}{10.0.0.233:9300}{ml.machine_memory=2095951872, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true}, clusterStateVersion=-1}]], but needed [2]), pinging again 无法启动 因为10.0.0.234未启动
es2:
yum install -y java
rpm -ivh elasticsearch-6.6.0.rpm
systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
systemctl daemon-reload
systemctl enable elasticsearch.service
[root@es2 ~]# scp root@10.0.0.233:/etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml
[root@es2 ~]# egrep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: linux
node.name: node-2
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.233", "10.0.0.234"]
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@es2 ~]# mkdir /data/elasticsearch -p
[root@es2 ~]# chown -R elasticsearch.elasticsearch /data/elasticsearch/
[root@es2 ~]# systemctl start elasticsearch.service
6. elasticsearch使用
节点角色: 默认情况 所有节点都是工作节点
主节点 负责调度数据返回数据
工作节点 负责处理数据
[root@es2 ~]# curl -XGET 'localhost:9200/_cat/nodes' 查看集群节点
10.0.0.234 20 96 0 0.00 0.01 0.05 mdi * node-2
10.0.0.233 18 96 0 0.00 0.01 0.05 mdi - node-1
[root@es2 ~]# curl -s -XGET 'localhost:9200/_cat/nodes' |wc -l 方便写脚本控制
2
[root@es2 ~]# curl -XGET 'localhost:9200/_cluster/health?pretty' 查看集群状态
{
"cluster_name" : "linux",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 10,
"active_shards" : 20,
"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
}
两个节点 一个不能用 需修改配置文件中 后重启服务
discovery.zen.minimum_master_nodes: 1
如果discovery.zen.minimum_master_nodes 不加参数 会出现脑裂问题 数据不一致
实验脑裂现象模拟:
将discovery.zen.minimum_master_nodes 注释
systemctl start firewalld 防火墙打开
systemctl restart elasticsearch.service
这样两边就相应于两个集群
[root@es2 ~]# curl -XGET 'localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "linux",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1, #变成一个节点
"number_of_data_nodes" : 1,
"active_primary_shards" : 10,
"active_shards" : 10,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 10,
"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" : 50.0
}
插入冲突数据 在两边
es1插入:
curl -XPOST http://localhost:9200/linux/test/18?pretty -H 'Content-Type: application/json' -d'
{
"sid" : "18",
"first_name" : "Test",
"age" : 35,
"about" : "I love to go mouse climbing"
}'
curl -XPOST http://localhost:9200/linux/test/20?pretty -H 'Content-Type: application/json' -d'
{
"sid" : "20",
"first_name" : "Test",
"age" : 35,
"about" : "I love to go mouse climbing"
}'
es2插入:
curl -XPOST http://localhost:9200/linux/test/19?pretty -H 'Content-Type: application/json' -d'
{
"sid" : "19",
"first_name" : "Test",
"age" : 35,
"about" : "I love to go mouse climbing"
}'
curl -XPOST http://localhost:9200/linux/test/20?pretty -H 'Content-Type: application/json' -d'
{
"sid" : "20",
"first_name" : "Test1",
"age" : 351,
"about" : "I love to go mouse climbing"
}'
注释取消 防火墙关闭 重启服务
集群状态恢复正常
数据出现不一致 不同的合并 相同以主节点的为准
7. 添加第三个Elasticsearch node节点
yum install -y java
rpm -ivh elasticsearch-6.6.0.rpm
systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
systemctl daemon-reload
systemctl enable elasticsearch.service
[root@es3 ~]# scp root@10.0.0.233:/etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml
[root@es3 ~]# egrep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: linux
node.name: node-3
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.233", "10.0.0.235"] #只需要添加任何一个集群中的ip即可
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@es3 ~]# mkdir /data/elasticsearch -p
[root@es3 ~]# chown -R elasticsearch.elasticsearch /data/elasticsearch/
[root@es3 ~]# systemctl start elasticsearch.service
8. 数据分片 副本
默认集群 5分片 1副本 默认3个节点 可以坏2个节点的但是如果两个节点同时坏掉就无法恢复数据
集群监控:
1.集群是否green
2.集群节点是否正常
3分片 1副本
curl -XPUT http://localhost:9200/index1?pretty -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}'
创建分片不能调整 但是副本可以调整 只是影响磁盘大小 副本变成2
curl -XPUT http://localhost:9200/index1/_settings?pretty -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_replicas" : 2
}
}'