其他分享
首页 > 其他分享> > 测试右移:线上质量监控 ELK 实战

测试右移:线上质量监控 ELK 实战

作者:互联网

目录


【测试右移】介绍

为什么要做测试右移?


测试右移主要实践:

image


为什么要搭建监控系统:


质量监控:

image


测试监控:

image


ELK Stack 介绍

ELK Stack 代指 Elasticsearch、Logstash 和 Kibana ,在这个生态圈慢慢发展过程中,加入了一个新成员 Beats

image


ELK 监控体系搭建

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.1.2
docker pull docker.elastic.co/kibana/kibana:8.1.2
docker pull docker.elastic.co/logstash/logstash:8.1.2

ES & Kibana 搭建

# 创建子网
docker create network elastic

# 启动 elasticsearch 容器
docker run -d --name es01 --net elastic -p 9200:9200 docker.elastic.co/elasticsearch/elasticsearch:8.1.2
# 由于启动时间较久,可使用 docker logs -f es01 查看启动日志

记录启动日志中的登录密码及 Token(注意有效期为半小时):

image

->  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  DB1ngqLjym5Zg3n-doni

->  HTTP CA certificate SHA-256 fingerprint:
  2cffe4439402214dcd28786c835fcd9fdce82f266a8cfd915cdd9fd52facdea3

->  Configure Kibana to use this cluster:
* Run Kibana and click the configuration link in the terminal when Kibana starts.
* Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjEuMiIsImFkciI6WyIxNzIuMTkuMC4yOjkyMDAiXSwiZmdyIjoiMmNmZmU0NDM5NDAyMjE0ZGNkMjg3ODZjODM1ZmNkOWZkY2U4MmYyNjZhOGNmZDkxNWNkZDlmZDUyZmFjZGVhMyIsImtleSI6InZ5UkZySUVCYzFJYTJ4eXZWUXRLOlJSYW03X1FnU3ZxdUZqT0lnOEZLTUEifQ==

