运维开发网

Elasticsearch集群安装脚本

运维开发网 https://www.qedev.com 2020-10-12 12:30 出处:51CTO 作者:fxl风
Elasticsearch集群安装
#!/bin/bash
#依赖包:jdk-8u152-Linux-x64.tar.gz elasticsearch-5.5.2.tar.gz elasticsearch-head-master.tar.gz node-v8.1.4-Linux-x64.tar.gz elasticsearch-analysis-ik-5.5.2.zip
Package_path=/tmp/packge/redis_cluster_packge       #包目录
Ip_Local=`ip addr|grep -w inet|grep -v "127.0.0.1"|awk -F '[ /]+' '{print $3}'`  #本机ip

Cluster_node=( 		
172.16.2.2
172.16.17.14
172.16.17.15
)			#es集群节点




Es_cluster_install()
#es 集群安装配置,需要在各节点执行该函数
{

Es_file=${Package_path}/elasticsearch-5.5.2.tar.gz
Es_install_path=/usr/local/
Es_Tar_Directory=elasticsearch-5.5.2/
Es_Data=/data/es_data
Es_Back_Data=/data/es_back_data

[ -d $Package_path ]||exit 1
[ -f $Es_file ]||exit 1
#内核配置
echo "fs.file-max=65536" >>/etc/sysctl.conf
echo "vm.max_map_count=262144" >>/etc/sysctl.conf
sysctl -p >/dev/null 2>&1
cp /etc/security/limits.conf /etc/security/limits.conf.bak
echo "* soft nofile 65536" > /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "* soft nproc 2048" >> /etc/security/limits.conf
echo "* hard nproc 4096" >> /etc/security/limits.conf
sed -i "5s/1024/2048/" /etc/security/limits.d/90-nproc.conf




#es安装配置
tar -xf $Es_file -C $Es_install_path
cd ${Es_install_path}$Es_Tar_Directory

echo  "cluster.name: es-cluster">> config/elasticsearch.yml
echo  "node.name: es-node-$Ip_Local">> config/elasticsearch.yml
echo  "network.host: $Ip_Local" >> config/elasticsearch.yml
echo  "network.bind_host: $Ip_Local" >> config/elasticsearch.yml
echo  "network.publish_host: $Ip_Local" >> config/elasticsearch.yml
echo  "transport.tcp.port: 9300" >> config/elasticsearch.yml
echo  "http.port: 9200" >> config/elasticsearch.yml
echo  "http.enabled: true" >> config/elasticsearch.yml
echo  "discovery.zen.ping_timeout: 60s" >> config/elasticsearch.yml
echo  "discovery.zen.ping.unicast.hosts: [\"${Cluster_node[0]}:9300\",\"${Cluster_node[1]}:9300\",\"${Cluster_node[2]}:9300\"]" >> config/elasticsearch.yml
echo  "path.data: $Es_Data" >> config/elasticsearch.yml
echo  "path.repo: $Es_Back_Data" >> config/elasticsearch.yml
echo  "bootstrap.memory_lock: false" >> config/elasticsearch.yml
echo  "bootstrap.system_call_filter: false" >> config/elasticsearch.yml
echo  "http.cors.enabled: true" >> config/elasticsearch.yml
echo  'http.cors.allow-origin: "*"' >> config/elasticsearch.yml


#设置相关目录权限为es
useradd  es
[ -d $Es_Data ]|| mkdir $Es_Data -p
chown -R es $Es_Data
[ -d $Es_Back_Data ]|| mkdir $Es_Back_Data -p
chown -R es $Es_Back_Data

chown -R es ${Es_install_path}${Es_Tar_Directory}

#启动es
su - es <<EOF
cd ${Es_install_path}${Es_Tar_Directory}
nohup bin/elasticsearch &
EOF
}


