Elasticsearch 是一个开源的分布式搜索和分析引擎,用于全文搜索、结构化搜索、分析和可视化大规模数据。它被设计成一个可扩展的实时搜索引擎,能够处理大规模数据,并提供快速的搜索和分析功能。
以下是一些 Elasticsearch 的特性和用途:
总的来说,Elasticsearch是一个功能强大且灵活的搜索和分析引擎,适用于各种场景,包括日志分析、实时监控、全文搜索等。
本例将在3台机器上搭建 Elasticsearch 集群:
192.168.72.151 node-1
192.168.72.152 node-2
192.168.72.153 node-3
下载并安装公共签名密钥
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在 /etc/yum.repos.d/ 创建 elasticsearch.repo
[elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md
执行 yum install 命令
yum install --enablerepo=elasticsearch elasticsearch -y
默认生成的目录
安装目录:/usr/share/elasticsearch
配置文件目录:/etc/elasticsearch
启动 Elasticsearch 命令:
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
首次启动 Elasticsearch 时,会为用户生成密码,并自动为用户配置 TLS ,可以随时调整 TLS 配置,更新节点证书
1、在任意节点上进入 Elasticsearch 的安装目录,使用 elasticsearch-certutil 为集群生成 CA 。
bin/elasticsearch-certutil ca
2、利用上一步的 elastic-stack-ca.p12 为集群生成证书和私钥。
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
3、将证书拷贝到其他节点上。
1、进入 Elasticsearch 配置目录,编辑 elasticsearch.conf 文件:
cluster.name: my-cluster
node.name: node-1
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
2、执行以下命令将证书密码保存到 Elasticsearch 的 keystore
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
3、为集群中的每个节点完成前面的步骤。
4、在集群的每个节点上,重新启动 Elasticsearch 。
1、 在任意单个节点上,从安装 Elasticsearch 的目录中,运行Elasticsearch HTTP 证书工具生成证书签名请求(CSR)。
bin/elasticsearch-certutil http
该命令生成一个 .zip 文件,其中包含 Elasticsearch 和 Kibana 使用的证书和密钥。每个文件夹都包含一个 README.txt ,说明如何使用这些文件。
2、解压缩生成的 elasticsearch-ssl-http.zip 文件。这个压缩文件包含用于Elasticsearch和Kibana的目录。
/elasticsearch
|_ README.txt
|_ http.p12
|_ sample-elasticsearch.yml
/kibana
|_ README.txt
|_ elasticsearch-ca.pem
|_ sample-kibana.yml
3、在集群中的每个节点上,完成以下步骤:
复制上面 elasticsearch 文件夹中的 http.p12 到 Elasticsearch 的配置目录下。
编辑 elasticsearch.yml ,启用 HTTPS 安全性,并指定 http.12 文件的位置。
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: http.p12
将私钥密码添加到 Elasticsearch 的安全设置中。
bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
4、重新启动 Elasticsearch
3个节点的配置文件除了ip、节点名之外,都一样
cluster.name: my-clusters
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
indices.memory.index_buffer_size: 20%
indices.memory.min_index_buffer_size: 96mb
thread_pool:
search:
size: 32
analyze:
size: 30
queue_size: 1000
indices.requests.cache.size: 2%
indices.queries.cache.size: 10%
indices.fielddata.cache.size: 20%
indices.breaker.fielddata.limit: 40%
network.host: 0.0.0.0
http.port: 9200
transport.port: 9300
transport.compress: true
http.max_content_length: 100mb
discovery.seed_hosts: ["192.168.72.151", "192.168.72.152","192.168.72.153"]
cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
cluster.fault_detection.leader_check.interval: 5s
discovery.cluster_formation_warning_timeout: 10s
cluster.publish.timeout: 30s
cluster.routing.allocation.node_initial_primaries_recoveries: 16
cluster.routing.allocation.node_concurrent_recoveries: 8
indices.recovery.max_bytes_per_sec: 125mb
集群默认是通过9300端口连接的,记得防火墙还要开通9300
依次启动各个节点
service elasticsearch start
设置密码
bin/elasticsearch-setup-passwords interactive
检查集群状态
curl -XGET --user elastic:password http://192.168.72.151:9200/_cluster/health?pretty
也可以在浏览器直接访问
请注意,上述步骤中的IP地址、端口和目录路径应根据你的实际环境进行调整。