Elasticsearch 6.6

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 条件查询
图片.png

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
图片.png

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"
}'
注释取消 防火墙关闭 重启服务
集群状态恢复正常 
数据出现不一致  不同的合并 相同以主节点的为准
图片.png

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
图片.png

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
  }
}'
删除建议关闭不删除数据.png

图片.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容