###elasticsearch集群部署####

elasticsearch集群 节点:主节点、数据节点、查询节点、摄取节点,其中主节点、数据节点最重要的(下面主节点,数据节点复用)

elasticsearch 包下载地址:https://www.elastic.co/downloads/past-releases/elasticsearch-7-14-0

前提:elasticsearch集群 只能用普通用户启动,root账号启动会失败

1、创建一个user_es普通用户,并修改密码

groupadd -g 10001 user_es
useradd -g 10001 -u 10001 -m user_es

echo user_es:qazwsxedc|chpasswd

2、为新用户授权目录

mv  elasticsearch-7.14.0 /home/user_es/elasticsearch

chown -R user_es.user_es  /home/user_es/elasticsearch

3、优化系统参数

ElasticSearch7支持open jdk11,也可以使用ElasticSearch7自带的jdk,如下配置是自带的jdk

openjdk11 安装:yum install java-11-openjdk-devel -y

查看jdk 是否安装成功:java --version

vim  /home/user_es/elasticsearch/bin/elasticsearch-env

添加如下配置:

if [ "$(uname -s)" = "Darwin" ]; then
# macOS has a different structure
  JAVA="$ES_HOME/jdk.app/Contents/Home/bin/java"
else
   JAVA="$ES_HOME/jdk/bin/java"
fi
JAVA_TYPE="bundled JDK"

#配置最大的文件数

vim /etc/security/limits.conf

# 在文件末尾中增加下面内容
user_es soft nofile 655360
user_es hard nofile 655360

# 配置最大进程数

vim /etc/security/limits.d/20-nproc.conf
# 在文件末尾中增加下面内容
user_es soft nofile 655360
user_es hard nofile 655360
* hard nproc 4096
# 注:* 带表Linux所有用户名称

#配置虚拟机最大内存
vim /etc/sysctl.conf
# 在文件中增加下面内容
vm.max_map_count=655360
#重新加载配置
sysctl -p

#关闭防火墙

systemctl stop firewalld.service

#创建数据和日志目录并授权

mkdir -p /opt/elasticsearch_data/data
mkdir -p /opt/elasticsearch_log/logs
chown -R user_es.user_es /opt/elasticsearch_data
chown -R user_es.user_es /opt/elasticsearch_log

4、优化elasticsearch 配置

#修改jvm 参数 默认是1g,es7版本以上配置4g,推荐将堆内存设置在总内存的50%以下

vim  /home/user_es/elasticsearch/config/jvm.options

-Xms4g
-Xmx4g

#修改GC

8-13:-XX:+UseConcMarkSweepGC 改为8-13:-XX:+UseG1GC

说明:G1GC是面向大内存的,适用于具有大量内存的应用程序,而CMS更适用于小型应用程序

#修改elasticsearch的配置文件

vim  /home/user_es/elasticsearch/config/elasticsearch.yml

#cluster
cluster.name: yunsale-aiot-cluster                    #集群名称3个必需相同
#Node
node.name: skywalking-elasticsearch-1           #节点名称
node.master: true                                             #当前是否选举为master节点 true:是、false:否
node.data: true                                                 #当前是否用于存储数据 true:是、false:否
#Paths
path.data: /opt/elasticsearch_data/data                    # 数据存储目录
path.logs: /opt/elasticsearch_log/logs                      # 日志存储目录
#Memory
bootstrap.memory_lock: false                        #需求锁住物理内存,是:true、否:false
#Network
network.host: 0.0.0.0                                     #绑定ip地址本机访问
#默认:9200-9300端口
http.port: 9200                                                # 主机访问的端口
#默认:9300-9400端口
transport.tcp.port: 9300                                 #集群内部通信端口
#Discovery
discovery.seed_hosts: ["192.168.6.8:9300", "192.168.6.9:9300","192.168.6.10:9300"]                            # 集群节点列表,此处已经提前做好host映射,可替换为真实ip
discovery.zen.minimum_master_nodes: 2                                                             #当在更新集群的状态时至少需要有两个主节点可用。通常情况下,这个值应该是集群中主节点数量的一半加一 ,防止发生脑裂
cluster.initial_master_nodes: ["192.168.6.8","192.168.6.9","192.168.6.10"]                                              #es7.x 之后新增的配置,初始主节点
http.cors.enabled: true                                                                                                                                         #是否支持跨域,是:true,在使用head插件时需要此配置
http.cors.allow-origin: "*"                                                                                                                                      # "*" 支持所有域名访问
#Various                                                                                                                                                               #配置账号密码Xpack认证

