Linux 安装es

文章目录

  • [1. 直接上脚本](#1. 直接上脚本)

1. 直接上脚本

bash 复制代码
vi install_es_offline.sh
bash 复制代码
#!/bin/bash
# Elasticsearch 在线安装脚本 for CentOS 7.9
# 服务器配置:2核8G(可平滑升级到4核16G),自动优化堆内存
# 以 root 用户执行

set -e

# ==================== 配置区域 ====================
CLUSTER_NAME="my-single-cluster"
NODE_NAME="node-1"
NETWORK_HOST="0.0.0.0"          # 生产环境请改为内网IP
HTTP_PORT=9200
TRANSPORT_PORT=9300
ES_MAJOR_VERSION="7.x"          # 使用 7.x 最新版
# ================================================

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

# 检查是否为 root
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}请使用 root 用户执行本脚本${NC}"
    exit 1
fi

echo -e "${GREEN}开始在线安装 Elasticsearch (${ES_MAJOR_VERSION})...${NC}"

# 1. 安装必要工具
yum install -y wget curl perl

# 2. 导入 GPG 密钥
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

# 3. 添加官方 YUM 仓库
cat > /etc/yum.repos.d/elasticsearch.repo <<EOF
[elasticsearch-${ES_MAJOR_VERSION}]
name=Elasticsearch repository for ${ES_MAJOR_VERSION} packages
baseurl=https://artifacts.elastic.co/packages/${ES_MAJOR_VERSION}/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF

# 4. 安装 Elasticsearch
yum install -y elasticsearch

# 5. 动态调整 JVM 堆内存(基于物理内存自动计算)
TOTAL_MEM_MB=$(free -m | awk '/^Mem:/{print $2}')
if [ $TOTAL_MEM_MB -ge 16384 ]; then
    HEAP_SIZE="8g"
    echo -e "${GREEN}检测到内存 ≥16G,设置堆内存为 8g${NC}"
elif [ $TOTAL_MEM_MB -ge 8192 ]; then
    HEAP_SIZE="4g"
    echo -e "${GREEN}检测到内存 8G~16G,设置堆内存为 4g${NC}"
else
    HEAP_SIZE="2g"
    echo -e "${GREEN}检测到内存 <8G,设置堆内存为 2g${NC}"
fi

if [ -f /etc/elasticsearch/jvm.options ]; then
    sed -i '/^-Xms/d' /etc/elasticsearch/jvm.options
    sed -i '/^-Xmx/d' /etc/elasticsearch/jvm.options
    sed -i "1i -Xms${HEAP_SIZE}\n-Xmx${HEAP_SIZE}" /etc/elasticsearch/jvm.options
    echo -e "${GREEN}JVM 堆内存已设置为 ${HEAP_SIZE}${NC}"
fi

# 6. 配置 elasticsearch.yml
cat > /etc/elasticsearch/elasticsearch.yml <<EOF
cluster.name: ${CLUSTER_NAME}
node.name: ${NODE_NAME}
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: ${NETWORK_HOST}
http.port: ${HTTP_PORT}
transport.tcp.port: ${TRANSPORT_PORT}
discovery.type: single-node
action.auto_create_index: true
bootstrap.memory_lock: false
EOF

# 7. 优化系统参数
if ! grep -q "vm.max_map_count" /etc/sysctl.conf; then
    echo "vm.max_map_count=262144" >> /etc/sysctl.conf
    sysctl -p
else
    sysctl -w vm.max_map_count=262144
fi

cat > /etc/security/limits.d/elasticsearch.conf <<EOF
elasticsearch soft nofile 65536
elasticsearch hard nofile 65536
elasticsearch soft nproc 4096
elasticsearch hard nproc 4096
EOF

# 8. 启动并启用服务
systemctl daemon-reload
systemctl enable elasticsearch
systemctl start elasticsearch

echo -e "${GREEN}等待 Elasticsearch 启动...${NC}"
sleep 10

if systemctl is-active --quiet elasticsearch; then
    echo -e "${GREEN}Elasticsearch 服务已启动成功${NC}"
else
    echo -e "${RED}Elasticsearch 启动失败,查看日志:journalctl -u elasticsearch${NC}"
    exit 1
fi

# 9. 防火墙放行(如果 firewalld 正在运行)
if systemctl is-active --quiet firewalld; then
    firewall-cmd --permanent --add-port=${HTTP_PORT}/tcp
    firewall-cmd --permanent --add-port=${TRANSPORT_PORT}/tcp
    firewall-cmd --reload
    echo -e "${GREEN}防火墙已开放端口 ${HTTP_PORT} 和 ${TRANSPORT_PORT}${NC}"
fi

# 10. 创建服务别名 es
if [ ! -f /etc/systemd/system/es.service ]; then
    ln -s /usr/lib/systemd/system/elasticsearch.service /etc/systemd/system/es.service
    systemctl daemon-reload
    echo -e "${GREEN}已创建服务别名: es (原 elasticsearch)${NC}"
    echo -e "现在可以使用 systemctl {start|stop|status|enable} es 来管理服务"
else
    echo -e "服务别名 es 已存在,跳过创建"
fi

# 11. 测试连接
if command -v curl &>/dev/null; then
    echo -e "${GREEN}测试 Elasticsearch 连接...${NC}"
    curl -s "http://localhost:${HTTP_PORT}" | head -n 1
else
    echo -e "未找到 curl 命令,跳过连接测试"
fi

echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}Elasticsearch 在线安装完成!${NC}"
echo -e "集群名称: ${CLUSTER_NAME}"
echo -e "节点名称: ${NODE_NAME}"
echo -e "监听地址: ${NETWORK_HOST}:${HTTP_PORT}"
echo -e "JVM 堆内存: ${HEAP_SIZE}"
echo -e "配置文件: /etc/elasticsearch/elasticsearch.yml"
echo -e "日志文件: /var/log/elasticsearch/"
echo -e "查看状态: systemctl status elasticsearch  或  systemctl status es"
echo -e "查看日志: journalctl -u elasticsearch -f"
echo -e "${GREEN}============================================${NC}"
bash 复制代码
chmod +x install_es_offline.sh
相关推荐
The Sheep 20231 小时前
Vue复习
linux·服务器·数据库
fengyehongWorld1 小时前
Linux rg命令
linux
pride.li2 小时前
海思视觉Hi3516CV610--开机自动设置ip
linux·网络·网络协议·tcp/ip
我叫张小白。2 小时前
CentOS 7 安装 Docker并配置镜像加速(完整指南)
linux·docker·centos
Titan20243 小时前
Linux动静态库
linux·服务器·c++
赵民勇3 小时前
Linux file命令详解
linux·运维
li-xun3 小时前
LINUX DO 社区注册机制调整与公益 AI 服务动态
linux·运维·人工智能
ba_pi4 小时前
k8s删除pod
linux·容器·kubernetes
wuminyu4 小时前
Java锁机制之park和unpark源码剖析
java·linux·c语言·jvm·c++