(1)业务发展越来越庞⼤,服务器越来越多;
(2)各种访问⽇志、应⽤⽇志、错误⽇志量越来越多,导致运维⼈员⽆法很好的去管理⽇志;
(3)开发⼈员排查问题,需要到服务器上查⽇志,不⽅便;
(4)运营⼈员需要⼀些数据,需要我们运维到服务器上分析⽇志。
如果我们查看某些服务为什么错误,可以直接使⽤grep等命令进⾏查看,可是如果我们查看规模 较⼤,⽇志较多的时候,此⽅法效率就⼩了很多。现在我们对待⼤规模的⽇志,解决思路是建⽴ 集中式⽇志收集系统,将所有节点上的⽇志统⼀收集,管理,访问。 ⼀个完整的集中式⽇志系统,需要包含以下⼏个主要特点: l 收集:能够采集多种来源的⽇志数据。 l 传输:能够稳定的把⽇志数据传输到中央系统。 l 存储:如何存储⽇志数据。 l 分析:可以⽀持 UI 分析。 l 警告:能够提供错误报告,监控机制。 ⽽ELK则提供⼀整套的解决⽅案,并且都是开源软件,之间相互配合,完美衔接,⾼效的满⾜了 很多场合的应⽤。
1、ELK是3个开源软件的缩写,分别为Elasticsearch 、 Logstash和Kibana , 它们都是开源软 件。不过现在还新增了⼀个Beats,它是⼀个轻量级的⽇志收集处理⼯具(Agent),Beats占⽤ 资源少,适合于在各个服务器上搜集⽇志后传输给Logstash,官⽅也推荐此⼯具,⽬前由于原本 的ELK Stack成员中加⼊了Beats⼯具所以已改名为Elastic Stack。
2、Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据3⼤功能。它的特点有: 分布式,零配置,⾃动发现,索引⾃动分⽚,索引副本机制,restful⻛格接⼝,多数据源,⾃动 搜索负载等。
3、Logstash主要是⽤来⽇志的搜集、分析、过滤⽇志的⼯具,⽀持⼤量的数据获取⽅式。⼀般 ⼯作⽅式为c/s架构,Client端安装在需要收集⽇志的主机上,server端负责将收到的各节点⽇志 进⾏过滤、修改等操作在⼀并发往Elasticsearch上去。
4、Kibana也是⼀个开源和免费的⼯具,Kibana可以为 Logstash和 ElasticSearch提供的⽇志分 析友好的 Web 界⾯,可以帮助汇总、分析和搜索重要数据⽇志。
5、Beats在这⾥是⼀个轻量级⽇志采集器,其实Beats家族有6个成员,早期的ELK架构中使⽤ Logstash收集、解析⽇志,但是Logstash对内存、CPU、io等资源消耗⽐较⾼。相⽐ Logstash,Beats所占系统的CPU和内存⼏乎可以忽略不计
IP | 节点划分 | 主机名称 |
---|---|---|
192.168.200.233 | Elasticsearch+Kibana | elk-1 |
192.168.200.234 | Elasticsearch+Logstash | elk-2 |
192.168.200.235 | Elasticsearch | elk-3 |
sudo hostname elk-1
sudo hostname elk-2
sudo hostname elk-3
systemctl stop firewalld
setenforce 0
[root@elk-1 ~]# vi /etc/hosts
[root@elk-1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.233 elk-1
192.168.200.233 elk-2
192.168.200.233 elk-3
###下载
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
###查看版本
java -version
[root@elk-1 ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.rpm
--2022-04-12 15:58:19-- https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.rpm
正在解析主机 artifacts.elastic.co (artifacts.elastic.co)... 34.120.127.130, 2600:1901:0:1d7::
正在连接 artifacts.elastic.co (artifacts.elastic.co)|34.120.127.130|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:27970243 (27M) [binary/octet-stream]
正在保存至: “elasticsearch-6.0.0.rpm”
100%[==========================================================>] 27,970,243 53.8KB/s 用时 5m 28s
2022-04-12 16:03:48 (83.2 KB/s) - 已保存 “elasticsearch-6.0.0.rpm” [27970243/27970243])
[root@elk-1 ~]# systemctl restart elasticsearch
Failed to restart elasticsearch.service: Unit not found.
[root@elk-1 ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1010/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1133/master
tcp6 0 0 :::22 :::* LISTEN 1010/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1133/master
[root@elk-1 ~]# ls
1.sh 2.sh anaconda-ks.cfg elasticsearch-6.0.0.rpm user_info
[root@elk-1 ~]# rpm -ivh elasticsearch-6.0.0.rpm
警告:elasticsearch-6.0.0.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中... ################################# [100%]
Creating elasticsearch group... OK
Creating elasticsearch user... OK
正在升级/安装...
1:elasticsearch-0:6.0.0-1 ################################# [100%]
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
sudo systemctl start elasticsearch.service
[root@elk-1 ~]#
[root@elk-1 ~]# vim /etc/elasticsearch/elasticsearch.yml
[root@ekl-1 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v ^#
cluster.name: ELK
node.name: elk-1
node.master: true
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.200.233
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-1","elk-2","elk-3"]
[root@elk-2 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v ^#
cluster.name: ELK
node.name: elk-2
node.master: true
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.200.234
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-1","elk-2","elk-3"]
[root@elk-3 ~]# cat /etc/elasticsearch/elasticsearch.yml |grep -v ^#
cluster.name: ELK
node.name: elk-3
node.master: true
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.200.235
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-1","elk-2","elk-3"]
[root@ekl-1 ~]# systemctl start elasticsearch
[root@ekl-1 ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1018/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1154/master
tcp6 0 0 192.168.200.233:9200 :::* LISTEN 12860/java
tcp6 0 0 192.168.200.233:9300 :::* LISTEN 12860/java
tcp6 0 0 :::22 :::* LISTEN 1018/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1154/master
[root@elk-1 ~]# curl '111.111.0.134:9200/_cluster/health?pretty'
{
"cluster_name" : "ELK",
"status" : "green",
//为green则代表健康没问题,yellow或者red 则是集群有问题
"timed_out" : false,
//是否有超时
"number_of_nodes" : 3,
//集群中的节点数量
"number_of_data_nodes" : 2,
//集群中data节点的数量
"active_primary_shards" : 1,
"active_shards" : 2,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
[root@elk-1 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-x86_64.rpm
--2022-04-12 16:33:44-- https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-x86_64.rpm
正在解析主机 artifacts.elastic.co (artifacts.elastic.co)... 34.120.127.130, 2600:1901:0:1d7::
正在连接 artifacts.elastic.co (artifacts.elastic.co)|34.120.127.130|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:63979183 (61M) [binary/octet-stream]
正在保存至: “kibana-6.0.0-x86_64.rpm”
100%[==========================================================>] 63,979,183 6.13MB/s 用时 14s
2022-04-12 16:33:58 (4.50 MB/s) - 已保存 “kibana-6.0.0-x86_64.rpm” [63979183/63979183])
[root@elk-1 ~]# rpm -ivh kibana-6.0.0-x86_64.rpm
警告:kibana-6.0.0-x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:kibana-6.0.0-1
[root@ekl-1 ~]# cat /etc/kibana/kibana.yml |grep -v ^#
server.port: 5601
server.host: 192.168.200.233
elasticsearch.url: "http://192.168.200.233:9200"
[root@ekl-1 ~]# systemctl start kibana
[root@ekl-1 ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.200.233:5601 0.0.0.0:* LISTEN 11877/node
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1018/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1154/master
tcp6 0 0 192.168.200.233:9200 :::* LISTEN 12860/java
tcp6 0 0 192.168.200.233:9300 :::* LISTEN 12860/java
tcp6 0 0 :::22 :::* LISTEN 1018/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1154/master
[root@elk-2 ~]# wget https://artifacts.elastic.co/downloads/logstash/logstash-6.0.0.rpm
--2022-04-12 17:39:11-- https://artifacts.elastic.co/downloads/logstash/logstash-6.0.0.rpm
正在解析主机 artifacts.elastic.co (artifacts.elastic.co)... 34.120.127.130, 2600:1901:0:1d7::
正在连接 artifacts.elastic.co (artifacts.elastic.co)|34.120.127.130|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:113288712 (108M) [binary/octet-stream]
正在保存至: “logstash-6.0.0.rpm”
100%[==========================================================>] 113,288,712 8.70MB/s 用时 13s
2022-04-12 17:39:26 (8.21 MB/s) - 已保存 “logstash-6.0.0.rpm” [113288712/113288712])
[root@elk-2 ~]# rpm -ivh logstash-6.0.0.0.rpm
warning: logstash-6.0.0.0.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing... ################################# [100%]
Updating / installing1:logstash-1:6.0.0-1 ################################# [100%]
Using provided startup.options file: /etc/logstash/startup.options
[root@elk-2 ~]# vi /etc/logstash/logstash.yml
http.host: "111.111.1.137"
###增加权限
[root@elk-2 ~]# chmod 644 /var/log/messages
[root@elk-2 ~]# chown -R logstash:logstash /var/log/logstash
###配置logstash收集syslog⽇志
[root@elk-2 ~]# cat /etc/logstash/conf.d/syslog.conf
input {
file {
path => "/var/log/messages"
type => "systemlog"
start_position => "beginning"
stat_interval => "3"
}
}
output {
elasticsearch {
hosts =>
["192.168.200.233:9200","192.168.200.234:9200","192.168.200.235:9200"]
index => "system-log-%{+YYYY.MM.dd}"
}
}
[root@elk-2 ~]# systemctl start logstash
配置完成后,选择Discover,进⼊“Discover”⻚⾯后,如果出现以下提示,则是代表⽆法查找到 ⽇志信息:
[root@elk-3 ~]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.0.0-
x86_64.rpm
[root@elk-3 ~]# rpm -ivh filebeat-6.0.0-x86_64.rpm
[root@elk-3 ~]# vim /etc/filebeat/filebeat.yml
filebeat.prospectors:
enabled: true
paths:
- /var/log/elasticsearch/elk.log //此处可⾃⾏改为想要监听的⽇志⽂件
output.elasticsearch:
hosts: ["elk-2:9200","elk-1:9200","elk-3:9200"]
[root@elk-3 ~]# systemctl start filebeat
[root@elk-3 ~]# curl 'elk1:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana yezntEXpQiOmYcgzo2yU2Q 1 1 2 0 14.3kb 14.3kb
yellow open filebeat-6.0.0-2022.04.25 DMk3qNSAQPyF88DVAePXzA 3 1 101 0 46.3kb 46.3kb
yellow open filebeat-6.0.0-2022.05.06 Qop9MkQyTvWWzBJPFI_LLw 3 1 0 0 233b 233b