运维

【ELK】Docker Compose部署ELK分布式集群

2020-02-06  本文已影响0人  下里巴人也

ELK官网:https://www.elastic.co/cn/
中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details
ELK-Docker:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

1. 什么是ELK

ELK 是 Elasticsearch、Logstrash 和 Kibana 的缩写,是一套成熟的日志管理系统,最近新增Beats,它是一个轻量级的日志收集处理工具(Agent),Beats 占用资源少,适合于在各个服务器上搜集日志后传输给 Logstash,官方也推荐此工具。ELK Stack 已经成为目前最流行的集中式日志解决管理方案。

2. 基于Filebeat分布式集群架构部署方案

具体基于Filebeat的ELK分布式集中日志解决方案架构如图所示:

ELK 分布式集群部署

日志采集流程如图:

日志采集流程

logstash是java应用,解析日志是非的消耗cpu和内存,logstash安装在应用部署的机器上显得非常的影响应用的性能。最常见的做法是用filebeat部署在应用的机器上,logstash单独部署,然后由filebeat将日志输出给logstash解析,解析完由logstash再传给elasticsearch

3. 部署配置

准备条件:两台云主机
主机1:模拟我们的生产环境,只部署filebeat
主机2:部署logstash,es集群(这里只部署一台),kibana

[root@localhost elk-filebeat]# ls -l
total 16
-rw-r--r-- 1 root root  522 Feb  6 10:38 docker-compose.yml
-rw-r--r-- 1 root root  667 Feb  6 10:40 filebeat.yml
-rwxr-xr-x 1 root root  777 Feb  6 10:39 init.sh
  1. docker-compose.yml
