站点图标 Liao's blog

DOCKER安装部署ELK (ELASTICSEARCH+KIBANA+LOGSTASH+FILEBEAT)

ELK是由 Elasticsearch、Logstash和Kibana 三部分组件组成。

Docker 镜像统一从 https://hub.docker.com/ 下载 (安装时请使用统一版本)。保险起见我安装的是7.17.1版本

一、Docker 安装镜像

官网镜像地址:
https://hub.docker.com//elasticsearch
https://hub.docker.com/
/kibana
https://hub.docker.com//logstash
https://hub.docker.com/
/filebeat

# 下载镜像 查看镜像
docker pull elasticsearch:7.17.1
docker pull kibana:7.17.1
docker pull logstash:7.17.1
docker pull store/elastic/filebeat:7.17.1
docker images

  1. Docker 安装启动 Elasticsearch
# 创建自定义的网络(用于连接到连接到同一网络的其他服务(例如Kibana))
docker network create elknetwork

# 运行 elasticsearch
docker run -d --name elasticsearch \
--net elknetwork  -p 9200:9200 -p 9300:9300 \
-e "ES_JAVA_OPTS=-Xms2048m -Xmx2048m" \
-e "discovery.type=single-node" \
elasticsearch:7.17.1

# 查看容器状态
docker ps

访问:http://IP:9200 Elasticsearch 安装启动完成

  1. Docker 安装启动 Kibana
# 汉化kibana
mkdir -p /data/dockerfile/kibana/config
cd /data/dockerfile/kibana/config
vim kibana.yml

    #
    # ** THIS IS AN AUTO-GENERATED FILE **
    #
    # Default Kibana configuration for docker target
    server.host: "0.0.0.0"
    server.shutdownTimeout: "5s"
    # 注意你的本地IP
    elasticsearch.hosts: [ "http://yourip:9200" ] 
    monitoring.ui.container.elasticsearch.enabled: true
    i18n.locale: "zh-CN"

# 启动kibana

docker run -d --name kibana --net elknetwork -p 5601:5601 \
-v /data/dockerfile/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml \
kibana:7.17.1

  1. Docker 安装启动 logstash
mkdir -p /data/dockerfile/logstash/conf.d
cd /data/dockerfile/logstash
vim logstash.yml
    http.host: "0.0.0.0"
    #配置elasticsearch集群地址
    xpack.monitoring.elasticsearch.hosts: [ "http://yourip:9200" ]
    ##允许监控
    xpack.monitoring.enabled: true
    ##目录为挂载目录,启动命令指定的
    path.config: /usr/share/logstash/conf.d/my.conf

vim conf.d/my.conf
    input {
       beats {
         port => 5044
         client_inactivity_timeout => 36000
      }
    }
    output {
      elasticsearch {
        hosts => ["http://yourip:9200"]
        index  => "index-%{+YYYY.MM}"
      }
    }

# 启动logstash
docker run -it -d -p 5044:5044 --name logstash  \
-v /data/dockerfile/logstash/logstash.yml:/usr/share/logstash/config/logstash.yml \
-v /data/dockerfile/logstash/conf.d/:/usr/share/logstash/conf.d/ \
logstash:7.17.1

  1. Docker 安装启动 filebeat
mkdir -p /data/dockerfile/filebeat
vim /data/dockerfile/filebeat/filebeat.yml

    # 定义应用的input类型、以及存放的具体路径
    filebeat.inputs:
      #nginx
    - type: log
      enabled: true
      paths:
        - /var/log/nginx/*.log
      tags: ["nginx"]
      fields:
        index: nginx
      #systemlogs
    - type: log
      enabled: true
      paths:
        - /var/log/messages
      tags: ["messages"]
      fields:
        index: messages

    #============================= Filebeat modules ===============================
    filebeat.config.modules:
      # Glob pattern for configuration loading
      path: ${path.config}/modules.d/*.yml
      # Set to true to enable config reloading
      reload.enabled: true

    # ============================== logstash =====================================  
    output.logstash:
      hosts: ["yourip:5044"] #localhost为logstash安装的服务器ip
      enabled: true
    #============================== Kibana =====================================
    setup.kibana:
      host: "yourip:5601"

    #============================== elasticsearch =====================================
    #output.elasticsearch:
    #  hosts: ["yourip:9200"]
    #  enabled: true

# 启动 filebeat

docker run -d  -u root --name filebeat --net elknetwork \
-v /usr/local/nginx/logs:/var/log/nginx:rw \
-v /var/log/messages:/var/log/messages:rw \
-v /data/dockerfile/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro \
store/elastic/filebeat:7.17.1

二、创建索引,查看ELK抓取日志

  1. 进入Stack management

  1. 选择 Kibana->索引模式->创建索引
    可以看到右侧已经自动获取到我在logstash配置文件下简建立的index索引:/usr/share/logstash/conf.d/my.conf

  1. 选择Discovre

可以看到监控的/var/log/messages以及/usr/local/nginx/logs/日志文件都显示出来了。

效果1

效果2

退出移动版