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

本文涉及的产品
日志服务 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日志并进行多维度分析。
目录
相关文章
|
27天前
|
存储 前端开发 数据可视化
Grafana Loki,轻量级日志系统
本文介绍了基于Grafana、Loki和Alloy构建的轻量级日志系统。Loki是一个由Grafana Labs开发的日志聚合系统,具备高可用性和多租户支持,专注于日志而非指标,通过标签索引而非内容索引实现高效存储。Alloy则是用于收集和转发日志至Loki的强大工具。文章详细描述了系统的架构、组件及其工作流程,并提供了快速搭建指南,包括准备步骤、部署命令及验证方法。此外,还展示了如何使用Grafana查看日志,以及一些基本的LogQL查询示例。最后,作者探讨了Loki架构的独特之处,提出了“巨型单体模块化”的概念,即一个应用既可单体部署也可分布式部署,整体协同实现全部功能。
351 69
Grafana Loki,轻量级日志系统
|
25天前
|
运维 应用服务中间件 nginx
docker运维查看指定应用log文件位置和名称
通过本文的方法,您可以更高效地管理和查看Docker容器中的日志文件,确保应用运行状态可控和可监测。
109 28
|
3月前
|
存储 运维 监控
API明细日志及运维统计日志全面提升API可运维性
在数字化转型的大潮中,数据已成为企业最宝贵的资产之一。而数据服务API可快速为数据应用提供数据接口。面对越来越多的API以及越来越多的应用调用,如何快速查看API的服务情况、异常情况及影响范围,以及查看API的调用详情,进行API的性能优化、错误排查变得越来越重要,本文将介绍如何配置和开通API运维统计及明细日志,以及如何查看日志进行介绍。
204 0
|
1月前
|
监控 运维
HTTPS 证书自动化运维:https证书管理系统- 自动化监控
本文介绍如何设置和查看域名或证书监控。步骤1:根据证书状态选择新增域名或证书监控,线上部署推荐域名监控,未部署选择证书监控。步骤2:查询监控记录详情。步骤3:在详情页查看每日定时检测结果或手动测试。
HTTPS 证书自动化运维:https证书管理系统- 自动化监控
|
1月前
|
Linux 持续交付 调度
HTTPS 证书自动化运维:https证书管理系统-自动化部署
本指南介绍如何部署Linux服务器节点。首先复制生成的Linux脚本命令,然后将其粘贴到目标服务器上运行。接着刷新页面查看节点记录,并点击“配置证书”选择证书以自动部署。最后,节点部署完成,后续将自动调度,无需人工干预。
HTTPS 证书自动化运维:https证书管理系统-自动化部署
|
1月前
|
运维
HTTPS 证书自动化运维:https证书管理系统之自动化签发
通过访问【https://www.lingyanspace.com】注册账户,进入证书服务菜单并新增证书。填写域名(单域名、多域名或泛域名),创建订单后添加云解析DNS记录进行质检。确认完成后可下载证书,并支持后续查看、更新和定时更新功能。证书过期前15天自动更新,需配置邮箱接收通知。
HTTPS 证书自动化运维:https证书管理系统之自动化签发
|
1月前
|
机器学习/深度学习 人工智能 运维
智能日志分析:用AI点亮运维的未来
智能日志分析:用AI点亮运维的未来
242 15
|
1月前
|
机器学习/深度学习 运维 监控
利用深度学习进行系统健康监控:智能运维的新纪元
利用深度学习进行系统健康监控:智能运维的新纪元
125 30
|
1月前
|
运维 监控 数据安全/隐私保护
HTTPS 证书自动化运维:HTTPS 证书管理系统之使用指南
本文详细介绍【灵燕空间HTTPS证书管理系统】(https://www.lingyanspace.com)的配置与使用,涵盖注册账户、邮箱配置及证书自动签发、监控和部署的一体化指南。通过页面顶部菜单的【视频教程】和【图文教程】,帮助用户从注册到实际应用全面掌握系统操作。最新迭代后,泛域名证书已包含根域名,无需额外申请多域名证书。
|
1月前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
91 8