ELK架构设计

逻辑架构图
在这里插入图片描述
部署架构图

在这里插入图片描述
部署规划
ELK部署
第二章 部署ELK
部署ES
配置解析
修改hosts
vim /etc/hosts

添加解析
192.168.1.55 node55
192.168.1.56 node56
192.168.1.57 node57
192.168.1.55 www.kun.com bbs.kun.com blog.kun.com
192.168.1.56 www.peng.com bbs.peng.com blog.peng.com

同步到其他服务器
scp -rp /etc/hosts 192.168.1.56:/etc/
scp -rp /etc/hosts 192.168.1.57:/etc/
安装配置 java
上传jdk-8u144-linux-x64.rpm到/usr/local/src/

切换目录
cd /usr/local/src

安装jdk
rpm -ivh jdk-8u144-linux-x64.rpm
写入环境变量
vim /etc/profile

尾部追加
JAVA_HOME=/usr/java/jdk1.8.0_144
JRE_HOME=/usr/java/jdk1.8.0_144/jre
PATH= J A V A H O M E / b i n : JAVA_HOME/bin: JAVAHOME/bin:JRE_HOME/bin: P A T H C L A S S P A T H = . : PATH CLASSPATH=.: PATHCLASSPATH=.:JAVA_HOME/lib/dt.jar: J A V A H O M E / l i b / t o o l s . j a r : JAVA_HOME/lib/tools.jar: JAVAHOME/lib/tools.jar:JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH

刷新生效
source /etc/profile
时间同步
需要先确认防火墙、selniux是否关闭
systemctl status firewalld
getenforce

使用yum安装
yum install ntp -y

然后查找ntp的配置文件,一般在/etc/ntp.conf
然后配置自己所在的州的服务器列表,我们在亚洲,所有只需要添加
server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst
将原来的默认配置服务器地址注销 使用#注销
最好是到光网上查询一下 ,现有的服务器列表,查询地址 www.pool.ntp.org

从服务器的server 地址写成主服务器
比如 server node0

然后 在配置文件最后一行 加入logfile /var/log/ntp.log 指定ntp服务的日志位置,这样有利于以后查询问题数据。

保存配置文件启动ntp服务,如果有防火墙必须配置ntp通过

firewall-cmd --add-service=ntp --permanent

firewall-cmd --reload

配好后,开启服务
#systemctl start ntpd
#systemctl enable ntpd
#systemctl status ntpd
#service ntpd stop

Ntp服务器需要首先获取到时间,然后才能对客户端提供服务,
如果ntp服务器需要从本地获取到时间则需要在服务器中添加如下配置:
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 8

验证服务是否开启
ntpq -p #查看同步情况,类似于ping

ntpq -p

结果含义如下:
Ø remote refid st t when poll reach delay offset jitter
Ø ==================================================================
Ø *10.247.160.31 10.240.241.5 4 u 53 64 377 0.240 0.374 0.240
Ø
Ø remote :响应这个请求的NTP服务器的名称。

Ø refid :NTP服务器使用的上一级ntp服务器。

Ø st :remote远程服务器的级别. 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.

Ø when: 上一次成功请求之后到现在的秒数。

Ø poll : 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小

Ø reach: 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加

Ø delay: 从本地机发送同步要求到ntp服务器的round trip time

Ø offset :主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。offset越接近于0,主机和ntp服务器的时间越接近

Ø jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小,主机的时间就越精确

Ø t :未知其含义,请补充。

ntpdate -q 0.asia.pool.ntp.org #指定服务器进行时间查询,不进行同步,使用该命令之前,请一定要先停止ntpd服务,否则会出现端口被占用的情况
ntpdate –u server #指定服务器进行同步,使用该命令之前,请一定要先停止ntpd服务

有返回值说明配置成功
重启进行验证

集群中的主服务器可以配置需要约束的访问服务器ip,一般没有必要做以下的操作:

restrict 10.221.18.112 mask 255.255.255.240 nomodify#
请检查从服务器是否正确的同步时间,只需要从主服务上配置即可

NTP的主配置文件/etc/ntp.conf解析

Permit time synchronization with our time source, but do not

permit the source to query or modify the service on this system.

允许其他客户端与本时间服务器进行同步,但不允许客户端去查询或者修改这个服务器上的服务资源。

restrict default kod nomodify notrap nopeer noquery 《=====拒绝所有IPv4的用户

restrict -6 default kod nomodify notrap nopeer noquery 《=====拒绝I所有Pv6的用户

Restrict 172.25.25.25 《=========允许这个IP进入NTP服务器


参数介绍:

ignore:拒绝所有类型的NTP连接

Nomodify:客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端仍可通过这部主机来进行网络校时

Notrap:不提供trap这个远程事件登陆(remote event logging)的功能

Noquery:客户端不能够使用ntpc与ntpq等命令来查询时间服务器,等于不提供NTP的网络校时

Notrust:拒绝没有认证的客户端

如果使用在没有参数的地方添加,表示该IP或者网段不受任何限制

如:

