Elasticsearch7.3学习笔记4-分布式集群环境搭建

测试环境介绍
我们搭建一个一主两从的集群环境,由于是测试虚拟机,我这边只在一台服务器上来演示主从环境。

操作系统 服务器ip 端口号 是否主节点
centos7 192.168.43.96 9200
centos7 192.168.43.96 9400
centos7 192.168.43.96 9500

一、主节点搭建
环境搭建详见https://blog.51cto.com/2262805/2441988文章,
elasticsearch.yml配置文件说明:
配置说明:

        cluster.name    集群名称,相同名称为一个集群
        node.name   节点名称,集群模式下每个节点名称唯一
        node.master     当前节点是否可以被选举为master节点,是:true、否:false
        node.data   当前节点是否用于存储数据,是:true、否:false
        path.data   索引数据存放的位置
        path.logs   日志文件存放的位置
        bootstrap.memory_lock   需求锁住物理内存,是:true、否:false
        bootstrap.system_call_filter    SecComp检测,是:true、否:false
        network.host    监听地址,用于访问该es
        network.publish_host    可设置成内网ip,用于集群内各机器间通信
        http.port   es对外提供的http端口,默认 9200
        discovery.seed_hosts    es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点
        cluster.initial_master_nodes    es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
        http.cors.enabled   是否支持跨域,是:true,在使用head插件时需要此配置
        http.cors.allow-origin  "*" 表示支持所有域名

我们要只需要在之前的基础上,打开配置文件elasticsearch.yml,添加如下三个配置:
        cluster.name: my-es   #集群名称
        node.name: node-master #主节点名称
        node.master: true #当前节点是否可以被选举为master节点,是:true、否:false
      discovery.seed_hosts: ["192.168.43.96", "192.168.43.96", "192.168.43.96"]#写入候选主节点的设备地址,在开启服务后可以被选为主节点
      cluster.initial_master_nodes: ["node-1"] #初始化一个新的集群时需要此配置来选举master

        修改完配置文件之后,只需要重新服务即可。

二、从节点配置
1.从节点1配置
我们再/mnt目录,解压之前下载的elasticsearch-7.3.0-linux-x86_64.tar.gz,并命名为elasticsearch1,并授权
chown -R testesuser:testes /mnt/elasticsearch1
进入/mnt/elasticsearch1目录config文件夹,修改elasticsearch.yml配置文件并保存。

     network.host: 192.168.43.96
                    http.port: 9400
                    discovery.seed_hosts: ["192.168.43.96", "192.168.43.96", "192.168.43.96"]
                    cluster.initial_master_nodes: ["node-1"]
                    http.cors.enabled: true
                    http.cors.allow-origin: "*"
                    cluster.name: my-es
                    node.name: node-slave1

                cd bin/
./elasticsearch #启动从环境1  一定要用testesuser用户来执行
          2.从节点二配置
          在/mnt目录下,继续解压之前的包并命名为为elasticsearch2,并授权
                `chown -R testesuser:testes /mnt/elasticsearch2`
                    进入/mnt/elasticsearch2目录config文件夹,修改elasticsearch.yml配置文件并保存。
                ```
    和上面的配置基本一致,只是   http.port改为9500,    node.name改为node-slave2
                            cd bin/
        ./elasticsearch #启动从环境2  一定要用testesuser用户来执行

三、Elasticsearch head验证主从环境
            启动完成后,我们用Elasticsearch head查看,主从环境配置正常。
![](https://s1.51cto.com/images/blog/201910/14/7e18de1fa5e928c72a894f6a7c6141db.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
            从图中可以看出五星代表是主节点,圆代表是从节点,

 四、异常处理
    因为我们配置了主从环境,单独启动一个主,不启动从,一直会出现此错误
        master not discovered or elected yet, an election requires at least 2 nodes with ids from [X2P0yBfUSHSyGgjWWvBYeg, ZFIHple7RSijNOrRYIlPbQ, Xoew_otiTimsat1dgsYTDQ], have discovered,
        只是因为我们只启动了主,从没有启动,我们只需要到从的启动路径启动从即可。

猜你喜欢

转载自blog.51cto.com/2262805/2442387