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
相关推荐
AlfredZhao2 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
武子康3 天前
调查研究-197 FAISS vs Elasticsearch 全面对比:从向量检索、全文搜索到 RAG 选型指南
人工智能·elasticsearch·agent
戴为沐3 天前
Linux内存扩容指南
linux
zylyehuo3 天前
Linux 彻底且安全地删除文件
linux
用户805533698034 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
Elasticsearch4 天前
Elasticsearch ES|QL:现已支持视图、子查询和读取时模式定义
elasticsearch
用户034095297914 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux
Web3探索者5 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo5 天前
Linux系统中网线与USB网络共享冲突
linux
Elasticsearch6 天前
Kibana 中的 SNMP 拓扑数据:从采集到 Canvas
elasticsearch