本文档提供了 Elasticsearch 5.6.5 版本的详细部署、配置和运维指南,包括安装步骤、证书配置、常见命令和操作等内容。
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
yum install openssl
在 /etc/sysctl.conf 中添加以下内容:
vm.max_map_count=262144
在 /etc/security/limits.conf 中添加以下内容:
* - nofile 65536 * - memlock unlimited
useradd elasticsearch passwd -l elasticsearch
# 上传并解压 JDK
tar -zxvf jdk-8u201-linux-x64.tar.gz
mv jdk-8u201-linux-x64 /usr/local/java1.8
编辑 /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
# 解压 Elasticsearch 安装包
tar -zxvf elasticsearch-5.6.5.tar.gz
# 更改目录权限
chown elasticsearch:elasticsearch elasticsearch-5.6.5/ -R
bin/elasticsearch-plugin install -b com.floragunn:search-guard-5:5.6.5-19.1
./gen_node_cert.sh 3 changeit capass
将 node-3*、ca/root-ca.pem 和 ca/truststore.jks 文件复制到新节点服务器目录 elasticsearch/config/。
编辑 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"
}'
内存不足:确保服务器有足够的内存分配给 Elasticsearch,可在 jvm.options 文件中调整内存设置。
文件描述符限制:确保已正确配置 /etc/security/limits.conf 中的文件描述符限制。
权限问题:确保 Elasticsearch 目录及其文件的所有者为 elasticsearch 用户。
网络问题:检查防火墙设置,确保 Elasticsearch 所需端口已开放。
Elasticsearch 日志默认位于 logs 目录,建议配置日志轮转以避免日志文件过大。