Elasticsearch集群部署
1、服务器规划
10.4.7.11 node1
10.4.7.12 node2
10.4.7.13 node3
1. 集群相关
一个运行中的 Elasticsearch 实例称为一个节点,而集群是由一个或者多个拥有相同 cluster.name 配置的
节点组成,它们共同承担数据和负载的压力。当有节点加入集群中或者从集群中移除节点时,集群将会重新平均
分布所有的数据。当一个节点被选举成为主节点时,它将负责管理集群范围内的所有变更,例如增加、删除索引,
或者增加、删除节点等。而主节点并不需要涉及到文档级别的变更和搜索等操作,所以当集群只拥有一个主节点
的情况下,即使流量的增加它不会成为瓶颈。 任何节点都可以成为主节点。我们的示例集群就只有一个节点,所
以它同时也成为了主节点。作为用户,我们可以将请求发送到 集群中的任何节点 ,包括主节点。 每个节点都知
道任意文档所处的位置,并且能够将我们的请求直接转发到存储我们所需文档的节点。 无论我们将请求发送到哪
个节点,它都能负责从各个包含我们所需文档的节点收集回数据,并将最终结果返回給客户端。 Elasticsearch
对这一切的管理都是透明的。
解释:
status 字段指示着当前集群在总体上是否工作正常。
它的三种颜色含义如下:
green:所有的主分片和副本分片都正常运行。
yellow:所有的主分片都正常运行,但不是所有的副本分片都正常运行。
red:有主分片没能正常运行
1. 分片:
一个分片是一个底层的工作单元 ,它仅保存了全部数据中的一部分。一个分片是一个 Lucene 的实例,它本身
就是一个完整的搜索引擎。我们的文档被存储和索引到分片内,但是应用程序是直接与索引而不是与分片进行交互。
Elasticsearch 是利用分片将数据分发到集群内各处的。分片是数据的容器,文档保存在分片内,分片又被分配到
集群内的各个节点里。 当你的集群规模扩大或者缩小时, Elasticsearch 会自动的在各节点中迁移分片,使得数据
仍然均匀分布在集群里。
一个分片可以是 主 分片或者 副本 分片。 索引内任意一个文档都归属于一个主分片,所以主分片的数目决定着
索引能够保存的最大数据量。一个索引必须创建主分片,副本分片可以没有。
分片和主流关系型数据库的表分区的概念有点类似。
2. 副本:
一个副本分片只是一个主分片的拷贝。副本分片作为硬件故障时保护数据不丢失的冗余备份,并为搜索和返回文档等读操作提供服务。
如果主分片有3个,那么一个副本replica就对应有1X3=3个replica shard副本分片。
副本分片数量计算公式 = 副本数量repilca num X 主分片数量primary shard num
在索引建立的时候就已经确定了主分片数,但是副本分片数可以随时修改。也就是说分片数量不允许修改,副本数量可以修改。
3. 分片与副本的关系:
每个主分片(primary shard)不会和副本分片(replica shard)存在于同一个节点中,有效的保证es的数据高可用性。
例如1:比如一个索引有3个分片和1副本,那么一共就有3*2=6个分片,3个是主分片,3个是副本分片,每个主分片都会对应一个副本分片。
例如2:只有2个节点,但是有3个分片和2个副本,这样的情况就会导致分片无法完全分配,因为主分片和副本分片不能存在于同一个节点中。
#三台都要执行初始化配置
[root@localhost ~]# vim /etc/sysctl.conf
vm.max_map_count=655360
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.ip_forward=1
[root@localhost ~]# sysctl -p #修改完执行使其生效
[root@localhost ~]# vim /etc/security/limits.conf
#新增内容如下:
* soft nofile 655360
* hard nofile 655360
* soft memlock unlimited
* hard memlock unlimited
* soft nproc 65535
* hard nproc 65535
* - as unlimited
2.安装es
[root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-linux-x86_64.tar.gz
[root@localhost ~]# mkdir -p /app /data/elasticsearch
[root@localhost ~]# tar -zxf elasticsearch-7.9.3-linux-x86_64.tar.gz -C /app
[root@localhost app]# ln -sv /app/elasticsearch-7.9.3 /app/elasticsearch
#修改配置文件
[root@localhost elasticsearch]# vi /app/elasticsearch/config/elasticsearch.yml
cluster.name: es-cluster #集群名称
node.name: node1
network.host: 10.4.7.11
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["10.4.7.11", "10.4.7.12", "10.4.7.21"]
cluster.initial_master_nodes: ["10.4.7.11", "10.4.7.12", "10.4.7.21"]
discovery.zen.minimum_master_nodes: 1
indices.query.bool.max_clause_count: 10240
cluster.max_shards_per_node: 10000
gateway.recover_after_nodes: 1
bootstrap.memory_lock: true
bootstrap.system_call_filter : false
#开启x-pack安全验证
xpack.security.enabled: true
xpack.security.audit.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
path.data: /data/elasticsearch
path.logs: /app/elasticsearch/logs
#调整内存大小
[root@localhost elasticsearch]# egrep -v "#|^$" /app/elasticsearch/config/jvm.options|head -2
-Xms2g
-Xmx2g
#修改elasticsearch自带jdk的路径变量,在文件最前面
[root@localhost app]# vi /app/elasticsearch/bin/elasticsearch
export JAVA_HOME=/app/elasticsearch/jdk
export PATH=$JAVA_HOME/bin:$PATH
#修改配置文件中node.name并且授权es用户目录权限
[root@localhost app]# useradd es && chown -R es.es /app/elasticsearch* && chown -R es.es /data/elasticsearch
# 开启x-pack安全验证
#生成证书
[root@localhost ~]# /app/elasticsearch/bin/elasticsearch-certutil cert -out /app/elasticsearch/config/elastic-certificates.p12 -pass ""
#以es用户启动服务
su -c '/app/elasticsearch/bin/elasticsearch -d' es
3.node2,node3安装
[root@localhost ~]# mkdir -p /app /data/elasticsearch
[root@localhost ~]# scp -rp 10.4.7.11:/app/elasticsearch-7.9.3/ /app/
[root@localhost ~]# ln -sv /app/elasticsearch-7.9.3 /app/elasticsearch
[root@localhost ~]# useradd es && chown -R es.es /app/elasticsearch* && chown -R es.es /data/elasticsearch
#node2,node3启动es
[root@localhost ~]# su -c '/app/elasticsearch-7.9.3/bin/elasticsearch -d' es
4.三台都起来了,设置密码,这里是手动设置指定的密码,如果三台都没起来,设置密码会报错,
下面都是一些用户设置密码,统一设置密码为:Elastic123
[root@k8s-node1 config]# /app/elasticsearch/bin/elasticsearch-setup-passwords interactive
设置集群分片最大数
[root@localhost ~]curl -XPUT -u elastic:Elastic123 -H "Content-Type: application/json" http://10.4.7.11:9200/_cluster/settings -d '{"transient": {"cluster": {"max_shards_per_node":10000}}}'
5.安装head插件,只需要再集群之间任何一台安装就可以了
[root@localhost ~]# curl -sL -o /etc/yum.repos.d/khara-nodejs.repo https://copr.fedoraproject.org/coprs/khara/nodejs/repo/epel-7/khara-nodejs-epel-7.repo
[root@localhost ~]# yum install -y nodejs nodejs-npm
[root@localhost ~]# npm --version
[root@localhost ~]# node --version
6. 安装grunt ,grunt是一个很方便的构建工具,可以进行打包压缩,测试,执行,5.0里的
head插件就是通过grunt启动的,
下载地址:https://github.com/mobz/elasticsearch-head
下载zip包
# 进入 elasticsearch-head 文件夹,执行命令
[root@localhost ~]# unzip elasticsearch-head-master.zip
[root@localhost ~]# cd elasticsearch-head-master/
#设置淘宝源,安装会快点
[root@localhost elasticsearch-head-master]# npm config set registry http://registry.npm.taobao.org
[root@localhost elasticsearch-head-master]# npm install -g grunt-cli
[root@localhost elasticsearch-head-master]# npm install
7. Gruntfile.js 文件
修改 elasticsearch-head 目录下的 Gruntfile.js 文件,在 options 属性内增加 hostname,设置为 0.0.0.0
connect: {
server: {
options: {
hostname: '0.0.0.0', #添加这行,注意逗号。
port: 9100,
base: '.',
keepalive: true
}
}
}
#修改head服务器连接es地址,添加es集群的任何节点。
[root@localhost elasticsearch-head-master]# vi _site/app.js
原来
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
修改localhost为:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://10.4.7.21:9200";
8.运行head启动node.js,必须在elasticsearch-head-master目录下
[root@localhost elasticsearch-head-master]# nohup grunt server > grunt.log 2>&1 &
9.es各个节点在elasticsearch.yml添加下面内容。
[root@localhost app]# vi /app/elasticsearch/config/elasticsearch.yml
#添加es-head可以跨域连接
http.cors.enabled: true
http.cors.allow-origin: "*"
#配置es-head验证Authorization
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: Authorization,X-Requested-With,X-Auth-Token,Content-Type, Content-Length
10,重启es集群各个节点
#配置es启动服务脚本
[root@localhost ~]# ps aux|grep elasticsearch | grep -v grep |awk '{print $2}'|xargs kill -9 2>/dev/null
[root@localhost ~]# su -c '/app/elasticsearch-7.9.3/bin/elasticsearch -d' es
11.浏览器登录es-head,配置es什么节点就访问什么ip,使用用户密码验证
http://10.4.7.21:9100/?auth_user=elastic&auth_password=Elastic123
12.如果记不得之前的账号,可以创建新的账号,具有超级用户内置角色。
#创建用户
[root@localhost opt]# /app/elasticsearch/bin/elasticsearch-users useradd admin -p Admin123 -r superuser
[root@localhost opt]# /app/elasticsearch/bin/elasticsearch-users list
admin : superuser
[root@localhost opt]# curl -u admin:Admin123 http://10.4.7.11:9200/_nodes/process?pretty
#基本操作
查看集群状态
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_cat/health?v
#查看节点状态
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_cat/nodes?v
#查看索引状态
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_cat/indices?v
#查看es集群
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_cluster/health?pretty
{
"cluster_name" : "es-cluster",
"status" : "green", #状态为green
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 4, #4个主分片
"active_shards" : 10, #10个分片
"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
}
#查看分片
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_cat/shards
13.备份security索引,担心删除,无法登录使用。
#查看以前的security的索引
[root@localhost nodes]# curl -u admin:Admin123 -XGET http://10.4.7.11:9200/.security-*
#删除security索引
[root@localhost nodes]# curl -u admin:Admin123 -X DELETE http://10.4.7.11:9200/.security-*
#创建备份目录,每个es集群节点都要添加。
[root@localhost ~]# mkdir -p /data/es-backups/
[root@localhost ~]# chown -R es.es /data/es-backups/
#添加内容到配置文件里
[root@localhost ~]# vi /app/elasticsearch/config/elasticsearch.yml
#备份配置目录
path.repo: ["/data/es-backups/"]
#重启es各个节点
[root@localhost ~]# ps aux|grep elasticsearch | grep -v grep |awk '{print $2}'|xargs kill -9 2>/dev/null
[root@localhost ~]# su -c '/app/elasticsearch-7.9.3/bin/elasticsearch -d' es
14.安装es的分词器
安装包地址:https://github.com/medcl/elasticsearch-analysis-ik/releases,
需要注意
要下载与自己版本一致的,版本不一致的可能会有问题。
在es的安装地址下,plugins文件夹中创建目录ik
解压安装包到ik文件夹中
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.3/elasticsearch-analysis-ik-7.9.3.zip
#安装ik分词器插件,先在10.4.7.11上操作,然后拷贝es集群其他节点。
[root@localhost ~]# wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.3/elasticsearch-analysis-ik-7.9.3.zip
[root@localhost ~]# mkdir /app/elasticsearch/plugins/analysis-ik/
[root@localhost ~]# cp /root/elasticsearch-analysis-ik-7.9.3.zip /app/elasticsearch/plugins/analysis-ik
[root@localhost ~]# cd /app/elasticsearch/plugins/analysis-ik
[root@localhost analysis-ik]# unzip elasticsearch-analysis-ik-7.9.3.zip
[root@localhost analysis-ik]# chown -R es.es /app/elasticsearch*
#拷贝
[root@localhost plugins]# scp -rp /app/elasticsearch/plugins/analysis-ik 10.4.7.12:/app/elasticsearch/plugins/
[root@localhost plugins]# scp -rp /app/elasticsearch/plugins/analysis-ik 10.4.7.21:/app/elasticsearch/plugins/
#重启es各个节点
[root@localhost ~]# chown -R es.es /app/elasticsearch*
[root@localhost ~]# ps aux|grep elasticsearch | grep -v grep |awk '{print $2}'|xargs kill -9 2>/dev/null
[root@localhost ~]# su -c '/app/elasticsearch/bin/elasticsearch -d' es
#查看日志,会有下面信息输出
[root@localhost ~]# tailf -200 /app/elasticsearch/logs/es-cluster.log
[2022-01-21T03:59:00,462][INFO ][o.e.p.PluginsService ] [node1] loaded module [x-pack-security]
[2022-01-21T03:59:00,462][INFO ][o.e.p.PluginsService ] [node1] loaded module [x-pack-sql]
[2022-01-21T03:59:00,464][INFO ][o.e.p.PluginsService ] [node1] loaded module [x-pack-stack]
[2022-01-21T03:59:00,464][INFO ][o.e.p.PluginsService ] [node1] loaded module [x-pack-voting-only-node]
[2022-01-21T03:59:00,466][INFO ][o.e.p.PluginsService ] [node1] loaded module [x-pack-watcher]
[2022-01-21T03:59:00,467][INFO ][o.e.p.PluginsService ] [node1] loaded plugin [analysis-ik] #这里插件
ik分词器提供3种分词模式
1: default : 把需要分词的文本一个词一个词来拆分。
2:ik_smart :将需要分词的文本做最大颗粒的拆分。
3:ik_max_word:将需要分词的文本最小颗粒的拆分,尽量分更多的词。
#测试分词器的,
[root@localhost opt]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XGET http://10.4.7.12:9200/_analyze?pretty -d '
{
"text": "美国留给?", # 对这个text类型文本进行分词
"analyzer": "ik_max_word" #指定ik分词模式
}'
{
"tokens" : [
{
"token" : "美国",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "留给",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
}
]
}
#基于fs来备份还原
1.注册共享仓库,10.4.7.22作为fs备份存储
#安装nfs服务,下一篇文件会更新,