Es_node_install(){
#安装之后请手动执行source /etc/profile
Es_node_file=${Package_path}/node-v8.1.4-Linux-x64.tar.gz    #elasticsearch-head-master 依赖包Node
[ -d $Package_path ]||exit 1
[ -f $Es_node_file ]||exit 1

tar -xf $Es_node_file -C /usr/local/
echo 'export NODE_HOME=/usr/local/node-v8.1.4-Linux-x64' >> /etc/profile
echo 'export PATH=$NODE_HOME/bin:$PATH' >>/etc/profile

echo -e "\033[31m Node installed successfully!\n {exec}:source /etc/profile \033[0m"
echo -e "\033[31m {node -v|npm -v} check   \033[0m"


}


Es_head_install(){
#Es图形界面安装函数,安装之后请手动执行source /etc/profile

Es_head_file=${Package_path}/elasticsearch-head-master.tar.gz
[ -d $Package_path ]||exit 1
[ -f $Es_head_file ]||exit 1

tar -xf $Es_head_file -C /usr/local/
cd /usr/local/elasticsearch-head-master/
npm install >/dev/null 2>&1
sleep 5
nohup npm run start & >/dev/null 2>&1
sleep 3
netstat -tunlp|grep 9100  >/dev/null 2>&1
[ $? -eq 0 ]&& echo -e "\033[31m es_head installed successfully! \033[0m" ||  echo -e "\033[31m es_head install failed! \033[0m"
}

Es_ik_install(){
#Es ik插件下载
Es_ik_file=${Package_path}/elasticsearch-analysis-ik-5.5.2.zip
Ik_install_path=/usr/local/elasticsearch-5.5.2/plugins/
[ -d $Package_path ]||exit 1
[ -d $Ik_install_path ]||exit 1
[ -f $Es_ik_file ]||exit 1

zip_check=`rpm -qa unzip |wc -l`
if [ $zip_check -eq 0 ];then
yum install -y unzip >/dev/null 2>&1
[ $? -eq 0 ]||exit 1

fi

unzip  $Es_ik_file -d $Ik_install_path >/dev/null 2>&1
if [ $? -eq 0 ];then
  echo -e "\033[31m Es_ik installed successfully!Restart es valid!  \033[0m"
fi

}

Jdk_install()
#jdk安装函数,执行之后请手动执行source /etc/profile
{
Jdk_file=${Package_path}/jdk-8u152-Linux-x64.tar.gz
Jdk_install_path=/usr/local/
Jdk_Es_Tar_Directory=jdk1.8.0_152/
[ -d $Package_path ]||exit 1
[ -f $Jdk_file ]||exit 1

java -version >/dev/null 2>&1
if [ $? -eq 0 ];then
    echo -e "\033[31m Jdk already exists! \033[0m"
    java -version
    exit 1 
  else
    tar -xf $Jdk_file -C $Jdk_install_path
    echo "export JAVA_HOME=${Jdk_install_path}${Jdk_Es_Tar_Directory}">>/etc/profile
    echo 'PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH'>>/etc/profile
    echo 'CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar'>> /etc/profile
    echo -e "\033[31m Jdk installed successfully!\n {exec}:source /etc/profile \033[0m"  

fi
}

Es_restart(){
Es_install_path=/usr/local/
Es_Tar_Directory=elasticsearch-5.5.2/
es_process_check=`ps -ef|grep elasticsearch|grep -v "grep"|wc -l`
if [ $es_process_check -eq 1 ];then
  es_pid=`ps -ef|grep elasticsearch|grep -v "grep" |awk  '{print $2}'`
  kill -9 $es_pid
  su - es <<EOF
  cd ${Es_install_path}${Es_Tar_Directory}
  nohup bin/elasticsearch &

EOF
  sleep 5
  else
  su - es <<EOF
  cd ${Es_install_path}${Es_Tar_Directory}
  nohup bin/elasticsearch &

EOF
  sleep 5
  

fi


}


case "$1" in

Es_restart)

Es_restart
result=$?

;;
jdk_install)

Jdk_install
result=$?

;;

esnode_install)
Es_node_install
result=$?

;;

eshead_install)
Es_head_install
result=$?

;;

Es_ik_install)

Es_ik_install
result=$?

;;

escluster_install)
Es_cluster_install

result=$?

;;

*)

echo "usage:$0 {jdk_install|escluster_install|esnode_install|eshead_install|Es_ik_install|Es_restart}"
exit 1

esac

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号