PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)

PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)

*## 继:**首先准备环境1:部署Zabbix监控服务器;在华为云上部署一台Zabbix监控服务器,监控其他主机。

  1. 安装LNMP环境 2.源码安装Zabbix 3.安装监控端主机,修改基本配置 4.初始化Zabbix监控Web页面 5.修改PHP配置文件,满足Zabbix需求 6.监控Zabbix_server自身系统状态
    PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
    步骤一、部署LNMP环境
    1)、购买华为云服务器
    基础配置:无
    网络配置:手动分配IP地址 192.168.1.51
    高级配置:云服务器名称 zabbix-server
    确认配置:1台
    2)、更新/etc/hosts
[root@ecs-proxy ~]# cat >> /etc/hosts <<EOF
192.168.1.51 zabbix-server
EOF

3)、更新/root/ansible/hosts配置文件

[root@ecs-proxy ~]# cat >> /root/ansible/hosts <<EOF
[zabbix]
192.168.1.51
EOF

将最新的/etc/hosts配置文件更新到所有的云主机上

[root@ecs-proxy ~]# cd  /etc/ansible
[root@ecs-proxy ansible]# ansible all -m copy -a 'src=/etc/hosts dest=/etc'

4)、安装LNMP所需软件包

[root@zabbix-server ~]# yum -y install gcc pcre-devel openssl-devel
[root@zabbix-server ~]# scp root@192.168.1.252:/root/project3/DAY04/nginx-1.12.2.tar.gz /root
[root@zabbix-server ~]# tar -xf /root/nginx-1.12.2.tar.gz
[root@zabbix-server ~]# cd /root/nginx-1.12.2/
[root@zabbix-server nginx-1.12.2]# ./configure --with-http_ssl_module
[root@zabbix-server nginx-1.12.2]# make && make install
[root@zabbix-server nginx-1.12.2]# yum -y install php php-mysql php-fpm
[root@zabbix-server nginx-1.12.2]# yum -y install mariadb mariadb-devel mariadb-server

5)、修改Nginx配置文件

配置Nginx支持PHP动态网站,因为有大量PHP脚本需要执行,因此还需要开启Nginx的各种fastcgi缓存,加速PHP脚本的执行速度。

[root@zabbix-server ~]# vim /usr/local/nginx/conf/nginx.conf
… …
http{
… …
fastcgi_buffers 8 16k;        #缓存php生成的页面内容,8个16k
fastcgi_buffer_size 32k;      #缓存php生产的头部信息
fastcgi_connect_timeout 300;  #连接PHP的超时时间
fastcgi_send_timeout 300;     #发送请求的超时时间
fastcgi_read_timeout 300;     #读取请求的超时时间
… …
server {
listen 8090;                    #将监听端口更改为8090
… …
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
6)、启动服务

```bash
[root@zabbix-server nginx-1.12.2]# systemctl enable --now mariadb
[root@zabbix-server nginx-1.12.2]# systemctl enable --now php-fpm
[root@zabbix-server nginx-1.12.2]# /usr/local/nginx/sbin/nginx 
[root@zabbix-server nginx-1.12.2]# echo /usr/local/nginx/sbin/nginx >> /etc/rc.local
[root@zabbix-server nginx-1.12.2]# chmod +x /etc/rc.local

步骤二、部署Zabbix服务端
多数源码包都是需要依赖包的,zabbix也一样,源码编译前需要先安装相关依赖包。

[root@zabbix-server nginx-1.12.2]# yum -y install net-snmp-devel curl-devel autoconf libevent-devel
[root@zabbix-server nginx-1.12.2]# scp root@192.168.1.252:/root/project3/DAY04/zabbix-3.4.4.tar.gz /root
[root@zabbix-server nginx-1.12.2]# tar -xf /root/zabbix-3.4.4.tar.gz -C /root
[root@zabbix-server nginx-1.12.2]# cd /root/zabbix-3.4.4/
[root@zabbix-server zabbix-3.4.4]# ./configure --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl
[root@zabbix-server zabbix-3.4.4]# make && make install
# --enable-server安装部署zabbix服务器端软件
# --enable-agent安装部署zabbix被监控端软件
# --enable-proxy安装部署zabbix代理相关软件
# --with-mysql指定mysql_config路径
# --with-net-snmp允许zabbix通过snmp协议监控其他设备(如交换机、路由器等)
# --with-libcurl安装相关curl库文件,这样zabbix就可以通过curl连接http等服务,测试被监控主机服务的状态

2)、创建并初始化数据库

[root@zabbix-server zabbix-3.4.4]# mysql
mysql> create database zabbix character set utf8;
#创建数据库,数据库名称为zabbix,支持中文字符集
mysql> grant all on zabbix.* to zabbix@'localhost' identified by 'zabbix';
#创建可以访问数据库的账户与密码,用户名是zabbix,密码是zabbix
[root@zabbix-server zabbix-3.4.4]# cd /root/zabbix-3.4.4/database/mysql/
[root@zabbixserver mysql]# mysql -uzabbix -pzabbix zabbix < schema.sql
[root@zabbixserver mysql]# mysql -uzabbix -pzabbix zabbix < images.sql
[root@zabbixserver mysql]# mysql -uzabbix -pzabbix zabbix < data.sql
#刚刚创建是空数据库,zabbix源码包目录下,有提前准备好的数据
#使用mysql导入这些数据即可(注意导入顺序)
#-u指定数据库用户名,-p指定数据库密码
```3)、修改zabbix_server配置并启动监控服务

