51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

部署Elasticsearch集群

对于脚本内变量的设置:
ES变量:你要安装的Elasticsearch的大版本号
ES_VERSION变量:具体的版本号
CLUSTER变量:集群名称
NODE_ID变量:节点编号
DATADIR变量:数据存放目录
IP变量:集群内的主机IP
NODENB变量:设置集群内最少要有几个节点在运行,否则集群无法访问
MEMORY变量:分配多少内存

#!/bin/bash
#
#********************************************************************
#Author:            OSSQ
#QQ:                1169887794
#Date:              2022-08-22
#FileName:          install_Elasticsearch.sh
#URL:               https://blog.ossq.cn
#Description:       The deploy script
#Copyright (C):     2022 All rights reserved
#********************************************************************
ES=7
ES_VERSION=$ES.17.5
CLUSTER=ELK-Cluster
NODE_ID=1
DATADIR=/data
IP='"10.0.0.101", "10.0.0.102","10.0.0.103"'
NODENB=2
MEMORY=2g
color () {
	RES_COL=60
	MOVE_TO_COL="echo -en \\033[${RES_COL}G"
	SETCOLOR_SUCCESS="echo -en \\033[1;32m"
	SETCOLOR_FAILURE="echo -en \\033[1;31m"
	SETCOLOR_WARNING="echo -en \\033[1;33m"
	SETCOLOR_NORMAL="echo -en \E[0m"
	echo -n "$1" && $MOVE_TO_COL
	echo -n "["
	if [ $2 = "success" -o $2 = "0" ] ;then
		${SETCOLOR_SUCCESS}
		echo -n $"  OK  "    
	elif [ $2 = "failure" -o $2 = "1"  ] ;then 
		${SETCOLOR_FAILURE}
		echo -n $"FAILED"
	else
		${SETCOLOR_WARNING}
		echo -n $"WARNING"
	fi
	${SETCOLOR_NORMAL}
	echo -n "]"
	echo 
}
. /etc/os-release
download(){
if ! [ -f $ES ];then
	echo "下载安装包"
	if [ $ID = "rocky" -o $ID = "centos" ];then
		wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-$ES.x/$ES_VERSION/elasticsearch-$ES_VERSION-x86_64.rpm &>/dev/null
	elif [ $ID = "ubuntu" ];then
		wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/apt/$ES.x/pool/main/e/elasticsearch/elasticsearch-$ES_VERSION-amd64.deb &>/dev/null
	else
		color OS不支持 1
		exit
	fi
	if ! [ $? -eq 0 ];then
		color 软件包下载失败 1
		exit
	fi
fi
if [ $ID = "rocky" -o $ID = "centos" ];then
	yum -y install $ES &>/dev/null
elif [ $ID = "ubuntu" ];then
	dpkg -i $ES &>/dev/null
else
	color OS不支持 1
	exit
fi
}
modify(){
	echo "更改配置文件"
	grep vm.max_map_count /etc/sysctl.conf &>/dev/null
	if ! [ $? -eq 0 ];then
		echo "vm.max_map_count = 262144" >> /etc/sysctl.conf
		sysctl -p &>/dev/null
	fi
	grep 'nofile          1000000' /etc/security/limits.conf &>/dev/null
	if ! [ $? -eq 0 ];then
	cat >> /etc/security/limits.conf <<-EOF
		*               soft   core           unlimited
		*               hard   core           unlimited
		*               soft   nproc           1000000
		*               hard   nproc           1000000
		*               soft   nofile          1000000
		*               hard   nofile          1000000
		*               soft   memlock         32000
		*               hard   memlock         32000
		*               soft   msgqueue        8192000
		*               hard   msgqueue        8192000
		EOF
	fi
	grep cluster.name: $CLUSTER /etc/elasticsearch/elasticsearch.yml &>/dev/null
	if ! [ $? -eq 0 ];then
		sed -i "s/#cluster.name: my-application/cluster.name: $CLUSTER/" /etc/elasticsearch/elasticsearch.yml
		sed -i "s/#node.name: node-1/node.name: node-$NODE_ID/" /etc/elasticsearch/elasticsearch.yml
		sed -i "s#path.data: /var/lib/elasticsearch#path.data: $DATADIR/es-data#" /etc/elasticsearch/elasticsearch.yml
		sed -i "s#path.logs: /var/log/elasticsearch#path.logs: $DATADIR/es-logs#" /etc/elasticsearch/elasticsearch.yml
		sed -i 's/#bootstrap.memory_lock: true/bootstrap.memory_lock: true/' /etc/elasticsearch/elasticsearch.yml
		sed -i 's/#network.host: 192.168.0.1/network.host: 0.0.0.0/' /etc/elasticsearch/elasticsearch.yml
		sed -i "/#discovery.seed/a discovery.seed_hosts: [$IP]" /etc/elasticsearch/elasticsearch.yml
		sed -i "/#cluster.initial/a cluster.initial_master_nodes: [$IP]" /etc/elasticsearch/elasticsearch.yml
		sed -i 's/#action.destructive_requires_name: true/action.destructive_requires_name: true/' /etc/elasticsearch/elasticsearch.yml
		sed -i "/#cluster.initial/a gateway.recover_after_nodes: $NODENB" /etc/elasticsearch/elasticsearch.yml
		mkdir -p /etc/systemd/system/elasticsearch.service.d/
		echo -e "[Service]\nLimitMEMLOCK=infinity" >> /etc/systemd/system/elasticsearch.service.d/override.conf
		sed -i "s/## -Xms4g/-Xms$MEMORY/" /etc/elasticsearch/jvm.options
		sed -i "s/## -Xmx4g/-Xmx$MEMORY/" /etc/elasticsearch/jvm.options
		sed -i 's/LimitNOFILE=65535/LimitNOFILE=1000000/' /usr/lib/systemd/system/elasticsearch.service
		sed -i 's/LimitNPROC=4096/LimitNPROC=65535/' /usr/lib/systemd/system/elasticsearch.service
		mkdir -p $DATADIR/es-{data,logs}
		chown -R elasticsearch.elasticsearch $DATADIR/es-{data,logs}
	fi
	color 更改完毕 0
}
run(){
	echo "启动"
	systemctl daemon-reload &>/dev/null
	systemctl enable --now elasticsearch.service &>/dev/null
	systemctl is-active elasticsearch.service &>/dev/null
	systemctl status elasticsearch.service &>/dev/null
	if [ $? -eq 0 ];then
		color 启动成功 0
	else
		color 启动失败 1
	fi
}
download
modify
run

赞(0)
未经允许不得转载:工具盒子 » 部署Elasticsearch集群