logo
0
0
WeChat Login
📝 docs: 完善 Elasticsearch 5.6.5 部署与运维指南

Elasticsearch 5.6.5 部署与运维指南

项目简介

本文档提供了 Elasticsearch 5.6.5 版本的详细部署、配置和运维指南,包括安装步骤、证书配置、常见命令和操作等内容。

版本信息

  • Elasticsearch 版本:5.6.5
  • Search Guard 版本:5.6.5-19.1
  • Java 版本:JDK 1.8

官方资源

Docker 镜像

docker.cnb.cool/zhiqiangwang/elasticsearch:5.6.5 docker.cnb.cool/zhiqiangwang/elasticsearch:kibana-5.6.5

系统准备

时间同步

# 确保 rc.local 可执行 chmod +x /etc/rc.d/rc.local # 添加定时任务,每天凌晨 1 点同步时间 00 01 * * * /usr/sbin/ntpdate -u cn.pool.ntp.org

安装 OpenSSL

yum install openssl

系统参数配置

配置系统文件使用数量

/etc/sysctl.conf 中添加以下内容:

vm.max_map_count=262144

/etc/security/limits.conf 中添加以下内容:

* - nofile 65536 * - memlock unlimited

安装步骤

1. 创建用户

useradd elasticsearch passwd -l elasticsearch

2. 安装 Java

# 上传并解压 JDK tar -zxvf jdk-8u201-linux-x64.tar.gz mv jdk-8u201-linux-x64 /usr/local/java1.8

3. 配置环境变量

编辑 /etc/profile 文件,添加以下内容:

# Java Environment Variables export JAVA_HOME=/usr/local/java1.8 export JRE_HOME=$JAVA_HOME/jre export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

使环境变量生效:

source /etc/profile

4. 安装 Elasticsearch

# 解压 Elasticsearch 安装包 tar -zxvf elasticsearch-5.6.5.tar.gz # 更改目录权限 chown elasticsearch:elasticsearch elasticsearch-5.6.5/ -R

5. 安装 Search Guard 插件

bin/elasticsearch-plugin install -b com.floragunn:search-guard-5:5.6.5-19.1

6. 证书配置

生成新节点证书

./gen_node_cert.sh 3 changeit capass

node-3*ca/root-ca.pemca/truststore.jks 文件复制到新节点服务器目录 elasticsearch/config/

修改 Elasticsearch 配置

编辑 elasticsearch/config/elasticsearch.yml 文件,配置证书相关参数。

检查证书过期日期

openssl x509 -noout -in node-1.crt.pem -dates # 示例输出: # notBefore=Dec 3 12:24:22 2025 GMT # notAfter=Dec 3 12:24:22 2027 GMT

运行与管理

启动命令

在控制台输出日志启动

bin/elasticsearch

守护进程启动

bin/elasticsearch -d

停止命令

# 查找 Elasticsearch 进程 ps aux | grep elasticsearch # 终止进程 kill <进程ID>

集群管理

检查集群状态

curl -XGET "http://localhost:9200/_cluster/health?pretty"

节点查看

# 查看所有节点信息 curl -XGET "http://localhost:9200/_cat/nodes?v" # 查看节点详细信息 curl -XGET "http://localhost:9200/_nodes?pretty"

插件查看

# 查看已安装的插件 bin/elasticsearch-plugin list # 查看插件详细信息 curl -XGET "http://localhost:9200/_nodes/plugins?pretty"

禁用自动分片分配

# 临时禁用 curl -XPUT http://localhost:9200/_cluster/settings -d '{ "transient": { "cluster.routing.allocation.enable": "none" } }' # 或同时设置持久化和临时禁用 curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d ' { "persistent": { "cluster.routing.allocation.enable": "none" }, "transient": { "cluster.routing.allocation.enable": "none" } }'

启用自动分片分配

当集群状态为绿色时,启用自动分片分配:

# 临时启用 curl -XPUT http://localhost:9200/_cluster/settings -d '{ "transient": { "cluster.routing.allocation.enable": "all" } }' # 或同时设置持久化和临时启用 curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d ' { "persistent": { "cluster.routing.allocation.enable": "all" }, "transient": { "cluster.routing.allocation.enable": "all" } }'

注意:如果集群没有设置持久化的自动分片参数,不要单独使用 persistent 来禁用自动分片。它要等到整个集群重启后才会生效,而且如果关闭单个节点,可能会导致分片迁移到其他节点。

备份与恢复

创建备份仓库

# 创建一个名为 backup 的备份仓库 curl -XPUT "http://localhost:9200/_snapshot/backup" -H 'Content-Type: application/json' -d ' { "type": "fs", "settings": { "location": "/path/to/backup/directory" } }'

创建备份

# 备份所有索引到 backup 仓库,命名为 snapshot_1 curl -XPUT "http://localhost:9200/_snapshot/backup/snapshot_1?wait_for_completion=true" # 备份指定索引到 backup 仓库,命名为 snapshot_2 curl -XPUT "http://localhost:9200/_snapshot/backup/snapshot_2?wait_for_completion=true" -H 'Content-Type: application/json' -d ' { "indices": "test_index" }'

查看备份

# 查看备份仓库中的所有备份 curl -XGET "http://localhost:9200/_snapshot/backup/_all?pretty"

恢复备份

# 从 backup 仓库恢复 snapshot_1 备份 curl -XPOST "http://localhost:9200/_snapshot/backup/snapshot_1/_restore?wait_for_completion=true" # 从 backup 仓库恢复 snapshot_2 备份,并只恢复 test_index 索引 curl -XPOST "http://localhost:9200/_snapshot/backup/snapshot_2/_restore?wait_for_completion=true" -H 'Content-Type: application/json' -d ' { "indices": "test_index" }'

故障排查

常见问题及解决方案

  1. 内存不足:确保服务器有足够的内存分配给 Elasticsearch,可在 jvm.options 文件中调整内存设置。

  2. 文件描述符限制:确保已正确配置 /etc/security/limits.conf 中的文件描述符限制。

  3. 权限问题:确保 Elasticsearch 目录及其文件的所有者为 elasticsearch 用户。

  4. 网络问题:检查防火墙设置,确保 Elasticsearch 所需端口已开放。

监控与维护

定期检查

  • 检查集群健康状态
  • 监控节点资源使用情况
  • 定期备份数据
  • 检查证书过期情况

日志管理

Elasticsearch 日志默认位于 logs 目录,建议配置日志轮转以避免日志文件过大。