install_elasticsearch

install_elasticsearch

#!/usr/bin/bash
#搭建elastic集群
#elastic服务必须要重启才可以进行安装完成,要不然系统句柄无法设置。
#--------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------#
#定义需要搭建的集群和密码
ip_addr="
192.168.98.173 123456
192.168.98.174 123456
192.168.98.175 123456
"
#定义hosts名称
name="elastic"
#定义安装包名称
elasticsearch_package_name="elasticsearch-7.14.0-linux-x86_64.tar.gz"
#创建的elastic用户
user="elastic"
#创建的elastic用户密码
pass="1234567"
#定义elastic的日志和数据存储路径
path_data="/opt/data"
path_log="/opt/log"
#权限问题必须定义data和log的上层目录要不然程序无法启动
path_ll="/opt"
#--------------------------------------------------------------------------------------#
#--------------------------------------------------------------------------------------#
node_id="1"

IFS='
'
#推送主机公钥

if [ ! -e $elasticsearch_package_name ];then
	exit
fi

for line in $ip_addr
do
	ip=`echo "$line" |awk '{print $1}'`
	passwd=`echo "$line" |awk '{print $2}'`
	ping -c1 -W1 $ip &>/dev/null
	if [ $? -eq 0 ];then
/usr/bin/expect <<EOF
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yes\r" ; exp_continue }
"password" { send "$passwd\r" ; exp_continue }
"*#" { send "\r"}
}
EOF
	else
	 echo "$ip"| tee -a fail_ip.txt
	fi
done
wait
echo "1、ssh-copy-id  is ok!"

IFS='
'
#修改主机hosts和hostname
for e_host in $ip_addr
do
	ip=`echo "$e_host" |awk '{print $1}'`
	net_addr=`ssh root@$ip " ip addr |egrep '\<inet\>'|grep -v '127.0.0.1'|awk '{print \\$2}'|awk -F'/' '{print \\$1}' "`
	ssh root@$ip " echo "$name-$net_addr" > /etc/hostname "
	gg=`ssh root@$ip " cat /etc/hostname "`
	ssh root@$ip " echo "$net_addr $gg" >> /etc/hosts "

done
wait
#拷贝安装包
IFS='
'
for a_copy in $ip_addr
do
	ip=`echo "$a_copy" |awk '{print $1}'`
	el_ip[++j]=`echo "$a_copy"|awk '{print $1}'`
	scp $elasticsearch_package_name  root@$ip:/tmp/
	ssh root@$ip " if [ -e "/tmp/$elasticsearch_package_name" ];then echo "$ip File copy complete" ; else echo "$ip File copy failed" exit ; fi "
done
wait
#获取ip传递给数组
for hq_ip in ${!el_ip[@]}
do
	#echo "${el_ip[hq_ip]}"
	echo "ipaddr"
done

#配置elastic
IFS='
'
for c_con in $ip_addr
do
	ip=`echo "$c_con" |awk '{print $1}'`
	
	ssh root@$ip " cd /tmp
tar -zxvf $elasticsearch_package_name 
mv elasticsearch-7.14.0 /usr/local/elasticsearch
useradd $user ; echo "$pass" |passwd --stdin $user &>/dev/null
chown -R $user:$user /usr/local/elasticsearch
mkdir $path_data
mkdir $path_log
chown -R $user:$user $path_ll
chown -R $user:$user $path_data
chown -R $user:$user $path_log
 
cat <<EOF >>/etc/security/limits.conf
$user soft nofile 65536
$user hard nofile 65536
* soft nproc 65536
* hard nproc 65536
EOF

cat <<EOF >>/etc/security/limits.d/20-nproc.conf
$user soft nofile 65536
$user hard nofile 65536
EOF

cat <<EOF >>/etc/sysctl.conf
vm.max_map_count=655360
EOF

cat <<EOF >>/usr/local/elasticsearch/config/elasticsearch.yml
cluster.name: elastic-application
node.name: node-$node_id
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9700
discovery.seed_hosts: ["${el_ip[1]}", "${el_ip[2]}","${el_ip[3]}"]
cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
path.data: $path_data
path.logs: $path_log
EOF

sysctl -p
systemctl stop firewalld
systemctl disable firewalld

cat <<EOF >>/usr/lib/systemd/system/elasticsearch.service
[Unit]
Description=elasticsearch
After=network.target

[Service]
Type=forking
User=elastic
ExecStart=/usr/local/elasticsearch/bin/elasticsearch -d
PrivateTmp=true
# 指定此进程可以打开的最大文件数
LimitNOFILE=65535
# 指定此进程可以打开的最大进程数
LimitNPROC=65535
# 最大虚拟内存
LimitAS=infinity
# 最大文件大小
LimitFSIZE=infinity
# 超时设置 0-永不超时
TimeoutStopSec=0
# SIGTERM是停止java进程的信号
KillSignal=SIGTERM
# 信号只发送给给JVM
KillMode=process
# java进程不会被杀掉
SendSIGKILL=no
# 正常退出状态
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
reboot
"
let ++node_id
done

sleep 1m
#重启服务器是limit配置句柄生效
IFS='
'
for d_res in $ip_addr
do
	echo -n "$ip 系统句柄为"
	ip=`echo "$d_res" |awk '{print $1}'`
	ssh root@$ip "ulimit -Hu"
	echo -n "$i 状态为"
        ssh root@$ip " systemctl status elasticsearch.service|grep Active"


done
wait
echo "请通过以下链接查看服务集群是否搭建完成" 
echo "http://${el_ip[1]}:9200/_cat/health?v"

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注