version: "3"
services:
  filebeat:
    # 容器名称
    container_name: filebeat
    # 主机名称
    hostname: filebeat
    # 镜像
    image: docker.elastic.co/beats/filebeat:7.1.1
    # 重启机制
    restart: always
    # 持久化挂载
    volumes:
      - ./filebeat/conf/filebeat.yml:/usr/share/filebeat/filebeat.yml
      # 映射到容器中[作为数据源]
      - /data/shigongbao/logs/django:/data/logs
      - ./filebeat/logs:/usr/share/filebeat/logs
      - ./filebeat/data:/usr/share/filebeat/data
  1. filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    # 当前目录下的所有.log文件
    - /data/logs/*.log
  multiline.pattern: ^\[
  multiline.negate: true
  multiline.match: after

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

setup.template.settings:
  index.number_of_shards: 1

setup.dashboards.enabled: false

setup.kibana:
  host: "http://主机2的ip:5601"

# 不直接传输至ES
#output.elasticsearch:
# hosts: ["http://es-master:9200"]
# index: "filebeat-%{[beat.version]}-%{+yyyy.MM.dd}"

output.logstash:
  hosts: ["主机2的ip:5044"]

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

4)部署脚本init.sh

#! /bin/bash

if [ ! -d "./filebeat/" ]; then
    mkdir -p ./filebeat/conf ./filebeat/logs ./filebeat/data
fi
if [ -d "./filebeat/" ]; then
    chmod 777 ./filebeat/data/ ./filebeat/logs/
fi
if [ -f "./filebeat.yml" ]; then
    cp ./filebeat.yml ./filebeat/conf
fi

docker-compose up --build -d
/data/elk-master
[root@xiabing elk-master]# ls -l
total 44
-rw-r--r-- 1 root root 2358 Feb  6 10:53 docker-compose.yml
-rw-r--r-- 1 root root  748 Feb  6 10:49 es-master.yml
-rw-r--r-- 1 root root  667 Feb  6 10:35 filebeat.yml
-rwxr-xr-x 1 root root 1943 Feb  5 15:17 init.sh
-rw-r--r-- 1 root root  152 Feb  6 10:42 kibana.yml
-rw-r--r-- 1 root root  444 Feb  6 10:36 logstash-filebeat.conf
-rw-r--r-- 1 root root  171 Feb  6 10:49 logstash.yml
  1. docker-compose.yml
version: "3"
services:
  es-master:
    container_name: es-master
    hostname: es-master
    image: elasticsearch:7.1.1
    restart: always
    ports:
      - 9200:9200
      - 9300:9300
    volumes:
      - /data/elk-master/elasticsearch/master/conf/es-master.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /data/elk-master/elasticsearch/master/data:/usr/share/elasticsearch/data
      - /data/elk-master/elasticsearch/master/logs:/usr/share/elasticsearch/logs
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"

  es-head:
    container_name: es-head
    image: mobz/elasticsearch-head:5
    restart: always
    ports:
      - 9100:9100
    depends_on:
      - es-master

  kibana:
    container_name: kibana
    hostname: kibana
    image: kibana:7.1.1
    restart: always
    ports:
      - 5601:5601
    volumes:
      - /data/elk-master/kibana/conf/kibana.yml:/usr/share/kibana/config/kibana.yml
    environment:
      - elasticsearch.hosts=http://本机外网ip:9200
    depends_on:
      - es-master

  filebeat:
    # 容器名称
    container_name: filebeat
    # 主机名称
    hostname: filebeat
    # 镜像
    image: docker.elastic.co/beats/filebeat:7.1.1
    # 重启机制
    restart: always
    # 持久化挂载
    volumes:
      - /data/elk-master/filebeat/conf/filebeat.yml:/usr/share/filebeat/filebeat.yml
      # 映射到容器中[作为数据源]
      - /data/shigongbao/logs/django:/data/logs
      - /data/elk-master/filebeat/logs:/usr/share/filebeat/logs
      - /data/elk-master/filebeat/data:/usr/share/filebeat/data
    # 将指定容器连接到当前连接,可以设置别名,避免ip方式导致的容器重启动态改变的无法连接情况
    links:
      - logstash
    # 依赖服务[可无]
    depends_on:
      - es-master

  logstash:
    container_name: logstash
    hostname: logstash
    image: logstash:7.1.1
    command: logstash -f ./conf/logstash-filebeat.conf
    restart: always
    volumes:
      # 映射到容器中
      - /data/elk-master/logstash/conf/logstash-filebeat.conf:/usr/share/logstash/conf/logstash-filebeat.conf
    environment:
      - elasticsearch.hosts=http://本机外网ip:9200
      # 解决logstash监控连接报错
      - xpack.monitoring.elasticsearch.hosts=http://本机外网ip:9200
    ports:
      - 5044:5044
    depends_on:
      - es-master

3)es-master.yml

# 集群名称
cluster.name: es-cluster
# 节点名称
node.name: es-node1
# 是否可以成为master节点
node.master: true
# 是否允许该节点存储数据,默认开启
node.data: true
# 网络绑定
network.host: 0.0.0.0
network.publish_host: 本机外网ip
# 设置对外服务的http端口
http.port: 9200
# 设置节点间交互的tcp端口
transport.port: 9300
# 集群发现
discovery.seed_hosts:
  - 本机外网ip
# 手动指定可以成为 mater 的所有节点的 name 或者 ip,这些配置将会在第一次选举中进行计算
cluster.initial_master_nodes:
  - es-node1
# 支持跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*"
# 安全认证
xpack.security.enabled: false
#http.cors.allow-headers: "Authorization"

4)filebeat.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
    # 当前目录下的所有.log文件
    - /data/logs/*.log
  multiline.pattern: ^\[
  multiline.negate: true
  multiline.match: after

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

setup.template.settings:
  index.number_of_shards: 1

setup.dashboards.enabled: false

setup.kibana:
  host: "http://本机外网ip:5601"

# 不直接传输至ES
#output.elasticsearch:
# hosts: ["http://es-master:9200"]
# index: "filebeat-%{[beat.version]}-%{+yyyy.MM.dd}"

output.logstash:
  hosts: ["本机外网ip:5044"]

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

5)kibana.yml

# 服务端口
server.port: 5601
# 服务IP
server.host: "0.0.0.0"
# ES
elasticsearch.hosts: ["http://本机外网ip:9200"]
# 汉化
i18n.locale: "zh-CN"

6)logstash.yml

# X-Pack Monitoring
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.hosts: ["http://本机外网ip:9200"]

# X-Pack Management
xpack.management.enabled: false

7)logstash-filebeat.conf

input {
    # 来源beats
    beats {
        # 端口
        port => "5044"
    }
}
# 分析、过滤插件,可以多个
filter {
    grok {
        match => { "message" => "%{COMBINEDAPACHELOG}"}
    }
    geoip {
        source => "clientip"
    }
}
output {
    # 选择elasticsearch
    elasticsearch {
        hosts => ["http://本机外网ip:9200"]
        index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    }
}

8)部署脚本init.sh

#! /bin/bash

# 创建目录
if [ ! -d "./elasticsearch/" ]; then
        mkdir -p ./elasticsearch/master/conf ./elasticsearch/master/data ./elasticsearch/master/logs
fi

if [ ! -d "./kibana/" ]; then
        mkdir -p ./kibana/conf ./kibana/logs
fi 

if [ ! -d "./logstash/" ]; then 
        mkdir -p ./logstash/conf ./logstash/logs 
fi

if [ ! -d "./filebeat/" ]; then
        mkdir -p ./filebeat/conf ./filebeat/logs ./filebeat/data
fi

# 目录授权(data/logs 都要授读/写权限)
if [ -d "./elasticsearch/" ]; then
        chmod 777 ./elasticsearch/master/data/ ./elasticsearch/master/logs/
fi

if [ -d "./filebeat/" ]; then
        chmod 777 ./filebeat/data/ ./filebeat/logs/
fi

# 移动配置文件
if [ -f "./es-master.yml" ]; then
        cp ./es-master.yml ./elasticsearch/master/conf
fi

if [ -f "./kibana.yml" ]; then
        cp ./kibana.yml ./kibana/conf
fi

if [ -f "./logstash-filebeat.conf" ]; then 
        cp ./logstash-filebeat.conf ./logstash/conf
fi

if [ -f "./logstash.yml" ]; then 
        cp ./logstash.yml ./logstash/conf
fi


if [ -f "./filebeat.yml" ]; then
        cp ./filebeat.yml ./filebeat/conf
fi

# 部署项目
docker-compose up --build -d

4. 常见问题

常见排查命令:

常见错误:

sysctl -w vm.max_map_count=262144
sysctl -p

5. 结果

es-head
kibana
上一篇下一篇

猜你喜欢

热点阅读