#配置Xpack认证                                                                                                     
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

#启动Elasticsearch及验证

su - user_es -c "/home/user_es/elasticsearch/bin/elasticsearch -d"                                                                  #后台启动

#单节点验证是否成功
浏览器访问curl 192.168.6.8:9200
#集群验证是否成功
# 节点的健康状态
curl 192.168.6.8:9200/_cat/health

#配置Xpack认证 步骤如下

1、创建配置认证存放目录,并授权user_es权限

mkdir -p /home/user_es/elasticsearch/config/certs/

chown -R user_es.user_es /home/user_es/elasticsearch/config/certs/

2、切换 user_es 用户生成加密自签证书默认下去,授权并传输到另外2个节点上面

elasticsearch/bin/elasticsearch-certutil ca -out /home/user_es/elasticsearch/config/certs/elastic-certificates.p12

chown -R user_es.user_es  /home/user_es/elasticsearch/config/certs/elastic-certificates.p12

scp -rp /home/user_es/elasticsearch/config/certs/ root@192.168.6.9:/home/user_es/elasticsearch/config/

scp -rp /home/user_es/elasticsearch/config/certs/ root@192.168.6.10:/home/user_es/elasticsearch/config/

3、启用Elasticsearch安全特性在elasticsearch.yml 上面添加如下配置上文已配置【红色部分】

#配置Xpack认证                                                                                                     
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

4、重启elasticsearch集群服务

5、配置内置用户密码

#切换普通用户

su - user_es

#配置elastic内置用户密码【如果是elasticsearch集群只需要修改其中一个节点即可,其他节点会同步过去】

elasticsearch/bin/elasticsearch-setup-passwords interactive

#创建user_es用户密码

curl -uelastic:密码 -XPUT "http://192.168.6.8:9200/_security/user/es_user" -H 'Content-Type: application/json' -d'
{
"password" : "JRNI46PYgE4A",
"roles" : ["superuser"]
}
'

#修改user_es密码

curl -H "Content-Type:application/json" -XPOST -u user_es 'http://192.168.6.8:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "JRNI46PYgE4a" }'

 ###es-header插件部署(用于连接ES的web UI)###

1、下载地址:https://pan.baidu.com/s/1fksEifwtAQ-L1DEd21KHZw   提取码:8888,先把刚刚下载的es-head.zip解压,然后把需要安装的第三方插件,后缀.crx 改成 .rar,然后解压,得到一个文件夹,进入页面后打开开发者模式,再加载已解压的扩展程序;选择刚才.rar解压好的文件夹,确定;确认新增扩展程序,点击添加,成功添加应用程序

2、ES 集群WEB管理工具(cerebro)

下载地址:https://github.com/lmenezes/cerebro/releases,下载安装包,直接执行脚本安装即可

### elasticsearch集群常用命令###

# 节点的健康状态
curl 192.168.6.8:9200/_cat/health
#集群的节点状态
curl 192.168.6.8:9200/_cat/nodes?v
#查看集群健康状态信息
curl 192.168.6.8:9200/_cluster/health?pretty
#查看所有索引
curl -XGET "192.168.6.8:9200/_cat/indices?v"
#创建一个customer的索引
curl -XPUT "192.168.6.8:9200/customer?pretty"
#删除索引
curl -XDELETE "192.168.6.8:9200/customer?pretty"

#查看分片信息

curl -XGET 'http://192.168.6.8:9200/_cat/shards?v'

#查看节点分配信息

curl -XGET ‘http://192.168.6.8:9200/_cat/allocation’

#设置分片数量