172.25.25.25 表示该IP不受任何限制

一般可以先关闭NTP的同步权限,再一个一个的添加可以同步的客户端IP或网段。


Permit all access over the loopback interface. This could

允许所有本地回环接口。

be tightened as well, but to do so would effect some of

the administrative functions.

restrict 127.0.0.1 《===放行本机回环IPv4形式

restrict -6 ::1 《====放行本机回环IPv6形式

Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 《====放行局域网来源

Use public servers from the pool.ntp.org project.

Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 0.rhel.pool.ntp.org ======上层同步的服务器

server 1.rhel.pool.ntp.org ======上层同步的服务器

server 2.rhel.pool.ntp.org ======上层同步的服务器

安装配置 elasticsearch
官方地址:
https://www.elastic.co/guide/index.html

下载包

找到官网下的跳转链接

进入链接,点击下载

点击版本选择

选择组件,选择版本(6.6.0)

下载对应的系统版本(迅雷多线程下载够快)

页面链接
https://www.elastic.co/cn/downloads/past-releases#elasticsearch

下载链接
https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.6.0-x86_64.rpm
https://artifacts.elastic.co/downloads/logstash/logstash-6.6.0.rpm
https://artifacts.elastic.co/downloads/kibana/kibana-6.6.0-x86_64.rpm
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm

上传安装包到/usr/local/src/

切换目录

cd /usr/local/src

复制到其他服务器

