分析日志的意义:
1、分析日志监控系统运行的状态
2、分析日志来定位程序的bug
3、分析日志监控网站访问流量
4、分析日志可以知道那些sql语句需要优化
。。。
1、mysql查询的方式是全表扫描,用时长,效率不高
?
1、日志易:监控、审计、权限管理,收费软件
2、splunk:按流量收费,是国外的软件
主要三个部件 Indexer、Search Head、Forwarder
3、elk:日志收集、分析、展示
1、elk/efk它是一个组合软件:
elasticsearch、logstash、kibana
2、elasticsearch:负责数据的存储、数据的分析、数据的搜索
基于luncene做的二开发,
3、logstash:负责客户端日志收集工具,替代工具(filebeat)
4、kibana:负责数据的展示,查询
关系型的数据库:
mysql orcal sql-server。。。
非关系型的数据库:
redis mongo elasticsearch。。
1、信息存储到es时,首先把每条语句拆分成一个一个的词语
2、根据搜索的内容,进行匹配,有匹配到的权重加1
3、把匹配到的语句给用记呈现出来
1、电商平台
2、高亮显示搜索的词条信息
3、日志分析elk
1、高性能:
es可以支持一主多从,水平扩展方便,
2、高可用性:一个主节点宕机后不影响用户的使用,
3、用户使用方便快捷:es采用java开发,即使不懂java代码,一样可以使用
4、功能丰富,配置简单
5、采用restful封装的接口,可以通过http发起请求
1、rpm包的方式来部署
2、tar包的方式来部署
3、ansible来部署
4、docker的方式来部署
1、安装一个java的环境
mkdir /opt/es-software
cd /opt/es-software
rpm -ivh jdk-8u102-linux-x64.rpm
#检查安装信息
java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
#上传es安装包
rpm -ivh elasticsearch-6.6.0.rpm
?
#看下都安装了那些东西
rpm -qc elasticsearch
/etc/elasticsearch/elasticsearch.yml#es的主要配置文件
/etc/elasticsearch/jvm.options#调整jvm内存配置文件
/etc/init.d/elasticsearch#启动脚本
/etc/sysconfig/elasticsearch#配置环境变量
/usr/lib/sysctl.d/elasticsearch.conf#配置环境变量
/usr/lib/systemd/system/elasticsearch.service#启动服务用的文件
#修改配置文件
vim /etc/elasticsearch/elasticsearch.yml
[root@es01 es-software]# egrep -v ‘^$|#‘ /etc/elasticsearch/elasticsearch.yml
node.name: oldboy01
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.240,127.0.0.1
http.port: 9200
#启动es服务
systemctl start elasticsearch
#在启动时发生内存锁定失败的问题
#官方文档介绍
https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-system-settings.html#sysconfig
#解决方案
systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
#需要重新载入
systemctl daemon-reload
systemctl restart elasticsearch
#检验是否能正常工作
命令行的方式:curl 10.0.0.240:9200
浏览器的方式:http://10.0.0.240:9200
[root@es01 ~]# curl localhost:9200
{
"name" : "oldboy01",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "yJ0eJGDyTWmcPywIJp5vDw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
?
1、索引:好比一个mysql当中一个数据库
2、类型:好比一个库当中的一个表
3、docs:好比表中的字段内容
#创建个索引
curl -XPUT ‘10.0.0.240:9200/oldboy?pretty‘
#往这个索引里面插入一条指定id号为1的数据
curl -XPUT ‘10.0.0.240:9200/oldboy/student/1?pretty‘ -H ‘Content-Type: application/json‘ -d‘
{
"first_name" : "zhang",
"last_name": "san",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports" ]
}‘
#往这个索引里面插入一条随机生成一个id的数据
curl -XPUT ‘10.0.0.240:9200/oldboy/student/2?pretty‘ -H ‘Content-Type: application/json‘ -d‘ {
"first_name": "li",
"last_name" : "si",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}‘
#查询索引当中的一条数据
curl -XGET ‘10.0.0.240:9200/oldboy/student/1?pretty‘
#删除索引当中的一条数据
curl -XDELETE ‘10.0.0.240:9200/oldboy/student/1?pretty‘
#删除指定的索引
curl -XDELETE ‘10.0.0.240:9200/oldboy/?pretty‘
#下载epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
?
1、docker方式来部署head插件
docker pull alivv/elasticsearch-head
docker run --name es-head -p 9100:9100 -dit elivv/elasticsearch-head
2、nodejs npm的方式来部署head
使用nodejs编译安装elasticsearch-head
yum install nodejs npm openssl screen -y#配置nodejs环境
node -v
npm -v
#安装组件
npm install -g cnpm --registry=https://registry.npm.taobao.org
cd /opt/
#克隆代码
官网https://github.com/mobz/elasticsearch-head
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
cnpm install
screen -S es-head
cnpm run start
Ctrl+A+D
#通过cnpm的方式来运行head时, 发现无法链接es服务器
vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
3、在谷歌浏览器中安装一个插件
?
#部署kibana工具
cd /opt/es-software
#上传需要的安装包并安装
rpm -ivh kibana-6.6.0-x86_64.rpm
#修改配置文件
vim /etc/kibana/kibana.yml
[root@es01 es-software]# egrep -v ‘^$|#‘ /etc/kibana/kibana.yml
server.port: 5601
server.host: "10.0.0.240"
elasticsearch.hosts: ["http://10.0.0.240:9200"]
kibana.index: ".kibana"
#启动kibana服务
systemctl start kibana
#在浏览器中测试kibana服务是否正常
http://10.0.0.240:5601
dev tools--->console
#创建一个索引
PUT oldboy
#在oldboy索引中插入一条数据
PUT oldboy/student/1
{
"name": "zhang san",
"age": "29",
"xuehao": 1
}
#随机插入一条数据
POST oldboy/student/
{
"name": "oldboy",
"age": "19",
"xuehao": 5
}
#查询数据
GET oldboy/student/2
GET oldboy/_search
#删除一条数据
DELETE oldboy/student/1
#修改系统默认的分片数和副本数
PUT _template/template_http_request_record
{
"index_patterns": ["*"],
"settings": {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
备注:
1、即使用有某些索引不再使用,不要立马删除,可以先关闭掉,确定一段时间内没有人使用,再删除
2、一旦索引被创建,分片的数量不能再二次修改,副本的数量可以修改
kibana页面无法打开,如何解决
#报错的信息
"Another Kibana instance appears to be migrating the index. Waiting for that migration to complete. If no other Kibana instance is attempting migrations, you can get past this message by deleting index .kibana_1 and restarting Kibana."}
#解决的方案
curl -XDELETE http://10.0.0.240:9200/.kibana_1
systemctl restart kibana
PUT /oldboy/_settings
{
"number_of_replicas": 2
}
#上传需要安装的包
mkdir /opt/es-software
cd /opt/es-software
jkd elasticsearch
#安装包
rpm -ivh jdk-8u102-linux-x64.rpm
rpm -ivh elasticsearch-6.6.0.rpm
#在10.0.0.240主机操作
vim /etc/elasticsearch/elasticsearch.yml
[root@es01 es-software]# grep -E -v ‘^$|#‘ /etc/elasticsearch/elasticsearch.yml
cluster.name: oldboy-cluster
node.name: oldboy01
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.240,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.240", "10.0.0.241"]
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"
?
#在10.0.0.241主机操作
vim /etc/elasticsearch/elasticsearch.yml
[root@es02 es-software]# grep -E -v ‘^$|#‘ /etc/elasticsearch/elasticsearch.yml
cluster.name: oldboy-cluster
node.name: oldboy02
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.241,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.241", "10.0.0.242"]
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"
?
#在10.0.0.242主机操作
vim /etc/elasticsearch/elasticsearch.yml
[root@es03 es-software]# grep -E -v ‘^$|#‘ /etc/elasticsearch/elasticsearch.yml
cluster.name: oldboy-cluster
node.name: oldboy03
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.242,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.242", "10.0.0.240"]
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"
#通过head插件来检查状态
分片:把存储到索引里面的数据,分隔一块块的,均匀的存储到不同的节点上面,构成分步式存储分担集群的压力
分片各类:主分片和副本分片
主分片:主要是响应修改数据的请求,同时提供查询的功能
副本分片:主要是提供查询的功能
副本:副本就是主分片的备份
1、如果一台一台的宕机,最多可以宕机2台(需要手动修改配置文件才能保障服务正常使用),正常情况下,一台一台的宕机(宕机一台修好一台,不影响集群的健康状态)
2、前提是1副本,如果同时宕机2台,那么数据就会丢失,集群处于红色状态
3、前提是2个副本,如果同一台一台的宕机没有什么影响,即使是同时宕机2台,通过修改配置文件,也可以正常提供服务。
1、索引一旦创建成功,分片的数量是不能再修改,但是副本的数量可以人为的修改
2、分片数量多少合适,如果集群中有3个节点,分片的数量为3*3个
如果集群中有2个节点,你可以采用默认的分片数量,也可分4个分片
3、在配置主机相互发现时,可以两两主机串起来配置
.curl ‘10.0.0.240:9200/_cluster/health?pretty‘
.GET _cat/nodes
.GET _cat/health
#nodejs官网地址
https://nodejs.org/en/
#部署node和nmp的环境,要有epel源
yum install nodejs npm openssl screen -y
#检查版本信息
node -v
npm -v
#使用国内淘宝镜像源(可能要等几分钟)
npm install -g cnpm --registry=https://registry.npm.taobao.org
#安装elasticdump工具
官网地址https://github.com/taskrabbit/elasticsearch-dump
#使用elasticdump工具备份索引
mkdir /data
elasticdump \
--input=http://10.0.0.240:9200/oldboy \
--output=/data/oldboy.json \
--type=data
#通过备份的数据来恢复
elasticdump \
--input=/data/oldboy.json \
--output=http://10.0.0.240:9200/oldboy
#把索引可以备份为一个压缩文件的形式(如果要恢复,必须要解压)
elasticdump \
--input=http://10.0.0.240:9200/oldboy \
--output=$ \
| gzip > /data/oldboy.json.gz
备注:使用nodejs_12.16.1会导致不兼容的情况,需要升级
npm install -g n
n latest
1、有些词语原本不是一个词语,通过中文分词器,人为的把不是一个词语的词,当成一个完整的词语,然后存到es内部的词典当中。比例“老男孩”
#官网下载地址
?
https://github.com/medcl/elasticsearch-analysis-ik/releases
#下载到指定目录
cd /opt/es-software
#使用命令安装(如果集群有多台机器,每台服务器都要安装)
cd /usr/share/elasticsearch
./bin/elasticsearch-plugin install file:///opt/es-software/elasticsearch-analysis-ik-6.6.0.zip
#把es服务重启一下
systemctl restart elasticsearch
#创建一个新的索引(在kibana界面)
PUT news
#应用中文分词器的模板
curl -XPOST http://localhost:9200/news/text/_mapping -H ‘Content-Type:application/json‘ -d‘
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}‘
#往已创建好的索引里面写数据
POST /news/text/1
{"content":"美国留给伊拉克的是个烂摊子吗"}
?
POST /news/text/2
{"content":"公安部:各地校车将享最高路权"}
?
POST /news/text/3
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
?
POST /news/text/4
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
#通过kibana进行验证词语是否能正常切分
POST /news/_search
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
?
#安装nginx服务
yum install nginx -y
#添加一个配置文件(gbk字符会出现冲突,如果发有问题,可以删除gbk)
cd /etc/nginx/conf.d/
vim www.conf
server {
listen 80;
server_name elk.oldboy.com;
location / {
root /usr/share/nginx/html/download;
charset utf-8,gbk;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
}
}
#检查代码是否误
nginx - t
#创建目录
mkdir /usr/share/nginx/html/download
#新建一个文件,存放临时要加入的词语
vim dic.txt
老男孩
老朋友
。。。
#域名解析
要修改一下host文件
10.0.0.240 elk.oldboy.com
#在浏览器中测试一下,(当dic.txt文件发生改变时,关注modifily-time etag是否发生变化)
elk.oldboy.com
#修改IKAnalyzer.cfg.xml(集群中要是有多台服务器,每台服务器都要修改)
cd /etc/elasticsearch/analysis-ik
vim IKAnalyzer.cfg.xml
<entry key="remote_ext_dict">http://10.0.0.240/download/dic.txt</entry>
#重启elasticsearch服务
systemctl restart elasticsearch
#关注日志文件,看是否有扩展的字典内容加载,如果有加载说明扩展词典已经生效
#在kibana界面测试,根据我们新加入的词语,来创建一个词条
POST /news/text/6
{"content":"北京老女孩教育"}
POST /news/text/7
{"content":"北京老男孩教育余小闯"}
#当词条写入成功之后,可以用命令检查
POST /news/_search
{
"query" : { "match" : { "content" : "余小闯" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
?
?
1、今天上午10点访问web站点排名前10的ip
2、统计今天访问前10的url
3、把今天上午10点之前访问量排名前10的ip和昨天这个时间段对比一下,有什么区别
4、发现页面的响应时间超1s,导致web服务响应过慢,找出是否爬虫ip
5、信息在5分钟内给我结果
代理层:nginx
web层:nginx tomcat php java
数据库:mysql mariadb es mongo
系统层:message rsyslog secure
#准备epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#安装nginx
yum install nginx -y
#添加配置文件
cd /etc/nginx/conf.d/
vim www.conf
server {
listen 80;
server_name elk.oldboy.com;
location / {
root /code/www;
index index.html index.htm;
}
}
#创建目录
mkdir /code/www -p
echo "Hello Oldboy Search Nginx Log! " > /code/www/index.html
#启动nginx服务
nginx -t
systemctl start nginx
#如果配置了域名,要记得做域名解析
?
#在浏览器中访问,可以产生一些访问日志
elk.oldboy01.com
#上传需要安装的软件包并安装
rpm -ivh filebeat-6.6.0-x86_64.rpm
#修改配置文件
vim /etc/filebeat/filebeat.yml
[root@web01-243 es-software]# egrep -v ‘^$|#‘ /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
?
output.elasticsearch:
hosts: ["10.0.0.240:9200"]
#启动filebeat服务
systemctl start filebeat
#在head插件界面,检查是否日志已经收集成功
?
?
#修改nginx的配置文件
cd /etc/nginx
vim nginx.conf
log_format json ‘{ "time_local": "$time_local", ‘
‘"remote_addr": "$remote_addr", ‘
‘"referer": "$http_referer", ‘
‘"request": "$request", ‘
‘"status": $status, ‘
‘"bytes": $body_bytes_sent, ‘
‘"agent": "$http_user_agent", ‘
‘"x_forwarded": "$http_x_forwarded_for", ‘
‘"up_addr": "$upstream_addr",‘
‘"up_host": "$upstream_http_host",‘
‘"upstream_time": "$upstream_response_time",‘
‘"request_time": "$request_time"‘
‘ }‘;
?
access_log /var/log/nginx/access.log json;
#检查代码是否有误
nginx -t
#重启nginx服务
systemctl restart nginx
#通过浏览器来访问nginx站点,生产日志,确保日志格式是json
#虽然nginx日志格式已经为json格式,但是filebeat无法解析json格式的日志(格式对齐)
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
?
#重启filebeat服务
systemctl restart filebeat
?
#在head插件界面,检查日志是否收集成功,是已经把日志切分开
management--->index-patterns
1、索引名称可以自定
2、把日志可以按月来收集,如果按天收集太乱
3、如果使用自定义的模板,要加入相应的配置信息
?
#修改filebeat的配置文件
vim filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
output.elasticsearch:
hosts: ["10.0.0.240:9200"]
indices:
- index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
source: "/var/log/nginx/access.log"
- index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
source: "/var/log/nginx/error.log"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
#重新启动服务
systemctl restart filebeat
#在head界面把之前的老索引删除
#重新再生产一些访问的日志和错误日志
#再回到head界面检查日志是否正常收集
?
#上传java安装包并安装
cd /opt/es-software
rpm -ivh jdk-8u102-linux-x64.rpm
#上传tomcat安装包并解压
tar xf apache-tomcat-8.5.49.tar.gz -C /opt/
ln -s /opt/apache-tomcat-8.5.49 /opt/tomcat
#启动tomcat服务,正常情况下会监听8080端口
cd /opt/tomcat/bin/
./startup.sh
#为了更好的分析日志,需要对tomcat的日志格式进行修改(162行)
cd /opt/tomcat/conf/server.xml
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","method":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","AgentVersion":"%{User-Agent}i"}"/>
#重启tomcat服务,让修改后的配置文件生效
./shutdown.sh
./startup.sh
#在浏览器中再次访问tomcat首页,产生一些新的日志
#把新的json日志,做校验处理,是否符合json格式
#进行filebeat主配置文件所在的目录
cd /etc/filebeat/
vim filebeat.yml
[root@web01-243 logs]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
- type: log
enabled: true
paths:
- /opt/tomcat/logs/localhost_access_log.2020-03-18.txt
json.keys_under_root: true
json.overwrite_keys: true
tags: ["tomcat"]
?
output.elasticsearch:
hosts: ["10.0.0.240:9200"]
indices:
- index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "access"
- index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "error"
- index: "tomcatl-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
tags: "tomcat"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
#重启filebeat服务
systemctl restart filebeat
#到head插件界面验证,日志是否能正常收集
?
#使用elasticsearch这个环境,修改es的配置文件
vim /etc/elasticsearch/elasticsearch.yml
aanode.name: oldboy02
#重启es服务
systemctl restart elasticsearch
#使用命令查看elasticsearch日志信息,发现有报错的信息输出
tail -f /var/log/ealsticsearch/elasticsearch.log
?
#安装filebeat工具,修改相应的主配置文件,把默认的配置文件改一名
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/elasticsearch/elasticsearch.log
multiline.pattern: ‘^\[‘
multiline.negate: true
multiline.match: after
?
output.elasticsearch:
hosts: ["10.0.0.240:9200"]
index: "es-%{[beat.version]}-%{+yyyy.MM}"
setup.template.name: "es"
setup.template.pattern: "es-*"
setup.template.enabled: false
setup.template.overwrite: true
#重启filebeat服务
systemctl restart filebeat
#head界面检查日志是否能正常收集
#安装mariadb
yum install mariadb-server -y
#启动mariadb服务
systemctl start mariadb
#确认慢日志功能是否开启
show variables like ‘%slow_query_log%‘;
--------------------+--------------------+
| Variable_name | Value |
+---------------------+--------------------+
| slow_query_log | OFF |
| slow_query_log_file | web01-243-slow.log
#开启慢日志功能
vim /etc/my.cnf
[mysqld]
slow_query_log=ON
slow_query_log_file=/var/log/mariadb/slow.log
long_query_time=1
#重新启动mariadb服务
systemctl restart mariadb
#生成一些慢查询语句
mysql
select sleep(2) user,host from mysql.user;
#确认慢日志是否生成
cat /var/log/mariadb/slow.log
#开启模块化功能(加动配置文件最下方)
vim /etc/filebeat/filebeat.yml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
reload.period: 10s
#重启filebeat服务
systemctl restart filebeat
#打开mysql模块(也可以通过修改模块的名称来开启)
filebeat modules list
filebeat modules enable mysql
#修改mysql模块配置
vim /etc/filebeat/modules.d/mysql.yml
?
- module: mysql
error:
enabled: true
var.paths: ["/var/log/mariadb/mariadb.log"]
?
slowlog:
enabled: true
var.paths: ["/var/log/mariadb/slow.log"]
#修改filebeat的主配置文件
vim /etc/filebeat/filebeat.yml
output.elasticsearch:
hosts: ["10.0.0.240:9200"]
indices:
- index: "mysql-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
source: "/var/log/mariadb/mariadb.log"
- index: "mysql-slow-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
source: "/var/log/mariadb/slow.log"
setup.template.name: "mysql"
setup.template.pattern: "mysql-*"
setup.template.enabled: false
setup.template.overwrite: true
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
reload.period: 10s
#重启filebeat服务
systemctl restart filebeat
#为了测试需要,别外生成一些慢日志和错误
#在head界面检查日志是否收集成功
?
#修改nginx日志的格式
vim /etc/nginx/nginx.conf
access_log /var/log/nginx/access.log main;
#检查语法
nginx -t
#启动nginx服务
systemctl start nginx
#检查日志信息
?
#进入相应的目录
cd /etc/filebeat/modules.d
#打开nginx模块功能
filebeat modules enble nginx
mv nginx.yml.disabled nginx.yml
#修改nginx模块
vim nginx.yml
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log"]
?
error:
enabled: true
var.paths: ["/var/log/nginx/error.log"]
#修改filebeat的主配置文件
vim /etc/filebeat/filebeat.yml
output.elasticsearch:
hosts: ["10.0.0.240:9200"]
indices:
- index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
source: "/var/log/nginx/error.log"
- index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
when.contains:
source: "/var/log/nginx/access.log"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
reload.period: 10s
#重启filebeat服务
systemctl restart filebeat
#发现es缺少相关的插件(如果是集群,每台服务器都要安装)
2020-03-20T12:56:11.819+0800 ERROR pipeline/output.go:100 Failed to connect to backoff(elasticsearch(http://10.0.0.240:9200)): Connection marked as failed because the onConnect callback failed: Error loading pipeline for fileset nginx/access: This module requires the following Elasticsearch plugins: ingest-user-agent, ingest-geoip. You can install them by running the following commands on all the Elasticsearch nodes:
sudo bin/elasticsearch-plugin install ingest-user-agent
sudo bin/elasticsearch-plugin install ingest-geoip
#解决方法,需要在每中es服务器上面安装两个插件
cd /usr/share/elasticsearch/
./bin/elasticsearch-plugin install file:///opt/es-software/ingest-geoip-6.6.0.zip
./bin/elasticsearch-plugin install file:///opt/es-software/ingest-user-agent-6.6.0.zip
#重启es服务和filebeat服务
systemctl restart elasticsearch
systemctl restart filebeat
#在head界面验证日志是否正常收集
?
?
#配置epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#安装redis
yum install redis -y
#修改redis配置文件
vim /etc/redis.conf
bind 10.0.0.240
#启动redis服务
systemctl start redis
?
#把nginx的日志格式改为json
#修改filebeat的主配置文件
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
?
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
?
output.redis:
hosts: ["10.0.0.240"]
keys:
- key: "nginx_access"
when.contains:
tags: "access"
- key: "nginx_error"
when.contains:
tags: "error"
?
setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true
#重启filebeat服务
systemctl restart filebeat
#连接redis
redis-cli -h 10.0.0.240
#使用命令检查
info
db0:keys=2,expires=0,avg_ttl=0
?
#进行目录
cd /opt/es-software
#上传logstash安装包,并安装
rpm -ivh logstash-6.6.0.rpm
#添加一个配置文件
cd /etc/logstash/con.d
vim nginx_log.conf
input {
redis {
host => "10.0.0.240"
port => "6379"
db => "0"
key => "nginx_access"
data_type => "list"
}
redis {
host => "10.0.0.240"
port => "6379"
db => "0"
key => "nginx_error"
data_type => "list"
}
}
?
filter {
mutate {
convert => ["upstream_time", "float"]
convert => ["request_time", "float"]
}
}
?
output {
stdout {}
if "access" in [tags] {
elasticsearch {
hosts => "http://10.0.0.240:9200"
manage_template => false
index => "nginx_access-%{+yyyy.MM}"
}
}
if "error" in [tags] {
elasticsearch {
hosts => "http://10.0.0.240:9200"
manage_template => false
index => "nginx_error-%{+yyyy.MM}"
}
}
}
#可以放到前台测试logstash能否正常取日志(如果正常,前台会打印日志信息)
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_log.conf
#在head界面验证
10.0.0.240 es01
10.0.0.241 es02
10.0.0.242 es03
架构图
#把需要的软件包上传
cd /opt/es_software
#解压软件并做软件连接
tar zxf zookeeper-3.4.11.tar.gz -C /opt/
ln -s /opt/zookeeper-3.4.11/ /opt/zookeeper
#创建一个数据目录
mkdir -p /data/zookeeper
#把zoo_sample.cfg复制一个zoo.cfg文件
cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
#修改zoo.cfg配置文件
vim /opt/zookeeper/conf/zoo.cfg
# 服务器之间或客户端与服务器之间维持心跳的时间间隔
# tickTime以毫秒为单位
tickTime=2000
# 集群中的follower服务器(F)与leader服务器(L)之间的初始连接心跳数
initLimit=10
# 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数
syncLimit=5
dataDir=/data/zookeeper
# 客户端连接端口
clientPort=2181
# 三个接点配置,格式为: server.服务编号=服务地址、LF通信端口、选举端口
server.1=10.0.0.240:2888:3888
server.2=10.0.0.241:2888:3888
server.3=10.0.0.242:2888:3888
echo "1" > /data/zookeeper/myid
cat /data/zookeeper/myid
/opt/zookeeper/bin/zkServer.sh start
/opt/zookeeper/bin/zkServer.sh status
#在一个节点上执行,创建一个频道
/opt/zookeeper/bin/zkCli.sh -server 10.0.0.240:2181
create /test "hello"
?
#在其他节点上看能否接收到
/opt/zookeeper/bin/zkCli.sh -server 10.0.0.240:2181
get /test
#es01操作
#上传安装包
cd /opt/es_software/
#解压并创建软连接
tar zxf kafka_2.11-1.0.0.tgz -C /opt/
ln -s /opt/kafka_2.11-1.0.0/ /opt/kafka
#创建一个日志目录
mkdir /opt/kafka/logs
#修改配置文件
vim /opt/kafka/config/server.properties
# broker的id,值为整数,且必须唯一,在一个集群中不能重复
broker.id=1
listeners=PLAINTEXT://10.0.0.240:9092
# 处理网络请求的线程数量,默认为3个
num.network.threads=3
# 执行磁盘IO操作的线程数量,默认为8个
num.io.threads=8
# socket服务发送数据的缓冲区大小,默认100KB
socket.send.buffer.bytes=102400
# socket服务接受数据的缓冲区大小,默认100KB
socket.receive.buffer.bytes=102400
# socket服务所能接受的一个请求的最大大小,默认为100M
socket.request.max.bytes=104857600
# kafka存储消息数据的目录
log.dirs=/opt/kafka/logs
# 每个topic默认的partition数量
num.partitions=1
# 在启动时恢复数据和关闭时刷新数据时每个数据目录的线程数量
num.recovery.threads.per.data.dir=1
#topic的offset的备份份数。建议设置更高的数字保证更高的可用性
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
#每个日志文件删除之前保存的时间
log.retention.hours=24
#这个属性就是每个文件的最大尺寸;当尺寸达到这个数值时,就会创建新文件
log.segment.bytes=1073741824
#检查日志分段文件的间隔时间,以确定是否文件属性是否到达删除要求。
log.retention.check.interval.ms=300000
# Zookeeper连接信息,如果是zookeeper集群,则以逗号隔开
zookeeper.connect=10.0.0.240:2181,10.0.0.241:2181,10.0.0.242:2181
# 连接zookeeper的超时时间,6s
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
/opt/kafka/bin/kafka-topics.sh --create --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --partitions 3 --replication-factor 3 --topic kafkatest
/opt/kafka/bin/kafka-topics.sh --describe --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --topic kafkatest
/opt/kafka/bin/kafka-topics.sh --delete --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --topic kafkatest
#创建命令
/opt/kafka/bin/kafka-topics.sh --create --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --partitions 3 --replication-factor 3 --topic messagetest
#测试发送消息
/opt/kafka/bin/kafka-console-producer.sh --broker-list 10.0.0.241:9092,10.0.0.242:9092,10.0.0.240:9092 --topic messagetest
#其他节点测试接收
/opt/kafka/bin/kafka-console-consumer.sh --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --topic messagetest --from-beginning
#测试获取所有的频道
/opt/kafka/bin/kafka-topics.sh --list --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
tags: ["access"]
?
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
?
output.kafka:
hosts: ["10.0.0.240:9092", "10.0.0.241:9092", "10.0.0.242:9092"]
topic: ‘filebeat‘
?
setup.template.name: "nginx"
setup.template.pattern: "nginx_*"
setup.template.enabled: false
setup.template.overwrite: true
#重启filebeat服务
systemctl restart filebeat
vim /etc/logstash/conf.d/kafka.conf
input {
kafka{
bootstrap_servers=>"10.0.0.240:9092"
topics=>["filebeat"]
group_id=>"logstash"
codec => "json"
}
}
?
filter {
mutate {
convert => ["upstream_time", "float"]
convert => ["request_time", "float"]
}
}
?
output {
stdout {}
if "access" in [tags] {
elasticsearch {
hosts => "http://10.0.0.240:9200"
manage_template => false
index => "nginx_access-%{+yyyy.MM}"
}
}
if "error" in [tags] {
elasticsearch {
hosts => "http://10.0.0.240:9200"
manage_template => false
index => "nginx_error-%{+yyyy.MM}"
}
}
}
#重新启动logstash服务
原文:https://www.cnblogs.com/wx1899325/p/12923911.html