1. 程式人生 > 實用技巧 >shell程式設計之自動化叢集搭建並啟動

shell程式設計之自動化叢集搭建並啟動

一、touch /opt/bigdata/elk/auto_elasticsearch.sh並寫入:

 1 #!/bin/bash
 2 
 3 # 1.確認安裝目錄
 4 
 5 confdir=$(cd `dirname $0` ;pwd) ## 安裝到elk目錄下,這種寫法可適用於任何路徑訪問!
 6 echo "將elasticsearch安裝到${confdir}目錄下嗎?yes/no"
 7 read answer                     ## 為了可以delete不變成^H,需要改xshell的鍵盤介面配置
 8 if [ $answer = "yes" ]          ## 使用 = 表示字串的相等
 
9 then 10 echo "已確認" 11 else 12 exit ## 退出該sh 13 fi 14 15 # 2.解壓到安裝目錄 16 # 2.1判斷是否存在.gz 17 gzpath="/opt/install/elk" 18 cd $gzpath 19 gzname=`ls |grep "elasticsearch-.*[gz]$"` ## 正則:.*單個字元匹配任意次,$:包含[gz]的結尾 20 if [ $? -ne 0 ] 21 then 22 echo "${gzpath}沒有elasticsearch的安裝包!
" 23 exit 24 else 25 echo "===正在安裝${gzname}到${confdir}路徑===" 26 tar -zxvf ${gzname} -C ${confdir} 27 fi 28 29 # 3.修改配置檔案 30 # 3.1改名 31 cd $confdir 32 OLDNAME=`ls |grep elasticsearch` 33 echo "OLDNAME:"$OLDNAME 34 NEWNAME=es622 35 mv $OLDNAME $NEWNAME 36 echo "NEWNAME:"$NEWNAME 37 38 # 3.2許可權 39
chown -R hewenyan:root $NEWNAME   ## 需要把es622許可權修改,才能啟動裡面的 40 41 # 3.3配置 42 cd $NEWNAME/config 43 FILE=elasticsearch.yml 44 45 HOST="network.host: "`hostname` 46 HTTPPORT="http.port: 9200" 47 HTTPCORS1='http.cors.enabled: true' 48 HTTPCORS2='http.cors.allow-origin: "*"' 49 50 echo "請輸入cluster.name:" 51 read CLUSTERNAME 52 CLUSTERNAME="cluster.name: "$CLUSTERNAME 53 echo "請輸入node.name:" 54 read NODENAME 55 NODENAME="node.name: "$NODENAME 56 echo "節點是否為master:true/false" 57 read IFMASTER 58 IFMASTER="node.master: "$IFMASTER 59 echo "請輸入建立的叢集,格式按照[\"A\",\"B\",\"C\"]": ## ' '不需要轉義," "需要! 60 read HOSTSSET 61 HOSTSSET="discovery.zen.ping.unicast.hosts: "$HOSTSSET 62 63 echo $CLUSTERNAME >> $FILE 64 echo $NODENAME >> $FILE 65 echo $IFMASTER >> $FILE 66 echo $HOST >> $FILE 67 echo $HOSTSSET >> $FILE 68 echo $HTTPPORT >> $FILE 69 echo $HTTPCORS1 >> $FILE ## echo "test" >> xxx 用echo追加 70 echo $HTTPCORS2 >> $FILE ## 這樣能一行一行的新增。一個echo新增不了多行 71 72 # 4.修改/etc/sysctl.conf 73 #echo "請輸入虛擬機器記憶體" 74 #read VM 75 echo "vm.max_map_count=655360" >> /etc/sysctl.conf 76 77 # 5.修改/etc/security/limits.conf 78 79 LIMITS=/etc/security/limits.conf 80 81 echo '* soft nofile 65536' >> $LIMITS 82 echo '* hard nofile 65536' >> $LIMITS 83 echo '* soft nproc 4096' >> $LIMITS 84 echo '* hard nproc 4096' >> $LIMITS 85 86 echo "====新增配置完畢=====" 87 88 # 6.讓配置生效 89 sysctl -p92 93 echo "===正在切換使用者hewenyan===" 94 cd /opt/shell 95 su hewenyan -c " sh /opt/shell/es.sh" ## 轉換使用者為hwy並-command執行命令,
                          ## 需要保證hwy使用者具有opt和子目錄的操作許可權

二、cd /opt/shell; touch es.sh esstatus.es 等等

 1 #!/bin/bash
 2 while [ "1"=="1" ]
 3 do
 4  echo "=====請輸入相應命令進行操作======"
 5  echo "-------1:檢視es執行狀態---------"
 6  echo "-------2:啟動es---------"
 7  echo "-------3:關閉es---------"
 8  echo "-------4:退出-----------"
 9  read -p "請輸入序號:" read_num
10  case $read_num in
11    "1")
12         echo -e "\033[34m -----1-----\033[0m"  
13         sh esstatus.sh
14 ;;
15    "2")
16         echo -e "\033[34m -----2-----\033[0m"
17         sh startupEs622.sh
18 ;;
19    "3")
20         echo -e "\033[34m -----3-----\033[0m"
21     sh esstop.sh
22 ;;
23    *)
24     echo -e "\033[34m -----4-----\033[0m"
25         break;
26 ;;
27 
28   esac
29 done
### esstatus
#!/bin/bash
ps=`jps|grep Elasticsearch`
arr=($ps)

#如果arr[0]為空
if [ -z "${arr[0]}"  ]
then 
 echo -e "\033[31m ELASTICSEARCH 未啟動----\033[0m"
else
 echo -e "\033[31m ELASTICSEARCH 程序為${arr[0]}----\033[0m"
fi
stop:
#!/bin/bash
ps=`jps|grep Elasticsearch`
arr=($ps)
if [ -z "${arr[0]}" ]
then 
 echo "未啟動"
else
 echo "正在停止程序${arr[0]}"
 kill -9 ${arr[0]}
 echo "程序已停止"
fi
start:
#!/bin/sh

cd /opt/bigdata/elk/es622/bin
sh elasticsearch