总体流程
- filebeat收集openresty应用日志传输到Redis集群中
- Logstash从Redis集群中拉取数据,并传输到Elasticsearch集群
- 使用Kibana可视化索引
- 使用Elasticsearch-head管理lasticsearch集群
注:Logstash不支持集群模式html
环境
均为CentOS 7.4 x64系统java
openresty 192.168.0.10 1.15.8版本 filebeat 192.168.0.10 7.3.0版本 Redis集群 192.168.0.11 6381-6386端口(暂采用伪集群的方式) 5.0.5版本 Logstash 192.168.0.12 7.3.0版本 Elasticsearch集群 192.168.0.13-15 7.3.0版本 Kibana 192.168.0.16 7.3.0版本 Elasticsearch-head 192.168.0.17linux
软件下载地址 filebeat|Logstash|Elasticsearch|Kibana:https://www.elastic.co/cn/downloads/past-releases#elasticsearch Elasticsearch-head:https://github.com/mobz/elasticsearch-head Redis:https://redis.io/downloadnginx
Redis集群开启外部访问,设置访问密码,防火墙放行端口git
配置
filebeat配置
安装
软件采用经过下载rpm包的方式进行安装github
rpm -ivh filebeat-7.3.0-x86_64.rpm systemctl daemon-reload systemctl enable filebeat.service systemctl start filebeat.service
修改filebeat.yml配置文件
openresty日志文件路径: access:/usr/local/openresty/nginx/logs/host.access.log error:/usr/local/openresty/nginx/logs/error.logweb
文件路径:/etc/filebeat/filebeat.ymlredis
注销其余的output输出shell
filebeat.inputs: - type: log enabled: true paths: - /usr/local/openresty/nginx/logs/host.access.log fields: log_source: access - type: log enabled: true paths: - /usr/local/openresty/nginx/logs/error.log fields: log_source: error output.redis: hosts: ["192.168.0.11:6381","192.168.0.11:6382","192.168.0.11:6383","192.168.0.11:6384","192.168.0.11:6385","192.168.0.11:6386"] password: foobar2000 db: 0 key: "log_list"
还有一些其余的优化参数,后期须要再添加数据库
测试
重启filebeat后,使用浏览器访问网址,确保先生成的有access和error日志,而后在Redis集群的第0库查看 会发现有一个key是log_list,值是list的数据,里面一行一行的数据就是收集过来的日志,这些日志加的有一些元数据 具体能够查看本博客的另外一篇文章《使用filebeat收集日志传输到redis的各类效果展现》,这里面有详细的讲解。
Logstash配置
安装
- 配置java环境
cd /usr/local/src tar -zxv -f openjdk-12_linux-x64_bin.tar.gz -C /usr/local/ vim /etc/profile.d/java.sh export JAVA_HOME=/usr/local/jdk-12 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar source /etc/profile.d/java.sh
- 安装logstash
软件采用经过下载rpm包的方式进行安装
rpm -ivh logstash-7.3.0.rpm systemctl daemon-reload systemctl enable logstash.service systemctl start logstash.service
修改启动文件,确保能找到java环境变量 文件路径:/usr/share/logstash/bin/logstash.lib.sh 在行首加入以下信息
export JAVA_HOME=/usr/local/jdk-12 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
msg.conf配置文件修改
logstash默认的配置文件时在/etc/logstash/conf.d/
目录下建立以conf结尾的文件,本例中建立的是msg.conf 文件路径:/etc/logstash/conf.d/msg.conf 该文件内容以下
input { redis { # 若报错该redis不能访问,则不使用Redis集群,只使用一台redis,须要修改filebeat.yml文件 host => "192.168.0.11" port => 6381 password => foobar2000 data_type => "list" key => "log_list" db => 0 } } output { # 根据filebeat.yml中设置的不一样日志来源进行区分,从而建立不一样的索引 if [fields][log_source] == 'access' { elasticsearch { hosts => ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"] index => "filebeat-access-%{+YYYY.MM.dd}" id => "access_id" } } if [fields][log_source] == 'error' { elasticsearch { hosts => ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"] index => "filebeat-error-%{+YYYY.MM.dd}" id => "error_id" } } }
测试
查看redis的key值对应的list数据还有没,没有的话说明已经把日志传输给logstash了。
Elasticsearch集群配置
安装
详细参考本博客另外一篇文章《ElasticSearch集群》
elasticsearch.yml配置文件修改
文件路径:/etc/elasticsearch/elasticsearch.yml 详细参考本博客另外一篇文章《ElasticSearch集群》
测试
详细参考本博客另外一篇文章《ElasticSearch集群》
Elasticsearch-head配置
yum -y install epel-release yum -y install git npm git clone git://github.com/mobz/elasticsearch-head.git cd elasticsearch-head npm install npm run start
启动后会占用当前窗口,改用脚本启动方式 在当前目录下建立一个start.sh文件
#!/bin/bash nohup npm run start > /tmp/head.log 2>&1 &
默认访问地址是http://localhost:9100
这时能够经过使用nginx代理的方式进行访问 能够配置一个域名,同时设置一个nginx网站访问密码,具体参考本博客的另外一篇文章《nginx配置访问密码,输入用户名和密码才能访问》
location /head { proxy_pass http://127.0.0.1:9100; #Proxy Settings proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; }
修改elasticsearch.yml配置文件,开启跨域访问 在每一个集群节点的elasticsearch.yml配置文件末尾加上以下两行数据
http.cors.enabled: true http.cors.allow-origin: "*"
Kibana配置
安装
软件采用经过下载rpm包的方式进行安装
rpm -Uvh kibana-7.3.0-x86_64.rpm systemctl daemon-reload systemctl enable kibana.service systemctl start kibana.service
kibana.yml配置文件修改
文件路径:/etc/kibana/kibana.yml 这个也采用nginx代理的方式进行访问,同时设置nginx的web访问密码 具体参考本博客的另外一篇文章《nginx配置访问密码,输入用户名和密码才能访问》
后期能够结合X-pack插件进行用户角色和权限管理等功能
server.port: 5601 server.host: "127.0.0.1" server.basePath: "/kibana" # 跟nginx那个路径对应,不然没法加载静态资源 elasticsearch.hosts: ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"] i18n.locale: "zh-CN"
nginx配置文件内容
location /kibana/ { proxy_pass http://127.0.0.1:5601; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; rewrite ^/kibana/(.*)$ /$1 break; }
使用http://ip/kibana进行访问,查看索引信息等
或者采用另外一种配置 nginx配置文件
server { listen 80; server_name xxx.xxx.com; root /usr/share/nginx/html; expires -1; client_max_body_size 10m; location / { proxy_http_version 1.1; proxy_cache_bypass $http_upgrade; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-Port $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://172.17.107.187:5600; } }
kibana.yml配置文件
server.port: 5601 server.host: "127.0.0.1" elasticsearch.hosts: ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"] i18n.locale: "zh-CN"
访问的话直接使用域名的方式,不加后缀kibana
注意这两种不一样方式的配置
测试
打开浏览器进行访问,查看索引,日志文件等
后期扩展
- 各软件之间的数据传输采用加密的方式进行
- 结合X-pack给kibana的访问设置用户角色和权限控制等
- filebeat模块的功能还有待研究
- 其余beats产品有待研究
- kibana的web界面使用还有待研究
- 读取MySQL数据库数据到Elasticsearch功能还有待研究
- 在Logstash中过滤数据还有待研究
- 其余未了解的也都待研究