# ELK+Filebeat+Kafka+ZooKeeper 构建海量日志分析平台
## 引言
什么要做日志分析平台?
随着业务量的增长,每天业务服务器将会产生上亿条的日志,单个日志文件达几个GB,这时我们发现用Linux自带工具,cat grep awk 分析越来越力不从心了,而且除了服务器日志,还有程序报错日志,分布在不同的服务器,查阅繁琐。
## 待解决的痛点:
1、大量不同种类的日志成为了运维人员的负担,不方便管理;
2、单个日志文件巨大,无法使用常用的文本工具分析,检索困难;
3、日志分布在多台不同的服务器上,业务一旦出现故障,需要一台台查看日志。
### 为了解决以上困扰:
接下来我们要一步步构建这个日志分析平台,架构图如下:
架构解读 : (整个架构从左到右,总共分为5层)
#### 第一层、数据采集层
最左边的是业务服务器集群,上面安装了filebeat做日志采集,同时把采集的日志分别发送给两个logstash服务。
#### 第二层、数据处理层,数据缓存层
logstash服务把接受到的日志经过格式处理,转存到本地的kafka broker+zookeeper 集群中。
#### 第三层、数据转发层
这个单独的Logstash节点会实时去kafka broker集群拉数据,转发至ES DataNode。
#### 第四层、数据持久化存储
ES DataNode 会把收到的数据,写磁盘,建索引库。
#### 第五层、数据检索,数据展示
ES Master + Kibana 主要协调ES集群,处理数据检索请求,数据展示。
笔者为了节约宝贵的服务器资源,把一些可拆分的服务合并在同一台主机。大家可以根据自己的实际业务环境自由拆分,延伸架构。
### 开 工 !
操作系统环境 : Ubuntu 16.04
各服务器角色分配 :
IP | 角色 | 所属集群
------------- | ------------------ | ------------- |
10.10.1.2 | 业务服务器+filebeat | 业务服务器集群
192.168.4.230 | Logstash+Kafka+ZooKeeper1_230 | Kafka Broker 集群
192.168.4.231 | Logstash+Kafka+ZooKeeper1_230 |
192.168.4.232 | Kafka+ZooKeeper |
192.168.4.233 | Logstash | 数据转发
192.168.4.236 | ES DataNode | Elasticsearch 集群
192.168.4.235 | ES DataNode |
192.168.4.234 | ES Master+Kibana |
### 一、安装部署Elasticsearch集群:
####布置ES 节点 192.168.4.234
#####1、安装jdk1.8,elasticsearch-7.2.0
jdk1.8安装
```shell
apt-get update
apt-get install openjdk-8-jdk
```
elasticsearch-7.2.0
```shell
mkdir -p /data/webapp
cd /data/webapp/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
tar -zxf elasticsearch-7.2.0-linux-x86_64.tar.gz
cd elasticsearch-7.2.0/
vim config/elasticsearch.yml
#################直接将以下内容贴到最后################
cluster.name: myes
node.name: node1
path.data: /data/webapp/elasticsearch-7.2.0/data
path.logs: /data/webapp/elasticsearch-7.2.0/logs
bootstrap.memory_lock: true
network.host: 192.168.4.234
http.port: 9200
discovery.seed_hosts: ["192.168.4.234", "192.168.4.235","192.168.4.236"]
cluster.initial_master_nodes: ["192.168.4.234", "192.168.4.235","192.168.4.236"]
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content- Length,Content-Type
#################内容结束################
mkdir /data/webapp/elasticsearch-7.2.0/data
```
#####2、系统调优,JVM调优、三台机器都得调整
```shell
vim /etc/sysctl.conf
#################配置系统最大打开文件描述符数,手动增加以下参数################
## fs.file-max=655350
## vm.max_map_count=655360
vim /etc/security/limits.conf
#################配置进程最大打开文件描述符,手动增加以下参数################
## * soft nofile 655350
## * hard nofile 655350
## elk soft memlock unlimited
## elk hard memlock unlimited
#################vim /data/webapp/elasticsearch-7.2.0/config/jvm.options################
## -Xms4g
## -Xmx4g
# 不超过机器内存50%
#配置生效
sysctl -p
```
##### 3、安装npm 环境
```shell
apt-get install -y python-software-properties software-properties-common
add-apt-repository ppa:chris-lea/node.js
apt-get update
apt-get install nodejs
apt install nodejs-legacy
apt install npm
npm config set registry https://registry.npm.taobao.org
npm config list
npm install n -g
```
##### 4、安装head开源插件
```shell
cd /data/webapp/elasticsearch-7.2.0/
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
cd /data/webapp/elasticsearch-7.2.0/elasticsearch-head-master/
npm install
npm install phantomjs-prebuilt@2.1.16 --ignore-scripts
```
####布置ES 节点 192.168.4.235
#####1、安装jdk1.8,elasticsearch-7.2.0
jdk1.8安装
```shell
apt-get update
apt-get install openjdk-8-jdk
```
elasticsearch-7.2.0
```shell
mkdir -p /data/webapp
cd /data/webapp/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
tar -zxf elasticsearch-7.2.0-linux-x86_64.tar.gz
cd elasticsearch-7.2.0/
vim config/elasticsearch.yml
#################直接将以下内容贴到最后################
cluster.name: myes
node.name: node2
path.data: /data/webapp/elasticsearch-7.2.0/data
path.logs: /data/webapp/elasticsearch-7.2.0/logs
bootstrap.memory_lock: true
network.host: 192.168.4.235
http.port: 9200
discovery.seed_hosts: ["192.168.4.234", "192.168.4.235","192.168.4.236"]
cluster.initial_master_nodes: ["192.168.4.234", "192.168.4.235","192.168.4.236"]
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content- Length,Content-Type
#################内容结束################
mkdir /data/webapp/elasticsearch-7.2.0/data
```
####1、系统调优,JVM调优、三台机器都得调整
```shell
vim /etc/sysctl.conf
#################配置系统最大打开文件描述符数,手动增加以下参数################
## fs.file-max=655350
## vm.max_map_count=655360
vim /etc/security/limits.conf
#################配置进程最大打开文件描述符,手动增加以下参数################
## * soft nofile 655350
## * hard nofile 655350
## elk soft memlock unlimited
## elk hard memlock unlimited
#################vim /data/webapp/elasticsearch-7.2.0/config/jvm.options################
## -Xms4g
## -Xmx4g
# 不超过机器内存50%
#配置生效
sysctl -p
```
####布置ES 节点 192.168.4.236
#####1、安装jdk1.8,elasticsearch-7.2.0
jdk1.8安装
```shell
apt-get update
apt-get install openjdk-8-jdk
```
elasticsearch-7.2.0
```shell
mkdir -p /data/webapp
cd /data/webapp/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz
tar -zxf elasticsearch-7.2.0-linux-x86_64.tar.gz
cd elasticsearch-7.2.0/
vim config/elasticsearch.yml
#################直接将以下内容贴到最后################
cluster.name: myes
node.name: node3
path.data: /data/webapp/elasticsearch-7.2.0/data
path.logs: /data/webapp/elasticsearch-7.2.0/logs
bootstrap.memory_lock: true
network.host: 192.168.4.236
http.port: 9200
discovery.seed_hosts: ["192.168.4.234", "192.168.4.235","192.168.4.236"]
cluster.initial_master_nodes: ["192.168.4.234", "192.168.4.235","192.168.4.236"]
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content- Length,Content-Type
#################内容结束################
mkdir /data/webapp/elasticsearch-7.2.0/data
```
#####2、系统调优,JVM调优、三台机器都得调整
```shell
vim /etc/sysctl.conf
#################配置系统最大打开文件描述符数,手动增加以下参数################
## fs.file-max=655350
## vm.max_map_count=655360
vim /etc/security/limits.conf
#################配置进程最大打开文件描述符,手动增加以下参数################
## * soft nofile 655350
## * hard nofile 655350
## elk soft memlock unlimited
## elk hard memlock unlimited
#################vim /data/webapp/elasticsearch-7.2.0/config/jvm.options################
## -Xms4g
## -Xmx4g
# 不超过机器内存50%
#配置生效
sysctl -p
```
#### 启动集群
```shell
##elasticsearch 不允许root用户启动
##创建用户并赋权 三台都得执行
useradd elk
chown -R elk /data/webapp/elasticsearch-7.2.0
su elk
##三台分别通过elk用户启动elasticsearch,-d后台启动
/data/webapp/elasticsearch-7.2.0/bin/elasticsearch -d
##后台启动
npm run start &
##访问http://192.168.4.234:9100/
##连接http://192.168.4.234:9200/
##如果三台机器都能正常显示,说明一切正常
```
####Elasticsearch安装Kiabna、X-Pack插件
Kibana是一个为 ElasticSearch 提供的数据分析的 Web 接口。可使用它对日志进行高效的搜索、可视化、分析等各种操作。
首先到官网下载最新版本的Kiabna压缩包。
可以使用如下命令,注意将最新的可用的下载链接填入:
```shell
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-linux-x86_64.tar.gz
tar -zxf kibana-7.2.0-linux-x86_64.tar.gz
cd /data/webapp/kibana-7.2.0-linux-x86_64
vim config/kibana.yml
=============================更改内容如下========================================================
#grep "^[a-Z]" config/kibana.yml
server.host: "192.168.4.234"
elasticsearch.hosts: ["http://192.168.4.234:9200","http://192.168.4.235:9200","http://192.168.4.236:9200"]
elasticsearch.username: "elastic"
elasticsearch.password: "AybhEjgdFdwe6hLG"
i18n.locale: "zh-CN"
map.includeElasticMapsService: false
map.tilemap.url: 'http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}'
=============================end===============================================================
##ES7.2.0默认已经安装了X-Pack,无需再安装
##(1)启动ES
##(2)启用trial license(30天试用)
## 官网说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-xpack.html
##选择其中一个节点执行如下命令
curl -H "Content-Type:application/json" -XPOST http://192.168.4.234:9200/_xpack/license/start_trial?acknowledge=true
##可以看到elasticsearch控制台显示license 已变为trial
破解后的文件地址
x-pack-core-7.2.0.jar
链接:https://pan.baidu.com/s/1XFUU9C9cJ9vjYoJW9bPMQg 密码:77f9
```
####X-Pack插件破解
#####1、重写x-pack下的1个类:LicenseVerifier.java 和 XPackBuild.java
```java
##使用Luyten打开org.elasticsearch/license/LicenseVerifier.class先保存为LicenseVerifier.java文件,然后修改内容如下
package org.elasticsearch.license;
public class LicenseVerifier
{
public static boolean verifyLicense(final License license, final byte[] encryptedPublicKeyData) {
return true;
}
public static boolean verifyLicense(final License license) {
return true;
}
}
##使用Luyten打开org.elasticsearch/xpack/core/XPackBuild.class先保存为XPackBuild.java文件,然后修改内容如下
package org.elasticsearch.xpack.core;
import org.elasticsearch.common.io.*;
import java.net.*;
import org.elasticsearch.common.*;
import java.nio.file.*;
import java.io.*;
import java.util.jar.*;
public class XPackBuild {
public static final XPackBuild CURRENT;
private String shortHash;
private String date;
@SuppressForbidden(reason = "looks up path of xpack.jar directly") static Path getElasticsearchCodebase() {
final URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
try { return PathUtils.get(url.toURI()); }
catch (URISyntaxException bogus) {
throw new RuntimeException(bogus); }
}
XPackBuild(final String shortHash, final String date) {
this.shortHash = shortHash;
this.date = date;
}
public String shortHash() {
return this.shortHash;
}
public String date(){
return this.date;
}
static {
final Path path = getElasticsearchCodebase();
String shortHash = null;
String date = null;
Label_0157: { shortHash = "Unknown"; date = "Unknown";
}
CURRENT = new XPackBuild(shortHash, date);
}
}
##上传文件到linux机器,执行此命令编译得到LicenseVerifier.class文件
javac -cp "/data/webapp/elasticsearch-7.2.0/lib/elasticsearch-7.2.0.jar:/data/webapp/elasticsearch-7.2.0/lib/lucene-core-8.0.0.jar:/data/webapp/elasticsearch-7.2.0/modules/x-pack-core/x-pack-core-7.2.0.jar:/data/webapp/elasticsearch-7.2.0/lib/elasticsearch-core-7.2.0.jar" LicenseVerifier.java
javac -cp "/data/webapp/elasticsearch-7.2.0/lib/elasticsearch-7.2.0.jar:/data/webapp/elasticsearch-7.2.0/lib/lucene-core-8.0.0.jar:/data/webapp/elasticsearch-7.2.0/modules/x-pack-core/x-pack-core-7.2.0.jar:/data/webapp/elasticsearch-7.2.0/lib/elasticsearch-core-7.2.0.jar" XPackBuild.java
##替换原有文件并重新打包
jar -xvf x-pack-core-7.2.0.jar
mv … /LicenseVerifier.class org/elasticsearch/license/
mv … /XPackBuild.class org/elasticsearch/xpack/core/
jar -cvf x-pack-core-7.2.0.jar ./*
##替换到原有路径(略
##修改配置文件
vim /data/webapp/elasticsearch-7.2.0/config/elasticsearch.yml
添加如下字符
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
##TLS/SSL
##生成认证文件 elastic-certificates.p12
bin/elasticsearch-certutil ca
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
##添加刚才输入的密码到 lasticsearch keystore ,会在 config 文件夹下生成elasticseaerch.keystore 文件
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
##复制 elastic-certificates.p12 到 config/certs 文件夹下
##真正起作用的是 config 文件夹下的 elasticseaerch.keystore 文件
##拷贝 elastic-certificates.p12 和 elasticseaerch.keystore 到所有节点机器
##重启所有elasticsearch
```
#####2.开启ES的登录功能
```shell
1.##设置用户名和密码
/data/webapp/elasticsearch-7.2.0/bin/elasticsearch-setup-passwords interactive
=============================输出内容如下========================================================
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
=============================end===============================================================
##修改密码方式
curl -H "Content-Type:application/json" -XPOST -u elastic 'http://192.168.4.234:9200/_xpack/security/user/elastic/_password' -d'{"password":"123456"}'
##增加kibana连接es密码
vim /data/webapp/kibana-7.2.0-linux-x86_64/config/kibana.yml
elasticsearch.username: "elastic"
elasticsearch.password: "123456"
##Web访问ES
http://192.168.4.234:9100/?auth_user=elastic&auth_password=123456
##用户名:elastic(默认)
##密码:123456
```
#####3、去官网申请license证书
```shell
##https://license.elastic.co/registration 官网地址
1.邮箱认真写,用来接收json文件的
2.country写china,其它都可以随便写
3.点击申请后邮箱马上会收到一个邮件
```
#####4、去官网申请license证书
```shell
主要修改这几个地方
1."type":"basic" 替换为 "type":"platinum" # 基础版变更为铂金版
2."expiry_date_in_millis":1561420799999 替换为 "expiry_date_in_millis":3107746200000# 1年变为50年
好好保存,修改后的文件可以重复使用到其它ES服务器
```
#####5.上传证书完成修改
```shell
1、打开elasticsearch服务 和 kibana服务
2.上传证书,就是那个json文件 upload license
3.上传成功.到此破解x-pack已经成功了.如图所示续命到了2068年,可以随意使用ES X-pack的一些付费功能 机器学习 安全登录
```
####布置Logstash 数据转发节点 192.168.4.233
#####1、安装jdk1.8
```shell
apt-get update
apt-get install openjdk-8-jdk
```
#####2、安装Logstash
```shell
mkdir -p /data/webapp
cd /data/webapp/
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.0.tar.gz
tar -zxf logstash-7.2.0.tar.gz
##下载全球IP库
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
tar -zxf GeoLite2-City.tar.gz
##添加从kafka到ES配置文件
vim logstash-7.2.0/config/kafka_to_es.conf
==============================================================================================
input{
kafka{
codec => json
bootstrap_servers => "192.168.4.230:9092,192.168.4.231:9092,192.168.4.232:9092"
auto_offset_reset => "latest" #从最新的偏移量开始消费
consumer_threads => 5
decorate_events => true #此属性会将当前topic、offset、group、partition等信息也带到message中
topics => "elk" #数组类型,可配置多个topic
}
# stdin{}
}
filter {
mutate {
gsub =>[
"message", "[\\]", '[//]'
]
}
json {
source => "message"
skip_on_invalid_json => true
}
mutate {
rename => {"[headers][http_slbip]" => "slbip" }
}
if [fields][logsource] == "nginx" {
if[http_x_connecting_ip] != "-" { #知道创宇
mutate {
rename => {"[http_x_connecting_ip]" => "slbip" }
}
}
if[http_cdn_src_ip] != "-" { # 网宿
mutate {
rename => {"[http_cdn_src_ip]" => "slbip" }
}
}
}
}
output{
elasticsearch {
hosts => ["192.168.4.234:9200"]
index => "logstash-%{[fields][dep]}-%{[fields][project]}-%{[fields][logsource]}-%{+YYYY.MM.dd}"
user => "elastic"
password => "shuqincq"
}
stdout{
codec => "rubydebug"
}
stdout {
}
}
================================end===========================================================
```
####布置Logstash 数据转发节点 192.168.4.230
#####1、安装jdk1.8
```shell
apt-get update
apt-get install openjdk-8-jdk
```
#####2、安装Logstash
```shell
mkdir -p /data/webapp
cd /data/webapp/
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.0.tar.gz
tar -zxf logstash-7.2.0.tar.gz
##添加从kafka到ES配置文件
vim logstash-7.2.0/config/logstash_in_kafka.conf
==============================================================================================
input{
kafka{
codec => json
bootstrap_servers => "192.168.4.230:9092,192.168.4.231:9092,192.168.4.232:9092"
auto_offset_reset => "latest" #从最新的偏移量开始消费
consumer_threads => 5
decorate_events => true #此属性会将当前topic、offset、group、partition等信息也带到message中
topics => "elk" #数组类型,可配置多个topic
}
# stdin{}
}
filter {
geoip {
source => "slbip" # 与日志中访问地址的key要对应
target => "geoip"
database => "/data/webapp/GeoLite2-City_20190709/GeoLite2-City.mmdb"
add_field => [ "[geoip][coordinates]","%{[geoip][longitude]}" ]
add_field => [ "[geoip][coordinates]","%{[geoip][latitude]}" ]
}
mutate {
convert => [ "[geoip][coordinates]","float" ]
}
}
output{
elasticsearch {
hosts => ["192.168.4.234:9200"]
index => "logstash-%{[fields][dep]}-%{[fields][project]}-%{[fields][logsource]}-%{+YYYY.MM.dd}"
user => "elastic"
password => "shuqincq"
}
stdout{
codec => "rubydebug"
}
stdout {
}
}
================================end===========================================================
```
####布置Logstash 数据转发节点 192.168.4.231
#####1、安装jdk1.8
```shell
apt-get update
apt-get install openjdk-8-jdk
```
#####2、安装Logstash
```shell
mkdir -p /data/webapp
cd /data/webapp/
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.0.tar.gz
tar -zxf logstash-7.2.0.tar.gz
##添加从kafka到ES配置文件
vim logstash-7.2.0/config/logstash_in_kafka.conf
##添加logstash启动配置文件
==============================================================================================
input {
beats {
port => 5044
ssl => false
}
http {
host => "0.0.0.0"
port => 5045
additional_codecs => {"application/json"=>"json"}
codec => "plain"
ssl => false
}
stdin{}
}
filter {
if [fields][logtype] == "proxylog" {
# grok { match => [ "message", "%{TIMESTAMP_ISO8601:date} - %{USER:proxy} - %{USER:status} - %{NOTSPACE:file} - %{USER:function} - %{NUMBER:row} - %{NOTSPACE:prompt}" ] }
# if [status] == "INFO" {
# drop { }
# }
if [fields][logtype] == "proxylog" {
json {
source => "message"
}
}
}
if [fields][logsource] == "http" and [fields][dep] != "exchange" {
drop {}
}
mutate {
rename => {"[headers][http_slbip]" => "slbip" }
}
}
output {
kafka {
id =>"my_kafka_id"
codec => json
topic_id =>"elk"
bootstrap_servers => "192.168.4.230:9092,192.168.4.231:9092,192.168.4.232:9092"
batch_size => 5
}
stdout {
}
}
================================end==============================================================
```
####安装zookeeper,Kafka数据转发节点 192.168.4.230、192.168.4.231、192.168.4.232
这里最好开三台一起执行
#####1、zookeeper, Kafka
```shell
echo "192.168.4.230 kafka1" >> /etc/hosts
echo "192.168.4.231 kafka2" >> /etc/hosts
echo "192.168.4.232 kafka3" >> /etc/hosts
mkdir -p /data/webapp
cd /data/webapp
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz
tar -zxf apache-zookeeper-3.5.5-bin.tar.gz
mkdir /data/webapp/apache-zookeeper-3.5.5-bin/data/
vim /data/webapp/apache-zookeeper-3.5.5-bin/conf/zoo.cfg
=============================添加如下内容========================================================
# 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
clientPort=2181
# leader 与 follower 之间发送和应答时间,如果该follower 在设置的时间内不能与leader 进行通信,那么
此 follower 将被视为不可用。
initLimit=10
# leader 与 follower 之间发送和应答时间,如果该follower 在设置的时间内不能与leader 进行通信,那么
此 follower 将被视为不可用。
syncLimit=5
# 服务器之间或客户端与服务器之间的单次心跳检测时间间隔,单位为毫秒
tickTime=2000
# 自定义的zookeeper保存数据的目录
dataDir=/data/webapp/apache-zookeeper-3.5.5-bin/data
# 服务器编号=服务器IP:LF数据同步端口:LF选举端口
server.1=kafka1:2888:3888
server.2=kafka2:2888:3888
server.3=kafka3:2888:3888
=============================end===============================================================
##配置zookeeper集群节点ID
##分别在三台机器上执行
echo "1" > /data/webapp/apache-zookeeper-3.5.5-bin/data/myid
echo "2" > /data/webapp/apache-zookeeper-3.5.5-bin/data/myid
echo "3" > /data/webapp/apache-zookeeper-3.5.5-bin/data/myid
##在三个节点上启动zookeeper
/data/webapp/apache-zookeeper-3.5.5-bin/bin/zkServer.sh start
##查看状态,如果没问题,可以继续
/data/webapp/apache-zookeeper-3.5.5-bin/bin/zkServer.sh status
##安装kafka
/data/webapp
wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz
tar -zxf kafka_2.12-2.3.0.tgz
cd /data/webapp/kafka_2.12-2.3.0
vim config/server.properties
=============================更改内容如下========================================================
#grep "^[a-Z]" config/zookeeper.properties
broker.id=1 #注意每台不一样
listeners=PLAINTEXT://192.168.4.230:9092 #注意每台不一样
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.168.4.230:2181,192.168.4.231:2181,192.168.4.232:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
=============================end===============================================================
##启动kafka
/data/webapp/kafka_2.12-2.3.0/bin/kafka-server-start.sh -daemon /data/webapp/kafka_2.12-2.3.0/config/server.properties
##可通过jps查看结果
22108 Jps
21693 Kafka
21694 QuorumPeerMain
```
####安装filebeat 应用服务器
#####下载地址https://www.elastic.co/downloads/beats/filebeat
```shell
mkdir /data/webapp
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.2.0-linux-x86_64.tar.gz
tar -zxf filebeat-7.2.0-linux-x86_64.tar.gz
less /data/webapp/filebeat-6.3.0-linux-x86_64/filebeat.yml
========= 以下用的是6.3.0版本 =========
filebeat.prospectors:
- input_type: log
paths:
- /var/log/nginx/access.log
document_type: "nginx_access"
fields:
dep: baoquan
project: baoquan
logsource: nginx
logtype: nginxacclog
output.logstash:
hosts: ["101.68.74.170:25044"]
loadbalance: true
```
supervisor相关配置脚本
kafka.conf
```shell
[program:kafka]
directory=/data/webapp/kafka_2.12-2.3.0 ; 程序文件夹
command=/data/webapp/kafka_2.12-2.3.0/bin/kafka-server-start.sh /data/webapp/kafka_2.12-2.3.0/config/server.properties ; 启动程序的命令
user=root ; 指定用户
priority=2 ; 优先级 默认:999,数值越小优先级越高
autostart=true ; 是否随supervisor启动而自动启动
autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
;startsecs=40 ; 启动正常运行多久,则为启动成功。默认为:1秒
stopasgroup=true ; 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true ; 默认为false,向进程组发送kill信号,包括子进程
redirect_stderr=true ; std_error日志重定向到std_out
stdout_logfile_maxbytes=50MB ; 日志最大大小
stdout_logfile_backups=10 ; 日志最多保留数量
stdout_logfile=/var/log/supervisor/supervisor_kafka.log ; 日志路径
```
zk.conf
```shell
[program:zk]
directory=/data/webapp/apache-zookeeper-3.5.5-bin ; 程序文件夹
command=/data/webapp/apache-zookeeper-3.5.5-bin/bin/zkServer.sh start-foreground ; 启动程序的命令
user=root ; 指定用户
priority=1 ; 优先级 默认:999,数值越小优先级越高
autostart=true ; 是否随supervisor启动而自动启动
autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
;startsecs=40 ; 启动正常运行多久,则为启动成功。默认为:1秒
stopasgroup=true ; 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true ; 默认为false,向进程组发送kill信号,包括子进程
redirect_stderr=true ; std_error日志重定向到std_out
stdout_logfile_maxbytes=50MB ; 日志最大大小
stdout_logfile_backups=10 ; 日志最多保留数量
stdout_logfile=/var/log/supervisor/supervisor_zk.log ; 日志路径
```
logstash.conf
```shell
[program:logstash]
directory=/data/webapp/logstash-7.2.0 ; 程序文件夹
command=/data/webapp/logstash-7.2.0/bin/logstash -f /data/webapp/logstash-7.2.0/config/logstash_in_kafka.conf ; 启动程序的命令
user=root ; 指定用户
priority=1 ; 优先级 默认:999,数值越小优先级越高
autostart=true ; 是否随supervisor启动而自动启动
autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
;startsecs=40 ; 启动正常运行多久,则为启动成功。默认为:1秒
stopasgroup=true ; 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括>子进程
killasgroup=true ; 默认为false,向进程组发送kill信号,包括子进程
redirect_stderr=true ; std_error日志重定向到std_out
stdout_logfile_maxbytes=50MB ; 日志最大大小
stdout_logfile_backups=10 ; 日志最多保留数量
stdout_logfile=/var/log/supervisor/supervisor_logstash.log ; 日志路径
```
els.conf
```shell
[program:els]
directory=/data/webapp/elasticsearch-7.2.0 ; 程序文件夹
command=su -c "/data/webapp/elasticsearch-7.2.0/bin/elasticsearch" elk ; 启动程序的命令
user=root ; 指定用户
priority=1 ; 优先级 默认:999,数值越小优先级越高
autostart=true ; 是否随supervisor启动而自动启动
autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
;startsecs=40 ; 启动正常运行多久,则为启动成功。默认为:1秒
stopasgroup=true ; 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true ; 默认为false,向进程组发送kill信号,包括子进程
redirect_stderr=true ; std_error日志重定向到std_out
stdout_logfile_maxbytes=50MB ; 日志最大大小
stdout_logfile_backups=10 ; 日志最多保留数量
stdout_logfile=/var/log/supervisor/supervisor_elasticsearch.log ; 日志路径
```
kibana.conf
```shell
[program:kibana]
directory=/data/webapp/kibana-7.2.0-linux-x86_64 ; 程序文件夹
command=/data/webapp/kibana-7.2.0-linux-x86_64/bin/kibana --allow-root ; 启动程序的命令
user=root ; 指定用户
priority=3 ; 优先级 默认:999,数值越小优先级越高
autostart=true ; 是否随supervisor启动而自动启动
autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
;startsecs=40 ; 启动正常运行多久,则为启动成功。默认为:1秒
stopasgroup=true ; 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true ; 默认为false,向进程组发送kill信号,包括子进程
redirect_stderr=true ; std_error日志重定向到std_out
stdout_logfile_maxbytes=50MB ; 日志最大大小
stdout_logfile_backups=10 ; 日志最多保留数量
stdout_logfile=/var/log/supervisor/supervisor_kibana.log ; 日志路径
```
elsHead.conf
```shell
[program:elsHead]
directory=/data/webapp/elasticsearch-7.2.0/elasticsearch-head-master ; 程序文件夹
command=/data/webapp/elasticsearch-7.2.0/elasticsearch-head-master/node_modules/grunt/bin/grunt server ; 启动程序的命令
user=root ; 指定用户
priority=2 ; 优先级 默认:999,数值越小优先级越高
autostart=true ; 是否随supervisor启动而自动启动
autorestart=true ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
;startsecs=40 ; 启动正常运行多久,则为启动成功。默认为:1秒
stopasgroup=true ; 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true ; 默认为false,向进程组发送kill信号,包括子进程
redirect_stderr=true ; std_error日志重定向到std_out
stdout_logfile_maxbytes=50MB ; 日志最大大小
stdout_logfile_backups=10 ; 日志最多保留数量
stdout_logfile=/var/log/supervisor/supervisor_elasticsearch.log ; 日志路径
```
定时删除任务脚本
```shell
#!/bin/bash
#删除ELK30天前的日志
DATE=`date -d "30 days ago" +%Y.%m.%d`
curl -s -H "Content-Type:application/json" -u elastic:123456 -XGET http://192.168.12.206:9200/_cat/indices?v |grep $DATE | awk -F '[ ]+' '{print $3}' | grep logstash-* >/tmp/elk.log
alias urlencode='python -c "import sys, urllib.parse as ul; print (ul.quote(sys.argv[1]))"'
for elk in `cat /tmp/elk.log`
do
echo $elk
index=`python -c "import sys, urllib.parse as ul; print (ul.quote('$elk'))"`
curl -s -H "Content-Type:application/json" -u elastic:shuqincq -XDELETE http://192.168.12.206:9200/$index
done
```
kibana 仪表盘页面
https://www.linuxidc.com/Linux/2016-12/138103.htm