scp -rp /usr/local/src/* 192.168.1.56:/usr/local/src/
scp -rp /usr/local/src/* 192.168.1.57:/usr/local/src/

安装包

rpm -ivh elasticsearch-6.6.0.rpm
systemctl daemon-reload
systemctl enable elasticsearch.service

相关配置目录及配置文件

/etc/elasticsearch/elasticsearch.yml #主配置文件
/etc/elasticsearch/jvm.options #java虚拟机配置
/etc/init.d/elasticsearch #init.d启动脚本
/etc/sysconfig/elasticsearch #环境变量相关信息,不需要动
/usr/lib/sysctl.d/elasticsearch.conf #环境变量相关
/usr/lib/systemd/system/elasticsearch.service #systemd启动脚本

备份配置文件

cp /etc/elasticsearch/elasticsearch.yml {,.ori}

修改主配置文件

vim /etc/elasticsearch/elasticsearch.yml

把配置拷贝到其他两个节点

scp -rp /etc/elasticsearch/elasticsearch.yml 192.168.1.56:/etc/elasticsearch/elasticsearch.yml
scp -rp /etc/elasticsearch/elasticsearch.yml 192.168.1.57:/etc/elasticsearch/elasticsearch.yml

查看修改后

grep 1 /etc/elasticsearch/elasticsearch.yml
node55节点

node56

node57

配置说明

cluster.name: kunpeng #集群模式,必须打开,同一个集群要全部一样
node.name: kun-1 #节点名称,每个节点都不一样
path.data: /data/elasticsearch #如果你更换了目录,要授权给es用户和组
path.logs: /var/log/elasticsearch #如果是集群模式,日志名为{集群名.log}
bootstrap.memory_lock: true #内存锁定,一定要打开,然后修改system配置
network.host: 192.168.1.55,127.0.0.1 #绑定内网IP,本地IP可以选择不做
http.port: 9200 #默认9200,还有一个隐藏的通讯端口,9300

#集群发现地址,只要包含自己和集群内任意一个节点就可以。
discovery.zen.ping.unicast.hosts: [“10.0.0.51”, “10.0.0.52”]
discovery.zen.minimum_master_nodes: 2 #选举相关参数,多数节点数量node/2 + 1

##############################下面两条是跨域设置
http.cors.enabled: true #为了让es-head可以访问es,同下
http.cors.allow-origin: “*” #为了让es-head可以访问es,同上

创建新目录

mkdir -p /data/elasticsearch

授权新目录

chown -R elasticsearch:elasticsearch /data/elasticsearch

###查看权限
ls -ld /data/elasticsearch/

开启锁功能

vim /usr/lib/systemd/system/elasticsearch.service

在Service块增加如下参数

[Service]
LimitMEMLOCK=infinity

重新启动

systemctl daemon-reload
systemctl start elasticsearch.service

检查存活状态

netstat -lntup|grep 9200

systemctl status elasticsearch.service

curl localhost:9200

安装es-head插件
官方地址:
https://github.com/mobz/elasticsearch-head

docker版本安装(可略)

docker pull alivv/elasticsearch-head
docker run --name es-head -p 9100:9100 -dit alivv/elasticsearch-head

nodejs编译安装(可略)

官方地址:
https://nodejs.org/en/download/package-manager/
https://nodejs.org/dist/latest-v10.x/
http://npm.taobao.org

下载安装

yum install nodejs npm openssl screen -y
node -v
npm -v
npm install -g cnpm --registry=https://registry.npm.taobao.org
cd /opt/
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
cnpm install
screen -S es-head
cnpm run start
Ctrl+A+D

谷歌WEB插件安装(推荐)

1.在线安装
进入谷歌应用商店:https://chrome.google.com/webstore/category/extensions
搜索关键字:elasticsearch-head
将其添加到应用程序

安装完成后点击右上角图标打开

安装完成后右上角点击相应图标打开

2.解压文件夹安装
下载该插件,地址:https://github.com/liufengji/es-head/blob/master/elasticsearch-head.crx
下载后的文件名是:elasticsearch-head.crx
若提示:程序包无效:“CRX_HEDER_INVALID”,则按下面的步骤进行操作
·
将文件elasticsearch-head.crx直接解压缩,得到一个elasticsearch-head文件夹
进入到该文件夹里,将_metadata文件夹重命名为metadata
再次进行加载文件夹

修改 ES 配置文件支持跨域(可略,上文已配)

访问测试: IP 地址:9100

部署kibana

上传安装包kibana-6.6.0-x86_64.rpm

安装kibana
rpm -ivh kibana-6.6.0-x86_64.rpm

备份配置文件

cp /etc/kibana/kibana.yml{,.ori}

修改配置
vim /etc/kibana/kibana.yml

修改后的kibana配置文件

grep “2” /etc/kibana/kibana.yml
server.port: 5601
server.host: “192.168.47.175”
elasticsearch.hosts: [“http://localhost:9200”]
kibana.index: ".kibana

启动kibana
systemctl start kibana

查看kibana状态

systemctl status kibana

查看kibana监听

netstat -lntup|grep 5601

登陆页面查看

部署logstash
###rpm安装
rpm -ivh logstash-6.6.0.rpm

###编写配置文件
vim /etc/logstash/logstash.yml
###修改前先备份
cp /etc/logstash/logstash.yml{,.ori}
接收数据(input)
本地接收(不推荐)
input {
file {
path => “/usr/local/nginx/logs/access.log”
}
}
output {
elasticsearch {
hosts => [“http://192.168.1.55:9200”]
}
}

普通接收(不推荐)
input {
redis {
host => “127.0.0.1”
port => “6379”
db => “0”
key => “bbs”
data_type => “list”
password: ‘kun’
}

redis {
host => “127.0.0.1”
port => “6379”
db => “0”
key => “www”
password: ‘kun’
data_type => “list”
}
}

#filter {

mutate {

convert => [“upstream_time”, “float”]

convert => [“request_time”, “float”]

}

#}

output {
if “bbs” in [tags] {
stdout {}
elasticsearch {
hosts => “http://192.168.1.55:9200”
manage_template => false
index => “nginx-bbs-%{+yyyy.MM}”
}
}

if "www" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-www-%{+yyyy.MM}"
  }
}

}

通过中间件接收(redis版)

添加思路

1.logstash input添加redis key
2.logstash output添加索引

修改配置文件

vim /etc/logstash/conf.d/redis.conf

配置文件内容

input {
redis {
host => “192.168.1.59”
port => “6380”
db => “0”
key => “all_key”
password => ‘kunpeng’
data_type => “list”
}
}

#filter {

mutate {

convert => [“upstream_time”, “float”]

convert => [“request_time”, “float”]

}

#}

output {
if “bbs” in [tags] {
stdout {}
elasticsearch {
hosts => “http://192.168.1.55:9200”
manage_template => false
index => “nginx-bbs-%{+yyyy.MM}”
}
}

if "www" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-www-%{+yyyy.MM}"
  }
}

if "blog" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-blog-%{+yyyy.MM}"
  }
}

if "tomcat" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-tomcat-%{+yyyy.MM}"
  }
}

if "test" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-test-%{+yyyy.MM}"
  }
}

}

启动logstatsh
启动配置文件
前台启动
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis.conf

后台启动
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis.conf &
多实例

数据分析
正则表达式
正则表达式
使用给定好的符号去表示某个含义
例如.代表任意字符
正则符号当普通符号使用需要加反斜杠

普通正则表达式
. 任意一个字符

  • 前面一个字符出现0次或者多次
    [abc] 中括号内任意一个字符
    [^abc] 非中括号内的字符
    [0-9] 表示一个数字
    [a-z] 小写字母
    [A-Z] 大写字母
    [a-zA-Z] 所有字母
    [a-zA-Z0-9] 所有字母+数字
    [^0-9] 非数字
    ^xx 以xx开头
    xx$ 以xx结尾
    \d 任何一个数字
    \s 任何一个空白字符

扩展正则表达式,在普通正则符号再进行了扩展
? 前面字符出现0或者1次

  • 前面字符出现1或者多次
    {n} 前面字符匹配n次
    {a,b} 前面字符匹配a到b次
    {,b} 前面字符匹配0次到b次
    {a,} 前面字符匹配a或a+次
    (string1|string2) string1或string2

信息过滤(未校验)
Grok提取Nginx日志
Grok使用(?提取内容)来提取xxx字段
提取客户端IP: (?[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})
提取时间: [(?[^ ]+ +[0-9]+)]

示例:
Nginx日志:
192.168.237.1 - - [24/Feb/2019:17:48:47 +0800] “GET /shijiange HTTP/1.1” 404 571 “-” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36”

Grok提取规则(一共划分为5个字段):
(?[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) - - [(?[^ ]+ +[0-9]+)] “(?[A-Z]+) (?[^ ]+) HTTP/\d.\d” (?[0-9]+) (?[0-9]+) “[^”]+" “(?[^”]+)"

logstash.yml模板:

input {
redis {
host => “192.168.1.59”
port => “6380”
db => “0”
key => “all_key”
password => ‘kunpeng’
data_type => “list”
}
}
filter {
grok {
match => {
“message” => ‘(?[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) - - [(?[^ ]+ +[0-9]+)] “(?[A-Z]+) (?[^ ]+) HTTP/\d.\d” (?[0-9]+) (?[0-9]+) “[^”]+" “(?[^”]+)"’
}
}
}
output {
if “bbs” in [tags] {
stdout {}
elasticsearch {
hosts => “http://192.168.1.55:9200”
manage_template => false
index => “nginx-bbs-%{+yyyy.MM}”
}
}

if "www" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-www-%{+yyyy.MM}"
  }
}

if "blog" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-blog-%{+yyyy.MM}"
  }
}

if "tomcat" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-tomcat-%{+yyyy.MM}"
  }
}

if "test" in [tags] {
  stdout {} 
  elasticsearch {
    hosts => "http://192.168.1.55:9200"
    manage_template => false
    index => "nginx-test-%{+yyyy.MM}"
  }
}

}}

去除字段
修改过滤器匹配规则
filter {
grok {
match => {
“message” => ‘(?[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) - - [(?[^ ]+ +[0-9]+)] “(?[A-Z]+) (?[^ ]+) HTTP/\d.\d” (?[0-9]+) (?[0-9]+) “[^”]+" “(?[^”]+)"’
}
remove_field => [“message”,"@version",“path”] #这里标识要去除的字段
}
}

部署filebeat
rpm -ivh filebeat-6.6.0-x86_64.rpm

备份配置文件

cp /etc/filebeat/filebeat.yml {,.ori}
ls -l /etc/filebeat/

修改配置文件
vim /etc/filebeat/filebeat.yml

  1. 普通日志收集(已缝合)
    官方网站:
    https://www.elastic.co/guide/en/beats/filebeat/7.x/filebeat-input-log.html
    https://www.elastic.co/guide/en/beats/filebeat/7.x/configuration-template.html
    https://www.elastic.co/guide/en/beats/filebeat/7.x/configuration-template.html
    例一:收集nginx

思路:
修改日志输出模式为json -> input采集日志 -> 输出到redis中间件

查看日志格式

tail -f /var/log/nginx/access.log

修改日志输出格式(main -> json)

vim /etc/nginx/nginx.conf

log_format main '{ “time_local”: “KaTeX parse error: Double superscript at position 22: …ocal", ' '̲"remote_addr": …remote_addr”, ’
'“referer”: “KaTeX parse error: Double superscript at position 24: …erer", ' '̲"request": "request”, ’
'“status”: $status, ’
‘“bytes”: KaTeX parse error: Double superscript at position 26: …_sent, ' '̲"agent": "http_user_agent", ’
‘“x_forwarded”: “KaTeX parse error: Double subscript at position 7: http_x_̲forwarded_for",…upstream_addr”,’
‘“up_host”: “KaTeX parse error: Double superscript at position 29: …_host",' '̲"upstream_time"…upstream_response_time”,’
‘“request_time”: “$request_time”’
’ }’

修改filebeat

tail -f /var/log/nginx/access.log
暂略

修改filebeat(参考模板)

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”]
      setup.template.settings:
      index.number_of_shards: 3
      setup.kibana:
      host: “192.168.47.175:5601”
      output.elasticsearch:
      hosts: [“localhost:9200”]
      indices:
    • index: “nginx_access-%{[beat.version]}-%{+yyyy.MM.dd}”
      when.contains:
      tags: “access”
    • index: “nginx_error-%{[beat.version]}-%{+yyyy.MM.dd}”
      when.contains:
      tags: “error”
      setup.template.name: “nginx”
      setup.template.pattern: “nginx_*”
      setup.template.enabled: false
      setup.template.overwrite: true

重启nginx

systemctl restart nginx
systemctl status nginx

压力测试

ab -c 10 -n 100 http://bbs.kun.com/
ab -c 10 -n 100 http://blog.kun.com/
ab -c 10 -n 100 http://www.kun.com/

查看日志格式是否为json

链接:https://www.sojson.com/
tail -f /var/log/nginx/www_access.log

校验

例二:收集tomcat

yum安装 tomcat

yum install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp tomcat-javadoc -y

启动

systemctl start tomcat
systemctl status tomcat

检查

yum install lsof -y
lsof -i:8080
curl 127.0.0.1:8080

修改日志为json格式

vim /etc/tomcat/server.xml
跳转到139行,替换掉指定行

被替换内容

           pattern="%h %l %u %t "%r" %s %b" />

替换内容

pattern="{“clientip”:"%h",“ClientUser”:"%l",“auth
enticated”:"%u",“AccessTime”:"%t",“method”:&q
uot;%r",“status”:"%s",“SendBytes”:"%b",“Q
uery?string”:"%q",“partner”:"%{Referer}i",“AgentVersio
n”:"%{User-Agent}i"}"/>

替换后

重启tomcat

systemctl restart tomcat
systemctl status tomcat

跟踪日志查看格式是否为json

tail -f /var/log/tomcat/localhost_access_log.日期.txt

格式校验

链接:https://www.sojson.com/

filebeat配置:

filebeat.inputs:

  • type: log
    enabled: true
    paths:
    - /var/log/tomcat/localhost_access_log*
    json.keys_under_root: true
    json.overwrite_keys: true

setup.kibana:
host: “192.168.47.175:5601”

output.elasticsearch:
hosts: [“localhost:9200”]
index: “tomcat-%{[beat.version]}-%{+yyyy.MM.dd}”

setup.template.name: “tomcat”
setup.template.pattern: “tomcatn-*”
setup.template.enabled: false
setup.template.overwrite: true

例三:普通日志模板(redis)
ilebeat.inputs:

  • type: log
    ebabled: true
    paths:
    • /var/log/nginx/bbs_access.log
      json.keys_under_root: true
      json.overwrite_keys: true

setup.kibana:
host: “192.168.1.55:5601”

output.redis:
hosts: [“192.168.1.55”]
key: “filebeat”
db: 0
timeout: 5

setup.template.name: “nginx”
setup.template.pattern: “nginx-*”
setup.template.json.enabled: false
setup.template.overwrite: true

  1. java日志收集(已缝合)
    官方地址:https://www.elastic.co/guide/en/beats/filebeat/6.6/multiline-examples.html
    参考配置文件:/etc/filebeat/filebeat.yml

修改filebeat

vim /etc/filebeat/filebeat.yml

增加字段,打开多行java收集功能(合并同类项)

filebeat.inputs:
#########TAG##########

  • type: log
    enabled: true
    paths:

    • /var/log/tomcat/localhost_acc_log.*.log
      tags: [“tomcat”]
      json.keys_under_root: true
      json.overwrite_keys: true
  • type: log
    enabled: true
    paths:

    • /var/log/elasticsearch/集群名称.log
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: [“java”]

##########功能选项########
multiline.pattern: ‘^[’
multiline.negate: true
multiline.match: after
json.keys_under_root: true
json.overwrite_keys: true

##########指定kibana#######
setup.kibana:
host: “192.168.47.175:5601”

##########输出源###########
output.elasticsearch:
hosts: [“192.168.1.55:9200”]
indices:
- index: “tomcat_access-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
tags: “tomcat”
- index: “es_access-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
tags: “java”

#########启用自定义模板#####
setup.template.name: “es”
setup.template.pattern: “es-*”
setup.template.enabled: false
setup.template.overwrite: true

重启tomcat服务

systemctl restart tomcat
systemctl status tomcat

重启filebeat

systemct restart filebeat
systemct status filebeat

  1. modules日志收集(未校验,有问题请参考官方文档)
    官方参考:
    https://www.elastic.co/guide/en/beats/filebeat/6.6/configuration-filebeat-modules.html

例一:nginx日志收集

打开filebeat modules list

在模板文件查找modules的配置文件,提取出来下面内容,写入字段
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
reload.period: 10s

开启modules.d目录下的模板功能

filebeat modules enable nginx

mv 文件名.yml.disable 文件名.yml

给模板文件添加nginx日志路径

vim /pwd/nginx.yml

filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
reload.period: 10s

setup.kibana:
hos: “192.168.1.55:5601”

output.elasticsearch:
hosts: [“192.168.1.55:9200”]
indices:
- index: “nginx_access-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
source: “/var/log/nginx/bbs_access.log”
- index: “nginx_error-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
filebeat.name: “error”

4.把nginx配置文件改回普通格式
vim /etc/nginx/nginx.conf
log_format main '{ “time_local”: “KaTeX parse error: Double superscript at position 40: … '̲"remote_addr": …remote_addr”, ’
'“referer”: “KaTeX parse error: Double superscript at position 42: … '̲"request": "request”, ’
'“status”: $status, ’
‘“bytes”: KaTeX parse error: Double superscript at position 44: … '̲"agent": "http_user_agent", ’
‘“x_forwarded”: “KaTeX parse error: Double subscript at position 7: http_x_̲forwarded_for",…upstream_addr”,’
‘“up_host”: “KaTeX parse error: Double superscript at position 47: … '̲"upstream_time"…upstream_response_time”,’
‘“request_time”: “$request_time”’
’ }’;

include /etc/nginx/conf.d/*.conf;
access_log /var/log/nginx/access.log main;

子配置修改

vim /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name blog.kunpeng.com;
location / {
root /html/blog;
index index.html;
}
access_log /var/log/nginx/blog_access.log main;
}

5.清空原日志

/var/log/nginx/*.log

6.安装nginx激活插件
###在线安装
sudo bin/elasticsearch-plugin install ingest-user-agent
sudo bin/elasticsearch-plugin install ingest-geoip

###离线安装
wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/ingest-user- agent/ingest-user-agent-6.6.0.zip
wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/ingest- geoip/ingest-geoip-6.6.0.zip

/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/ingest-geoip- 6.6.0.zip
/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/ingest-user- agent-6.6.0.zip

###添加kinbana配置进入filebeat(可略,前面已添加)
setup.kibana:
host: “192.168.1.55:5601”

###重启es和filebeat
systemctl restart es
filebeat setup -e
systemctl restart filebeat

###kibana添加数据
pass

9.改进kibana图像识别
###备份删除不必要的视图文件并导入到kibana
cp -a /usr/share/filebeat/kibana /root

###切换目录
cd /kibana/7/dashboard

###关闭kibana,删除多余,删除索引
find . -type f ! -name “nginx”|xargs rm -rf
find . -type f ! -name “ml*”|xargs rm -rf

###编辑匹配规则,让规则去匹配我们修改了的filebeat格式
vim Filebeat-nginx-logs.json
cat Filebeat-nginx-logs.json

###看到第99行,替换指定字符,画图识别是因为这里匹配的是filebeat字段,而不是nginx字段
sed -n ‘s#filebeat-*#nginx_*#gp’ Filebeat-nginx-logs.json
sed -i ‘s#filebeat-*#nginx_*#g’ Filebeat-nginx-logs.json
sed -i ‘s#filebeat-*#nginx_*#g’ Filebeat-nginx-overview.json

###切换目录
cd …/index-pattern
ls
sed -n ‘s#filebeat-*#nginx_*#gp’ filebeat.json
sed -i ‘s#filebeat-*#nginx_*#g’ filebeat.json

###指定读取位置
cd /
filebeat setup --dashboards -E setup.dashboards.directory=/root/kibana/

###删除kibana库,重启kibana和filebeat
在es-head上删除

例二:mysql慢日志收集

开启慢日志

set global slow_query_log=ON;

设置慢查询日志存放位置

set global slow_query_log_file=’/var/lib/mysql/slow.log’;

或查询慢日志存放位置
show variables like ‘slow_query%’;

设置超过5s就记录

set global long_query_time=5

追加到配置文件

vim /etc/my.conf
slow_query_log=ON slow_query_log_file=/var/lib/mysql/slow.log
long_query_time=5

手动查询漫日志

select user,host from mysql.user ;
select sleep(1) user,host from mysql.user ;

开启mysql日志收集

filebeat modules enable mysql
filebeat modules list

修改modules配置文件

vim /etc/filebeat/modules.d/mysql.yml

  • module: mysql
    error:
    enabled: true
    var.paths: ["/data/mysql/data/db03.err"]
    slowlog:
    enabled: true
    var.paths: ["/data/mysql/data/db03-slow.log"]

修改filebeat配置文件

vim /etc/filebeat/filebeat.yml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: true
reload.period: 10s

setup.kibana:
hos: “192.168.1.55:5601”

output.elasticsearch:
hosts: [“192.168.1.55:9200”]
indices:
- index: “mysql_slowlog-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
fileseat.name: “slowlog”
- index: “mysql_error-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
fileseat.name: “error”

setup.template.name: “mysql”
setup.template.pattern: “mysql_*”
setup.template.enabled: false
setup.template.overwrite: true

修改视图

###备份删除不必要的视图文件并导入到kibana
cp -a /usr/share/filebeat/kibana /root

###切换目录
cd /kibana/7/dashboard

###关闭kibana,删除多余,删除索引
find . -type f ! -name “mysql”|xargs rm -rf
find . -type f ! -name “ml*”|xargs rm -rf

###替换关键字
sed -i ‘s#filebeat-*#mysql-*#g’ Filebeat-mysql-logs.json
sed -i ‘s#filebeat-*#mysql-*#g’ Filebeat-mysql-overview.json

  1. docker日志收集(未缝合)

下载镜像

docker pull nginx
docker run --name nginx -p 80:80 -d nginx
docker ps
doocker logs -f nginx
docker start nginx

修改filebeta,添加字段(单容器收集)

filebeat.inputs:

  • type: docker
    containers.ids:
  • ‘containers id’

filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
#reload.period: 10s

setup.kibana:
hos: “192.168.1.55:5601”

output.elasticsearch:
hosts: [“192.168.1.55:9200”]
#- index: ‘mysql_slowlog-%{[beat.version]}-%{+yyyy.MM}’
indices:
- index: “mysql_slowlog-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
fileseat.name: “slowlog”
- index: “mysql_error-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
fileseat.name: “error”

setup.template.name: “mysql”
setup.template.pattern: “mysql_*”
setup.template.enabled: false
setup.template.overwrite: true

修改filebeta,添加字段(多容器收集,二选一模式)

filebeat.inputs:

  • type: docker
    containers.ids:
    #(选填,二选一模式)
    path: “/var/lib/docker/containers”
    stream: “stdout”
    ids:
    • “*”
      #(选填,二选一模式)
  • ‘*’
多容器收集进阶

####创建docker-compose目录
###切换目录
cd /
###创建目录
mkdir docker_compose

###编写docker-compose.yml
version: ‘3’
services:
nginx:
image: nginx:v2
# 设 置 labels
labels:
service: nginx
# logging设置增加labels.service
logging:
options:
labels: “service”
ports:
- “8080:80”
mysql:
image: nginx:latest
# 设 置 labels
labels:
service: mysql
# logging设置增加labels.service
logging:
options:
labels: “service”
ports:
- “80:80”

###启动容器编写
docker-compose up

###查看容器日志
cd /var/lib/docker/containers/
curl 192.168.1.55:8080
curl 192.168.1.55:80
cat id

###修改filebeat
filebeat.inputs:

  • type: log
    enabled: true
    paths:
    • /var/lib/docker/containers//-json.log
      json.keys_under_root: true
      json.overwrite_keys: true

setup.kibana:
host: “192.168.1.55:5601”

output.elasticsearch:
hosts: [“192.168.1.55:9200”]
indices:
- index: “docker-nginx-access-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
########服务lable标签字段#############
attrs.service: “nginx”
########标准错误输出#############
stream: “stdout”
- index: “docker-nginx-error-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
attrs.service: “nginx”
stream: “stderr”
- index: “docker-mysql-access-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
attrs.service: “mysql”
stream: “stdout”
- index: “docker-mysql-error-%{[beat.version]}-%{+yyyy.MM}”
when.contains:
attrs.service: “mysql”
stream: “stderr”

setup.template.name: “mysql”
setup.template.pattern: “mysql_*”
setup.template.enabled: false
setup.template.overwrite: true

  1. 缝合成品(redis+java+普通日志)
    添加消息队列
    官方参考:https://www.elastic.co/guide/en/beats/filebeat/6.6/redis-output.html
    官方参考:https://www.elastic.co/guide/en/logstash/6.6/plugins-inputs-redis.html

添加思路

1.filebeat input中添加路径和tag
2.filebeat output添加路径和tag

修改filebeat

#########input引入日志##########
#########引入main格式的nginx日志###
filebeat.inputs:

  • type: log
    enabled: true
    paths:

    • /var/log/nginx/bbs_access.log
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: [“bbs”]
  • type: log
    enabled: true
    paths:

    • /var/log/nginx/www_access.log
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: [“www”]
  • type: log
    enabled: true
    paths:

    • /var/log/nginx/blog_access.log
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: [“blog”]

#########引入java格式的tomcat日志###

  • type: log
    enabled: true
    paths:
    • /var/log/tomcat/localhost.*.log
      multiline.pattern: ‘^[’
      multiline.negate: true
      multiline.match: after
      tags: [“tomcat”]

#########引入任意普通格式的日志###

  • type: log
    enabled: true
    paths:
    • /var/log/tomcat/catalina.2020-07-24.log
      tags: [“test”]

########指定kibana###############
setup.kibana:
host: “192.168.1.55:5601”

#######指定redis中间件###########
output.redis:
hosts: [“192.168.1.59”]
port: 6380
key: “all_key”
password: ‘kunpeng’
timeout: 5

#######覆盖模板###################
setup.template.name: “nginx”
setup.template.pattern: “nginx-*”
setup.template.enabled: false
setup.template.overwrite: true

docker部署elk
version: ‘3’
services:
elasticsearch:
image: elasticsearch:v1
container_name: elasticsearch
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- “ES_JAVA_OPTS=-Xms8g -Xmx8g”
- “discovery.zen.ping.unicast.hosts=elasticsearch”
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /data/docker_es_data:/usr/share/elasticsearch/data
ports:
- 19200:9200
networks:
- esnet
elasticsearch-head:
image: elasticsearch-head:v1
container_name: elasticsearch-head
ports:
- 19100:9100
networks:
- esnet
kibana:
image: kibana:v1
container_name: kibana
environment:
- ELASTICSEARCH_URL=“http://elasticsearch:9200”
- kibana.index=".kibana"
ports:
- 15601:5601
networks:
- esnet
logstash:
image: logstash:v1
container_name: logstash
environment:
- ELASTICSEARCH_URL=“http://elasticsearch:9200”
ports:
- 16379:6379
networks:
- esnet
networks:
esnet:

第三章 部署keepalived
keepalived安装

yum安装
yum install -y keepalived

二进制安装

修改配置

备份原文件

cp /etc/keepalived/keepalived.conf{,.ori}
ls -l /etc/keepalived/

清空配置文件

/etc/keepalived/keepalived.conf

修改配置文件

vim /etc/keepalived/keepalived.conf

配置nginx主备

将下面的信息写入配置文件(请注意环境变量)

主nginx节点

global_def {
router_id web01
}

vrrp_instance VI_1 {
state MASTER
interface ens3
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.59
}
}

备nginx节点

global_def {
router_id web02
}

vrrp_instance VI_1 {
state BACKUP
interface ens3
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.59
}
}

systemctl restart keepalived
systemctl status keepalived

第四章 部署redis
安装GCC插件
yum install gcc -y
yum install gcc-c++ -y
yum install -y jemalloc jemalloc-devel tcl tcl-devel -y

安装redis
解压安装包到 / home 目录下
tar -xvf /usr/local/src/redis-3.2.5.tar.gz -C /home
改名
mv /home/redis-3.2.5 /home/redis
进入redis目录进行编译
cd /home/redis
make -j test && make install
编译完成后进入目录 utils 执行
./install_server.sh
安装过程选项 默认按回车键
复制一份之前上传的6379.conf的配置文件并
cp /usr/local/src/6379.conf /etc/redis/6379.conf 覆盖原本的文件
修改配置文件6379.conf
vim /etc/redis/6379.conf

bind 0.0.0.0

protected-mode no

daemonize yes

logfile /var/log/redis_6379.log #指定日志文件的地方

save 900 1
save 300 10
save 60 50

slave-read-only yes

appendonly no #持久化模式关闭no

requirepass kunpeng #设置Redis连接密码(末尾自己加)

保存以上配置文件
把之前启动的redis进程 kill 掉
ps -ef | grep redis
kill 运行中的redis的进程号
重启redis
/etc/init.d/redis_6379 start
查看Redis状态
ps -ef | grep redis
redis优化
修改启动项目,禁用THP
在/etc/rc.local 中添加如下代码
vim /etc/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

保存后 赋予rc.local 执行权限
chmod +x rc.local
重启系统后,命令生效

在/etc/sysctl.conf中新增配置如下
vim /etc/sysctl.conf

vm.overcommit_memory = 1
net.core.somaxconn= 20000
保存后
sysctl -p #刷新配置
把之前启动的redis进程 kill 掉
ps -ef | grep redis
kill 运行中的redis的进程号
重启redis
/etc/init.d/redis_6379 start

第五章 部署nginx
nginx安装
yum 安装
yum install -y nginx
rpm 安装
上传rpm包进入/usr/local/src中
选用版本nginx-1.10.3-1.el7.ngx.x86_64.rpm
[root@nginx ~]#rpm –ivh nginx

修改配置
yum安装后,配置文件会出现在
etc/nginx/nginx.conf #nginx的配置文件

创建子文件

mkdir /html/{www,blog,bbs} -p

写入页面提示

web01
echo “web01 www” > /html/www/index.html
echo “web01 bbs” > /html/bbs/index.html
echo “web01 blog” > /html/blog/index.html

web02
echo “web02 www” > /html/www/index.html
echo “web02 bbs” > /html/bbs/index.html
echo “web02 blog” > /html/blog/index.html

编写配置文件,一共三个子配置文件

vim /etc/nginx/conf.d/www.conf

子配置文件内容

##模板
server {
listen 80;
server_name www.kunpeng.com;
location / {
root /html/www;
index index.html;
}
access_log /var/log/nginx/www_access.log json;
}

web01
server {
listen 80;
server_name www.kun.com;
location / {
root /html/www;
index index.html;
}
access_log /var/log/nginx/www_access.log main;
}
[root@node55 src]# cat /etc/nginx/conf.d/bbs.conf
server {
listen 80;
server_name bbs.kun.com;
location / {
root /html/bbs;
index index.html;
}
access_log /var/log/nginx/bbs_access.log main;
}
[root@node55 src]# cat /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name blog.kun.com;
location / {
root /html/blog;
index index.html;
}
access_log /var/log/nginx/blog_access.log main;
}

web02
[root@node56 ~]# vim /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.peng.com;
location / {
root /html/www;
index index.html;
}
access_log /var/log/nginx/www_access.log json;
}
[root@node56 ~]# vim /etc/nginx/conf.d/bbs.conf
server {
listen 80;
server_name bbs.peng.com;
location / {
root /html/bbs;
index index.html;
}
access_log /var/log/nginx/bbs_access.log json;
}
[root@node56 ~]# vim /etc/nginx/conf.d/blog.conf
server {
listen 80;
server_name blog.peng.com;
location / {
root /html/blog;
index index.html;
}
access_log /var/log/nginx/blog_access.log json;
}

用户授权
chown -R nginx:nginx /html/
ls -ld /html
web01

web02

nginx -t
systemctl start nginx
systemctl enable nginx
systemctl status nginx
systemctl restart nginx

负载均衡

尾部添加 stream 模块,要在 nginx.conf 里最后添加,而不是在 conf.d 里面添加子配置

vim /etc/nginx/nginx.conf
…web01、web02点保持一致
stream {
upstream redis {
server 192.168.1.55:6379 max_fails=2 fail_timeout=10s;
server 192.168.1.56:6379 max_fails=2 fail_timeout=10s backup;
}

server {
listen 6380;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass redis;
}
}


  1. a-z ↩︎

  2. a-z ↩︎

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值