# ELK6.5.0+Filebeat 日志系统部署

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击联系[博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## 1. 引言

* Elasticsearch是实时全文搜索和分析引擎，提供搜集、分析、存储数据三大功能；是一套开放REST和JAVA API等结构提供高效搜索功能，可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。
* Logstash是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志，包括系统日志、错误日志和自定义应用程序日志。它可以从许多来源接收日志，这些来源包括 syslog、消息传递（例如 RabbitMQ）和JMX，它能够以多种方式输出数据，包括电子邮件、websockets和Elasticsearch。
* Kibana是一个基于Web的图形界面，用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据，不仅允许用户创建他们自己的数据的定制仪表板视图，还允许他们以特殊的方式查询和过滤数据。
* Filebeat是本地文件的日志数据采集器。 作为服务器上的代理安装，Filebeat监视日志目录或特定日志文件，tail file，并将它们转发给Elasticsearch或Logstash进行索引、kafka 等。

### 1.1 编写目的

ELK 不是一款软件，而是 Elasticsearch、Logstash 和 Kibana 三种软件产品的首字母缩写。这三者都是开源软件，通常配合使用，而且又先后归于 Elastic.co 公司名下，所以被简称为 ELK Stack。根据 Google Trend 的信息显示，ELK Stack 已经成为目前最流行的集中式日志解决方案。

![](/files/-LxQD8Y6vII0Z10v4P8p)

## 2. 部署对象说明

### 2.1 程序名称

Elasticsearch：分布式搜索和分析引擎，具有高可伸缩、高可靠和易管理等特点。基于 Apache Lucene 构建，能对大容量的数据进行接近实时的存储、搜索和分析操作。通常被用作某些应用的基础搜索引擎，使其具有复杂的搜索功能；

Logstash：数据收集引擎。它支持动态的从各种数据源搜集数据，并对数据进行过滤、分析、丰富、统一格式等操作，然后存储到用户指定的位置；

Kibana：数据分析和可视化平台。通常与 Elasticsearch 配合使用，对其中数据进行搜索、分析和以统计图表的方式展示；

Filebeat：ELK 协议栈的新成员，一个轻量级开源日志文件数据搜集器，基于 Logstash-Forwarder 源代码开发，是对它的替代。在需要采集日志数据的 server 上安装 Filebeat，并指定日志目录或日志文件后，Filebeat 就能读取数据，迅速5.Logstash 进行解析，亦或直接发送到 Elasticsearch 进行集中式存储和分析。

如果您对 ELK Stack 还尚不了解，或是想了解更多，请点击[集中式日志系统](http://www.ibm.com/developerworks/cn/opensource/os-cn-elk/) [ELK](http://www.ibm.com/developerworks/cn/opensource/os-cn-elk/) [协议](http://www.ibm.com/developerworks/cn/opensource/os-cn-elk/)[栈](http://www.ibm.com/developerworks/cn/opensource/os-cn-elk/)[详解](http://www.ibm.com/developerworks/cn/opensource/os-cn-elk/)，查看具体介绍。

### 2.2 程序功能

* Elasticsearch：分布式的搜索引擎和数据分析引擎
* Logstash：数据收集引擎
* Kibana：数据分析和可视化平台
* Filebeat：轻量型日志采集器

## 3. 部署操作

### 3.1 环境准备

ELK5.3以后依赖JDK，所以我们先在服务器安装依赖 （JDK版本1.8.0及以上）

OpenJDK下载网站：

{% embed url="<http://jdk.java.net/>" %}

OpenJDK安装文档网站：

{% embed url="<https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html>" %}

```
yum -y install java-1.8.0-openjdk  wget lrzsz vim net-tools coreutils npm
yum groupinstall 'Development Tools'
```

#### 检查一下JAVA环境及版本

```
java -version
```

#### 关闭防火墙和配置SELinux

```
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config 
setenforce 0                      #可以设置配置文件永久关闭
systemctl stop firewalld.service
```

#### ELK官方下载网站：&#x20;

{% embed url="<https://www.elastic.co/cn/downloads/past-releases>" %}

![](/files/-LxQD8Y7oUTv4OKDiosk)

#### Elasticsearch下载：

```
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.0.tar.gz
```

#### Logstash下载：

```
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.0.zip
```

#### Kibana下载：

```
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.0-linux-x86_64.tar.gz
```

#### Filebeat下载：

```
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.0-linux-x86_64.tar.gz
```

### 3.2 安装步骤

#### 安装elasticsearch

```
tar -zvxf elasticsearch-6.5.0.tar.gz -C /usr/local/

vim /usr/local/elasticsearch-6.5.0/config/elasticsearch.yml
17 cluster.name: elk-cluster     #集群名称
23 node.name: node-1             #节点名称
33 path.data: /usr/local/elasticsearch-6.5.0/data #设置索引数据的存储路径，默认是es根目录下的data文件夹，可以设置多个存储路径，用逗号隔开
37 path.logs: /usr/local/elasticsearch-6.5.0/logs #设置日志文件的存储路径，默认是es根目录下的logs文件夹
55 network.host: 10.88.216.26          #当前节点IP地址
59 http.port: 9200                     #设置对外服务的http端口，默认为9200。
68 discovery.seed_hosts: ["10.88.216.17","10.88.216.19", "10.88.216.22"]     #集群每个节点IP地址，也可以使用els、els.shuaiguoxia.com等名称，需要各节点能够解析
72 cluster.initial_master_nodes: ["node-1", "node-2","node-3"]               #初始主节点应由其标识 节点名称，默认为其主机名。确保值cluster.initial_master_nodes与节点名称确切匹配。
80 gateway.recover_after_nodes: 3      #设置集群中N个节点启动时进行数据恢复，默认为3。
```

#### 具体详细配置阅读官方文档了解更多：

{% embed url="<https://www.elastic.co/guide/en/elasticsearch/reference/current/targz.html>" %}

```
mkdir -p /usr/local/elasticsearch-6.5.0/data
```

#### 修改sysctl内核参数

```
echo "vm.max_map_count=262144" >> /etc/sysctl.conf && sysctl -p
vim /etc/security/limits.conf （添加下面参数）
*       soft    nofile  65536
*       hard    nofile  65536
```

### 3.3 程序运行成功状态检查

**ES插件的使用**

head插件(查看,库级别的操作)

#### 安装node

由于head插件本质上还是一个node.js的工程，因此需要安装node，使用npm来安装依赖的包。（npm可以理解为maven）

官网下载对应版本的node:&#x20;

{% embed url="<http://nodejs.org/dist/>" %}

```
wget http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz
```

安装编译软件

```
yum gcc gcc-c++ kernel-devel make
```

然后解压node的安装包

```
tar -xvf node-v0.10.24.tar.gz -C /usr/local/
cd /usr/local/node-v0.10.24/
```

源码编译安装

```
./configure && make && make install
```

查看是否安装成功，-v查看版本

```
node -v
```

![](/files/-LxQQJHA57_sFtR7HGsU)

使用下面的命令取消ssl验证

```
npm config set strict-ssl false
```

如遇错误`error-code-ELIFECYCLE`，执行如下命令

```
npm cache clear --force
npm install -g npm
```

#### 下载elasticsearch-head插件

head下载地址：[https://github.com/mobz/](https://github.com/mobz/elasticsearch-head)elasticsearch-head

```
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install         #安装依赖
```

**修改启动文件**

所有依赖包安装成功后，修改 elasticsearch-head 目录下的 Gruntfile.js 文件，在 options 属性内增加 hostname，设置为 0.0.0.0。

```
vim Gruntfile.js
```

![](/files/-LxQD8Y9neN9gdvImwIG)

**修改 Elasticsearch 配置文件 config/elasticsearch.yml支持跨域访问**

**具体配置参考官方文档**

{% embed url="<https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-http.html#modules-http>" %}

```
vim /usr/local/elasticsearch-6.5.0/config/elasticsearch.yml （在末行添加如下两条配置）
89 http.cors.enabled: true
90 http.cors.allow-origin: '*'
```

![](/files/-LxQD8YAiquYgbv62O-z)

Elasticsearch自5版本之后，处于安全考虑，不允许使用root用户运行。

解决：创建一个普通用户，将elasticsearch 安装目录权限修改一下，切换至普通用户运行elasticsearch就可以了

```
useradd elasticsearch
mv elasticsearch-head /usr/local/
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-head
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.5.0
```

#### 切换用户启动服务

```
su – elasticsearch
nohup /usr/local/elasticsearch-6.5.0/bin/elasticsearch & #忽略输入并把输出追加到"nohup.out”
cd /usr/local/elasticsearch-head/
nohup npm run start &
```

#### 查看服务启动端口

```
ss -ntulp | grep -E "9100|9200|9300"
```

![](/files/-LxQD8YB0_sgIJcwz3NC)

访问 [http://10.88.216.26:9100](http://10.88.216.26:9100/) 地址，就可以看到当前 Elasticsearch 集群信息

![](/files/-LxQD8YCXS5jodxdlgHz)

#### Cerebo是kopf在es5上的替代者(集群操作)

Cerebro**下载网站：**

{% embed url="<https://github.com/lmenezes/cerebro/releases>" %}

```
wget https://github.com/lmenezes/cerebro/releases/download/v0.8.4/cerebro-0.8.4.tgz
tar -zvxf cerebro-0.8.4.tgz -C /usr/local/
cd /usr/local/cerebro-0.8.4/
vim /usr/local/cerebro-0.8.4/conf/application.conf
```

![](/files/-LxQQ4o6-2p_dYihEL5k)

```
nohup ./bin/cerebro -Dhttp.port=[定义启动的端口] -Dhttp.address=[定义启动IP] &
```

![](/files/-LxQD8YEC-GiYwx_cUFx)

查看启动并访问节点

[http://10.88.216.26:9000](http://10.88.216.26:9000/)

![](/files/-LxQD8YFFGmoABwcLnFR)

Bigdesk插件(集群状态查看)

{% embed url="<https://github.com/hlstudio/bigdesk>" %}

```
wget https://github.com/hlstudio/bigdesk/archive/master.zip
unzip master.zip -d /usr/local/
cd /usr/local/bigdesk-master/_site/
```

#### 启动web服务器

```
python -m SimpleHTTPServer                 #默认监听端口号是 8000
nohup python -m SimpleHTTPServer 8900 &    #指定启动端口,并后台启动
```

#### 查看启动并访问节点

![](/files/-LxQD8YGo9DYRJdmL3fS)

#### 访问并连接节点查看

访问[http://10.88.216.26:8900](http://10.88.216.26:8900/)即可进入监控页面

![](/files/-LxQD8YHxB8-c5BEqr4W)

Beats可以直接（或者通过Logstash）将数据发送到Elasticsearch，在那里你可以进一步处理和增强数据，然后在Kibana中将其可视化。

**Filebeat**安装部署

```
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.0-linux-x86_64.tar.gz
tar -zvxf filebeat-6.5.0-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/filebeat-6.5.0-linux-x86_64/
```

**配置Filebeat**

**官方参考文档：**&#x20;

{% embed url="<https://www.elastic.co/guide/en/beats/filebeat/index.html>" %}

```
vim /usr/local/filebeat-6.5.0-linux-x86_64/filebeat.yml
```

#### Logstash安装部署

![](/files/-LxQD8YIUBXa186TZ5Ai)

Logstash的数据处理过程主要包括：**输入**，**过滤器**，**输出**三部分，另外在输入和输出中可以使用**Codecs**对数据格式进行处理。这四个部分均以插件形式存在，用户通过定义管道配置文件，设置需要使用的输入，过滤器，输出，编解码器插件，以实现特定的数据采集，数据处理，数据输出等功能

* （1）**输入**：用于从数据源获取数据，常见的插件如file，syslog，redis，beats等\[ [详细参考](https://www.elastic.co/guide/en/logstash/5.6/input-plugins.html) ]
* （2）**过滤器**：用于处理数据如格式转换，数据派生等，常见的插件如grok，mutate，drop，clone，geoip等\[ [详细参考](https://www.elastic.co/guide/en/logstash/5.6/output-plugins.html) ]
* （3）**输出**：用于数据输出，常见的插件如elastcisearch，file，graphite，statsd等\[ [详细参考](https://www.elastic.co/guide/en/logstash/5.6/filter-plugins.html) ]
* （4）**编解码器**：编解码器不是一个单独的流程，而是在输入和输出等插件中用于数据转换的模块，用于对数据进行编码处理，常见的插件如json，multiline \[ [详细参考](https://www.elastic.co/guide/en/logstash/5.6/codec-plugins.html) ]

#### Logstash下载：（需要预先安装JDK8）

```
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.0.zip
unzip logstash-6.5.0.zip -d /usr/local/
cd /usr/local/logstash-6.5.0/
#简单测试
# bin/logstash -e 'input { stdin { } } output { stdout {} }'
```

**logstash配置语句详解**

logstash配置文件包含三个配置部分，分别为：input{}、filter{}、output{}。

{} 定义区域，区域内可以定义一个或多个插件，通过插件对数据进行收集，加工处理，输出。

&#x20;logstash.yml

* 包含Logstash配置标志，你可以在这个文件中设置标志，而不是在命令行中传递标志，在命令行中设置的任何标志都覆盖logstash.yml文件中的相应设置，更多信息见[logstash.yml](https://www.elastic.co/guide/en/logstash/current/logstash-settings-file.html%22%20/t%20%22_blank)[官](https://www.elastic.co/guide/en/logstash/current/logstash-settings-file.html%22%20/t%20%22_blank)[方](https://www.elastic.co/guide/en/logstash/current/logstash-settings-file.html%22%20/t%20%22_blank)[参考文档](https://www.elastic.co/guide/en/logstash/current/logstash-settings-file.html%22%20/t%20%22_blank)。

Logstash没有默认的配置文件,需要手动配

**数据类型：**

* 布尔值类型: ssl\_enable => true
* 字节类型: bytes => "1MiB"
* 字符串类型: name => "xkops"
* 数值类型: port => 22
* 数组: match => \["datetime","UNIX"]
* 哈希: options => {key1 => "value1",key2 => "value2"}
* 编码解码: codec => "json"
* 路径: file\_path => "/tmp/filename"
* 注释: #

**条件判断**：

* 等于: ==
* 不等于: !=
* 小于: <
* 大于: >
* 小于等于: <=
* 大于等于: >=
* 匹配正则: =\~
* 不匹配正则: !\~
* 包含: in
* 不包含: not in
* 与:   and
* 或:    or
* 非与: nand
* 非或:   xor
* 复合表达式: ()
* 取反符合: !()

#### 创建logstash.conf配置文件：

```
mkdir /usr/local/logstash-6.5.0/config.d
vim /usr/local/logstash-6.5.0/config.d/logstash.conf
input {
 beats {
 port => 5044 #定义服务监听的端口
 }
}
filter {
 if [fields][logtype] == "syslog" {
 grok {
 match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
 add_field => [ "received_at", "%{@timestamp}" ]
 add_field => [ "received_from", "%{host}" ]
 }
 syslog_pri { }
 date {
 match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
 }
 }
 if [fields][logtype] == "spring-boot-log4j2" {
 json {
 source => "message"
 target => "data"
 }
 }
}
output {
 if [fields][logtype] == "spring-boot-log4j2"{
 elasticsearch {
 hosts => ["10.88.216.26:9200"]
 index => "spring-boot-log4j2-%{+YYYY.MM.dd}"
 }
 }
 if [fields][logtype] == "syslog"{
 elasticsearch {
 hosts => ["10.88.216.26:9200"]
 index => "filebeat-%{+YYYY.MM.dd}"
 }
 }
}
```

**检查配置并启动Logstash**

```
cd /usr/local/logstash-6.5.0/
bin/logstash -f config.d/logstash.conf --config.test_and_exit       #--config.test_and_exit选项的意思是解析配置文件并报告任何错误
bin/logstash -f config.d/logstash.conf --config.reload.automatic    #config.reload.automatic选项的意思是启用自动配置加载，以至于每次你修改完配置文件以后无需停止然后重启Logstash
```

![](/files/-LxQD8YJshxlw-02ibAe)

**logstash三种启动方式**：

```
-e                          #sting类型启动
-f                          #指定配置文件启动
service logstash start      #服务启动
```

#### &#x20;**logstash常用插件**

[**文档及插件参考地址**](https://github.com/logstash-plugins)

**查看插件的方式**

```
cd /usr/local/logstash-6.5.0/bin
./logstash-plugin list
```

* input插件，常用的插件：file、tcp、udp、syslog，beats
* filter插件，常用的插件：json、grok
* output插件，常用的插件：file; tcp/udp; redis/kfaka; elasticsearch
* codec类插件，常用的插件：plain、json、json\_lines、rubydebug、multiline等

**启动filebeat和logstash**

```
cd /usr/local/filebeat-6.5.0-linux-x86_64/
nohup ./filebeat -e -c filebeat.yml -d "publish" &
cd /usr/local/logstash-6.5.0/
nohup bin/logstash -f config.d/logstash.conf --config.reload.automatic &
```

#### 安装kibana

```
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.0-linux-x86_64.tar.gz
tar -zvxf kibana-6.5.0-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/kibana-6.5.0-linux-x86_64/
```

#### 修改配置文件

```
vim /usr/local/kibana-6.5.0-linux-x86_64/config/kibana.yml
```

![](/files/-LxQD8YK0Sa7PA37KEO2)

汉化kibana插件下载地址：

{% embed url="<https://github.com/anbai-inc>" %}

```
git clone https://github.com/anbai-inc/Kibana_Hanization.git
cd Kibana_Hanization/old/
python main.py /usr/local/kibana-6.5.0-linux-x86_64
```

汉化成功后启动kibana

```
cd /usr/local/kibana-6.5.0-linux-x86_64/bin/
nohup ./kibana &
ss -ntulp | grep 5601
```

![](/files/-LxQD8YLdVMv4gnDPS2r)

浏览器访问：<http://服务器IP:5601/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/yun-wei-huan-jing-da-jian/ri-zhi-xi-tong-elk6.5.0+filebeat-bu-shu-wen-dang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
