shell 脚本实现 elasticsearch 配置文件的集中管理

elasticsearch 节点较多,手动一个一个更改配置文件费时费力易出错,查询 ES 官网,官网推荐了三款工具:Puppet、Chef、Ansible,链接如下:
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/install-elasticsearch.html#config-mgmt-tools
看了一下,发现这些运维工具比较复杂,目前我们的需求还不需要这么复杂的工具,然后想着自己写一个 shell 脚本实现一下 ES 配置文件的集中管理。本方案主要有两个文件,一个 handle.sh 是需要运行的脚本,一个 elasticsearch_all.yml 是集中管理的各个节点的配置信息。


handle.sh

#!/bin/bash
ipSegment="192.168.100."
ipAddrArr=("11" "12" "13")
for i in ${ipAddrArr[@]}
    do
        grep "^<$i> " elasticsearch_all.yml | sed -r "s/^<$i> //" > elasticsearch.yml
        scp elasticsearch.yml myid@$ipSegment$i:~/elasticsearch-x.x.x/config/
        rm elasticsearch.yml
    done

脚本循环三次,在 elasticsearch_all.yml 文件中分别找到以 <11>、<12>、<13> 开头的三个段落,每次循环中使用 sed 去掉 <11> 等标识,并将找到的一个段落写入本地的 elasticsearch.yml 文件,然后通过 scp 将本地的 elasticsearch.yml 文件 copy 到目标节点的 ES 配置文件夹中,然后 rm 本地的 elasticsearch.yml 文件。


elasticsearch_all.yml

<11> node.name: node_1
<11> node.master: true
<11> node.data: true
<11> network.bind_host: 192.168.100.11
<11> network.publish_host: 192.168.100.11
<11> network.host: 0.0.0.0
<11> http.max_content_length: 1024mb
<11> discovery.zen.ping.unicast.hosts: ["192.168.100.11","192.168.100.12","192.168.100.13"]
<11> http.cors.enabled: true
<11> http.cors.allow-origin: "*"
<11> discovery.zen.ping_timeout: 10s
<11> discovery.zen.fd.ping_timeout: 10000s
<11> discovery.zen.fd.ping_retries: 10

<12> node.name: node_2
<12> node.master: true
<12> node.data: true
<12> network.bind_host: 192.168.100.12
<12> network.publish_host: 192.168.100.12
<12> network.host: 0.0.0.0
<12> http.max_content_length: 1024mb
<12> discovery.zen.ping.unicast.hosts: ["192.168.100.11","192.168.100.12","192.168.100.13"]
<12> http.cors.enabled: true
<12> http.cors.allow-origin: "*"
<12> discovery.zen.ping_timeout: 10s
<12> discovery.zen.fd.ping_timeout: 10000s
<12> discovery.zen.fd.ping_retries: 10

<13> node.name: node_3
<13> node.master: true
<13> node.data: true
<13> network.bind_host: 192.168.100.13
<13> network.publish_host: 192.168.100.13
<13> network.host: 0.0.0.0
<13> http.max_content_length: 1024mb
<13> discovery.zen.ping.unicast.hosts: ["192.168.100.11","192.168.100.12","192.168.100.13"]
<13> http.cors.enabled: true
<13> http.cors.allow-origin: "*"
<13> discovery.zen.ping_timeout: 10s
<13> discovery.zen.fd.ping_timeout: 10000s
<13> discovery.zen.fd.ping_retries: 10

elasticsearch_all.yml 文件中各个节点有一些属性取值是相同的,可以统一成一个 <all> 的段落,handle.sh 文件也做相应的修改即可,改动后的两个文件如下:


handle.sh

#!/bin/bash
ipSegment="192.168.100."
ipAddrArr=("11" "12" "13")
for i in ${ipAddrArr[@]}
    do
        grep "^<$i> \|^<all> " elasticsearch_all.yml | sed -r "s/^<$i> //" > elasticsearch.yml
        scp elasticsearch.yml myid@$ipSegment$i:~/elasticsearch-x.x.x/config/
        rm elasticsearch.yml
    done

elasticsearch_all.yml

<11> node.name: node_1
<11> node.master: true
<11> node.data: true
<11> network.bind_host: 192.168.100.11
<11> network.publish_host: 192.168.100.11

<12> node.name: node_2
<12> node.master: true
<12> node.data: true
<12> network.bind_host: 192.168.100.12
<12> network.publish_host: 192.168.100.12

<13> node.name: node_3
<13> node.master: true
<13> node.data: true
<13> network.bind_host: 192.168.100.13
<13> network.publish_host: 192.168.100.13

<all> network.host: 0.0.0.0
<all> http.max_content_length: 1024mb
<all> discovery.zen.ping.unicast.hosts: ["192.168.100.11","192.168.100.12","192.168.100.13"]
<all> http.cors.enabled: true
<all> http.cors.allow-origin: "*"
<all> discovery.zen.ping_timeout: 10s
<all> discovery.zen.fd.ping_timeout: 10000s
<all> discovery.zen.fd.ping_retries: 10
相关推荐
<p> <strong><span style="font-size:20px;color:#FF0000;">本课程主要针对计算机相关专业正在做毕设学生与需要项目实战练习Java学习者</span></strong> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">1. 包含:<span style="color:#FFFF00;background-color:#FF0000;">项目源码、</span><span style="color:#FFFF00;background-color:#FF0000;">项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">2. 手把手带你从零开始部署运行本套系统</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">3. 该项目附带源码资料可作为毕设使用</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">4. 提供技术答疑和远程协助指导</span></strong></span><strong><span style="font-size:18px;"></span></strong> </p> <p> <br /> </p> <p> <span style="font-size:18px;"><strong>项目运行截图:</strong></span> </p> <p> <strong><span style="font-size:18px;">1)系统登陆界面</span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015433522.png" alt="" /><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">2)学生模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">3)教师模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">4)系统管理员</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">更多Java毕设项目请关注我毕设系列课程 <a href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页