-> Configure other nodes to join this cluster:
* Copy the following enrollment token and start new Elasticsearch nodes with `bin/elasticsearch --enrollment-token <token>` (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjEuMiIsImFkciI6WyIxNzIuMTkuMC4yOjkyMDAiXSwiZmdyIjoiMmNmZmU0NDM5NDAyMjE0ZGNkMjg3ODZjODM1ZmNkOWZkY2U4MmYyNjZhOGNmZDkxNWNkZDlmZDUyZmFjZGVhMyIsImtleSI6IndTUkZySUVCYzFJYTJ4eXZWUXVsOlF0bFNtVnVCVFhXVV83UThUcVZETkEifQ==

  If you're running in Docker, copy the enrollment token and run:
  `docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.1.2`

证书认证:

# 拷贝证书到本地
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .

# 进行认证
curl --cacert http_ca.crt -u elastic https://localhost:9200
# 输入刚刚记录下的密码

image

启动 Kibana:

docker run -d \
  --name kibana \
  --link elasticsearch:elasticsearch \
  --net elastic \
  -p 5601:5601 \
  docker.elastic.co/kibana/kibana:8.1.2

image

输入 ES 启动日志中的 Token 及 Kibana 启动日志中的 code:

image

输入 ES 启动日志中的用户名/密码:

image


Nginx 日志自动采集

示例:使用 ES 提供的集成工具(Nginx Agent),自动采集服务器上的 Nginx 日志数据到 ES 中

Nginx Agent

image


image


image


使用默认配置进行保存:

image


image


image


image

将上述 ES 提供的日志采集 Agent 下载到 Nginx 服务器中:

[root@localhost es_agent]# wget https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.1.2-linux-x86_64.tar.gz

创建 elastic-agent.yml 配置文件:

image


解压 agent 并执行:

image

[root@localhost es_agent]# tar -zxvf elastic-agent-8.1.2-linux-x86_64.tar.gz
...
[root@localhost es_agent]# ls
elastic-agent-8.1.2-linux-x86_64  elastic-agent-8.1.2-linux-x86_64.tar.gz  elastic-agent.yml
[root@localhost es_agent]# ./elastic-agent-8.1.2-linux-x86_64/elastic-agent install
Elastic Agent will be installed at /opt/Elastic/Agent and will run as a service. Do you want to continue? [Y/n]:y
Do you want to enroll this Agent into Fleet? [Y/n]:n
Elastic Agent has been successfully installed.
# 此时 agent 会被成功安装在 /opt/Elastic/Agent/ 目录

替换刚下载的配置文件:

[root@localhost Agent]# mv elastic-agent.yml elastic-agent.yml_bak
[root@localhost Agent]# mv ../elastic-agent.yml .
[root@localhost Agent]# vi elastic-agent.yml

image

# 重启 agent
[root@localhost Agent]# ./elastic-agent restart

安装 Nginx 服务器

# 下载 nginx
[root@localhost Agent]# yum install nginx

# 启动 nginx
[root@localhost Agent]# systemctl start nginx

# 查看 nginx 日志,发现已被 agent 监控
[root@localhost Agent]# less /var/log/nginx/access.log
127.0.0.1 - - [29/Jun/2022:12:42:23 +0800] "GET /nginx_status HTTP/1.1" 404 3971 "-" "Elastic-Metricbeat/8.1.2 (linux; amd64; 6118f25235a52a7f0c4937a0a309e380c92d8119; 2022-03-29 22:45:47 +0000 UTC)" "-"
127.0.0.1 - - [29/Jun/2022:12:42:40 +0800] "GET /nginx_status HTTP/1.1" 404 3971 "-" "Elastic-Metricbeat/8.1.2 (linux; amd64; 6118f25235a52a7f0c4937a0a309e380c92d8119; 2022-03-29 22:45:47 +0000 UTC)" "-"
...

数据分析

确认日志数据正常被采集到 ES 中:

image

image


创建自定义视图(正则匹配所需要的日志文件):

image


创建完成后,会自动解析字段:

image


查看自定义视图的统计图及明细数据:

image


更多的数据分析功能:

image


Logstash 搭建


配置文件:vi $PWD/pipeline/logstash.conf

# 输入
input {
  file {
    path => [ "/data/*.log" ]  # logstash的data目录下的log
  }
}

# 过滤
filter {
      grok {
        match => { "message" => ["%{IPORHOST:[nginx][access][remote_ip]} - %{DATA:[nginx][access][user_name]} \[%{HTTPDATE:[nginx][access][time]}\] \"%{WORD:[nginx][access][method]} %{DATA:[nginx][access][url]} HTTP/%{NUMBER:[nginx][access][http_version]}\" %{NUMBER:[nginx][access][response_code]} %{NUMBER:[nginx][access][body_sent][bytes]} \"%{DATA:[nginx][access][referrer]}\" \"%{DATA:[nginx][access][agent]}\""] }
        remove_field => "message"
      }
}

# 输出
output {
  stdout {}  # 标准输出
  elasticsearch {  # 输出到 es
    hosts => ["https://192.168.3.222:9200"]  # es主机
    index => "logstash-nginx-%{+YYYY.MM.dd}"  # 生成的索引
    user => "elastic"  # es用户名
    password => "-yAoRIIni3qy*RW*Q8Nc"  # es密码
    ssl_certificate_verification => false  # 关闭es证书认证
  }
}

启动 logstash:

# 配置文件
[root@localhost logstash]# vi logstash.yml
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: ["https://192.168.3.222:9200"]
xpack.monitoring.elasticsearch.username: "elastic"
xpack.monitoring.elasticsearch.password: "-yAoRIIni3qy*RW*Q8Nc"
xpack.monitoring.enabled: true

# 启动 logstash
docker run --name logstash --rm -d --net elastic \
  -v $PWD/logstash.yml:/usr/share/logstash/config/logstash.yml \  # 挂载配置文件
  -v $PWD/pipeline:/usr/share/logstash/pipeline/ \  # 挂载采集配置文件目录
  -v /var/log/nginx/access.log:/data/access.log \  # 挂载nginx的访问日志
  -v /var/log/nginx/error.log:/data/error.log \  # 挂载nginx的错误日志
  docker.elastic.co/logstash/logstash:8.1.2 \
  --config.reload.automatic  # 热更新配置文件

查看成功生成的索引:

image

接着便可创建自定义视图(data view)并进行数据分析。

标签:右移,ELK,elastic,Agent,agent,nginx,elasticsearch,线上,docker
来源: https://www.cnblogs.com/juno3550/p/16428854.html