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

简介: 运维必备——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日志分析平台搭建完成!!!!!

相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
目录
相关文章
|
5月前
|
数据采集 运维 数据可视化
AR 运维系统与 MES、EMA、IoT 系统的融合架构与实践
AR运维系统融合IoT、EMA、MES数据,构建“感知-分析-决策-执行”闭环。通过AR终端实现设备数据可视化,实时呈现温度、工单等信息,提升运维效率与生产可靠性。(238字)
WGLOG日志管理系统是怎么收集日志的
WGLOG通过部署Agent客户端采集日志,Agent持续收集指定日志文件并上报Server,Server负责展示与分析。Agent与Server需保持相同版本。官网下载地址:www.wgstart.com
|
5月前
|
Prometheus 监控 Cloud Native
基于docker搭建监控系统&日志收集
Prometheus 是一款由 SoundCloud 开发的开源监控报警系统及时序数据库(TSDB),支持多维数据模型和灵活查询语言,适用于大规模集群监控。它通过 HTTP 拉取数据,支持服务发现、多种图表展示(如 Grafana),并可结合 Loki 实现日志聚合。本文介绍其架构、部署及与 Docker 集成的监控方案。
500 122
基于docker搭建监控系统&日志收集
|
5月前
|
传感器 人工智能 运维
AR智慧运维系统介绍
阿法龙XR云平台是一款面向工业领域的增强现实(AR)智能化平台,助力企业实现数字化转型。平台集成智能巡检工作流、远程协助、AI视频验收、人脸识别等功能模块,支持AR眼镜与移动终端,提供虚实融合的运维体验。具备高度定制化能力,适配多种工业场景,提升运维效率与智能化水平。
|
5月前
|
消息中间件 Java Kafka
搭建ELK日志收集,保姆级教程
本文介绍了分布式日志采集的背景及ELK与Kafka的整合应用。传统多服务器环境下,日志查询效率低下,因此需要集中化日志管理。ELK(Elasticsearch、Logstash、Kibana)应运而生,但单独使用ELK在性能上存在瓶颈,故结合Kafka实现高效的日志采集与处理。文章还详细讲解了基于Docker Compose构建ELK+Kafka环境的方法、验证步骤,以及如何在Spring Boot项目中整合ELK+Kafka,并通过Logback配置实现日志的采集与展示。
1045 64
搭建ELK日志收集,保姆级教程
|
6月前
|
数据采集 运维 监控
运维靠经验拍脑袋?不如上车:构建“数据驱动”的智能决策系统
运维靠经验拍脑袋?不如上车:构建“数据驱动”的智能决策系统
224 0
|
5月前
|
机器学习/深度学习 运维 监控
运维日志里的“读心术”:深度学习能看出啥?
运维日志里的“读心术”:深度学习能看出啥?
299 74
|
7月前
|
人工智能 运维 监控
聚焦“AI+运维”深度融合,龙蜥系统运维联盟 MeetUp 圆满结束
现场 40 多位开发者进行了深入的技术交流,探索 AI 与运维深度融合的未来路径。
|
8月前
|
监控 API 开发工具
HarmonyOS Next的HiLog日志系统完全指南:从入门到精通
本文深入解析HarmonyOS Next的HiLog日志系统,涵盖日志级别、核心API、隐私保护与高级回调功能,助你从入门到精通掌握这一重要开发工具。

热门文章

最新文章