ElasticStack早期名称为elk。
elk分别代表了3个组件:
- ElasticSearch
负责数据存储和检索。
- Logstash:
负责数据的采集,将源数据采集到ElasticSearch进行存储。
- Kibana:
负责数据的展示。
由于Logstash是一个重量级产品,安装包超过300MB+,很多同学只是用于采集日志,
于是使用其他采集工具代替,比如flume,fluentd等产品替代。
后来elastic公司也发现了这个问题,于是开发了一堆beats产品,其中典型代表就是Filebeat,metricbeat,heartbeat等。
而后,对于安全而言,又推出了xpack等相关组件,以及云环境的组件。
后期名称命名为elk stack,后来公司为了宣传ElasticStack
下面是我用的测试服务器及其操作系统:
环境准备:
主机名及IP地址
10.0.0.91 elk91
10.0.0.92 elk92
10.0.0.93 elk93
硬件配置
CPU: 2core+
Memory: 4GB+
Disk: 50GB+
系统要求
Ubuntu 22.04 LTS
网卡环境
单网卡
二进制部署ES单点环境
1.ElasticSearch的版本选择及下载
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.28-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512
[root@elk91 ~]# shasum -a 512 -c elasticsearch-7.17.28-linux-x86_64.tar.gz.sha512
elasticsearch-7.17.28-linux-x86_64.tar.gz: OK
[root@elk91 ~]#
2.解压软件包
[root@elk91 ~]# tar xf elasticsearch-7.17.28-linux-x86_64.tar.gz -C /usr/local/
3.修改配置文件
[root@elk91 ~]# vim /usr/local/elasticsearch-7.17.28/config/elasticsearch.yml
[root@elk91 ~]#
[root@elk91 ~]# egrep -v "^#|^$" /usr/local/elasticsearch-7.17.28/config/elasticsearch.yml
cluster.name: oldboyedu-linux96
path.data: /var/lib/es7
path.logs: /var/log/es7
network.host: 0.0.0.0
discovery.type: single-node
[root@elk91 ~]#
相关参数说明:
cluster.name
集群的名称
path.data
ES的数据存储路径。
path.logs
ES的日志存储路径。
network.host
ES服务监听的地址。
discovery.type
指的ES集群的部署类型,此处的"single-node",表示的是一个单点环境。
4.创建用户并授权
[root@elk91 ~]# useradd -m oldboyedu
[root@elk91 ~]# chown oldboyedu:oldboyedu -R /usr/local/elasticsearch-7.17.28/
[root@elk91 ~]# ll /usr/local/elasticsearch-7.17.28/jdk/bin -d
drwxr-x--- 2 oldboyedu oldboyedu 4096 Feb 20 17:09 /usr/local/elasticsearch-7.17.28/jdk/bin/
5.创建数据目录和日志目录
[root@elk91 ~]# install -d /var/{lib,log}/es7 -o oldboyedu -g oldboyedu (创建目录 并修改属主属组为 oldboyedu)
[root@elk91 ~]#
[root@elk91 ~]# ll -d /var/{lib,log}/es7
drwxr-xr-x 2 oldboyedu oldboyedu 4096 Mar 11 11:07 /var/lib/es7/
drwxr-xr-x 2 oldboyedu oldboyedu 4096 Mar 11 10:59 /var/log/es7/
[root@elk91 ~]#
6.启动ElasticSearch服务
[root@elk91 ~]# su - oldboyedu -c '/usr/local/elasticsearch-7.17.28/bin/elasticsearch -d' #-c 表示不进去,在oldboyedu执行输入命令
[root@elk91 ~]#
[root@elk91 ~]# ss -ntl | egrep "9[2|3]00" #查询端口
LISTEN 0 4096 *:9300 *:*
LISTEN 0 4096 *:9200 *:*
[root@elk91 ~]#
7.查看服务验证(此服务需要查询端口和访问页面内容确认服务情况)
[root@elk93 ~]# curl http://10.0.0.91:9200
{
"name" : "elk91",
"cluster_name" : "oldboyedu-linux96",
"cluster_uuid" : "f8m0l-IiQAG1ZHKFjQgqFA",
"version" : {
"number" : "7.17.28",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "139cb5a961d8de68b8e02c45cc47f5289a3623af",
"build_date" : "2025-02-20T09:05:31.349013687Z",
"build_snapshot" : false,
"lucene_version" : "8.11.3",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
[root@elk93 ~]#
[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/nodes
10.0.0.91 34 97 7 0.11 0.09 0.03 cdfhilmrstw * elk91
[root@elk93 ~]#
卸载二进制的ES程序
1.停止服务
kill `ps -ef | grep java | grep -v grep | awk '{print $2}'`
2.删除程序,数据,日志目录
[root@elk91 ~]# rm -rf /usr/local/elasticsearch-7.17.28/ /var/{lib,log}/es7/
3.移除普通用户
[root@elk91 ~]# userdel -r oldboyedu
基于deb包安装ES单点
1.下载软件包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.28-amd64.deb
svip:
[root@elk91 ~]# wget http://192.168.16.253/Resources/ElasticStack/softwares/7.17.28/elasticsearch-7.17.28-amd64.deb
2.安装es
[root@elk91 ~]# dpkg -i elasticsearch-7.17.28-amd64.deb
3.添加别名
[root@elk91 ~]# tail -1 .bashrc
alias yy='egrep -v "^#|^$"'
[root@elk91 ~]#
[root@elk91 ~]# source .bashrc
[root@elk91 ~]#
4.修改配置文件
[root@elk91 ~]# yy /etc/elasticsearch/elasticsearch.yml
cluster.name: oldboyedu-linux96
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node
[root@elk91 ~]#
5.启动ES服务
[root@elk91 ~]# systemctl enable --now elasticsearch.service
[root@elk91 ~]#
[root@elk91 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0 4096 *:9300 *:*
LISTEN 0 4096 *:9200 *:*
[root@elk91 ~]#
6.访问ES的WebUI
[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/nodes
10.0.0.91 17 97 0 0.20 0.09 0.03 cdfhilmrstw * elk91
[root@elk93 ~]#
[root@elk93 ~]# curl http://10.0.0.91:9200/
{
"name" : "elk91",
"cluster_name" : "oldboyedu-linux96",
"cluster_uuid" : "h5Eru0tzSKmIHxgyw2q3rg",
"version" : {
"number" : "7.17.28",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "139cb5a961d8de68b8e02c45cc47f5289a3623af",
"build_date" : "2025-02-20T09:05:31.349013687Z",
"build_snapshot" : false,
"lucene_version" : "8.11.3",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
[root@elk93 ~]#
- 索引: Index
用户进行数据的读写单元。
- 分片: Shard
一个索引至少要有一个分片,如果一个索引仅有一个分片,意味着该索引的数据只能全量存储在某个节点上,且分片是不可拆分的,隶属于某个节点。
换句话说,分片是ES集群最小的调度单元。
一个索引数据也可以被分散的存储在不同的分片上,且这些分片可以放在不同的节点,从而实现数据的分布式存储。
- 副本: replica
副本是针对分片来说的,一个分片可以有0个或多个副本。
当副本数量为0时,意味着只有主分片(priamry shard),当主分片所在的节点宕机时,数据就无法访问了。
当副本数量大于0时,意味着同时存在主分片和副本分片(replica shard):
- 主分片负责数据的读写(read write,rw)
- 副本分片负责数据的读的负载均衡(read only,ro)
- 文档: document:
指的是用户存储的数据。其中包含元数据和源数据。
- 元数据:
用于描述源数据的数据。
- 源数据:
用户实际存储的数据。
- 分配: allocation
指的是将索引的不同分片(包含主分片和副本分片)分配到整个集群的过程。
查看集群状态:
[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1741676068 06:54:28 oldboyedu-linux96 green 1 1 3 3 0 0 0 0 - 100.0%
[root@elk93 ~]#
- ES集群环境部署
1.停止单点服务
[root@elk91 ~]# systemctl stop elasticsearch.service
[root@elk91 ~]#
2.拷贝软件包
[root@elk91 ~]# scp elasticsearch-7.17.28-amd64.deb 10.0.0.92:~
[root@elk91 ~]# scp elasticsearch-7.17.28-amd64.deb 10.0.0.93:~
3.其他节点安装ES环境
[root@elk92 ~]# dpkg -i elasticsearch-7.17.28-amd64.deb
[root@elk93 ~]# dpkg -i elasticsearch-7.17.28-amd64.deb
4.修改ES集群配置文件
[root@elk91 ~]# vim /etc/elasticsearch/elasticsearch.yml
[root@elk91 ~]#
[root@elk91 ~]# yy /etc/elasticsearch/elasticsearch.yml
cluster.name: oldboyedu-linux96
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.91", "10.0.0.92","10.0.0.93"]
cluster.initial_master_nodes: ["10.0.0.91","10.0.0.92","10.0.0.93"]
[root@elk91 ~]#
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.92:/etc/elasticsearch/
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.93:/etc/elasticsearch/
5.所有节点同时启动ES服务
[root@elk91 ~]# systemctl enable --now elasticsearch.service
[root@elk91 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0 4096 *:9300 *:*
LISTEN 0 4096 *:9200 *:*
[root@elk91 ~]#
[root@elk92 ~]# systemctl enable --now elasticsearch.service
[root@elk92 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0 4096 *:9300 *:*
LISTEN 0 4096 *:9200 *:*
[root@elk92 ~]#
[root@elk93 ~]# systemctl enable --now elasticsearch.service
[root@elk93 ~]# ss -ntl | egrep "9[2|3]00"
LISTEN 0 4096 *:9300 *:*
LISTEN 0 4096 *:9200 *:*
[root@elk93 ~]#
6.检查集群是否正常工作
[root@elk93 ~]# curl http://10.0.0.91:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.0.0.93 27 97 0 0.28 0.17 0.06 cdfhilmrstw - elk93
10.0.0.91 8 97 0 0.23 0.14 0.05 cdfhilmrstw * elk91
10.0.0.92 27 97 4 0.80 0.33 0.12 cdfhilmrstw - elk92
[root@elk93 ~]#
错误案例:也是我之前遇到的问题,因为是测试,就直接删除了,
快速部署校验ES集群解决方案
报错信息: 集群缺少master
[root@elk3 ~]# curl http://10.0.0.92:9200/_cat/nodes?v
{"error":{"root_cause":[{"type":"master_not_discovered_exception","reason":null}],"type":"master_not_discovered_exception","reason":null},"status":503}
[root@elk3 ~]# curl 10.0.0.91:9200
{
"name" : "elk91",
"cluster_name" : "oldboyedu-linux96",
"cluster_uuid" : "_na_",
...
}
[root@elk3 ~]#
[root@elk3 ~]# curl 10.0.0.92:9200
{
"name" : "elk2",
"cluster_name" : "oldboyedu-linux96",
"cluster_uuid" : "_na_",
...
}
[root@elk3 ~]#
[root@elk3 ~]#
[root@elk3 ~]# curl 10.0.0.93:9200
{
"name" : "elk3",
"cluster_name" : "oldboyedu-linux96",
"cluster_uuid" : "_na_",
...
}
[root@elk3 ~]#
解决方案:
- 停止集群的ES服务
[root@elk91 ~]# systemctl stop elasticsearch.service
[root@elk92 ~]# systemctl stop elasticsearch.service
[root@elk93 ~]# systemctl stop elasticsearch.service
- 删除数据,日志,和临时数据
[root@elk91 ~]# rm -rf /var/{lib,log}/elasticsearch/* /tmp/*
[root@elk92 ~]# rm -rf /var/{lib,log}/elasticsearch/* /tmp/*
[root@elk93 ~]# rm -rf /var/{lib,log}/elasticsearch/* /tmp/*
- 检查集群各节点的配置
[root@elk91 ~]# yy /etc/elasticsearch/elasticsearch.yml
cluster.name: oldboyedu-linux96
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.91", "10.0.0.92","10.0.0.93"]
cluster.initial_master_nodes: ["10.0.0.91","10.0.0.92","10.0.0.93"]
[root@elk91 ~]#
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.92:/etc/elasticsearch/
[root@elk91 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.93:/etc/elasticsearch/
- 启动ES服务
[root@elk91 ~]# systemctl restart elasticsearch.service
[root@elk92 ~]# systemctl restart elasticsearch.service
[root@elk93 ~]# systemctl restart elasticsearch.service
- 测试验证
[root@elk91 ~]# for i in `seq 91 93`; do curl -s 10.0.0.$i:9200 | grep cluster_uuid; done
"cluster_uuid" : "-5ly4d8-Tl6biIMmp4pzKw",
"cluster_uuid" : "-5ly4d8-Tl6biIMmp4pzKw",
"cluster_uuid" : "-5ly4d8-Tl6biIMmp4pzKw",
[root@elk91 ~]#
[root@elk91 ~]# curl 10.0.0.93:9200/_cat/nodes
10.0.0.91 11 94 4 0.43 0.36 0.29 cdfhilmrstw - elk91
10.0.0.93 11 96 2 0.47 0.43 0.28 cdfhilmrstw - elk93
10.0.0.92 5 97 3 0.32 0.44 0.33 cdfhilmrstw * elk92
[root@elk91 ~]#
- MySQL属于关系型数据库:
增删改查: 基于SQL
- ES属于文档型数据库,和MangoDB很相似
增删改查: DSL语句,属于ES独有的有种查询语言。
针对模糊查询,mysql无法充分利用索引,性能较低,而是用ES查询模糊数据,是非常高效的。
- ES的DSL语句初体验:
1.往ES集群添加单条数据
curl --location --request POST '10.0.0.91:9200/oldboyedu_linux/doc' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "孙悟空",
"hobby": [
"蟠桃",
"金丹",
"紫霞"
]
}'
2.往ES集群添加多条数据
curl --location --request POST 'http://10.0.0.91:9200/_bulk' \
--header 'Content-Type: application/json' \
--data-raw '{ "create" : { "_index" : "oldboyedu-linux96", "_id" : "1001" } }
{ "name" : "猪八戒","hobby": ["猴哥","高老庄"] }
{ "create" : { "_index" : "oldboyedu-linux96", "_id" : "1002" } }
{ "name" : "沙和尚","hobby": ["流沙河","挑行李"] }
{ "create" : { "_index" : "oldboyedu-linux96", "_id" : "1003" } }
{ "name" : "白龙马","hobby": ["大师兄,师傅被妖怪抓走啦"] }
'
3.查询单条数据
curl --location --request GET '10.0.0.93:9200/oldboyedu-linux96/_doc/1003'
4.过滤查询数据
curl --location --request GET '10.0.0.93:9200/oldboyedu-linux96/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {
"hobby": "猴哥"
}
}
}'
5.删除数据
curl --location --request DELETE '10.0.0.93:9200/oldboyedu-linux96/_doc/1003'
kibana是针对ES做的一款可视化工具。将来的操作都可以在ES中完成。
1.下载kibana
[root@elk91 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.28-amd64.deb
2.安装kibana
[root@elk91 ~]# dpkg -i kibana-7.17.28-amd64.deb
3.修改kibana的配置文件
[root@elk91 ~]# vim /etc/kibana/kibana.yml
[root@elk91 ~]#
[root@elk91 ~]# yy /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://10.0.0.91:9200","http://10.0.0.92:9200","http://10.0.0.93:9200"]
i18n.locale: "zh-CN"
[root@elk91 ~]#
相关参数说明:
server.port
kibana监听的端口。
server.host
kibana监听的IP地址。
elasticsearch.hosts
kibana管理的ES集群信息。
i18n.locale
安装时选择的语言。
4.启动kibana服务
[root@elk91 ~]# systemctl enable --now kibana.service
[root@elk91 ~]# ss -ntl | grep 5601
LISTEN 0 511 0.0.0.0:5601 0.0.0.0:*
[root@elk91 ~]#
5.访问kibana的WebUI
http://10.0.0.91:5601/
2.安装Filebeat
[root@elk92 ~]# dpkg -i filebeat-7.17.28-amd64.deb
3.编写Filebeat配置文件
[root@elk92 ~]# cat /etc/filebeat/config/01-log-to-console.yaml
# 定义数据从哪里来
filebeat.inputs:
# 指定数据源的类型是log,表示从文件读取数据
- type: log
# 指定文件的路径
paths:
- /tmp/student.log
# 定义数据到终端
output.console:
pretty: true
[root@elk92 ~]#
4.启动Filebeat实例
[root@elk92 ~]# filebeat -e -c /etc/filebeat/config/01-log-to-console.yaml
5.向源数据文件("/tmp/student.log")写入测试数据
略,见视频。
温馨提示:
- 1.filebeat默认是按行采集数据;
- 2.filebeat默认会在"/var/lib/filebeat"目录下记录已经采集的文件offset信息,以便于下一次采集接着该位置继续采集数据;
# 将数据写入到ES集群
output.elasticsearch:
hosts:
- 10.0.0.91:9200
- 10.0.0.92:9200
- 10.0.0.93:9200
[root@elk92 ~]#
2.启动Filebeat实例
[root@elk92 ~]# rm -rf /var/lib/filebeat/
[root@elk92 ~]# filebeat -e -c /etc/filebeat/config/02-log-to-es.yaml
3.数据流走向
echo ---> /tmp/student.log ---> filebeat ---> ES集群 ---> Kibana
- Filebeat写入ES集群自定义索引实战案例
1.编写配置文件
[root@elk92 ~]# cat /etc/filebeat/config/03-log-to-es-custom-index.yaml
filebeat.inputs:
- type: log
paths:
- /tmp/student.log
# 将数据写入到ES集群
output.elasticsearch:
hosts:
- 10.0.0.91:9200
- 10.0.0.92:9200
- 10.0.0.93:9200
# 指定索引的名称
index: oldboyedu-linux95-tmp-%{+yyyy.MM.dd}
# 禁用索引生命周期管理(index lifecycle management,ILM)
# 如果启用了此配置,则忽略自定义索引的所有信息
setup.ilm.enabled: false
# 定义索引模板(就是创建索引的规则)的名称
setup.template.name: "oldboyedu-linux95"
# 定义索引模板的匹配模式,表示当前索引模板针对哪些索引生效。
setup.template.pattern: "oldboyedu-linux95-*"
# 如果索引模板存在,是否覆盖,默认值为false,如果明确需要,则可以将其设置为ture。
# 但是官方建议将其设置为false,原因是每次写数据时,都会建立tcp链接,消耗资源。
setup.template.overwrite: true
# 定义索引模板的规则信息
setup.template.settings:
# 指定索引能够创建的分片的数量
index.number_of_shards: 5
# 指定每个分片有多少个副本
index.number_of_replicas: 0
[root@elk92 ~]#
2.启动Filebeat实例
[root@elk92 ~]# rm -rf /var/lib/filebeat/
[root@elk92 ~]# filebeat -e -c /etc/filebeat/config/03-log-to-es-custom-index.yaml
项目案例: filebeat采集nginx实战案例
1.安装nginx服务
[root@elk92 ~]# apt -y install nginx
2.启动nginx服务
[root@elk92 ~]# systemctl enable --now nginx
3.访问测试
[root@elk92 ~]# curl 10.0.0.92
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
4.查看日志信息
[root@elk92 ~]# cat /var/log/nginx/access.log
10.0.0.92 - - [11/Mar/2025:18:27:23 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.81.0"
[root@elk92 ~]#
5.使用Filebeat采集nginx日志
[root@elk92 ~]# cat /etc/filebeat/config/04-nginx-to-es.yaml
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log*
output.elasticsearch:
hosts:
- 10.0.0.91:9200
- 10.0.0.92:9200
- 10.0.0.93:9200
index: oldboyedu-linux95-nginx-%{+yyyy.MM.dd}
setup.ilm.enabled: false
setup.template.name: "oldboyedu-linux95"
setup.template.pattern: "oldboyedu-linux95-*"
setup.template.overwrite: true
setup.template.settings:
index.number_of_shards: 5
index.number_of_replicas: 0
[root@elk92 ~]#
6.启动Filebeat实例
[root@elk92 ~]# filebeat -e -c /etc/filebeat/config/04-nginx-to-es.yaml
7.kibana查看数据
8.发送测试数据
[root@elk93 ~]# for i in `seq 10`;do curl http://10.0.0.92;done
- filebeat多实例
1.启动实例1
filebeat -e -c /etc/filebeat/config/01-log-to-console.yaml --path.data /tmp/xixi
2.启动实例2
filebeat -e -c /etc/filebeat/config/02-log-to-es.yaml --path.data /tmp/haha