ElasticSearch搜索引擎-6_学习笔记(2021.6.14)
ElasticSearch集群搭建
前言:
一个 Elasticsearch 集群有一个唯一的名字标识,这个名字默认就 是”elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集群的名字,来加入 这个集群。
集群中包含很多服务器,一个节点就是其中的一个服务器。作为集群的一部分,它存储 数据,参与集群的索引和搜索功能。 一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色 的名字,这个名字会在启动的时候赋予节点。这个名字对于管理工作来说挺重要的,因为在 这个管理过程中,你会去确定网络中的哪些服务器对应于 Elasticsearch 集群中的哪些节点。 一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点 都会被安排加入到一个叫做“elasticsearch”的集群中,这意味着,如果你在你的网络中启动了 若干个节点,并假定它们能够相互发现彼此,它们将会自动地形成并加入到一个叫做 “elasticsearch”的集群中。
在一个集群里,只要你想,可以拥有任意多个节点。而且,如果当前你的网络中没有运 行任何 Elasticsearch 节点,这时启动一个节点,会默认创建并加入一个叫做“elasticsearch”的 集群。
1.0 Docker
下的ES集群
1.1 创建第一个ES节点
1.1.1 使用目录挂载的方式启动ES
# 先创建一个容器
docker run -id --name=MyElasticSearch01 -p 9200:9200 -p 9300:9300 9337ed510a0c
# 然后copy容器中的需要目录挂载的文件出去
docker cp 067d5060cf6a:/usr/share/elasticsearch/config /usr/local/zhihao_ElasticSearch_01
docker cp 067d5060cf6a:/usr/share/elasticsearch/config /usr/local/zhihao_ElasticSearch_01
docker cp 067d5060cf6a:/usr/share/elasticsearch/data /usr/local/zhihao_ElasticSearch_01
建议使用
mkdir
的方式创建挂载目录, 例如:mkdir /usr/local/zhihao_ElasticSearch_01
然后在里面创建对应3个名称文件夹
1.1.2 然后复制多2份挂载出来的配置文件
记得给文件夹递归所有权限, 然后记得删除
/elasticsearch/data
里面文件夹的全部内容
cp -r /usr/local/zhihao_ElasticSearch_01 /usr/local/zhihao_ElasticSearch_02
cp -r /usr/local/zhihao_ElasticSearch_01 /usr/local/zhihao_ElasticSearch_03
1.1.3 然后修改所有的配置文件
/usr/local/zhihao_ElasticSearch_01/config/elasticsearch.yml
#集群名称
cluster.name: docker-cluster-es
#节点名称,每个节点的名称不能重复
node.name: ES-01
# 设置可以访问的ip,默认为0.0.0.0,这里全部设置通过
network.bind_host: 0.0.0.0
# 设置其它结点和该结点交互的ip地址 (这里为外网IP)
network.publish_host: 119.29.xxx.xxx
#是不是有资格主节点与数据存储
node.master: true
node.data: true
#设置对外服务的http端口,默认为9200
http.port: 9200
#设置节点之间交互的tcp端口,默认是9300
transport.tcp.port: 9300
# head 插件需要这打开这两个配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.max_content_length: 200mb
#es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
cluster.initial_master_nodes: ["ES-01","ES-02","ES-03"]
#es7.x 之后新增的配置,节点发现
discovery.seed_hosts: ["119.29.xxx.xxx:9301","119.29.xxx.xxx:9302"]
gateway.recover_after_nodes: 3
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
#集群内同时启动的数据任务个数,默认是 2 个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
#添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
cluster.routing.allocation.node_concurrent_recoveries: 16
#初始化数据恢复时,并发恢复线程的个数,默认 4 个
cluster.routing.allocation.node_initial_primaries_recoveries: 16
/usr/local/zhihao_ElasticSearch_02/config/elasticsearch.yml
#集群名称
cluster.name: docker-cluster-es
#节点名称,每个节点的名称不能重复
node.name: ES-02
# 设置可以访问的ip,默认为0.0.0.0,这里全部设置通过
network.bind_host: 0.0.0.0
# 设置其它结点和该结点交互的ip地址 (这里为外网IP)
network.publish_host: 119.29.xxx.xxx
#是不是有资格主节点与数据存储
node.master: true
node.data: true
#设置对外服务的http端口,默认为9200
http.port: 9201
#设置节点之间交互的tcp端口,默认是9300
transport.tcp.port: 9301
# head 插件需要这打开这两个配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.max_content_length: 200mb
#es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
cluster.initial_master_nodes: ["ES-01","ES-02","ES-03"]
#es7.x 之后新增的配置,节点发现
discovery.seed_hosts: ["119.29.xxx.xxx:9300","119.29.xxx.xxx:9302"]
gateway.recover_after_nodes: 3
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
#集群内同时启动的数据任务个数,默认是 2 个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
#添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
cluster.routing.allocation.node_concurrent_recoveries: 16
#初始化数据恢复时,并发恢复线程的个数,默认 4 个
cluster.routing.allocation.node_initial_primaries_recoveries: 16
/usr/local/zhihao_ElasticSearch_03/config/elasticsearch.yml
#集群名称
cluster.name: docker-cluster-es
#节点名称,每个节点的名称不能重复
node.name: ES-03
# 设置可以访问的ip,默认为0.0.0.0,这里全部设置通过
network.bind_host: 0.0.0.0
# 设置其它结点和该结点交互的ip地址 (这里为外网IP)
network.publish_host: 119.29.xxx.xxx
#是不是有资格主节点与数据存储
node.master: true
node.data: true
#设置对外服务的http端口,默认为9200
http.port: 9202
#设置节点之间交互的tcp端口,默认是9300
transport.tcp.port: 9302
# head 插件需要这打开这两个配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.max_content_length: 200mb
#es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
cluster.initial_master_nodes: ["ES-01","ES-02","ES-03"]
#es7.x 之后新增的配置,节点发现
discovery.seed_hosts: ["119.29.xxx.xxx:9300","119.29.xxx.xxx:9301"]
gateway.recover_after_nodes: 3
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
#集群内同时启动的数据任务个数,默认是 2 个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
#添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
cluster.routing.allocation.node_concurrent_recoveries: 16
#初始化数据恢复时,并发恢复线程的个数,默认 4 个
cluster.routing.allocation.node_initial_primaries_recoveries: 16
1.1.4 修改宿主机相关配置文件
# 关闭防火墙 (或者开放相关ES端口)
systemctl status firewalld
systemctl stop firewalld.service
systemctl disable firewalld.service
修改limits.conf 分发文件
vim /etc/security/limits.conf
在最后添加: *表示所用用户
* hard nofile 65536
* soft nofile 65536
* soft nproc 65536
* hard nproc 65536
修改20-nproc.conf 分发文件
vim /etc/security/limits.d/20-nproc.conf
在最后添加: *表示所用用户
* hard nofile 65536
* soft nofile 65536
* soft nproc 65536
* hard nproc 65536
修改sysctl.conf
vim /etc/sysctl.conf
在最后添加
vm.max_map_count=655360
关闭selinux
vim /etc/sysconfig/selinux
将 SELINUX=enforcing 改为 SELINUX=disabled
最终重新加载
sysctl -p
1.1.5 然后删除之前没有目录挂载的容器, 并且以目录挂载的方式启动集群
# 删除
docker rm 067d5060cf6a
# 以目录挂载方式启动 ES-01
docker run -id --name=ES-01 -p 9200:9200 -p 9300:9300 -v /usr/local/zhihao_ElasticSearch_01/config:/usr/share/elasticsearch/config
-v /usr/local/zhihao_ElasticSearch_01/plugins:/usr/share/elasticsearch/plugins -v /usr/local/zhihao_ElasticSearch_01/data:/usr/share/elasticsearch/data -e ES_JAVA_OPTS="-Xms128m -Xmx128m" 9337ed510a0c
# 以目录挂载方式启动 ES-02
docker run -id --name=ES-02 -p 9201:9201 -p 9301:9301 -v /usr/local/zhihao_ElasticSearch_02/config:/usr/share/elasticsearch/config -v /usr/local/zhihao_ElasticSearch_02/plugins:/usr/share/elasticsearch/plugins -v /usr/local/zhihao_ElasticSearch_02/data:/usr/share/elasticsearch/data -e ES_JAVA_OPTS="-Xms128m -Xmx128m" 9337ed510a0c
# 以目录挂载方式启动 ES-03
docker run -id --name=ES-03 -p 9202:9202 -p 9302:9302 -v /usr/local/zhihao_ElasticSearch_03/config:/usr/share/elasticsearch/config -v /usr/local/zhihao_ElasticSearch_03/plugins:/usr/share/elasticsearch/plugins -v /usr/local/zhihao_ElasticSearch_03/data:/usr/share/elasticsearch/data -e ES_JAVA_OPTS="-Xms128m -Xmx128m" 9337ed510a0c
注意哈, 节点的名称、端口、挂载文件夹路径各不相同
-e ES_JAVA_OPTS="-Xms128m -Xmx128m"
因为云服务器内存过小, 所以配置了内存参数, 这个参数越大ES越快, 默认不配有自动默认值
1.1.6 进行测试
浏览器输入:
http://119.29.xxx.xxx:9200/_cluster/health
http://119.29.xxx.xxx:9200/_cat/nodes
遇到的坑:
1.0
启动第二个节点的时候, 查看日志一直报通信异常
RemoteTransportException
最终发送是因为, 拷贝配置文件时候, 必须清空
data
文件夹下面内容, 让新挂载的容器自动生成对应数据
1