运维必备——ELK日志分析系统(下)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
日志服务 SLS,月写入数据量 50GB 1个月
简介: 运维必备——ELK日志分析系统(下)

4、安装elasticsearch-head插件——node1和node2


可以先等node1安装完这个插件之后,node2在安装,安装时过程较长,node2可以先做上面的步骤
—————————————————————————————————扩展———————————————————————————————————
elasticsearch在5.0版本之后,elasticsearch-head插件需要作为独立服务进行安装
需要使用npm安装,并且需要提前安装node和phantomjs
node:基于chrome V8引擎和javascript运行环境
phantomjs:基于webkit的javascriptAPI。这个可以理解为一个隐形的浏览器,可以做到webkit浏览器的作用
————————————————————————————————————————————————————————————————————————
******(1)编译安装node,需要四十分钟左右
[root@node1 ~]# ll (上传node和phantomjs的软件包)
总用量 122152
-rw-------. 1 root root     1264 1月  12 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 33396354 3月  30 22:08 elasticsearch-5.5.0.rpm
-rw-r--r--  1 root root 37926436 3月  30 22:08 elasticsearch-head.tar.gz
-rw-r--r--  1 root root 30334692 3月  30 23:13 node-v8.2.1.tar.gz
-rw-r--r--  1 root root 23415665 3月  30 23:13 phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node1 ~]# tar xf node-v8.2.1.tar.gz  
[root@node1 ~]# cd node-v8.2.1
[root@node1 node-v8.2.1]# ./configure && make && make install (时间较长,大约半小时左右)
******(2)安装phantomjs
[root@node1 ~]# ll
总用量 122156
-rw-------.  1 root root      1264 1月  12 18:27 anaconda-ks.cfg
-rw-r--r--   1 root root  33396354 3月  30 22:08 elasticsearch-5.5.0.rpm
-rw-r--r--   1 root root  37926436 3月  30 22:08 elasticsearch-head.tar.gz
drwxr-xr-x  10  502 games     4096 3月  30 23:45 node-v8.2.1
-rw-r--r--   1 root root  30334692 3月  30 23:13 node-v8.2.1.tar.gz
-rw-r--r--   1 root root  23415665 3月  30 23:13 phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node1 ~]# tar xf phantomjs-2.1.1-linux-x86_64.tar.bz2  -C /usr/src/
[root@node1 ~]# cp /usr/src/phantomjs-2.1.1-linux-x86_64/bin/phantomjs  /usr/local/bin/ 
******(3)安装elasticsearch-head
[root@node1 ~]# tar xf elasticsearch-head.tar.gz -C /usr/src/ (解压)
[root@node1 ~]# cd /usr/src/elasticsearch-head/
[root@node1 elasticsearch-head]# npm install (使用npm安装)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/karma/node_modules/chokidar/node_modules/fsevents):
npm WARN network SKIPPING OPTIONAL DEPENDENCY: request to https://registry.npmjs.org/fsevents failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
npm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expression
up to date in 2.707s
[root@node1 elasticsearch-head]# cd
******(4)修改elasticsearch主配置文件
[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
。。。。。。
末尾添加
http.cors.enabled: true       #开启跨域访问支持,默认为false,所以需要改成true
http.cors.allow-origin: "*"   #跨域访问允许所有的域名地址
保存退出
[root@node1 ~]# systemctl restart elasticsearch (重启服务)
******(5)启动elasticsearch-head服务
————————————————————注意!!!!—————————————————
此操作必须在解压的目录下启动,进程会读取目录中的gruntfile.js文件,否则可能会失败
监听端口为:9100
————————————————————————————————————————————————
[root@node1 ~]# cd /usr/src/elasticsearch-head/ (进入解压目录)
[root@node1 elasticsearch-head]# ll Gruntfile.js  (查看指定文件)
-rw-r--r-- 1 root root 2231 7月  27 2017 Gruntfile.js
[root@node1 elasticsearch-head]# npm start & (挂到后台启动)
[1] 61130
[root@node1 elasticsearch-head]# 
> elasticsearch-head@0.0.0 start /usr/src/elasticsearch-head
> grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
[root@node1 elasticsearch-head]# netstat -anpt | grep 9100 (检查是否成功启动)
tcp        0      0 0.0.0.0:9100            0.0.0.0:*               LISTEN      61140/grunt      
[root@node1 elasticsearch-head]# netstat -anpt | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      1154/java         


使用浏览器进行访问:http://192.168.100.1:9100

在最上面输入http://192.168.100.1:9200,点击连接,就可以看到node1节点了


20210330171754344.png

******(6)插入索引,进行测试,索引名称为index-demo,类型为test
[root@node1 elasticsearch-head]# curl -XPUT '192.168.100.1:9200/index-demo/test/1?pretty&pretty' -H 'Content-Type:application/json' -d'{"aaa":"bbb","ccc":"ddd"}'
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
———————————————————选项——————————————————————
-X 指定命令,默认的命令是get
-P 通过ftp进行传输
-U 指定上传用户和密码
-T 指定上传
-H http协议的头部信息
-d 提交的数据信息
—————————————————————————————————————————————


使用浏览器查看

点击数据浏览,就会看到添加的索引


20210330172700751.png

这个0-4就是五个分片



20210330191424644.png

5、node2配置——安装logstash


Logstash使用管道方式进行日志的搜集处理和输出,有点像linux系统中的管道符"|",即执行完一个执行下一个


在Logstash中,包括了三个阶段:

输入input——处理filter(不是必须的)——输出output


这三个阶段都会有很多的插件来配置,比如:file、elasticsearch、redis等

每个阶段也可以指定多种方式,比如输出既可以输出到elasticsearch中,也可以指定到stdout在控制台打印,由于这种插件式的组织方式,使得logstash变得易于扩展和定制


logstash常用命令:


-f 通过这个命令可以指定logstash的配置文件,根据配置文件来配置logstash
-e 后面跟着字符串,该字符串可以被当作logstash的配置,如果是""则默认使用stdin作为输入,stdout作为输出
-t 测试配置文件是否正确,然后退出
******(1)安装logstash,安装这个需要java环境,之前两台node已经装过了
[root@node2 ~]# ll (上传logstash的rpm包)
总用量 161612
-rw-------. 1 root root     1264 1月  12 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 33396354 3月  30 22:08 elasticsearch-5.5.0.rpm
-rw-r--r--  1 root root 37926436 3月  30 22:08 elasticsearch-head.tar.gz
-rw-r--r--  1 root root 94158545 3月  30 23:17 logstash-5.5.1.rpm
[root@node2 ~]# rpm -ivh logstash-5.5.1.rpm  (使用rpm安装)
警告:logstash-5.5.1.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:logstash-1:5.5.1-1               ################################# [100%]
Using provided startup.options file: /etc/logstash/startup.options
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Successfully created system startup script for Logstash
[root@node2 ~]# systemctl start logstash.service (开启服务)
[root@node2 ~]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/ (添加软连接,优化命令执行路径)
******(2)输入采用标准输入,输出采用标准输出
[root@node2 ~]# logstash -e 'input{stdin{}}output{stdout{}}' 
。。。。。。
等待一段时间,当下面这段文字出现后再进行操作
The stdin plugin is now waiting for input:
01:47:27.213 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com  (手动输入)
2021-03-30T17:51:24.294Z node2 www.baidu.com (这个就是标准输出)
www.sina.com   (手动输入)
2021-03-30T17:51:28.962Z node2 www.sina.com  (标准输出)
Ctrl+C退出
******(3)使用rubydebug显示详细输出,dodec是一种编码器
[root@node2 ~]# logstash -e 'input { stdin{} } output { stdout { codec=>rubydebug } }'
。。。。。。
再次等待一段时间,出现下面字段后进行操作
01:56:30.916 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com (标准输入)
{
    "@timestamp" => 2021-03-30T17:56:36.133Z,  (这个就是处理过后的输出)
      "@version" => "1",
          "host" => "node2",
       "message" => "www.baidu.com"
}
Ctrl+C退出
******(4)使用logstash将信息写入到elasticsearch中,并且远程主机192.168.100.1:9200
[root@node2 ~]# [root@node2 ~]# logstash -e 'input { stdin{} } output {  elasticsrch { hosts=>["192.168.100.1:9200"]  } }'
。。。。。。
02:01:27.882 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com (三个标准输入,会发现没有进行输出,这是因为发送到了elasticsearch中了)
www.sina.com
www.aaa.com


浏览http://192.168.100.1:9100查看结果

点击数据浏览,就会看到

20210330180503461.png


******(5)编辑logstash配置文件,收集系统日志,输出到elasticsearch中
————————————————————————————————华丽分割线————————————————————————————————————
logstash配置文件基本上是由三部分组成的,input、output以及用户需要时才添加的filter
标准的配置文件格式为:
input{。。。。}
filter{。。。。}
output{。。。。}
在每个部分中,可以指定多个访问方式,例如:想要指定两个日志来源文件,那么就可以这样写,指定多个也是这种格式即可
input {
 file { path => "/var/log/messages" type => "sylog" }
 file { path => "/var/log/apache/access.log" type => "apache" }
} 
—————————————————————————————————————————————————————————————————————————————
[root@node2 ~]# ll /var/log/messages 
-rw-------. 1 root root 1184764 3月  31 02:06 /var/log/messages
[root@node2 ~]# chmod o+r /var/log/messages (给系统日志添加其他用户的只读权限) 
[root@node2 ~]# ll /var/log/messages  
-rw----r--. 1 root root 1185887 3月  31 02:07 /var/log/messages
[root@node2 ~]# ls /etc/logstash/conf.d/
[root@node2 ~]# vim /etc/logstash/conf.d/system.conf (编写一个新文件)
input {
        file {   #从文件中读取
           path => "/var/log/messages" #文件路径
           type => "system"
           start_position => "beginning" #是否从头开始读取
        }
}
output {
        elasticsearch {  #输出到elasticsearch中
          hosts => ["192.168.100.1:9200"] #指定elasticsearch的主机地址和端口
          index => "system-%{+YYYY.MM.dd}" #索引名称,这个是日期
        }
}
保存退出
[root@node2 ~]# systemctl restart logstash (重启服务)

浏览器访问http://192.168.100.1:9100进行验证,查看是否有系统日志存在


20210330182002811.png


6、node1安装Kibana

******(1)使用rpm方式安装kibana
[root@node1 ~]# ll kibana-5.5.1-x86_64.rpm  (上传rpm包)
-rw-r--r-- 1 root root 52255853 3月  31 02:22 kibana-5.5.1-x86_64.rpm
[root@node1 ~]# rpm -ivh kibana-5.5.1-x86_64.rpm 
。。。。。。
[root@node1 ~]# systemctl enable kibana (设置为开机自启)
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.
******(2)配置kibana主配置文件
[root@node1 ~]# vim /etc/kibana/kibana.yml 
。。。。。。
  2 server.port: 5601  #打开kibana的端口
  3 
。。。。。。
  7 server.host: "0.0.0.0"  #kibana侦听的地址,0.0.0.0为侦听所有
  8 
。。。。。。
 21 elasticsearch.url: "http://192.168.100.1:9200" #和elasticsearch的主机建立连接,指定地址和端口
 22 
。。。。。。
 30 kibana.index: ".kibana"  #再elasticsearch中添加.kibana索引
 31 
。。。。。。
保存退出
******(3)启动Kibana服务
[root@node1 ~]# systemctl start kibana
[root@node1 ~]# netstat -anpt | grep 5601
tcp        0      0 0.0.0.0:5601            0.0.0.0:*               LISTEN      1373/node  

浏览器访问http://192.168.100.1:5601,进入kibana控制台


20210330182937648.png


第一次登录需要添加一个elasticsearch索引,添加前面的索引即可

依次点击:

management——index patterns——create index pattern

index name or pattern选项是索引名称,随便起就行,我这里添加system*

time filter field name选项是设置时间过滤,选择I don't want to use the time filter

填写完后点击Create

20210330183207434.png

点击Discover进行查看


20210330183504376.png


可以点击这些选项进行筛选,想要筛选什么就点击该选项的”add“即可,然后会进行筛选


20210330184226647.png


如果添加后想要取消这个筛选条件,点击指定选项的×号即可



20210330184327292.png



三、扩展——添加apache日志


要求:将apache服务器的日志添加到elasticsearch并且通过kibana显示

添加一个主机,用于安装apache


apache 192.168.100.3 安装httpd和logstash

-步骤

******(1)先做基础配置
[root@Centos7 ~]# hostnamectl set-hostname apache
[root@Centos7 ~]# su
[root@apache ~]# systemctl stop firewalld
[root@apache ~]# setenforce 0
setenforce: SELinux is disabled
[root@apache ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
       /dev/sr0 已经挂载到 /mnt 上
******(2)安装摇晃httpd,并且写一个网页
[root@apache ~]# yum -y install httpd
。。。。。。
完毕!
[root@apache ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@apache ~]# systemctl start httpd
[root@apache ~]# echo "<h1>aaaaaaa</h1> " > /var/www/html/index.html
******(3)安装java
[root@apache ~]# yum -y install java
。。。。。。
完毕!
[root@apache ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
******(4)上传logstash的rpm包,进行安装、配置
[root@apache ~]# ls
anaconda-ks.cfg  logstash-5.5.1.rpm
[root@apache ~]# rpm -ivh logstash-5.5.1.rpm  
[root@apache ~]# systemctl daemon-reload
[root@apache ~]# systemctl enable logstash.service
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.
[root@apache ~]# vim /etc/logstash/conf.d/apache_log.conf
input {
        file {
          path => "/etc/httpd/logs/access_log"
          type => "access"
          start_position => "beginning"
        }
        file {
          path => "/etc/httpd/logs/error_log"
          type => "error"
          start_position => "beginning"
        }   
}
output {
        if [type] == "access" {
          elasticsearch {
                hosts => ["192.168.100.1:9200"]
                index => "apache_access-%{+YYYY.MM.dd}"
          } 
        }
        if [type] == "error" {
          elasticsearch {
                hosts => ["192.168.100.1:9200"]
                index => "apache_error-%{+YYYY.MM.dd}"
          }
        }
}
保存退出
[root@apache ~]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
[root@apache ~]# logstash -f /etc/logstash/conf.d/apache_log.conf 
。。。。。。
03:07:07.932 [Api Webserver] INFO  logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.aaaa.com  (随便输入几个域名)
www.bbbbb.com
abc.www.com

-验证


登录kibana,访问http://192.168.100.1:9100查看索引是否存在


2021033019090910.png


登录kibana界面添加索引,访问http://192.168.100.1:5601

和上面相同的方法,添加一个新的apache索引

20210330191102976.png

20210330191221275.png


至此,ELK日志分析平台搭建完成!!!!!

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
19天前
|
存储 数据采集 数据处理
【Flume拓扑揭秘】掌握Flume的四大常用结构,构建强大的日志收集系统!
【8月更文挑战第24天】Apache Flume是一个强大的工具,专为大规模日志数据的收集、聚合及传输设计。其核心架构包括源(Source)、通道(Channel)与接收器(Sink)。Flume支持多样化的拓扑结构以适应不同需求,包括单层、扇入(Fan-in)、扇出(Fan-out)及复杂多层拓扑。单层拓扑简单直观,适用于单一数据流场景;扇入结构集中处理多源头数据;扇出结构则实现数据多目的地分发;复杂多层拓扑提供高度灵活性,适合多层次数据处理。通过灵活配置,Flume能够高效构建各种规模的数据收集系统。
26 0
|
20天前
|
存储 消息中间件 人工智能
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
早期 MiniMax 基于 Grafana Loki 构建了日志系统,在资源消耗、写入性能及系统稳定性上都面临巨大的挑战。为此 MiniMax 开始寻找全新的日志系统方案,并基于阿里云数据库 SelectDB 版内核 Apache Doris 升级了日志系统,新系统已接入 MiniMax 内部所有业务线日志数据,数据规模为 PB 级, 整体可用性达到 99.9% 以上,10 亿级日志数据的检索速度可实现秒级响应。
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
|
3天前
|
存储 弹性计算 运维
自动化监控和响应ECS系统事件
阿里云提供的ECS系统事件用于记录云资源信息,如实例启停、到期通知等。为实现自动化运维,如故障处理与动态调度,可使用云助手插件`ecs-tool-event`。该插件定时获取并转化ECS事件为日志存储,便于监控与响应,无需额外开发,适用于大规模集群管理。详情及示例可见链接文档。
|
19天前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
8天前
|
JSON 缓存 fastjson
一行日志引发的系统异常
本文记录了一行日志引发的系统异常以及作者解决问题的思路。
|
12天前
|
运维 监控 应用服务中间件
自动化运维:打造高效、稳定的系统环境
【8月更文挑战第30天】本文将探讨如何通过自动化运维技术,提升系统的稳定性和效率。我们将从基础概念出发,逐步深入到实践应用,分享一些实用的工具和技术,以及如何将这些工具和技术融入到日常的运维工作中。无论你是运维新手,还是有一定经验的老手,都能在这篇文章中找到有价值的信息。让我们一起探索自动化运维的世界,提升我们的工作效率,让系统运行得更加平稳。
|
15天前
|
消息中间件 Kafka 开发工具
rsyslog+ELK收集Cisco日志
rsyslog+ELK收集Cisco日志
|
21天前
|
存储 监控 数据可视化
在Linux中,如何查看系统日志?
在Linux中,如何查看系统日志?
|
18天前
|
运维 Cloud Native 容灾
核心系统转型问题之支撑高效研发与运维发布如何解决
核心系统转型问题之支撑高效研发与运维发布如何解决
|
20天前
|
监控 安全 Linux
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?