#!/bin/sh
#################################################
#用法:source auto_deploy_elastic.sh ip nodename#
#################################################
# 创建用户和用户组
#set -e
Usage(){
echo "Usage: source auto_deploy_elastic.sh ip1 ip2 ip3 master|node1|node2"
}
if [ $# -lt 1 ]; then
Usage
exit 0
else
cmd=${!#}
fi
user=elasticsearch
group=elasticsearch
datadir="/data/shineenergy/data"
logdir="/data/shineenergy/log"
egrep "^${group}" /etc/group >& /dev/null
if [ $? -ne 0 ]; then
groupadd elasticsearch
fi
egrep "^${user}" /etc/passwd >& /dev/null
if [ $? -ne 0 ]; then
useradd -g elasticsearch elasticsearch
echo "xxxx" | passwd --stdin elasticsearch
fi
cnt19200=$(netstat -nap|grep 19200|awk '{print $7}'|wc -l)
if [ ${cnt19200} -ne 0 ]; then
$(ps -ef|grep elasticsearch|egrep "*Xmx*"|awk '{print $2}'| xargs kill -9)
sleep 8
fi
rm -rf /app/elasticsearch-7.8.0/*
rm -rf /app/jdk-8u212-linux-x64.rpm*
rm -rf /app/shineenergy
cd /app/
# 创建java8环境
# wget xxxxxx:18080/ansible/jdk-8u212-linux-x64.rpm
# rpm -ivh /app/jdk-8u212-linux-x64.rpm
#rpm -ivh /install/jdk-8u212-linux-x64.rpm
#
#
#declare -x export PATH=/usr/java/jdk1.8.0_212-amd64/bin:$PATH
#declare -x export JAVA_HOME=/usr/java/jdk1.8.0_212-amd64
#cnt=$(grep "export PATH=/usr/java/jdk1.8.0_212-amd64/bin:\$PATH" ~/.bash_profile | wc -l)
#if [ ${cnt} -eq 0 ]
#then
# echo 'export PATH=/usr/java/jdk1.8.0_212-amd64/bin:$PATH' >> ~/.bash_profile
# echo "export JAVA_HOME=/usr/java/jdk1.8.0_212-amd64/" >> ~/.bash_profile
#fi
#wget xxxxxx:18080/ansible/elasticsearch-7.8.0-linux-x86_64.tar.gz
cp /install/elasticsearch-7.8.0-linux-x86_64.tar.gz /app/
tar -xzvf /app/elasticsearch-7.8.0-linux-x86_64.tar.gz
if [ $# -eq 4 ]; then
if [ $4 = "master" ]; then
echo -e "\n\n" | /bin/sh /app/elasticsearch-7.8.0/bin/elasticsearch-certutil ca
echo -e "\n\n\n" | /bin/sh /app/elasticsearch-7.8.0/bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
\cp -rf /app/elasticsearch-7.8.0/*.p12 /home/osoper/
mv /app/elasticsearch-7.8.0/*.p12 /app/elasticsearch-7.8.0/config/
fi
else
echo -e "\n\n" | /bin/sh /app/elasticsearch-7.8.0/bin/elasticsearch-certutil ca
echo -e "\n\n\n" | /bin/sh /app/elasticsearch-7.8.0/bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
\cp -rf /app/elasticsearch-7.8.0/*.p12 /home/osoper/
mv /app/elasticsearch-7.8.0/*.p12 /app/elasticsearch-7.8.0/config/
fi
chown -R elasticsearch:elasticsearch /app/elasticsearch-7.8.0
# 修改elasticsearch.yml
ip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|egrep "^10")
echo -e "\n" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "cluster.name: myes" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
if [ $# -eq 2 ]; then
echo "node.name: $2" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "node.master: true" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "node.data: true" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
fi
if [ $# -eq 4 ]; then
echo "node.name: $4" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
if [ $4 = "master" ]; then
echo "node.master: true" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "node.data: false" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
else
# echo "node.master: false" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "node.data: true" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
fi
fi
if [ ! -d $datadir ]; then
mkdir -p $datadir
fi
if [ ! -d $logdir ]; then
mkdir -p $logdir
fi
chown -R elasticsearch:elasticsearch $datadir
chown -R elasticsearch:elasticsearch $logdir
echo "path.data: $datadir" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "path.logs: $logdir" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "network.host: 0.0.0.0" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "http.port: 19200" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "transport.tcp.port: 19300" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
#echo "discovery.seed_hosts: ['${ip}:19300']" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
if [ $# -eq 2 ]; then
echo "discovery.seed_hosts: ['$1:19300']" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "cluster.initial_master_nodes: ['$2']" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
else
echo "discovery.seed_hosts: ['$1:19300','$2:19300','$3:19300']" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo 'cluster.initial_master_nodes: ["master","node1","node2"]' >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
fi
#echo "cluster.max_shards_per_node: 10000" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
#echo "bootstrap.system_call_filter: false" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo -e "\n" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
#Xpack配置
echo "xpack.security.enabled: true" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "xpack.security.transport.ssl.enabled: true" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "xpack.security.transport.ssl.verification_mode: certificate" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "xpack.security.transport.ssl.keystore.path: elastic-certificates.p12" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo "xpack.security.transport.ssl.truststore.path: elastic-certificates.p12" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
echo -e "\n" >> /app/elasticsearch-7.8.0/config/elasticsearch.yml
#change elasticsearch-env
sed -i "s/<[[:space:]]<(env)/<<<'env'/g" /app/elasticsearch-7.8.0/bin/elasticsearch-env
#change jvm conf
totaljvm=$(free -h|egrep "^Mem"|awk '{print $2}'|sed "s/G//")
halfjvm=$(($totaljvm/2))
\cp -rf /app/elasticsearch-7.8.0/config/jvm.options /app/elasticsearch-7.8.0/config/jvm.options.bak
sed -i "s/-Xms1g/-Xms${halfjvm}g/" /app/elasticsearch-7.8.0/config/jvm.options
sed -i "s/-Xmx1g/-Xmx${halfjvm}g/" /app/elasticsearch-7.8.0/config/jvm.options
##### 修改电脑配置以契合ES #####
#修改sysctl.conf
cnt=$(grep "vm.max_map_count=262144" /etc/sysctl.conf | wc -l)
if [ ${cnt} -eq 0 ]
then
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
/sbin/sysctl -p
fi
confpath="/etc/security/limits.d/"
confile=""
for i in `ls ${confpath}`
do
confile=${confpath}$i
done
cntfile=$(ls ${confpath}|wc -l)
if [ ${cntfile} -eq 1 ]; then
if [ -f "$confile" ]; then
sed -i -r '/^\*/{s/[0-9]+/4096/}' $confile
hardCnt=$(grep "hard" $confile | wc -l)
if [ ${hardCnt} -eq 0 ]; then
echo "* hard nproc 4096" >> $confile
fi
#else
# echo "90file doesn't exist!"
# for i in `ls ${confpath}`
# do
# echo ${confpath}$i
# done
fi
fi
##### 修改电脑配置以契合ES #####
# 切换用户拉起ES服务
#su - ${user} <<!
#
#/bin/sh /app/elasticsearch-7.8.0/bin/elasticsearch -d && sleep 120
#
#echo -e "y\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\nxxx\n" | /bin/sh /app/elasticsearch-7.8.0/bin/elasticsearch-setup-passwords interactive
#
#!
#
##验证集群状态是否健康
#if [ $# -eq 2 ]; then
# # 主节点发送请求验证密码是否设置成功
# if [ $2 = "master" ]; then
# result=$(curl -u elastic:Qswslhk_1314151 http://${ip}:19200/_cluster/health?pretty)
# if [[ $result =~ "green" ]]; then
# echo -e "\033[32m${result}\033[0m"
# else
# echo -e "\033[31m${result}\033[0m"
# fi
# fi
#else
# if [ $4 = "master" ]; then
# result=$(curl -u elastic:Qswslhk_1314151 http://${ip}:19200/_cluster/health?pretty)
# if [[ $result =~ "green" ]]; then
# echo -e "\033[32m${result}\033[0m"
# else
# echo -e "\033[31m${result}\033[0m"
# fi
# fi
#fi
#
记录下,后期会持续优化!!!