一、ELK介绍

ELK是三款开源软件的缩写,即:ElasticSearch + Logstash + Kibana。这三个工具组合形成了一套实用、易用的监控架构,可抓取系统日志、apache日志、nginx日志、mysql日志等多种日志类型,目前很多公司用它来搭建可视化的集中式日志分析平台。

ElasticSearch:是一个分布式的RESTful风格的搜索和数据分析引擎,同时还提供了集中存储功能,它主要负责将logstash抓取来的日志数据进行检索、查询、分析等。

Logstash:日志处理工具,负责日志收集、转换、解析等,并将解析后的日志推送给ElasticSearch进行检索。

Kibana:Web前端,可以将ElasticSearch检索后的日志转化为各种图表,为用户提供数据可视化支持。

Filebeat:轻量型日志采集器,负责采集文件形式的日志,并将采集来的日志推送给logstash进行处理。

Winlogbeat:轻量型windows事件日志采集器,负责采集wondows的事件日志,并将采集来的日志推送给logstash进行处理。

 

二、部署环境

由于我这边是测试环境,所以ElasticSearch + Logstash + Kibana这三个软件我都是装在一台机器上面,如果是生产环境,建议分开部署,并且ElasticSearch可配置成集群方式。

IP:192.168.2.207(ELK服务器,CentOS 7)

IP:192.168.2.203(filebeat,nginx服务器,CentOS 7)

IP:192.168.2.204(filebeat,apache服务器,CentOS 7)

IP:192.168.2.206(winlogbeat,windows 10)

 

三、安装前的准备工作

1、关闭 selinux 和防火墙(这里暂时关闭iptables,部署完成后再开启)

 

sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

sed -i 's/SELINUXTYPE=targeted/#&/' /etc/selinux/config

setenforce 0  # 可以设置配置文件永久关闭

systemctl stop firewalld.service

2、安装配置iptables

 

yum -y install iptables iptables-services

vim /etc/sysconfig/iptables # 添加如下端口策略

-A INPUT -p tcp -m state --state NEW -m tcp --dport 9200 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 5601 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 5044 -j ACCEPT

启动iptables

 

systemctl start iptables.service

systemctl enable iptables.service   # 将iptables加入开机启动

查看iptables状态

systemctl status iptables.service

重启系统

reboot # 更改selinux需要重启系统才会生效

3、安装java 8及相关软件

yum -y install vim wget java java-devel

查看java版本

java -version

CentOS 7安装部署ELK 6.2.4

4、下载ELK及相关软件

ELK服务器需下载

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-x86_64.rpm

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.rpm

Linux节点服务器需下载

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.4-x86_64.rpm

windows节点服务器需下载

CentOS 7安装部署ELK 6.2.4

 

四、安装配置ELK

1、yum方式安装ELK

 

yum localinstall -y elasticsearch-6.2.4.rpm

yum localinstall -y kibana-6.2.4-x86_64.rpm

yum localinstall -y logstash-6.2.4.rpm

2、创建ELK存放数据和日志目录

 

mkdir -pv /data/elasticsearch/{data,logs}

mkdir -pv /data/logstash/{data,logs}

chown -R elasticsearch.elasticsearch /data/elasticsearch/

chown -R logstash.logstash /data/logstash/

3、修改ELK配置文件

 

vim /etc/elasticsearch/elasticsearch.yml

path.data: /data/elasticsearch/data

path.logs: /data/elasticsearch/logs

network.host: localhost

http.port: 9200

vim /etc/logstash/logstash.yml

path.data: /data/logstash/data

path.logs: /data/logstash/logs

vim /etc/logstash/conf.d/logstash.conf  # 添加如下内容

input {

  beats {

    port => 5044

    codec => plain {

          charset => "UTF-8"

    }

  }

}

 

output {

  elasticsearch {

    hosts => "127.0.0.1:9200"

    manage_template => false

    index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"

    document_type => "%{[@metadata][type]}"

  }

}

vim /etc/kibana/kibana.yml

server.port: 5601

server.host: "192.168.2.207"

elasticsearch.url: "http://localhost:9200"

4、安装配置nginx

安装nginx和http用户认证工具

 

yum -y install epel-release

yum -y install nginx httpd-tools

修改nginx配置

 

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

vim /etc/nginx/nginx.conf

把下图中这一段注释掉

CentOS 7安装部署ELK 6.2.4

 

vim /etc/nginx/conf.d/kibana.conf  # 添加如下内容

server {

    listen 80;

 

    server_name kibana;

 

    auth_basic "Restricted Access";

    auth_basic_user_file /etc/nginx/kibana-user;  //http认证文件

 

    location / {

        proxy_pass http://192.168.2.207:5601;  //代理的kibana地址

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection 'upgrade';

        proxy_set_header Host $host;

        proxy_cache_bypass $http_upgrade;

    }

}

