赞
踩
此Bash脚本用于自动化在CentOS 7系统上安装和配置Elasticsearch(ES)分布式搜索引擎。脚本包括了创建集群、配置节点、安装Java环境、以及设置必要的系统和ES参数。执行脚本后,将会完成Elasticsearch的安装、配置和启动。
chmod +x elasticsearch_setup_script.sh
./elasticsearch_setup_script.sh
脚本步骤:
CLUSTER_NAME
:Elasticsearch集群的名称。NODE_NAME
:当前节点的名称。IPS
:节点的IP地址列表。HOSTS
:节点对应的主机名列表。/etc/hosts
文件。#!/bin/bash set -e # 定义变量 CLUSTER_NAME="tech-es" ## 节点名称修改 NODE_NAME="node-2" IPS=("192.168.2.49" "192.168.2.50" "192.168.2.51") HOSTS=("node-1" "node-2" "node-3") SEED_HOSTS=$(printf ',"%s"' "${IPS[@]}") SEED_HOSTS=[${SEED_HOSTS:1}] INITIAL_MASTER_NODES=$(printf ',"%s"' "${HOSTS[@]}") INITIAL_MASTER_NODES=[${INITIAL_MASTER_NODES:1}] ES_PKG="/usr/local/dhtech/autoinstall/lib/elasticsearch-7.17.8-x86_64.rpm" ES_CONFIG="/etc/elasticsearch/elasticsearch.yml" LIMITS_CONF="/etc/security/limits.conf" PROC_CONF="/etc/security/limits.d/20-nproc.conf" SYSCTL_CONF="/etc/sysctl.conf" ES_DIR="/dhxdata/es" PROFILE="/etc/profile" JAVA_HOME_DIR="/usr/local/java/jdk1.8" # 更新 /etc/hosts 文件 for (( i=0; i<${#IPS[@]}; i++ )); do if ! grep -q "${IPS[i]} ${HOSTS[i]}" /etc/hosts; then echo "${IPS[i]} ${HOSTS[i]}" >> /etc/hosts fi done # 检查并创建用户 if id "elasticsearch" >/dev/null 2>&1; then echo "elasticsearch user exists" else useradd elasticsearch echo "elasticsearch user has been created" fi # 检查ES的存储目录和日志目录是否存在,不存在则创建 if [ ! -d "$ES_DIR/data" ]; then mkdir -p "$ES_DIR/data" fi if [ ! -d "$ES_DIR/logs" ]; then mkdir -p "$ES_DIR/logs" fi # 检查Java环境 java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}') if [[ ! -n "${java_version}" ]]; then echo "未安装Java开发环境:${java_version}" exit 1 fi # 安装 if [ ! -f "$ES_PKG" ]; then echo "Elasticsearch安装包不存在,确认文件后请重新运行脚本。" exit 1 fi rpm -ivh "$ES_PKG" # 修改elasticsearch.yml配置 sed -i "s|^[#]*\s*cluster.name:.*|cluster.name: $CLUSTER_NAME|" "$ES_CONFIG" sed -i "s|^[#]*\s*node.name:.*|node.name: $NODE_NAME|" "$ES_CONFIG" sed -i "s|path.data:.*|path.data: $ES_DIR/data|" "$ES_CONFIG" sed -i "s|path.logs:.*|path.logs: $ES_DIR/logs|" "$ES_CONFIG" sed -i "s|^[#]*\s*network.host:.*|network.host: 0.0.0.0|" "$ES_CONFIG" sed -i "s|^[#]*\s*http.port:.*|http.port: 9200|" "$ES_CONFIG" sed -i "s|^[#]*\s*discovery.seed_hosts:.*|discovery.seed_hosts: $SEED_HOSTS|" "$ES_CONFIG" # sed -i "s|^[#]*\s*cluster.initial_master_nodes:.*|cluster.initial_master_nodes: $INITIAL_MASTER_NODES|" "$ES_CONFIG" sed -i "s|^[#]*\s*cluster.initial_master_nodes:.*|cluster.initial_master_nodes: [\"node-1\"]|" "$ES_CONFIG" # 添加以下三行 echo "xpack.security.enabled: true" >> "$ES_CONFIG" echo "xpack.license.self_generated.type: basic" >> "$ES_CONFIG" echo "xpack.security.transport.ssl.enabled: true" >> "$ES_CONFIG" # 修改系统配置 echo "elasticsearch soft nofile 65536 elasticsearch hard nofile 65536" >> "$LIMITS_CONF" echo "elasticsearch soft nofile 65536 elasticsearch hard nofile 65536 * hard nproc 4096" >> "$PROC_CONF" echo "vm.max_map_count=655360" >> "$SYSCTL_CONF" sysctl -p # 创建数据和日志的目录,并授予权限 chown -R elasticsearch:elasticsearch "$ES_DIR" # 配置ES_JAVA_HOME echo "ES_JAVA_HOME=$JAVA_HOME_DIR export ES_JAVA_HOME" >> "$PROFILE" source "$PROFILE" # 启动Elasticsearch systemctl enable elasticsearch systemctl start elasticsearch # 允许9200端口通过防火墙 firewall-cmd --zone=public --add-port=9200/tcp --permanent # 重新加载防火墙配置 firewall-cmd --reload # 设置密码 /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive echo "Elasticsearch安装已完成!"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。