curl -XPUT -ues_user:密码 http://192.168.6.8:9200/customer/_settings -H 'Content-Type: application/json' -d'{"settings": {"number_of_shards": 2,"number_of_replicas": 2}}'  注意:现有索引不支持扩容主分片数,所以配置索引的时候要设置好主分片数,可以配置副本分片数

#设置副本分片数

curl -XPUT -ues_user:密码 http://10.x.x.6:9200/oom/_settings -H 'Content-Type: application/json' -d'{"index": {"number_of_replicas": 3}}'

#查看mapping

curl -XGET -ues_user:密码http://192.168.6.8:9200/customer/_mapping

#查看所有的索引数据

curl -XGET  -ues_user:密码 http://192.168.6.8:9200/_search?pretty  -H 'Content-Type: application/json' -d'{ "query":{"match_all":{}}}'

#查看myj索引的数据
curl -XGET -ues_user:密码 http://192.168.6.8:9200/myj/_search  -H 'Content-Type: application/json' -d'{ "query":{"match_all":{}}}'

#创建索引到添加文档数据三步骤

1、创建索引并配置分片数

curl -XPUT -ues_user:密码  http://192.168.6.8:9200/myj?pretty -H 'Content-Type: application/json' -d'{"settings": {"number_of_shards": 1,"number_of_replicas": 1}}'

2、为索引创建映射【类似表结构】

curl -XPUT -ues_user:密码 http://192.168.6.8:9200/myj/_mapping  -H 'Content-Type: application/json' -d'{ "properties": { "name": { "type": "text" }, "id": { "type": "keyword" }, "age": { "type": "integer" } }}'

3、向文档添加数据

curl -XPOST -ues_user:密码  http://192.168.6.8:9200/myj/_doc  -H 'Content-Type: application/json' -d'{ "name":"zbh", "id":"test", "age":100}' #加上文档ID可以使用PUT

4、 删除文档数据  【需要指定索引,文档类型[es7后文档类型概念已被弃用],文档ID,创建文档数据时,如果不指定文档id 会自动生成uuid 作为文档ID】

curl -XDELETE -ues_user:密码 http://192.168.6.8:9200/myj/_doc/1 

5、更新数据【PUT 覆盖修改;POST 配合update更新单个字段】

curl -XPUT -ues_user:密码 http://192.168.6.8:9200/myj/_doc/mHWIYowB3nK2JNUJa5nT -H 'Content-Type: application/json' -d'{ "name":"zbh", "id":"test", "age":101}'

curl -XPOST -ues_user:密码 http://192.168.6.8:9200/myj/_update/mHWIYowB3nK2JNUJa5nT -H 'Content-Type: application/json' -d'{ "doc":{ "age":102}}'

 #####elasticsearch数据迁移方式#########

COS快照:数据量大的场景(GB、TB、PB级别)对迁移速度要求较高的场景

logstash:

迁移全量或增量数据,且对实时性要求不高的场景需要对迁移的数据通过esquery进行
简单的过滤的场景需要对迁移的数据进行复杂的过滤或处理的场景版本跨度较大的数据迁
移场景,如5.x版本迁移到6.x版本或7.x版本

elasticsearch-dump:数据量较小的场景

ESM:数据量大的场景

####ES冷热分离配置#####

1、配置节点冷热标签

cat /home/user_es/elasticsearch/config/elasticsearch.yml
node.attr.box_type: cold
node.attr.box_type: hot

2、#查看分离情况

curl -XGET -u账户:密码 http://10.x.x.6:9200/_cat/nodeattrs?v
3、#配置冷热索引

curl -XPUT -u账户:密码 'http://10.x.x.6:9200/waimai/_settings/?pretty' -H 'content-Type:application/json' -d '{"index.routing.allocation.require.box_type": "hot" }'

4、#验证,主分片会分布到hot 节点

curl -XGET -u账户:密码'http://10.x.x.6:9200/_cat/shards/waimai?v&h=index,shard,prirep,node&s=node'

posted on 2023-05-08 17:43  MhaiM  阅读(170)  评论(0编辑  收藏  举报