Elasticsearch:集群安装

1. 准备工作(所有节点执行)

1.1 创建专用用户和目录

1
2
3
4
5
6
7
8
9
10
# 创建ES用户
useradd esadmin
passwd esadmin # 设置密码

# 创建安装目录
mkdir -p /export/servers/es
chown -R esadmin:esadmin /export/servers/es

# 授予sudo权限
visudo

添加以下内容:

1
esadmin ALL=(ALL) ALL

1.2 系统参数优化

1
2
# 修改文件描述符限制
vim /etc/security/limits.conf

添加以下内容:

1
2
3
4
5
6
7
8
9
* soft nofile 65535
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
esadmin soft memlock unlimited
esadmin hard memlock unlimited
bash
# 修改内核参数
vim /etc/sysctl.conf

添加:

1
2
vm.max_map_count=655360
fs.file-max=655350

应用修改:

1
sysctl -p

2. 安装Elasticsearch(所有节点执行)

2.1 下载并解压

1
2
3
4
5
6
su - esadmin
cd /export/servers/es
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0-linux-x86_64.tar.gz
tar zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz
cd elasticsearch-7.4.0/
mkdir -p ./datas

2.2 配置文件设置

1
2
3
cd config/
mv elasticsearch.yml elasticsearch.yml.bak
vim elasticsearch.yml

集群配置示例(根据节点修改):

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
# 集群名称(所有节点相同)
cluster.name: myes

# 节点名称(每个节点唯一)
node.name: node01

# 数据存储路径
path.data: /export/servers/es/elasticsearch-7.4.0/datas
path.logs: /export/servers/es/elasticsearch-7.4.0/logs

# 网络配置
network.host: 0.0.0.0
http.port: 9200

# 集群发现
discovery.seed_hosts: ["192.168.147.63", "192.168.147.64", "192.168.147.65"]
cluster.initial_master_nodes: ["node01", "node02", "node03"]

# 安全配置
bootstrap.memory_lock: false
http.cors.enabled: true
http.cors.allow-origin: "*"

# 其他优化
indices.fielddata.cache.size: 30%
indices.breaker.fielddata.limit: 60%

2.3 JVM配置调整

1
vim jvm.options

根据服务器内存调整(建议不超过物理内存的50%):

1
2
-Xms16g
-Xmx16g

3. 启动集群

3.1 启动服务(每个节点执行)

1
2
3
su - esadmin
cd /export/servers/es/elasticsearch-7.4.0/
bin/elasticsearch -d

3.2 验证集群状态

1
2
curl -XGET 'http://localhost:9200/_cluster/health?pretty'
curl -XGET 'http://localhost:9200/_cat/nodes?v'

4. 集群管理命令

4.1 常用API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看集群健康状态
curl -XGET 'http://localhost:9200/_cluster/health?pretty'

# 查看节点信息
curl -XGET 'http://localhost:9200/_cat/nodes?v'

# 查看索引状态
curl -XGET 'http://localhost:9200/_cat/indices?v'

# 创建索引
curl -XPUT 'http://localhost:9200/test_index'

# 删除索引
curl -XDELETE 'http://localhost:9200/test_index'

4.2 服务管理

1
2
3
4
5
6
7
8
9
10
11
# 查找进程ID
ps -ef | grep elasticsearch

# 停止服务
kill -15 <pid>

# 强制停止
kill -9 <pid>

# 后台启动
nohup ./bin/elasticsearch > /dev/null 2>&1 &

5. 注意事项

  1. 内存配置:JVM堆内存不要超过物理内存的50%,且不超过32GB
  2. 集群配置
    • discovery.seed_hosts 包含所有候选主节点
    • cluster.initial_master_nodes 只在首次启动集群时需要
  3. 生产建议
    • 配置至少3个主节点
    • 数据节点和主节点分离
    • 考虑使用X-Pack安全功能
  4. 监控:建议配置Kibana或Prometheus进行监控
  5. 日志:定期清理日志或配置日志轮转

6. 故障排查

  1. 启动失败检查
    1
    tail -n 100 logs/myes.log
  2. 常见问题
    • 内存不足:调整JVM参数
    • 文件描述符不足:检查limits.conf配置
    • 无法加入集群:检查网络和discovery配置

7. 扩展配置(可选)

7.1 插件安装

1
2
# 安装analysis-ik中文分词插件
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.0/elasticsearch-analysis-ik-7.4.0.zip

7.2 生产环境推荐配置

1
2
3
4
# 在elasticsearch.yml中添加:
thread_pool.search.size: 20
thread_pool.search.queue_size: 1000
indices.queries.cache.size: 10%