概述
在之前的文章中,我们一直搭建的其实都是单实例的 ElasticSearch 的服务。
而在生产环境中,为了保证服务的高可用和水平扩展能力,我们需要部署为 ElasticSearch 集群的模式。
节点功能说明
在之前的核心概念讲解中,我们已经讲解了 ElasticSearch 中不同节点类型的功能。
在生产环境中,我们建议每个节点只承担一个角色,如下表所示:
节点类型 | node.master | node.data | node.ingest |
---|---|---|---|
Master节点 | true | false | false |
Data节点 | false | true | false |
Ingest节点 | false | false | true |
Coordinate节点 | false | false | false |
每个节点的承担单一角色的优势如下:
- 可以根据不同节点的类型选择合适的资源;
- 高可用
- 高性能
典型集群模式
下面,我们来介绍一个典型的 ElasticSearch 集群的架构:
如上图所示,集群中部署了三种类型的节点:
- 3个Master节点(为防止脑裂,Master节点数目应该是奇数);
- n个Data节点,n可以根据数据量的变化来动态的增减;
- m个Coordinating节点,m可以根据数据写入/查询速率来控制;
- m个Kibana实例,kibana实例可以和Coordinating节点同机部署;
- 最前面通过一个 LoadBalance 来对外提供服务。
实战部署
下面,我们就以上述的典型集群模式的架构为例,来部署一套高可用、可扩展的 ElasticSearch 集群。
首先,我们来先来部署 Master 节点,示例配置如下:
# --------------------------------- Basic ----------------------------------
cluster.name: elasticsearch
node.name: elasticsearch-master-1/2/3
node.master: true
node.data: false
node.ingest: false
node.ml: false
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
# --------------------------------- Discovery ----------------------------------
discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.unicast.hosts: ["10.10.10.1", "10.10.10.2", "10.10.10.3", "10.10.10.4", "10.10.10.5", "10.10.10.6", "10.10.10.7", "10.10.10.8"]
# ---------------------------------- Secure -----------------------------------
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.truststore.path: certs/elastic-certificates.p12
接下来,我们来看一下 Data 节点,示例配置如下:
# --------------------------------- Basic ----------------------------------
cluster.name: elasticsearch
node.name: elasticsearch-data-1/2/3
node.master: false
node.data: true
node.ingest: false
node.ml: false
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
# --------------------------------- Discovery ----------------------------------
discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.unicast.hosts: ["10.10.10.1", "10.10.10.2", "10.10.10.3", "10.10.10.4", "10.10.10.5", "10.10.10.6", "10.10.10.7", "10.10.10.8"]
# ---------------------------------- Secure -----------------------------------
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.truststore.path: certs/elastic-certificates.p12
然后,我们来部署 Coordinating 节点,实例配置如下:
# --------------------------------- Basic ----------------------------------
cluster.name: easyenv-elasticsearch
node.name: easyenv-elasticsearch-escoordinating-2
node.master: false
node.data: false
node.ingest: true
node.ml: false
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
# --------------------------------- Discovery ----------------------------------
discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.unicast.hosts: ["10.10.10.1", "10.10.10.2", "10.10.10.3", "10.10.10.4", "10.10.10.5", "10.10.10.6", "10.10.10.7", "10.10.10.8"]
# ---------------------------------- Secure -----------------------------------
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.http.ssl.truststore.path: certs/elastic-certificates.p12
最后,我们在每个 Coordinating 节点所在的机器上再部署一个 Kibana 实例吧,配置如下:
server.port: 5601
server.host: "0.0.0.0"
server.name: "kibana"
server.basePath: "/kibana"
server.rewriteBasePath: true
elasticsearch.hosts: ["https://10.10.10.1:9200", "http://10.10.10.2:9200"]
elasticsearch.username: "kibana_system"
elasticsearch.password: "kibana_system"
elasticsearch.ssl.certificateAuthorities: [ "/home/work/kibana-7.14.0-linux-x86_64/config/certs/elastic-ca.pem" ]
elasticsearch.ssl.verificationMode: certificate
i18n.locale: "zh-CN"
此外,我们其实还可以再部署一个 Nginx 作为统一的流量入口和负载均衡,其中 Nginx 的配置如下:
user work;
worker_processes 48;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream kibana {
ip_hash;
server 10.10.10.1:5601;
server 10.10.10.2:5601;
}
server {
client_max_body_size 3000m;
listen 80;
location /kibana {
proxy_pass http://kibana;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
}
}
}