生成http用户认证文件,生成文件kibana-user,并添加用户henhh

htpasswd -cm /etc/nginx/kibana-user henhh

此处需要输入两遍密码

5、启动ELK和nginx

 

systemctl daemon-reload  # 重新加载所有配置文件

systemctl start elasticsearch logstash kibana nginx  # 启动ELK和nginx

systemctl enable elasticsearch logstash kibana nginx  # 将ELK和nginx加入开机启动

systemctl status elasticsearch logstash kibana nginx  #查看ELK和nginx启动状态

查看端口是否已监听

CentOS 7安装部署ELK 6.2.4

6、查看elasticsearch状态

curl -XGET 'http://192.168.2.207:9200/_cluster/state/nodes?pretty'

CentOS 7安装部署ELK 6.2.4

查看elasticsearch的master

 

curl -XGET 'http://192.168.2.207:9200/_cluster/state/master_node?pretty'

curl -XGET 'http://192.168.2.207:9200/_cat/master?v'

CentOS 7安装部署ELK 6.2.4

查看健康状态

 

curl -XGET 'http://192.168.2.207:9200/_cat/health?v'

curl -XGET 'http://192.168.2.207:9200/_cluster/health?pretty'

CentOS 7安装部署ELK 6.2.4

对于这个健康状态green(绿色)为最好

 

五、Linux节点服务器安装配置filebeat

安装filebeat,进入到之前下载安装包的目录,执行yum方式安装

yum localinstall -y filebeat-6.2.4-x86_64.rpm

修改filebeat配置

 

vim /etc/filebeat/filebeat.yml

- type: log

  enabled: true

    - /var/log/*.log

    - /var/log/messages

filebeat.config.modules:

  path: ${path.config}/modules.d/*.yml

  reload.enabled: false

setup.template.settings:

  index.number_of_shards: 3

setup.kibana:

  host: "192.168.2.207:5601"

#output.elasticsearch:    //我们输出到logstash,把这行注释掉

  #hosts: ["localhost:9200"]   //这行也注释掉

output.logstash:

  hosts: ["192.168.2.207:5044"]

启用nginx模块

filebeat modules enable nginx

修改nginx模块配置

 

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

- module: nginx

  access:

enabled: true

var.paths: ["/var/log/nginx/access.log*"]

  error:

enabled: true

var.paths: ["/var/log/nginx/error.log*"]

启用apache模块

filebeat modules enable apache2

修改apache模块配置

 

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

- module: apache2

  access:

enabled: true

var.paths: ["/var/log/httpd/access_log*"]

  error:

enabled: true

var.paths: ["/var/log/httpd/error_log*"]

启动filebeat

 

systemctl start filebeat

systemctl enable filebeat

systemctl status filebeat

 

 

六、windows节点服务器安装配置winlogbeat

解压winlogbeat-6.2.4-windows-x86_64.zip,以管理员方式运行PowerShell,进入到解压后的目录,执行.\install-service-winlogbeat.ps1来安装服务。如果报错提示在此系统上禁止脚本运行,那就执行PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-winlogbeat.ps1,便可安装成功。

CentOS 7安装部署ELK 6.2.4

修改配置文件 :winlogbeat.yml

 

winlogbeat.event_logs:

  - name: Application

    ignore_older: 72h

  - name: Security

  - name: System

setup.template.settings:

  index.number_of_shards: 3

setup.kibana:

  host: "192.168.2.207:5601"

#output.elasticsearch:   //我们输出到logstash,所以这行注释掉

  #hosts: ["localhost:9200"]   //这行也注释掉

output.logstash:

  hosts: ["192.168.2.207:5044"]

logging.to_files: true

logging.files:

  path: D:/winlogbeat/winlogbeat/Logs

logging.level: info

使用以下命令检查配置文件的正确性,出现Config OK说明配置文件正确。

.\winlogbeat.exe test config -c .\winlogbeat.yml -e

CentOS 7安装部署ELK 6.2.4

启动winlogbeat服务

打开service(服务),找到winlogbeat,启动它。

CentOS 7安装部署ELK 6.2.4

命令行启动方式,执行下面命令

Start-Service winlogbeat

 

七、创建索引模式

浏览器访问http://192.168.2.207,输入之前通过htpasswd认证的用户名和密码登陆kibana。

CentOS 7安装部署ELK 6.2.4

点击Management,然后点击Index Patterns,再点击Create index pattern

CentOS 7安装部署ELK 6.2.4

输入filebeat-,然后点击Next step

CentOS 7安装部署ELK 6.2.4

选择@timestamp,然后点击Create index pattern

CentOS 7安装部署ELK 6.2.4

按照此方法再创建一个名为winlogbeat-的索引模式。

CentOS 7安装部署ELK 6.2.4

创建好后,点击Discover,就可以看到如下图页面的日志内容了。