脚本是同事的,感谢飞哥。
场景:这是一个基于docker环境的部署,脚本整合了单机模式,多服务器集群,单机集群模式。
#!/bin/bash
# install elasticsearch
# Recommend user id is 1001
# Use "id -u" to check normal user id
# Local config
DEPLOY_DIR="/home/elasticsearch"
# data dir default value is ${DEPLOY_DIR}/data
DATA_DIR=""
# empty means solo, the number of cluster's member is 3
IPS=(192.168.100.180 192.168.100.181 192.168.100.182)
# 检查部署集群状态
# curl -XGET http://127.0.0.1:9200/_cluster/stats?pretty
# curl -XGET http://localhost:9200/_cat/health?v
ES_VERSION="7.17.20-debian-12-r1"
ES_IMAGE="bitnami/elasticsearch:${ES_VERSION}"
TAR_NAME="elasticsearch-${ES_VERSION}.tar"
SCRIPT_NAME=$(basename ${0})
#usage: check_data_dir [canshu]
check_data_dir(){
while :
do
if [ "${DATA_DIR}u" == "u" ];then
echo "Data directory is not set"
fi
echo " Use default directory: ${DEPLOY_DIR}${1}/data"
read -p "Confirm[y/n]: " Confirm
case ${Confirm} in
Y|y|"yes"|"YES")
DATA_DIR=${DEPLOY_DIR}${1}/data
break
;;
N|n|no|NO)
echo
echo "Please modify script ${SCRIPT_NAME}, the variable DATA_DIR"
exit 66
;;
*)
echo "Error input"
esac
done
}
#usage:check_user [cansu]
#UID is linux environment variable, bitnami image use 1001 privilige, chmod -u 1001 #testuser
check_user(){
if [ ${UID} -ne 1001 -a ${UID} -ne 0 ];then
echo "Please use the user whose id is 1001 or 0."
echo "Current user id is ${UID}."
exit 88
fi
mkdir -p ${DATA_DIR} && chown 1001.1001 ${DATA_DIR}
cd ${DEPLOY_DIR}${1}/
}
deploy_standalone(){
cat>>docker-compose.yml<<EOF
version: '2'
services:
elasticsearch:
image: ${ES_IMAGE}
container_name: elasticsearch
restart: always
environment:
LANG: en_US.UTF-8
TZ: Asia/Shanghai
ELASTICSEARCH_HTTP_PORT_NUMBER: 9200
volumes:
- ${DATA_DIR}:/bitnami/elasticsearch/data
ports:
- 9200:9200
EOF
#if have network can use "#" zhushi,if have image file,can LAN deploy
docker images | grep -q ${ES_VERSION}
if [ $? -ne 0 ];then
if [ -f es-${ES_VERSION}.tar ];then
docker load < ${TAR_NAME}
else
echo "There is not a image in loacalhost."
exit 77
fi
fi
#modify /etc/profile add -> alias docker-compose='docker compose';source /etc/profile;
#docker compose up -d
docker-compose up -d
#docker compose logs -f
docker-compose logs -f
}
deploy_cluster(){
#pwd
#stand-alone mode cluster
if [ "${IPS[0]}" == "${IPS[1]}" ];then
N=${1}
CLUSTER=${IPS[0]}:9300,${IPS[1]}:9301,${IPS[2]}:9302
else
N=0
CLUSTER=${IPS[0]},${IPS[1]},${IPS[2]}
fi
c=${1}
cat>docker-compose.yml<<EOF
version: '2'
services:
elasticsearch${1}:
image: ${ES_IMAGE}
container_name: elasticsearch${1}
restart: always
environment:
- LANG=en_US.UTF-8
- TZ=Asia/Shanghai
- ELASTICSEARCH_HTTP_PORT_NUMBER=920${N}
- ELASTICSEARCH_TRANSPORT_PORT_NUMBER=930${N}
- ELASTICSEARCH_ADVERTISED_HOSTNAME=${IPS[c]}
- ELASTICSEARCH_BIND_ADDRESS=0.0.0.0
- ELASTICSEARCH_CLUSTER_NAME=MVcluster
- ELASTICSEARCH_CLUSTER_HOSTS=${CLUSTER}
- ELASTICSEARCH_NODE_NAME=node${N}
- ELASTICSEARCH_HEAP_SIZE=1g
volumes:
- ${DATA_DIR}:/bitnami/elasticsearch/data
ports:
- 920${N}:920${N}
- 930${N}:930${N}
EOF
docker images | grep -q ${ES_VERSION}
if [ $? -ne 0 ];then
if [ -f es-${ES_VERSION}.tar ];then
docker load < ${TAR_NAME}
else
echo "There is not a image in loacalhost."
exit 77
fi
fi
docker-compose up -d
#docker-compose logs -f
}
main(){
if [ ${#IPS[@]} -le 1 ];then
MODE=standalone
else
MODE=cluster
fi
echo " deploy mode:${MODE}"
if [ "${MODE}" == "standalone" ];then
check_data_dir
check_user
deploy_standalone
else
c=0
for ip in ${IPS[@]}
do
hostname -I |grep -w "${ip}"
if [ $? -eq 0 ];then
check_data_dir ${c}
check_user ${c}
deploy_cluster ${c}
fi
((c += 1))
done
fi
}
main
留意编码 cat -A jiaoben