```bash
[root@zabbix-server mysql]# sed -n '38p;95p;111p;119p' /usr/local/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log #38行,日志的位置,排错使用,仅查看以下即可(默认已经配置OK)
DBHost=localhost # 85行,定义数据库服务器在哪台电脑(localhost本机)
DBName=zabbix #95行,设置数据库名称。(默认已经配置OK)
DBUser=zabbix #111行,设置数据库账户。(默认已经配置OK)
DBPassword=zabbix #119行,设置数据库密码
[root@zabbix-server mysql]# useradd -s /sbin/nologin zabbix
[root@zabbix-server mysql]# zabbix_server
[root@zabbix-server mysql]# echo zabbix_server >> /etc/rc.local  #设置开机自启
[root@zabbix-server mysql]# chmod +x /etc/rc.local
#确认连接状态,端口10051
[root@zabbix-server mysql]# netstat -antpu | grep zabbix_server 
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 13890/zabbix_server 

4)、修改Zabbix_agent配置文件,启动Zabbix_agent服务。

[root@zabbix-server mysql]# vim /usr/local/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log          #30行,定义日志文件的位置(默认已经配置OK)
Server=127.0.0.1,192.168.1.51           #93行,允许哪些主机监控本机
ServerActive=127.0.0.1,192.168.1.51    #134行,允许哪些主机通过主动模式监控本机
Hostname=Zabbix_server #145行,设置本机主机名
Include=/usr/local/etc/zabbix_agentd.conf.d/        #264行,加载配置文件目录。
UnsafeUserParameters=1 #280行,自定义监控可以传递参数。默认为0,表示不允许自定义key。
[root@zabbix-server mysql]# zabbix_agentd                          #启动监控agent
[root@zabbix-server mysql]# echo zabbix_agentd >> /etc/rc.local   #设置开机自启
[root@zabbix-server mysql]# netstat -antpu | grep zabbix_agentd  #查看端口信息为10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 14095/zabbix_agentd 

5)、部署访问页面

[root@zabbix-server mysql]# cp -r /root/zabbix-3.4.4/frontends/php/* /usr/local/nginx/html/
[root@zabbix-server mysql]# chmod -R 777 /usr/local/nginx/html/

6)、设置监听器,添加后端服务器。
【服务器列表】—>【弹性负载均衡ELB】—>【(自定义ELB名称)】—>【监听器】—>【添加监听器】,如图-1所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
7)、访问Zabbix Web界面,http://华为云公网IP:8090/ 根据错误提示,修改PHP配置文件,满足Zabbix_server的Web环境要求。
#第一次访问,初始化PHP页面会检查计算机环境是否满足要求,如果不满足会给出修改建议
#默认会提示PHP的配置不满足环境要求,需要修改PHP配置文件

[root@zabbix-server mysql]# yum -y install php-gd php-xml php-ldap  php-bcmath php-mbstring
[root@zabbix-server mysql]# vim /etc/php.ini
max_execution_time = 300         #384行,最大执行时间
max_input_time = 300             #394行,服务器接收数据的时间限制
memory_limit = 128M              #405行,内存容量限制(默认已经配置,确认下即可)
post_max_size = 32M              #672行,POST数据最大容量
date.timezone = Asia/Shanghai   #878行,设置时区
[root@zabbix-server mysql]# systemctl restart php-fpm

修改完PHP配置文件后,再次使用浏览器访问zabbix-server服务器,则会提示
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
初步到此Zabbix环境就搭建完成,然后根据个人要求进行数据的导入监控项目。

第二章节: ELK 架构日志分析

1:ES集群安装
本案例要求部署ES集群,用于ELK日志分析平台的构建。
购买华为云服务器更新ecs-proxy主机配置文件
部署ES集群
查看ES集群信息
安装head插件
实验环境,配置主机名称、IP地址。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
2 步骤一、购买华为云服务,并更新ecs-proxy主机配置文件,运行JAVA程序对内存占用较大,故购买2G内存的云主机。如图
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)

基础配置:规格 s6.mediume.2 2G内存
网络配置:手动分配IP地址 192.168.1.71
高级配置:云服务器名称 es
确认配置:3台
基础配置:规格 s6.mediume.2 2G内存
网络配置:手动分配IP地址 192.168.1.74
高级配置:云服务器名称 kibana
确认配置:1台
基础配置:规格 s6.mediume.2 2G内存
网络配置:手动分配IP地址 192.168.1.75
高级配置:云服务器名称 logstash
确认配置:1台
2)、更新/root/ansible/hosts配置文件

[root@ecs-proxy ~]# cat >> /root/ansible/hosts <<EOF
[es]
192.168.1.7[1:3]
[kinbana]
192.168.1.74
[logstash]
192.168.1.75
EOF

3)、更新/etc/hosts

[root@ecs-proxy ~]# cat >> /etc/hosts <<EOF
192.168.1.71 es-0001
192.168.1.72 es-0002
192.168.1.73 es-0003
192.168.1.74 kibana
192.168.1.75 logstash
EOF
将最新的/etc/hosts配置文件更新到所有的云主机上
[root@ecs-proxy ansible]# ansible all -m copy -a 'src=/etc/hosts dest=/etc'

4)、将相关软件包做好YUM仓库
[root@ecs-proxy ansible]# rm -rf /var/ftp/localrepo/elk
[root@ecs-proxy ansible]# mkdir /var/ftp/localrepo/elk
[root@ecs-proxy ansible]# cp /root/project3/DAY05/*.rpm /var/ftp/localrepo/elk
[root@ecs-proxy ansible]# createrepo --update /var/ftp/localrepo/
步骤二、集群安装配置

1)安装基础环境软件

[root@ecs-proxy ansible]# ansible es -m yum -a “name=java-1.8.0-openjdk-devel”
#检测JDK环境安装是否成功
[root@ecs-proxy ansible]# ansible es -m shell -a “java -version”
192.168.1.72 | SUCCESS | rc=0 >>
openjdk version “1.8.0_252”
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

[root@ecs-proxy ansible]# ansible es -m yum -a “name=elasticsearch”
2)、配置es-0001

[root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: es #第17行,集群的名称。
node.name: es-0001 #第23行,该节点主机名。
network.host: 192.168.1.71 #第55行,该节点主机的IP地址。
http.port: 9200 #第59行,对外服务的http端口。
discovery.zen.ping.unicast.hosts: [“es-0001”, “es-0002”, “es-0003”] #第68行,集群节点主机列表。
discovery.zen.minimum_master_nodes: 2 #第72行,最少主节点数。
[root@es-0001 ~]# systemctl enable --now elasticsearch
[root@es-0001 ~]# systemctl status elasticsearch
3)、配置es-0002

[root@es-0002 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: es #第17行,集群的名称。
node.name: es-0002 #第23行,该节点主机名。
network.host: 192.168.1.72 #第54行,该节点主机的IP地址。
http.port: 9200 #第58行,对外服务的http端口。
discovery.zen.ping.unicast.hosts: [“es-0001”, “es-0002”, “es-0003”] #第68行,集群节点主机列表。
discovery.zen.minimum_master_nodes: 2 #第72行,最少主节点数。
[root@es-0002 ~]# systemctl enable --now elasticsearch
4)、配置es-0003

[root@es-0003 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: es #第17行,集群的名称。
node.name: es-0003 #第23行,该节点主机名。
network.host: 192.168.1.73 #第54行,绑定的IP地址。
http.port: 9200 #第58行,对外服务的http端口。
discovery.zen.ping.unicast.hosts: [“es-0001”, “es-0002”, “es-0003”] #第68行,集群节点主机列表。
discovery.zen.minimum_master_nodes: 2 #第72行,最少主节点数。
[root@es-0003 ~]# systemctl enable --now elasticsearch
5)、查看ES集群信息

[root@ecs-proxy ansible]# curl -XGET http://192.168.1.71:9200/_cluster/health?pretty
{
"cluster_name" : "my-es",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 0,
"active_shards" : 0,
"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
}

步骤三、安装head插件

ES官方没有为ES提供界面管理工具,仅仅提供了后台服务。elasticsearch-head是一个为ES开发的web页面客户端工具。

head提供了以下安装方式:

A、源码安装,通过npm run start 启动

B、通过docker安装(推荐)

C、通过chrome插件安装

1)、部署插件

由于前后端分离开发,所以会存在跨域问题,需要在服务端做CORS的配置。(前后端分离:前端所有用到的数据都是后端通过异步接口的方式提供的,前端只管页面的展示及效果。)

在配置文件末尾手动添加以下内容即可。
[root@es-0001 ~]# tail -2 /etc/elasticsearch/elasticsearch.yml 
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@es-0001 ~]# systemctl restart elasticsearch
[root@es-0001 ~]# scp 192.168.1.252:/root/project3/DAY05/elasticsearch-head.tar /root 
[root@es-0001 ~]# yum -y install docker-ce
[root@es-0001 ~]# systemctl    enable  --now docker
[root@es-0001 ~]# docker load  -i elasticsearch-head.tar
[root@es-0001 ~]# docker run  --name es-head -td  -p 9100:9100  elasticsearch-head:5 
914e41f15e9de8873a57e2a53e250231592ed18e635fdd227733e99542f42218
[root@es-0001 ~]# netstat -antpu | grep :9100
tcp6       0      0 :::9100                 :::*                    LISTEN      12971/docker-proxy

2)、创建监听器(9200),添加后端服务器群组。

【服务器列表】—>【弹性负载均衡ELB】—>【(自定义ELB名称)】—>【监听器】—>【添加监听器】。

更改监听器名称,并且配置前端协议端口为9200,如图
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)配置后端服务器组,更改名称,点击完成。如图
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
点击【后端服务器群组】,找到我们刚才添加的【server_group-es】,并单击。【添加】后端真实服务器。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)添加es-0001为后端服务器成员,点击下一步。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
配置后端服务器提供服务的端口9200,点击完成。如图
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
2)、创建监听器(9100),添加后端服务器群组。

参照创建监听器(9200)的步骤。

3)、访问head插件,如图-7所示。

http://公网IP:9100

默认打开是未连接状态。将http://localhost:9200/改为http://公网IP:9200,并点击连接。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
2:部署kibana,本案例要求在华为云服务器上安装kibana
安装kibana
通过web页面访问kibana
Kibana是一款开源的数据分析和可视化平台,它是Elastic Stack成员之一。可以使用Kibana对Elasticsearch索引中的数据进行搜索、查看、交互操作。可利用图表、表格等对数据进行多元化的分析和显现。
2.2 步骤一、部署kibana
1)安装kibana软件包
[root@kibana ~]# yum -y install kibana
2)更改kibana服务配置文件
ot@kibana ~]# vim /etc/kibana/kibana.yml
server.port: 5601 #第2行,提供服务的端口。
server.host: “192.168.1.74” #第7行,服务器监听地址。
elasticsearch.hosts: [“http://192.168.1.71:9200”] #第28行,用于查询的es实例的主机地址,集群里面任选一个即可。
3)启动服务器并查看端口是否启用
[root@knbana ~]# systemctl enable --now kibana
[root@kibana ~]# netstat -antpu | grep 5601
tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 8840/node
步骤二、通过浏览器访问kibana
1)按照案例1步骤二的方法,创建监听器,并添加后端服务器。
【服务器列表】—>【弹性负载均衡ELB】—>【(自定义ELB名称)】—>【监听器】—>【添加监听器】 此次监听端口为5601
2)访问kibana界面,如图所示。
http://公网IP:5601
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
3)、用head插件访问es集群,会查看到.kibana的索引信息,如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
3、部署logstash 本案例要求在华为云上部署Logstash。
步骤一、安装软件
[root@logstash ~]# yum -y install java-1.8.0-openjdk-devel
[root@logstash ~]# yum -y install logstash
[root@logstash ~]# /opt/logstash/bin/logstash --version
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
logstash 2.3.4
[root@logstash ~]# /usr/share/logstash/bin/logstash-plugin list #查看插件
4、使用Filebeat收集Nginx访问日志,本案例要求在Nginx集群上安装Filebeat,并收集Nginx访问日志。
安装Filebeat软件,使用Filebeat读取文件
使用Filebeat输出到Elasticsearch
使用Filebeat读取Nginx日志文件
使用Filebeat的Nginx Module
4.2 方案
Beats平台集合了多种单一用途的数据采集器,他们从成百上千台机器和系统向Logstash或Elasticsearch发送数据。
(官方网址: https://www.elastic.co/cn/downloads/beats)
Filebeat是一个轻量型日志采集器,用于转发和汇总日志文件。占用资源少,配置简单。
在nginx-0001、nginx-0002、nginx-0003上部署Filebeat,收集访问日志。
4.3 步骤
实现此案例需要按照如下步骤进行。步骤一、部署Filebeat软件。
1)、在Nginx主机上安装Filebeat软件
[root@ecs-proxy ansible]# ansible web -m yum -a “name=filebeat”
2)、编写配置文件。为了防止原始配置文件中其他内容干扰,新建文件编写。
[root@nginx-0001 ~]# cat /etc/filebeat/tedu_log.yml
filebeat.inputs:

  • type: stdin
    enabled: true
    setup.template.settings:
    index.number_of_shards: 3
    output.console:
    pretty: true
    enable: true
    [root@nginx-0001 ~]# filebeat -e -c /etc/filebeat/tedu_log.yml
    (输入一些测试的内容,会以JSON格式输出在屏幕中)
    -e:输出到标准输出
    -c:指定文配置文件
    步骤二:使用Filebeat输出数据到Elasticsearch
    1)、编写配置文件
    [root@nginx-0001 ~]# cat /etc/filebeat/tedu_log.yml
    filebeat.inputs:
  • type: log
    enabled: true
    paths:
    • /root/*.log
      setup.template.settings:
      index.number_of_shards: 3
      output.elasticsearch:
      hosts: [“192.168.1.71:9200”,“192.168.1.72:9200”]
      [root@nginx-0001 ~]# filebeat -e -c /etc/filebeat/tedu_log.yml
      2)、另开一个终端,向日志文件中写入数据。
      [root@nginx-0001 ~]# echo aaa >> /root/a.log
      [root@nginx-0001 ~]# echo bbb >> /root/a.log
      3)、刷新es-head界面,如图所示,索引通常都会分解成不同部分, 而这些分布在不同节点的数据就是分片(shards),而副本是这个分片的备份。
      PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
      4)、点击数据浏览,查看新添加的索引数据。如图所示。
      PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)步骤三、使用Filebeat读取Nginx日志文件
      1)、编写配置文件
      [root@nginx-0001 ~]# cat /etc/filebeat/tedu_log.yml
      filebeat.inputs:
  • type: log
    enabled: true
    paths:
    • /root/*.log
      setup.template.settings:
      index.number_of_shards: 3
      output.elasticsearch:
      hosts: [“192.168.1.71:9200”,“192.168.1.72:9200”]
      [root@nginx-0001 ~]# filebeat -e -c /etc/filebeat/tedu_log.yml
      2)、另开启一个终端,向a.log文本文件中插入日志文件。
      [root@nginx-0001 ~]# tail -2 /usr/local/nginx/logs/access.log >> /root/a.log
      3)、查看索引数据,如图所示。
      PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
      步骤四、使用Filebeat的Nginx Module,在Filebeat中,有大量的Module,可以简化配置。
      1)、查看有哪些模块可以使用,并开启nginx模块。
[root@nginx-0001 filebeat]# filebeat modules list           //查看模块信息
Enabled:
Disabled:
apache2
auditd
elasticsearch
haproxy
……

内置了很多Modules,但是都没有启用,如果需要启用,可执行以下操作。

[root@nginx-0001 filebeat]# filebeat  modules  enable nginx  //开启nginx模块
[root@nginx-0001 filebeat]# filebeat  modules  list
Enabled:
nginx
Disabled:
apache2
……

2)、修改模块的配置文件。(模块配置文件路径:/etc/filebeat/modules.d/,如果模块没有开启,是以disabled后缀结尾。)
[root@nginx-0001 ~]# sed -n ‘8p’ /etc/filebeat/modules.d/nginx.yml
var.paths: ["/usr/local/nginx/logs/access.log"]
[root@nginx-0001 ~]# sed -n ‘19p’ /etc/filebeat/modules.d/nginx.yml
var.paths: ["/usr/local/nginx/logs/error.log"]
改为之后的配置如下:
[root@nginx-0001 ~]# grep -Ev “^$|#” /etc/filebeat/modules.d/nginx.yml

  • module: nginx
    access:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/access*.log"]
    error:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/error.log"]
    3)、修改配置文件

[root@nginx-0001 filebeat]# cat /etc/filebeat/tedu_log.yml
setup.template.settings:
index.number_of_shards: 3
output.elasticsearch:
hosts: [“192.168.1.71:9200”,“192.168.1.72:9200”]
indices:
- index: “filebeat-nginx_log-%{+yyyy.MM.dd}”
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
4)、删除之前记录的偏移量,读取整个日志文件内容。
[root@nginx-0001 ~]# rm -rf /var/lib/filebeat/registry
[root@nginx-0001 ~]# filebeat -e -c /etc/filebeat/tedu_log.yml
5)、查看es中收集日志信息。如图所示
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
6)、按照如上方案,收集nginx-0001 nginx-0002和nginx-0003日志。

[root@ecs-proxy ansible]# ansible web -m shell -a “filebeat modules enable nginx”
[root@ecs-proxy ansible]# ansible web -m copy -a “src=/root/project3/DAY05/nginx.yml dest=/etc/filebeat/modules.d/”
[root@ecs-proxy ansible]# ansible web -m copy -a “src=/root/project3/DAY05/filebeat.yml dest=/etc/filebeat/”
[root@ecs-proxy ansible]# ansible web -m service -a “name=filebeat state=restarted”
5、TOA插件配置,本案例要求收集访问Nginx集群的真实IP。
方案:获取客户端真实IP地址方式
参考文档https://support.huaweicloud.com/elb_faq/elb_faq_0090.html
TOA内核模块主要用来获取ELB转化过的访问者真实IP地址(仅支持IPv4),该插件安装在ELB后端服务器。
5.3 步骤一、准备编译环境。
1)、确保安装过编译器和make工具。
[root@nginx-0001 ~]# yum -y install gcc make
2)、查看内核版本。安装内核模块开发包,开发包头文件与库的版本需要与内核版本一致。
[root@nginx-0001 ~]# uname -r
3.10.0-1062.12.1.el7.x86_64
如果自带源里没有对应的内核开发包,可以到如下地址中去下载对应的rpm包。
地址:https://mirror.netcologne.de/oracle-linux-repos/ol7_latest/getPackage/
2、编译内核模块。
1)、给所有的Web主机,发送TOA内核模块源代码。
[root@ecs-proxy ansible]# ansible web -m copy -a “src=/root/elk/TCP_option_address-master.zip dest=/root”
[root@nginx-0001 ~]# unzip TCP_option_address-master.zip
[root@nginx-0001 ~]# cd /root/TCP_option_address-master/src/
[root@nginx-0001 src]# ls
Makefile toa.c toa.h
[root@nginx-0001 src]# make
3、加载内核模块
1)、执行如下命令,加载内核模块。
[root@nginx-0001 src]#insmod toa.ko
2)执行如下命令,验证模块加载情况,查看内核输出信息。
[root@nginx-0001 src]#dmesg | grep TOA
#若提示信息包含“TOA: toa loaded”,说明内核模块加载成功。
4、自动加载内核模块
为了使TOA内核模块在系统启动时生效,可以将加载TOA内核模块的命令加到客户的启动脚本中。
参考以下操作步骤配置启动脚本。
1)在“/etc/sysconfig/modules/”目录下新建toa.modules文件。该文件包含了TOA内核模块的加载脚本。toa.modules文件内容。

[root@nginx-0001 src]# cat >> /etc/sysconfig/modules/toa.modules <<EOF
#!/bin/sh
/sbin/modinfo -F filename /root/TCP_option_address-master/src/toa.ko > /dev/null 2>&1
if [ $? -eq 0 ]; then
/sbin/insmod /root/TCP_option_address-master/src/toa.ko
fi
EOF

#其中“/root/toa/toa.ko”为TOA内核模块文件的路径,需要将其替换为自己编译的TOA内核模块路径。
2)、执行以下命令,为toa.modules启动脚本添加可执行权限。
[root@nginx-0001 src]# chmod +x /etc/sysconfig/modules/toa.modules
6、Metricbeat收集系统指标,本案例要求使用Metricbeat收集系统指标,使用Metricbeat的Nginx Module收集Nginx相关指标。方案
Metricbeat是轻量型指标采集器,用于定期从系统和服务收集指标(CPU、内存),可存储到Elasticsearch中,进行实时分析。
6.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:部署Metricbeat软件
1)、在nginx-0001上安装Metricbeat软件

[root@ecs-proxy ansible]# ansible web -m copy -a "src=/root/project3/DAY05/metricbeat-6.8.8-x86_64.rpm  dest=/root"
[root@nginx-0001 ~]# yum -y install metricbeat-6.8.8-x86_64.rpm
[root@nginx-0001 ~]# sed -n '94p' /etc/metricbeat/metricbeat.yml
  hosts: ["192.168.1.71:9200","192.168.1.72:9200","192.168.1.73:9200"]
[root@nginx-0001 ~]# systemctl  restart  metricbeat

2)、查看es中的数据,如图所示。可以看出到nginx-0001系统CPU相关性能指标。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
步骤二:部署Metricbeat软件
1)、列出nginx-0001上已开启Metricbeat的模块。

[root@nginx-0001 metricbeat]# metricbeat modules list
Enabled:
system
Disabled:
aerospike
apache
ceph
……

2)更改Nginx配置文件,可查看Nginx的一些状态信息。

[root@nginx-0001 ~]# sed -n '42,44p' /usr/local/nginx/conf/nginx.conf
        location /nginx-status {
            stub_status on;
        }
[root@nginx-0001 ~]# /usr/local/nginx/sbin/nginx  -s reload
[root@nginx-0001 ~]# curl http://192.168.1.11/nginx-status
Active connections: 1 
server accepts handled requests
 3 3 1 
Reading: 0 Writing: 1 Waiting: 0

3)、启用Metricbeat的Nginx模块

[root@nginx-0001 metricbeat]# metricbeat  modules enable nginx
Enabled nginx
[root@nginx-0001 metricbeat]# metricbeat modules list
Enabled:
nginx
system
Disabled:
aerospike
apache
……

4)、更改Metricbeat的Nginx模块的配置文件。

[root@nginx-0001 ~]# grep -Ev "^$|#" /etc/metricbeat/modules.d/nginx.yml
- module: nginx
  period: 10s
  hosts: ["http://192.168.1.11"]
  server_status_path: "nginx-status"
[root@nginx-0001 ~]# systemctl restart  metricbeat

5)、查看es中的数据。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
7: Kibana数据展现,将前面案例收集的数据,进行可视化展示。并使用Metricbeat自带的仪表盘进行数据展示。
步骤一: 图表展示
1)、设置索引。
点击Management,单击”Index Patterns”,如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
2)、设置Index name,可以采用通配符。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
3)、时间字段选择@timestamp,选择完成后单击“Create Index pattern ”。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)4)、单击Discover等待Searching完成后,可以看到数据。
有时候只想关注一些指定的字段,那么可以将鼠标移动到索引下面的字段上,然后选add即可。同样的移动上面已经选择的字段选择remove进行移除。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)按照同样的方法,添加host.name、nginx.stubstatus.active两个字段,选择后,结果就会以表格的形式进行展示。

同时可选择什么时间段内的数据,以及数据刷新时间。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
5)、点击最上方的save按钮,保存当前表格。如图所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
步骤二: Metricbeat仪表盘安装以及展现

1)、修改metricbeat配置文件

[root@nginx-0001 ~]# sed -n ‘67p’ /etc/metricbeat/metricbeat.yml
host: “192.168.1.74:5601”
加载仪表盘到Kibana(确保Kibana是开启的并且可连接)
[root@nginx-0001 ~]# metricbeat setup –dashboards
Loading dashboards (Kibana must be running and reachable)
Loaded dashboards
[root@nginx-0001 ~]# systemctl restart metricbeat
2)、点击Dashboard,会有很多仪表盘。在搜索框里写入关键词host,会出现和搜索关键词相关的表盘。点击“Host overview”。如图-24、图-25所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
3)、点击Dashboard,在搜索框里写入关键词Nginx,会出现相关的仪表盘。点击“[Metricbeat Nginx]Overview”。如图-26、图-27所示。
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
PROJECT2: 华为云 >> 企业云平台完整架构实例应用分解(第三部Zabbix + ELK 架构日志分析)
###Wa ~ 期待以久的,最终上线了,###到此, 能有这样的效果出来!~ 就说明 整个实验 环境 就 已经 OK完美,漂亮~ 由于 时间 的问题, 难免会有些疏漏之处~ 还望各路大侠多多指导感激不尽~! 在此谢过! # >------感谢 大家查阅! 一路相伴同行,砥砺前行! — Bye!-----------
————————————————

上一篇:基础拾遗:除了&和&&的区别,你还要知道位运算的这5个运算符


下一篇:Spring Boot(十七):使用Spring Boot上传文件