ES安装脚本

#!/bin/bash

cluster_name=$1
ip_list=$2
node_name=`hostname`

if [ -z $cluster_name ];then
    echo '缺少"集群名称“参数'
    exit 1
fi

if [ -z "$ip_list" ];then
    echo '缺少IP列表参数,输入集群机器IP列表,格式:"10.8.6.20 10.8.6.21 10.8.6.22"'
    exit 1
fi
es_pid=`ps aux|grep elastic|grep -v "grep" |grep "java"|awk '{print $2}'`
if [ ! -z "$es_pid" ];then
   kill -9 $es_pid
fi
rm -rf elasticsearch-6.5.0.tar.gz  
rm -rf /usr/local/elasticsearch-6.5.0/
rm -rf elasticsearch_exporter-1.1.0rc1.linux-amd64
rm -rf elasticsearch_exporter-1.1.0rc1.linux-amd64.tar.gz
rm -rf /data/elasticsearch
rm -rf /data/es_backup

#1.系统环境准备
yum -y install expect wget
wget http://172.17.0.30/monitor_elk/es/jdk-8u172-linux-x64.rpm
rpm -ivh jdk-8u172-linux-x64.rpm
rm -f jdk-8u172-linux-x64.rpm
sed -i '/^*/d' /etc/security/limits.conf  
echo "* - nofile 165535
* - nproc 165535
* soft nofile 165535
* hard nofile 165535
* soft nproc 165535
* hard nproc 165535
* soft memlock unlimited
* hard memlock unlimited" >> /etc/security/limits.conf
sed -i '/max_map_count/d' /etc/sysctl.conf
echo "vm.max_map_count=655360" >> /etc/sysctl.conf
sysctl -p

#2.下载ES安装包,安装部署
wget http://172.17.0.30/monitor_elk/es/elasticsearch-6.5.0.tar.gz
tar xf elasticsearch-6.5.0.tar.gz -C /usr/local/
rm -f elasticsearch-6.5.0.tar.gz
cd /usr/local/elasticsearch-6.2.4/bin/
node_num=0
for ip in $ip_list
do
  node_num=$(($node_num+1))
  echo "$ip:9300" >> /usr/local/elasticsearch-6.5.0/config/discovery-file/unicast_hosts.txt 
done
minimum_master_nodes=$(($node_num/2 +1))
max_jvm=`free -m |grep Mem |awk '{print $2 /2000}'|awk -F. '{if(substr($2,1,1)>=5)$1+=1;print $1}'`
if [ $max_jvm -gt 32 ]; then 
  max_jvm=31
fi 
sed -i "/ms16g/s/16/${max_jvm}/g" /usr/local/elasticsearch-6.5.0/config/jvm.options
sed -i "/mx16g/s/16/${max_jvm}/g" /usr/local/elasticsearch-6.5.0/config/jvm.options
sed -i "/cluster.name/s/cluster_name/${cluster_name}/g" /usr/local/elasticsearch-6.5.0/config/elasticsearch.yml
sed -i "/node.name/s/node_name/${node_name}/g" /usr/local/elasticsearch-6.5.0/config/elasticsearch.yml
sed -i "/recover_after_nodes/s/2/${minimum_master_nodes}/g" /usr/local/elasticsearch-6.5.0/config/elasticsearch.yml
sed -i "/minimum_master_nodes/s/2/${minimum_master_nodes}/g" /usr/local/elasticsearch-6.5.0/config/elasticsearch.yml

#3.启动ES服务
mkdir -p /data/elasticsearch
mkdir -p /data/es_backup
chown -R devuser:devuser /usr/local/elasticsearch-6.5.0
chown -R devuser:devuser /data/elasticsearch/
chown -R devuser:devuser /data/es_backup
su - devuser -c "/usr/local/elasticsearch-6.5.0/bin/elasticsearch -d"
sleep 30

#4.在Master节点导入证书和初始化ES内置用户密码
local_host=`ip a |grep eth0|grep inet |awk '{print $2}'| awk -F"/" '{print $1}'`
i=0
while [ $i -le 10 ] 
do
    cluster_status=`curl -s -uelastic:elastic "http://$ip:9200/_cluster/health" |grep '"status":"green"' |wc -l`
    if [ $cluster_status -eq 1 ]; then
        master_ip=`curl -s -uelastic:elastic "http://$ip:9200/_cat/nodes" |grep '*' |awk '{print $1}'`
        if [ $local_host == $master_ip ]; then
            wget http://172.17.0.30/monitor_elk/es/license.json
            curl -XPUT -uelastic:elastic  "http://$master_ip:9200/_xpack/license?acknowledge=true" -H "Content-Type: application/json" -d "@license.json"
            echo 
            rm -f license.json
            expect <<EOF  
                set timeout 60
                spawn  /usr/local/elasticsearch-6.5.0/bin/elasticsearch-setup-passwords interactive
                expect { 
                    "y/N" { send "y\n"; exp_continue} 
                    "elastic]:" { send "gaojihealth\n"; exp_continue } 
                    "elastic]:" { send "gaojihealth\n"; exp_continue } 
                    "apm_system]:" { send "apm_system\n"; exp_continue } 
                    "apm_system]:" { send "apm_system\n"; exp_continue } 
                    "kibana]:" { send "kibana\n"; exp_continue } 
                    "kibana]:" { send "kibana\n"; exp_continue } 
                    "logstash_system]:" { send "logstash_system\n"; exp_continue } 
                    "logstash_system]:" { send "logstash_system\n"; exp_continue } 
                    "beats_system]:" { send "beats_system\n"; exp_continue } 
                    "beats_system]:" { send "beats_system\n"; exp_continue } 
                    "remote_monitoring_user]:" { send "remote_monitoring_user\n"; exp_continue } 
                    "remote_monitoring_user]:" { send "remote_monitoring_user\n"} expect eof 
               }
EOF
        fi
        break
    fi
    let i++
    sleep 5
done

#5.安装ES的export
wget http://172.17.0.30/monitor_elk/es/elasticsearch_exporter-1.1.0rc1.linux-amd64.tar.gz
tar xf elasticsearch_exporter-1.1.0rc1.linux-amd64.tar.gz 
cd elasticsearch_exporter-1.1.0rc1.linux-amd64
nohup ./elasticsearch_exporter --es.uri=http://elastic:gaojihealth@"${local_host}":9200 &

 

posted @ 2024-07-04 09:35  羊脂玉净瓶  阅读(10)  评论(0)    收藏  举报