ElasticSearch|部署指南

单机部署

新建用户

ES默认不允许root身份进行运行,需要先创建对应的用户

1
2
adduser elasticsearch
passwd elasticsearch

解压,修改权限

1
2
cp -R elasticsearch-xxx /usr/local/
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-xxx/

修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: test-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: test-node
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
# 当前机器的ip绑定
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["test-node"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#
# ---------------------------------- Security ----------------------------------
#
# *** WARNING ***
#
# Elasticsearch security features are not enabled by default.
# These features are free, but require configuration changes to enable them.
# This means that users don’t have to provide credentials and can get full access
# to the cluster. Network connections are also not encrypted.
#
# To protect your data, we strongly encourage you to enable the Elasticsearch security features.
# Refer to the following documentation for instructions.
#
# https://www.elastic.co/guide/en/elasticsearch/reference/7.16/configuring-stack-security.html

修改系统配置

这里主要是为了调整系统对用户资源的限制,特别是文件描述符(nofile)和进程数(nproc)。这样做主要是为了确保ES能够高效运行,并避免由于默认的资源限制导致运行出现相关问题

修改 /etc/security/limits.conf

1
2
3
4
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096

之后退出用户重进方可生效

修改/etc/sysctl.conf

在文件的最后一行新增内容

1
vm.max_map_count=655360

之后刷新一下

1
sysctl -p

启动程序

1
2
3
4
5
6
su elasticsearch

# 进入 bin 目录

# 后台启动
sh elasticsearch -d

测试访问

访问9200端口如果服务正常应该返回JSON基本参数

设置用户密码

ES建议设置用户密码进行访问,如果需要设置用户名密码,需要先 kill 相关进程,之后修改对应配置进行重启

配置文件

首先确保配置文件开启安全认证

1
xpack.security.enabled: true

设置内置用户密码

1
2
3
4
5
6
7
8
#先停原先进程
kill -9 PID
#启动ES
sh bin/elasticsearch
#交互设置密码
sh bin/elasticsearch-setup-passwords interactive
#后台运行
sh bin/elasticsearch -d

设置密码后后续所有的API请求都需要传入用户名和密码

索引结构迁移

对于某个环境中已有的索引结构,我们希望获取结构定义,然后将其同步到新的环境中

查询源ES结构

1
2
curl -u "username:password" -X GET "http://ip:9200/index_name/_settings?pretty"
curl -u "username:password" -X GET "http://ip:9200/index_name/_mapping?pretty"

通过API获取源ES的mapping结构(重要),settings根据环境可能存在差异

同步到新环境

1
2
3
4
5
curl -u "username:password" -X PUT "http://target-es-host:9200/index_name" -H 'Content-Type: application/json' -d'
{
"settings": { /* 从原始索引获取的设置 */ },
"mappings": { /* 从原始索引获取的映射 */ }
}'

ElasticSearch|部署指南
http://example.com/2025/03/07/ElasticSearch-部署指南/
作者
Noctis64
发布于
2025年3月7日
许可协议