es集群一键安装、启动、停止脚本

 es集群一键安装脚本

#!/bin/bash
#配置ES的安装目录 修改的地方1 脚本可以自己创建
currentTime=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "请输入es的安装目录,不存在脚本自动创建,最后一个/不要写 /bigdata/install"
read esinstallpath

#创建ES安装的目录
if [ ! -d $esinstallpath ]; then
   mkdir -p $esinstallpath
fi 
if [ ! -d $esinstallpath ]; then
  echo "创建目录$esinstallpath失败!请检查目录是否有权限"
  exit
fi

#解压tar包
currentdir=$(cd $(dirname $0); pwd)
ls | grep 'elasticsearch-.*[gz]$'
if [ $? -ne 0 ]; then
   #当前目录没有es的压缩包
   echo "在$currentdir下没有发现elasticsearch-*.tar.gz,请自行上传!"
   exit
else
   #解压
   tar -zxvf $currentdir/$(ls | grep 'elasticsearch-.*[gz]$') -C $esinstallpath
fi

esbanben=`ls $esinstallpath| grep 'elasticsearch-.*'`

#PATH设置
#末行插入
echo "">>~/.bash_profile
echo "#ES $currentTime">>~/.bash_profile
echo "export ES_HOME=$esinstallpath/$esbanben">>~/.bash_profile
echo 'export PATH=$PATH:$ES_HOME/bin'>>~/.bash_profile
source ~/.bash_profile

confpath=$esinstallpath/$esbanben/config

#修改配置文件
echo -e "请输入cluster.name的值:一个集群的名字保证一样 例如 gskjescluster"
read esname
echo "cluster.name: $esname" >>$confpath/elasticsearch.yml

echo -e "请输入node.name的值:唯一值 例如 1"
read nodename
echo "node.name: node-$nodename">>$confpath/elasticsearch.yml


echo -e "请输入path.data的值:例如 /es6.5.4/data/data/"
read pathdata
echo "path.data: $pathdata">>$confpath/elasticsearch.yml

echo -e "请输入path.logs的值:例如 /es6.5.4/data/logs/"
read pathlogs
echo "path.logs: $pathlogs">>$confpath/elasticsearch.yml

echo "bootstrap.memory_lock: false">>$confpath/elasticsearch.yml
echo "bootstrap.system_call_filter: false">>$confpath/elasticsearch.yml
echo "network.host: 0.0.0.0">>$confpath/elasticsearch.yml

echo -e "请输入httpport的值 默认值 9200"
read httpport
echo "http.port: $httpport">>$confpath/elasticsearch.yml

echo -e "请输入transporttcpport的值 默认值 9300"
read transporttcpport
echo "transport.tcp.port: $transporttcpport">>$confpath/elasticsearch.yml


echo -e '请输入es集群的所有节点,严格符合以下格式  "cdh01","cdh02","cdh03" '
read unicasthosts
echo "discovery.zen.ping.unicast.hosts: [$unicasthosts]">>$confpath/elasticsearch.yml

array=(${unicasthosts//,/ })
len=${#array[@]}
masternodes=`expr $len / 2 + 1`
#判断集群节点个数>2,才加
if [[ $len > 2 ]]; then
  echo "discovery.zen.minimum_master_nodes: $masternodes">>$confpath/elasticsearch.yml
fi


echo -e "是否修改jvm.options 请输入y/n"
read jvmisflag
if [[ $jvmisflag == "y" ]]; then
 echo -e "请输入jvm大小,注意g是小写 例如 16g"
 read jvmsize
 sed -i "s/^-Xms1g/-Xms$jvmsize/" $confpath/jvm.options
 sed -i "s/^-Xmx1g/-Xmx$jvmsize/" $confpath/jvm.options
	
fi

#修改并分发安装文件
echo -e "是否远程复制 请输入y/n"
read flag
if [[ $flag == "y" ]]; then

espath=$esinstallpath/$esbanben
espathtemp=$esinstallpath/$esbanben-temp
cp -r $espath $espathtemp

echo "以下输入的节点必须做免密登录"
echo -e '请输入除当前之外的节点(当前节点cdh01),严格符合以下格式IP:esID,空格隔开, cdh02:2 cdh03:3 cdh04:4 cdh05:5'
read allnodes
user=`whoami`
array2=(${allnodes// / })
for allnode in ${array2[@]}
do
 array3=(${allnode//:/ })
 ip=${array3[0]}
 esid=${array3[1]}
 echo ======= $ip  =======
 
 #修改文件
 ssh $ip "rm -rf $espath"
 ssh $ip "mkdir -p $espath"
 
 bak_dir="node.name: node-$nodename"
 new_dir="node.name: node-$esid"
 sed -i "s!${bak_dir}!${new_dir}!g" $espathtemp/config/elasticsearch.yml

 scp -r $espathtemp/* ${user}@$ip:$espath/
 
 ssh $ip "echo ''>>~/.bash_profile"
 ssh $ip "echo '#ES $currentTime'>>~/.bash_profile"
 ssh $ip "echo 'export ES_HOME=$esinstallpath/$esbanben'>>~/.bash_profile"
 ssh $ip 'echo "export PATH=\$PATH:\$ES_HOME/bin">>~/.bash_profile'
 ssh $ip "source ~/.bash_profile"
 
 
 #再次修改回来 防止修改错误
 new_dir="node.name: node-$nodename"
 bak_dir="node.name: node-$esid"
 sed -i "s!${bak_dir}!${new_dir}!g" $espathtemp/config/elasticsearch.yml

 echo ======= $ip 远程复制完成  =======
done

#删除临时文件
rm -rf $espathtemp

for allnode in ${array3[@]}
do
 echo ======= 在 $allnode 手动执行 source ~/.bash_profile 在通过 elasticsearch 查看是否安装成功 =======
done

fi

 

es集群一键启动脚本

#!/bin/bash
#配置ES的安装目录 修改的地方1 脚本可以自己创建
esServers='cdh01 cdh02'
#启动所有的zk
for es in $esServers
do
    ssh -T $es <

 

es集群一键停止脚本

#!/bin/bash

esServers='cdh01 cdh02'
#启动所有的zk
for es in $esServers
do
    ssh -T $es <

 

 

你可能感兴趣的:(es集群一键安装、启动、停止脚本)