本文收录在 Linux 运维企业架构实战系列
一 elselk 的介绍
- 1els,elk
- els:ElasticSearch,Logstash,Kibana,Beats
- elk:ElasticSearch,Logstash,Kibana
ElasticSearch 搜索引擎
ElasticSearch 是一个基于 Lucene 的搜索引擎, 提供索引, 搜索功能它提供了一个分布式多用户能力的全文搜索引擎, 基于 RESTful web 接口 Elasticsearch 是用 Java 开发的, 并作为 Apache 许可条款下的开放源码发布, 是当前流行的企业级搜索引擎设计用于云计算中, 能够达到实时搜索, 稳定, 可靠, 快速, 安装使用方便
Logstash 接收, 处理, 转发日志的工具
Logstash 是一个开源的服务器端数据处理流水线, 它可以同时从多个数据源获取数据, 并将其转换为最喜欢的 "存储"(Ours is Elasticsearch, naturally.)
Beats: 采集日志信息(加上 beats 就是 els),GO 开发的, 所以高效很快
- Filebeat:Log Files
- Metricbeat:Metrics
- Packetbeat:Network Data
- Winlogbeat:Windows Event Logs
- Heartbeat:Uptime Monitoring
Kibana: 独立的美观的图形数据展示界面
Kibana 让你可视化你的 Elasticsearch 数据并导航 Elastic Stack, 所以你可以做任何事情, 从凌晨 2:00 分析为什么你得到分页, 了解雨水可能对你的季度数字造成的影响
2 实验前准备
实验所需要的包(我用的是 5.5.1 版本), 都放在我的网盘里了, 需要的私聊 https://pan.baidu.com/s/1c2An0Co
环境准备
机器名称 | IP 配置 | 服务角色 |
els | 192.168.1.101(私) 192.168.10.101(公) | elasticsearch(搜索引擎) |
logstash | 192.168.1.102(私) 192.168.10.102(公) | logstash(日志处理) redis(缓冲队列) |
filebeat | 192.168.1.103(私) 192.168.10.103(公) | filebeat(日志收集) httpd/mysql(生成日志) |
kibana | 192.168.1.104(私) 192.168.10.104(公) | kibana(展示界面) |
修改主机名 hosts 文件, 确保节点直接能通过主机名连通
- [root@els ~]# hostnamectl set-hostname els.along.com
- [root@logstash ~]# hostnamectl set-hostname logstash.along.com
- [root@filebeat ~]# hostnamectl set-hostname filebeat.along.com
- [root@kibana ~]# hostnamectl set-hostname kibana.along.com
centos7 修改主机名:
[root@server1 ~]# hostnamectl set-hostname 主机名
修改 hosts
- [root@server1 ~]# vim /etc/hosts
- 192.168.1.101 els.along.com
- 192.168.1.102 logstash.along.com
- 192.168.1.103 filebeat.along.com
- 192.168.1.104 kibana.along.com
互相测试
[root@els ~]# ping filebeat.along.com
关闭防火墙 selinux
- [root@server1 ~]# iptables -F
- [root@server3 ~]# setenforce 0
同步时间
[root@server1 ~]# systemctl restart chronyd
二安装搭建 elasticsearch 和 head 插件
1 安装 elasticsearch
Elasticsearch 是一个分布式的 RESTful 风格的搜索和数据分析引擎, 能够解决不断涌现出的各种用例作为 Elastic Stack 的核心, 它集中存储您的数据, 帮助您发现意料之中以及意料之外的情况
(1)官网下载, 各种版本
https://www.elastic.co
(2)安装 java 环境
安装 JDK, 至少 1.8 以上的版本
因为是 java 语言编写的, 需安装 JDK
[root@server1 ~]# yum install java-1.8.0-openjdk-devel -y
(3)安装 els
在网上找自己需要的版本, 我下好了 5.5.1 版本, 在网盘里
[root@server1 ~]# rpm -ivh elasticsearch-5.5.1.rpm
2 配置 elasticsearch
(1)修改 JVM 的配置
- [root@server1 ~]# vim /etc/elasticsearch/jvm.options 修改分配的空间大小
- -Xms1g #初始化大小
- -Xmx1g #最大大小
注意: 不要超过 32G, 如果空间大, 多跑几个实例, 不要让一个实例太大内存
生产环境, 建议都给 32; 为了实验方便, 我直接改成了 1g
(2)配置 els
- [root@els ~]# vim /etc/elasticsearch/elasticsearch.yml
- cluster.name: alongels #集群名字
- node.name: els #节点名
- path.data: /els/data #索引路径
- path.logs: /els/logs #日志存储路径
- network.host: 192.168.10.101 #对外通信的地址, 依次修改为自己机器对外的 IP
- #http.port: 9200 #默认端口
(3)创建出所需的路径, 并修改权限
[root@els ~]# mkdir -pv /els/{data,logs} && chown -R elasticsearch.elasticsearch /els/*
(4)开启服务
[root@els ~]# systemctl start elasticsearch.service
(5)测试 elasticsearch
开启服务, 会打开 9200 端口
[root@server1 ~]# curl 192.168.10.101:9200
3 搭建 elasticsearch 集群(自己选择)
我因机器缺少, 所以没有做集群; 若机器够用, 建议做集群
(1)配置 els
- [root@server1 ~]# vim /etc/elasticsearch/elasticsearch.yml
- cluster.name: alongels #集群名字
- node.name: server1 #节点名, 依次修改为 server2,server3
- path.data: /els/data #索引路径
- path.logs: /els/logs #日志存储路径
- network.host: 192.168.10.101 #对外通信的地址, 依次修改为自己机器对外的 IP
- #http.port: 9200 #默认端口
- discovery.zen.ping.unicast.hosts: ["server1", "server2","server3"] #发现方式, 采用单播
- discovery.zen.minimum_master_nodes: 2 #数量要大于集群中节点的半数
(2)创建出所需的路径, 并修改权限
- [root@server1 ~]# mkdir -pv /els/{data,logs} && chown -R elasticsearch.elasticsearch /els/*
- [root@server2 ~]# mkdir -pv /els/{data,logs} && chown -R elasticsearch.elasticsearch /els/*
- [root@server3 ~]# mkdir -pv /els/{data,logs} && chown -R elasticsearch.elasticsearch /els/*
(3)开启 3 个节点的服务
[root@server3 ~]# systemctl start elasticsearch.service
4 安装 elasticsearch 的 head 插件(Web 前端)
https://github.com/mobz/elasticsearch-head 这里有 github 上的详细步骤
(1)安装 head 插件
下载 git 环境
- [root@server1 ~]$ cd /usr/local/
- [root@server1 local]$ yum -y install git
git 克隆
- [root@server1 local]$ git clone git://github.com/mobz/elasticsearch-head.git
- [root@server1 local]$ cd elasticsearch-head/
安装 npm 包
[root@server1 elasticsearch-head]$ yum -y install npm
安装 npm 的各种模块
[root@server1 elasticsearch-head]$ npm install
注意: 需连网, 安装需一段时间
中间会出错, 提示解压一个包失败, 手动解开就好
Error: Command failed: tar jxf /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
解决办法:
- bunzip2 /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
- tar -xvf /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar
再次执行[root@server1 elasticsearch-head]$ npm install
成功
(2)再次修改配置文件, 在最后加一个配置段
- [root@server1 ~]$ vim /etc/elasticsearch/elasticsearch.yml
- # ------------------------ Enable CORS in elasticsearch -------------------------
- http.cors.enabled: true
- http.cors.allow-origin: "*" #授所有权限
(3)运行 head
重启 elasticsearch 服务, 打开了 9100 端口
[root@server1 ~]$ service elasticsearch restart
启动 head
- [root@server1 ~]# cd /usr/local/elasticsearch-head/
- [root@server1 elasticsearch-head]# npm run start 前端运行
- [root@server1 elasticsearch-head]# nohup npm run start & 后台运行
- [root@server1 elasticsearch-head]# jobs 查看后台运行的任务
- [1]+ Running nohup npm run start &
5 测试 elasticsearch-head
网页访问 http://192.168.1.101:9100/
6elasticsearch-head 的全功能文本搜索引擎库: lucene
https://lucene.apache.org/core/ 下载地址
Apache Lucene 是一个完全用 Java 编写的高性能, 全功能的文本搜索引擎库它几乎适用于任何需要全文搜索的应用程序, 特别是跨平台的应用程序
有经典的查询分析器语法结构, 根据自己需要安装
三安装介绍 logstash
1 介绍
(1)定义
查看官方文档 https://www.elastic.co/cn/products/logstash
官方介绍: Logstash is an open source data collection engine with real-time pipelining capabilities 简单来说 logstash 就是一根具备实时数据传输能力的管道, 负责将数据信息从管道的输入端传输到管道的输出端; 与此同时这根管道还可以让你根据自己的需求在中间加上滤网, Logstash 提供里很多功能强大的滤网以满足你的各种应用场景
Logstash 的事件 (logstash 将数据流中等每一条数据称之为一个 event) 处理流水线有三个主要角色完成: inputs > filters > outputs:
inpust: 必须, 负责产生事件(Inputs generate events), 常用: Filesyslogredisbeats(如: Filebeats)
filters: 可选, 负责数据处理与转换(filters modify them), 常用: grokmutatedropclonegeoip
outpus: 必须, 负责数据输出(outputs ship them elsewhere), 常用: elasticsearchfilegraphitestatsd
(2)有自己的搜索模式, 事先定义好的
所谓的搜索模式就是像变量一样, 把用的较多的事先定义好, 便于多次引用
- [root@centos7-1 conf.d]# rpm -ql logstash |grep pattern
- [root@centos7-1 conf.d]# less /usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.1.1/patterns/grok-patterns
/usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.1.1/patterns
例如: httpd 的搜索模式
(3)文档, 有较详细的用法介绍
选择对应版本
input
filter
output
2 下载安装
去官网下载对应版本的 logstash , 我下载的是 5.5.1 版本
- https://www.elastic.co/cn/downloads/logstash
- [root@centos7-1 els]# rpm -ivh logstash-5.5.1.rpm
修改环境变量
- [root@centos7-1 els]# vim /etc/profile.d/logstash.sh
- export PATH=$PATH:/usr/share/logstash/bin/
- [root@centos7-1 els]# . /etc/profile.d/logstash.sh
四 logstash 演示用法
进入自配置文件的目录
[root@centos7-1 logstash]# cd /etc/logstash/conf.d/
1 示例 1: 标准输入输出
input => 标准输入
filter => 无
output => 标准输出
编辑配置文件
- [root@centos7-1 conf.d]# vim test.conf
- input {
- stdin {}
- }
- output {
- stdout {
- codec => rubydebug
- }
- }
-t 测试语法, 这个命令级慢
[root@centos7-1 conf.d]# logstash -f ./test.conf -t
执行
[root@centos7-1 conf.d]# logstash -f ./test.conf
2 示例 2: 从文件输入数据, 经 grok 拆分插件过滤之后输出至标准输出
input => 标准输入
filter => grokdate 模块
grok: 拆分字段
date: 修改时间格式
output => 标准输出
(1)事前准备, 生成日志
下载开启 httpd 服务
- [root@centos7-1 ~]# yum install httpd
- [root@centos7-1 ~]# systemctl start httpd
生成一个主页
- [root@centos7-1 ~]# vim /var/www/html/index.html
- Home Page
生成 20 个测试页面
[root@centos7-1 ~]# for i in {1..20}; do echo "Test Page ${i}" > /var/www/html/test${i}.html; done
测试页面正常访问
循环生成 httpd 日志
[root@centos7-1 ~]# for i in {1..200}; do j=$[$RANDOM +1]; curl http://192.168.1.102:80/test${j}.html; done
(2)编辑配置文件
- [root@centos7-1 conf.d]# vim test2.conf
- input {
- stdin {}
- }
- filter {
- grok { #拆分 message 字段
- match => { #指定匹配哪个字段
- "message" => "%{COMBINEDAPACHELOG}" #引用定义好的搜索模式
- }
- remove_field => "message" #删除 message 字段
- }
- date { #改变时间格式
- match => ["timestamp","dd/MMM/YYYY:H:m:s Z"]
- remove_field => "timestamp" #删除原有的时间字段
- }
- }
- output {
- stdout {
- codec => rubydebug
- }
- }
(3)执行
- [root@centos7-1 conf.d]# logstash -f test2.conf -t
- [root@centos7-1 conf.d]# logstash -f test2.conf
3 示例 3:filter 的 datemutate 插件
input => 标准输入
filter => datemutate 模块
mutate: 修改字段
output => 标准输出
编辑配置文件, 由于 inputoutput 还是标准输入输出, 就没有贴出
- [root@centos7-1 conf.d]# vim test3.conf
- filter {
- grok {
- match => {
- "message" => "%{HTTPD_COMBINEDLOG}"
- }
- }
- date {
- match => ["timestamp","dd/MMM/YYYY:H:m:s Z"]
- }
- mutate {
- rename => {
- "agent" => "user_agent"
- }
- }
- }
执行
- [root@centos7-1 conf.d]# logstash -f test3.conf -t
- [root@centos7-1 conf.d]# logstash -f test3.conf
4 示例 4:filter 的 geoip 模块
input => 标准输入
filter => geoip 模块
geoip: 利用这个模块解析 ip 的地址, 利于后边 kibana 的地理位置展示图
output => 标准输出
(1)准备 ip 数据库
网上下载数据库, 因为是记录世界的 IP 地址, 所以经常有变动, 可以写一个计划任务, 每隔一周去网上下载一次, 解包, 链接到 maxmind 下
- [root@centos7-1]# tar -xvf GeoLite2-City.tar.gz
- [root@centos7-1]# mv GeoLite2-City_20170704/ /etc/logstash/
- [root@centos7-1 logstash]# mv GeoLite2-City_20170704/ maxmind
(2)模拟一个公网 ip 访问
[root@centos7-1 ~]# echo '112.168.1.102 - - [08/Feb/2018:15:28:53 +0800]"GET /test6.html HTTP/1.1"200 12"-""curl/7.29.0"' >> /var/log/httpd/access_log
(3)执行
编辑配置文件, 由于 inputoutput 还是标准输入输出, 就没有贴出
- [root@centos7-1 conf.d]# vim tes4.conf
- filter {
- grok {
- match => {
- "message" => "%{HTTPD_COMBINEDLOG}"
- }
- }
- geoip {
- source => "clientip" #哪个源 ip 字段, 转换为地理位置
- target => "geoip" #目标信息存储时的健名
- database => "/etc/logstash/maxmind/GeoLite2-City.mmdb" #数据库路径
- }
- }
执行
- [root@centos7-1 conf.d]# logstash -f test4.conf -t
- [root@centos7-1 conf.d]# logstash -f test4.conf
如果是内网地址, 会显示失败
[root@centos7-1 ~]# echo '192.168.1.102 - - [08/Feb/2018:15:28:53 +0800]"GET /test6.html HTTP/1.1"200 12"-""curl/7.29.0"' >> /var/log/httpd/access_log
5 示例 5:output 输出给 elasticsearch
input => 来自 httpd 的访问日志
filter => geoip 模块
output => 输出给 elasticsearch
(1)执行
编辑配置文件
- [root@centos7-1 conf.d]# vim test5.conf
- input {
- file {
- path => ["/var/log/httpd/access_log"]
- start_position => "beginning"
- }
- }
- filter {
- grok {
- match => {
- "message" => "%{HTTPD_COMBINEDLOG}"
- }
- }
- geoip {
- source => "clientip"
- target => "geoip"
- database => "/etc/logstash/maxmind/GeoLite2-City.mmdb"
- }
- }
- output {
- elasticsearch {
- hosts => ["http://192.168.10.101:9200/"] #主机
- index => "logstash-%{ YYYY.MM.dd}" #索引
- document_type => "apache_logs" #文档类型标识, 自己定义
- }
- }
执行
logstash -f test5.conf
(2)验证
网页登录到 elasticsearch, 可以查看到
搜索查询的演示
(a)基于 2 个字段 CN 的搜索
(b)响应码前缀搜索
(c)范围搜索
6 示例 6:output 输出给 redis
input => 来自 httpd 的访问日志
filter => geoip 模块
output => 输出给 redis
(1)准备 redis 服务
- [root@logstash]# vim /etc/redis.conf
- bind 0.0.0.0 #监听所有端口
- requirepass ilinux.io #加密码, 为了安全运行
其他一些优化配置:
只做队列, 没必要持久存储, 性能更好
把内存的淘汰策略关掉
把内存空间最大, 调大一点
把所有持久化功能关掉: AOF 快照
开启 redis
[root@centos7-1 conf.d]# systemctl start redis 打开 6379 端口
(2)运行 logstash
配置文件
- [root@centos7-1 conf.d]# vim test6.conf
- output {
- redis {
- batch => true #批量写入
- host => "192.168.10.102" #主机
- password => "ilinux.io" #密码
- port => 6379 #端口
- #db => 0 #默认就是 0 号库
- data_type => "list" #数据格式, 列表
- key => "apachelogs" #自己定义的键
- }
- }
- [root@logstash]# logstash -f test6.conf
(3)测试
- [root@centos7-1 ~]# redis-cli -a ilinux.io
- 127.0.0.1:6379> KEYS *
- 127.0.0.1:6379> LLEN apachelogs
- 127.0.0.1:6379> LINDEX apachelogs 1
(4)注: logstash 开启的方法:
logstash 指令指定配置文件启动
logstash -f test.conf
systemctl start logstash 命令启动
此命令启动, 要确保 / etc/logstash/conf.d 目录下没有其他多余的配置文件
五 Beats 轻量型数据采集器
Beats 平台集合了多种单一用途数据采集器这些采集器安装后可用作轻量型代理, 从成百上千或成千上万台机器向 Logstash 或 Elasticsearch 发送数据
1 下载
去官网下载对应版本
[root@centos7-1 ~]# rpm -ivh filebeat-5.5.1-x86_64.rpm
2 示例 1: 配置 filebeats, 目标 elasticsearch
源 => 本机 httpd 的日志
目标 => 直接给 elasticsearch
(1)配置文件
- [root@filebeat ~]# vim /etc/filebeat/filebeat.yml
- #================= Filebeat prospectors =====================
- filebeat.prospectors:
- paths:
- - /var/log/httpd/*log
- #exclude_lines: ["^DBG"] 黑名单
- #include_lines: ["^ERR", "^WARN"] 白名单
- #-------------------------- Elasticsearch output ------------------------------
- output.elasticsearch:
- # Array of hosts to connect to.
- hosts: ["192.168.10.101:9200"] #主机和端口
- #protocol: "https" #如果是 https
- #username: "elastic" #用户, 如果 elasticsearch 设置的有的话
- #password: "changeme" #密码
(2)启动服务
[root@centos7-1 filebeat]# systemctl start filebeat.service
(3)生成 httpd 日志
下载开启 httpd 服务
- [root@centos7-1 ~]# yum install httpd
- [root@centos7-1 ~]# systemctl start httpd
生成一个主页
- [root@centos7-1 ~]# vim /var/www/html/index.html
- Home Page
生成 20 个测试页面
[root@centos7-1 ~]# for i in {1..20}; do echo "Test Page ${i}" > /var/www/html/test${i}.html; done
测试页面正常访问
循环生成 httpd 日志
[root@centos7-1 ~]# for i in {1..20}; do j=$[$RANDOM +1]; curl http://192.168.1.102:80/test${j}.html; done
(4)测试
直接访问 elasticsearch 页面, 可以查看到
3 示例 2: 完整的 ELK > 配置 filebeat, 目标给 logstash
(1)配置 filebeat
- [root@filebeat ~]# vim /etc/filebeat/filebeat.yml
- filebeat.prospectors:
- - input_type: log
- paths:
- - /var/log/httpd/*log
- output.logstash:
- hosts: ["192.168.10.102:5044"]
启动服务
[root@centos7-1 filebeat]# systemctl start filebeat.service
(2)在 logstash 服务器上配置
- [root@logstash conf.d]# vim /etc/logstash/conf.d/apachelogs.conf
- input {
- beats {
- port => 5044
- }
- }
- filter {
- grok {
- match => {
- "message" => "%{HTTPD_COMBINEDLOG}"
- }
- }
- }
- output {
- elasticsearch {
- hosts => ["http://192.168.10.101:9200/"]
- index => "logstash-%{ YYYY.MM.dd}"
- document_type => "apache_logs"
- }
- }
- [root@filebeat ~]# vim /etc/filebeat/filebeat.yml
- filebeat.prospectors:
- - input_type: log
- paths:
- - /var/log/httpd/*log
- #----------------------------- Redis output --------------------------------
- output.redis:
- hosts: ["192.168.10.102"]
- password: "ilinux.io"
- key: "httpdlogs"
- datatype: "list"
- db: 0
- timeout: 5
- [root@logstash conf.d]# vim /etc/logstash/conf.d/apachelogs.conf
- input {
- redis {
- host => "192.168.10.102"
- port => "6379"
- password => "ilinux.io"
- data_type => "list"
- key => "httpdlogs"
- threads => 2
- }
- }
- filter {
- grok {
- match => {
- "message" => "%{HTTPD_COMBINEDLOG}"
- }
- }
- date {
- match => ["timestamp","dd/MMM/YYYY:H:m:s Z"]
- remove_field => "timestamp"
- }
- }
- output {
- elasticsearch {
- hosts => ["http://192.168.10.101:9200/"]
- index => "logstash-%{ YYYY.MM.dd}"
- document_type => "apache_logs"
- }
- }
- vim /etc/kibana/kibana.yml
- server.port: 5601
- server.host: "0.0.0.0"
- server.name: "kibana.along.com"
- elasticsearch.url: "http://192.168.10.101:9200"
来源: https://www.cnblogs.com/along21/p/8509123.html