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
相关推荐
ltl9 小时前
内核追踪:ftrace、kprobe、uprobe、tracepoint 生产实战
linux
·薯条大王9 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
互联网中的一颗神经元10 小时前
04 — 安全撤销:改错了怎么退回去
大数据·安全·elasticsearch
小小、码农12 小时前
Linux进程控制:从fork到exec,手写一个简易Shell
linux·运维·服务器
不会就选b12 小时前
Linux之进程间通信(二)---模拟进程池
linux·运维·服务器
沫璃染墨14 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
AIgorithmGEEK15 小时前
Linux 环境变量完全指南:从入门到理解底层原理
linux·运维·服务器
Elasticsearch15 小时前
Elasticsearch:列式索引模式 - Columnar index mode
elasticsearch
酷可达拉斯16 小时前
Linux操作系统-升级OpenSSH修复漏洞
linux·运维·服务器·云计算
Molesidy16 小时前
【Linux】基于busybox的根文件系统的完善
linux·服务器·嵌入式linux