分布式日志管理ELK部署

一、ELK简介

实际上ELK是三款软件的简称,分别是Elasticsearch、 Logstash、Kibana组成。
Elasticsearch 基于java,是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Kibana 基于nodejs,也是一个开源和免费的工具,Kibana可以为Logstash和ElasticSearch提供的日志分析友好的Web 界面,可以汇总、分析和搜索重要数据日志。
Logstash 基于java,是一个开源的用于收集,分析和存储日志的工具。

二、Elasticsearch安装部署

2.1 下载

官网下载地址: https://www.elastic.co/cn/downloads/elasticsearch

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.2-linux-x86_64.tar.gz

2.2 安装

解压到相应目录

tar -zxvf elasticsearch-7.10.2-linux-x86_64.tar.gz -C /usr/local

修改配置

cd /usr/local/elasticsearch-7.10.2/config/
vim elasticsearch.yml
node.name: node-1
path.data: /usr/local/elasticsearch-7.10.2/data
path.logs: /usr/local/elasticsearch-7.10.2/logs
network.host: 127.0.0.1
http.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["node-1"]

# 缓存回收大小,无默认值
# 有了这个设置,最久未使用(LRU)的 fielddata 会被回收为新数据腾出空间
# 控制fielddata允许内存大小,达到HEAP 20% 自动清理旧cache,默认是无限缓存下去,肯定会超内存然后内存溢出
indices.fielddata.cache.size: 20%
# -----------------------------------主要是添加上面的那一个配置-------------------------------
indices.breaker.total.use_real_memory: false
# fielddata 断路器默认设置堆的 60% 作为 fielddata 大小的上限。
indices.breaker.fielddata.limit: 40%
# request 断路器估算需要完成其他请求部分的结构大小,例如创建一个聚合桶,默认限制是堆内存的 40%。
indices.breaker.request.limit: 40%
# total 揉合 request 和 fielddata 断路器保证两者组合起来不会使用超过堆内存的 70%(默认值)。
indices.breaker.total.limit: 95%

创建es用户 因为ElasticSearch不支持Root用户直接操作,因此我们需要创建一个es用户

useradd es
chown -R es:es /usr/local/elasticsearch-7.10.2

2.3 启动

切换用户成es用户进行操作

su - es
/usr/local/elasticsearch-7.10.2/bin/elasticsearch

后台启动

/usr/local/elasticsearch-7.10.2/bin/elasticsearch -d 

2.4 验证

在浏览器访问9200端口地址:http://127.0.0.1:9200,如果返回了elasticsearch信息,就表示已经启动成功

三、Logstash安装部署

3.1 下载

官网下载地址: https://www.elastic.co/cn/downloads/logstash

wget https://artifacts.elastic.co/downloads/logstash/logstash-7.10.2-linux-x86_64.tar.gz

2.2 安装

解压到相应目录

tar -zxvf logstash-7.10.2-linux-x86_64.tar.gz -C /usr/local

新增配置文件,收集elasticsearch日志

cd /usr/local/logstash-7.10.2/bin
vim logstash-elasticsearch.conf
input {
    stdin {}
}
output {
    elasticsearch {
        hosts => '127.0.0.1:9200'
    }
    stdout {
        codec => rubydebug
    }
}

2.3 启动

./logstash -f logstash-elasticsearch.conf

后台启动

nohup ./logstash -f logstash-elasticsearch.conf>/dev/null 2>&1 &

2.4 日志收集

Apache对应服务器安装logstash,配置规则,例如新建logstash-apache.conf

input {
  file {
    path => "/usr/local/dlmz-adminlogs/sys-*.log"
    start_position => beginning
    sincedb_path => "/dev/null"
    codec => multiline {
      pattern => "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"
      negate => true
      auto_flush_interval => 3
      what => previous
    }
    type => "project1-log"
  }
}

file {
    path => "/usr/local/dlmz-adminlogs/info-*.log"
    start_position => beginning
    sincedb_path => "/dev/null"
    codec => multiline {
      pattern => "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"
      negate => true
      auto_flush_interval => 3
      what => previous
    }
    type => "project2-log"
  }
}

filter {
  if [path] =~ "info" {
    mutate { replace => { type => "sys-info" } }
    grok {
      match => { "message" => "%{COMBINEDAPACHELOG}" }
    }
    date {
      match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
    }
  } else if [path] =~ "error" {
    mutate { replace => { type => "sys-error" } }
  } else {
    mutate { replace => { type => "random_logs" } }
  }
}

output {
  elasticsearch {
    hosts => '127.0.0.1:9200'
    #index => "logstash-project-%{+YYYY.MM.dd}"
    #user => 'elastic'
    #password => 'password'
  }
  stdout { codec => rubydebug }
}

启动logstash

./logstash -f logstash-apache.conf

Thinkphp对应服务器安装logstash,配置规则,例如新建logstash-thinkphp.conf

input {
  file {
    path => "/www/wwwroot/project/runtime/log/*/*.log"
    start_position => beginning
    sincedb_path => "/dev/null"
    codec => multiline {
      pattern => "^-"
      negate => true
      auto_flush_interval => 3
      what => previous
    }
    type => "project-log"
  }
}

output {
  elasticsearch {
    hosts => 'http://127.0.0.1:9200'
    #index => "logstash-project-%{+YYYY.MM.dd}"
    #user => 'elastic'
    #password => 'password'
  }
  stdout { codec => rubydebug }
}

启动logstash

./logstash -f logstash-thinkphp.conf

四、Kibana安装部署

4.1 下载

官网下载地址: https://www.elastic.co/cn/downloads/kibana

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

2.2 安装

解压到相应目录

tar -zxvf kibana-7.10.2-linux-x86_64.tar.gz -C /usr/local
mv /usr/local/kibana-7.10.2-linux-x86_64 /usr/local/kibana-7.10.2

修改配置

cd /usr/local/kibana-7.10.2/config
vim kibana.yml
server.port: 5601 
server.host: "0.0.0.0" 
elasticsearch.hosts: ["http://127.0.0.1:9200"] 
kibana.index: ".kibana"
i18n.locale: "zh-CN"

授权es用户

chown -R es:es /usr/local/kibana-7.10.2/

2.3 启动

切换用户成es用户进行操作

su - es
/usr/local/kibana-7.10.2/bin/kibana

后台启动

/usr/local/kibana-7.10.2/bin/kibana &

2.4 验证

在浏览器访问5601端口地址:http://127.0.0.1:5601,如果打开了Kibana系统界面,就表示已经启动成功。然后可以通过Discover可视化检索各个服务日志数据。

五、ELK开启密码

5.1 修改elasticsearch.yml配置

cd /usr/local/logstash-7.10.2/bin
vim logstash-elasticsearch.conf

加入这两个参数

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

5.2 重新启动elasticsearch,然后要设置6个账号和密码,包括elasticsearch、kibana等

./bin/elasticsearch-setup-passwords interactive

5.3 重新启动elasticsearch,验证浏览器重新输入密码正确进入

5.4 Kibana设置密码

ElasticSearch设置密码后刷新Kibana,
发现报错: Kibana server is not ready yet,这就是因为kibana没设置密码导致

修改kibana.yml

cd /usr/local/kibana-7.10.2/config
vim kibana.yml

加入这两个参数

elasticsearch.username: "elastic"
elasticsearch.password: "password"

5.5 重启&访问验证

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容