当前位置:   article > 正文

Docker elacticsearch+filebeat+logstash+kibana docker-compose一键启动 日志管理系统 本地安装 详细版_docker elasticsearch + logstash + filebeat + kiban

docker elasticsearch + logstash + filebeat + kibanaa

思路 (使用离线本地搭建)

1.安装docker以及docker-compose(自行安装 版本不低即可)

 安装docker:

(4条消息) Centos7安装Docker_玩物丧志的快乐的博客-CSDN博客_centos7 docker

安装docker-compose:

(4条消息) 安装docker-compose的两种方式_沙漠之鹰的博客-CSDN博客_如何安装docker-compose

2.创建网络并查看(开始elkf安装搭建)

修改系统参数:

进入目录   /etc/sysctl.conf 加入

vm.max_map_count=655360  

进入目录   /etc/security/limits.conf 加入

*               soft     nofile        65535
*               hard     nofile        65535
*               soft     nproc         65535
*               hard     nproc         65535
*               soft     memlock       unlimited
*               hard     memlock       unlimited

创建网络并查看

docker network create elk
docker network ls







3.编写一个docker-compose.yml文件进行服务定义和运行

创建一个主目录存放安装文件在进入该目录

mkdir /root/elkf

cd elkf

 vi /root/elkf/docker-compose.yml

version: "3"                                            #docker-compose版本

services:                                                #需要定义运行的服务
  nginx:
    restart: always
    image: nginx                                    
    container_name: nginx
    hostname: nginx
    ports:                                                #映射端口主机到容器
     - 80:80
    volumes:                                          #卷挂载路径主机到容器
     - /var/log/nginx:/var/log/nginx

  filebeat:
    restart: always
    depends_on:
     - "nginx"
    build:
      context: ./filebeat
      dockerfile: Dockerfile
    container_name: filebeat
    hostname: filebeat
    volumes:
     - /var/log/nginx:/var/log/nginx

  elasticsearch:
    restart: always
    depends_on:
     - "nginx"
    build:
      context: ./elasticsearch
      dockerfile: Dockerfile
    container_name: elasticsearch
    hostname: elasticsearch
    ports:
     - 9200:9200
     - 9300:9300
    volumes:
     - /var/log/elasticsearch:/var/log/elasticsearch

  logstash:
    restart: always
    depends_on:
     - "nginx"
    build:
      context: ./logstash
      dockerfile: Dockerfile
    container_name: logstash
    hostname: logstash
    ports:
     - 5044:5044
    volumes:
     - /opt/logstash/conf:/opt/logstash/conf

  kibana:
    restart: always
    depends_on:
     - "nginx"
    build:
      context: ./kibana
      dockerfile: Dockerfile
    container_name: kibana
    hostname: kibana
    ports:
     - 5601:5601
    

networks:                                             #定义添加的网络
  default:
    external:
      name: elk
 

这里边 就定义了需要运行的elkf的四个服务 

4.构建镜像

软件包下载地址  

http://www.haojiang.online/other/download.tar.gz   (ps:这里我已经打包好了全部4个安装包)

1、构建elacticsearch镜像

cd /root/elkf/

mkdir elasticsearch

cd elasticsearch

 创建Dockerfile文件并写入信息

vi Dockerfile

FROM centos:7.9.2009
MAINTAINER wzlu
RUN yum -y install java-1.8.0-openjdk vim telnet lsof
ADD elasticsearch-6.1.0.tar.gz /usr/local/
RUN cd /usr/local/elasticsearch-6.1.0/config
RUN mkdir -p /data/behavior/log-node1
RUN mkdir /var/log/elasticsearch
COPY elasticsearch.yml /usr/local/elasticsearch-6.1.0/config/
RUN useradd es && chown -R es:es /usr/local/elasticsearch-6.1.0
RUN chmod +x /usr/local/elasticsearch-6.1.0/bin/*
RUN chown -R es:es /var/log/elasticsearch/
RUN chown -R es:es /data/behavior/log-node1
EXPOSE 9200
EXPOSE 9300
CMD su es /usr/local/elasticsearch-6.1.0/bin/elasticsearch

 创建一个elasticsearch.yml文件 写入 如下信息

  vi elasticsearch.yml

  1. # ======================== Elasticsearch Configuration =========================
  2. #
  3. # NOTE: Elasticsearch comes with reasonable defaults for most settings.
  4. # Before you set out to tweak and tune the configuration, make sure you
  5. # understand what are you trying to accomplish and the consequences.
  6. #
  7. # The primary way of configuring a node is via this file. This template lists
  8. # the most important settings you may want to configure for a production cluster.
  9. #
  10. # Please consult the documentation for further information on configuration options:
  11. # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
  12. #
  13. # ---------------------------------- Cluster -----------------------------------
  14. #
  15. # Use a descriptive name for your cluster:
  16. #
  17. cluster.name: my-elk
  18. #
  19. # ------------------------------------ Node ------------------------------------
  20. #
  21. # Use a descriptive name for the node:
  22. #
  23. node.name: node-1
  24. #
  25. # Add custom attributes to the node:
  26. #
  27. #node.attr.rack: r1
  28. #
  29. # ----------------------------------- Paths ------------------------------------
  30. #
  31. # Path to directory where to store the data (separate multiple locations by comma):
  32. #
  33. #path.data: /path/to/data
  34. #
  35. # Path to log files:
  36. #
  37. #path.logs: /path/to/logs
  38. #
  39. # ----------------------------------- Memory -----------------------------------
  40. #
  41. # Lock the memory on startup:
  42. #
  43. #bootstrap.memory_lock: true
  44. #
  45. # Make sure that the heap size is set to about half the memory available
  46. # on the system and that the owner of the process is allowed to use this
  47. # limit.
  48. #
  49. # Elasticsearch performs poorly when the system is swapping the memory.
  50. #
  51. # ---------------------------------- Network -----------------------------------
  52. #
  53. # Set the bind address to a specific IP (IPv4 or IPv6):
  54. #
  55. network.host: 0.0.0.0
  56. #
  57. # Set a custom port for HTTP:
  58. #
  59. http.port: 9200
  60. #
  61. # For more information, consult the network module documentation.
  62. #
  63. # --------------------------------- Discovery ----------------------------------
  64. #
  65. # Pass an initial list of hosts to perform discovery when new node is started:
  66. # The default list of hosts is ["127.0.0.1", "[::1]"]
  67. #
  68. #discovery.zen.ping.unicast.hosts: ["host1", "host2"]
  69. #
  70. # Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
  71. #
  72. #discovery.zen.minimum_master_nodes:
  73. #
  74. # For more information, consult the zen discovery module documentation.
  75. #
  76. # ---------------------------------- Gateway -----------------------------------
  77. #
  78. # Block initial recovery after a full cluster restart until N nodes are started:
  79. #
  80. #gateway.recover_after_nodes: 3
  81. #
  82. # For more information, consult the gateway module documentation.
  83. #
  84. # ---------------------------------- Various -----------------------------------
  85. #
  86. # Require explicit names when deleting indices:
  87. #
  88. #action.destructive_requires_name: true

再导入elacticsearch的安装包

elactissearch下目录结构

2、构建logstash镜像

cd /root/elkf/

mkdir logstash

cd logstash

 写入logstash配置文件

 mkdir -p /opt/logstash/conf

vim /opt/logstash/conf/logstash-nginx-log.conf

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.
input {
  beats { port => 5044 }
}
 
filter {
    date { match => [ "timestamp","yyyy-MM-dd HH:mm:ss,SSS" ] }
    if "nginx-access" in [tags] {
    grok {
        match => {
            "message" => '%{IP:remote_addr} - (%{WORD:remote_user}|-)

"%{WORD:method} %{NOTSPACE:request} HTTP/%{NUMBER}" %{NUMBER:status} %{NUMBER:body_bytes_sent} %{QS} %{QS:http_user_agent}'
        }
          }
     urldecode {
            all_fields => true
        }
    date {
            match => [ "time_local" , "dd/MMM/YYYY:HH:mm:ss Z" ]
          }
    }
}
 
output {
  if "nginx-access" in [tags] {
    elasticsearch {
      hosts => [ "elasticsearch:9200" ]
      manage_template => false
      index => "nginx-access-%{+YYYY.MM.dd}"
    }
  }
}
 

创建Dockerfile文件并写入信息

vi Dockerfile 

FROM centos:7.9.2009
MAINTAINER wzlu
RUN yum -y install java-1.8.0-openjdk vim telnet lsof
ADD logstash-6.1.0.tar.gz /usr/local/
RUN cd /usr/local/logstash-6.1.0
ADD run.sh /run.sh
RUN chmod 755 /*.sh
EXPOSE 5044
CMD ["/run.sh"]

 编写执行的脚本文件

#!/bin/bash
/usr/local/logstash-6.1.0/bin/logstash -f /opt/logstash/conf/logstash-nginx-log.conf

注意:一定要与刚才存放的logstash配置文件路径一致

再导入logstash的安装包 

logstash下目录结构 

3、构建kibana镜像

cd /root/elkf/

mkdir kibana

cd kibana

创建Dockerfile文件并写入信息

vi Dockerfile

FROM centos:7.9.2009
MAINTAINER wzlu
RUN yum -y install java-1.8.0-openjdk vim telnet lsof
ADD kibana-6.1.0-linux-x86_64.tar.gz /usr/local/
RUN cd /usr/local/kibana-6.1.0-linux-x86_64
COPY kibana.yml /usr/local/kibana-6.1.0-linux-x86_64/config/
EXPOSE 5601
CMD ["/usr/local/kibana-6.1.0-linux-x86_64/bin/kibana"]

创建kibana.yml文件 在写入一下信息

   vi  kibana.yml     (复制即可)

  1. # Kibana is served by a back end server. This setting specifies the port to use.
  2. server.port: 5601
  3. # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
  4. # The default is 'localhost', which usually means remote machines will not be able to connect.
  5. # To allow connections from remote users, set this parameter to a non-loopback address.
  6. server.host: "0.0.0.0"
  7. # Enables you to specify a path to mount Kibana at if you are running behind a proxy. This only affects
  8. # the URLs generated by Kibana, your proxy is expected to remove the basePath value before forwarding requests
  9. # to Kibana. This setting cannot end in a slash.
  10. #server.basePath: ""
  11. # The maximum payload size in bytes for incoming server requests.
  12. #server.maxPayloadBytes: 1048576
  13. # The Kibana server's name. This is used for display purposes.
  14. server.name: "kibana"
  15. # The URL of the Elasticsearch instance to use for all your queries.
  16. elasticsearch.url: "http://elasticsearch:9200"
  17. # When this setting's value is true Kibana uses the hostname specified in the server.host
  18. # setting. When the value of this setting is false, Kibana uses the hostname of the host
  19. # that connects to this Kibana instance.
  20. #elasticsearch.preserveHost: true
  21. # Kibana uses an index in Elasticsearch to store saved searches, visualizations and
  22. # dashboards. Kibana creates a new index if the index doesn't already exist.
  23. #kibana.index: ".kibana"
  24. # The default application to load.
  25. #kibana.defaultAppId: "home"
  26. # If your Elasticsearch is protected with basic authentication, these settings provide
  27. # the username and password that the Kibana server uses to perform maintenance on the Kibana
  28. # index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
  29. # is proxied through the Kibana server.
  30. #elasticsearch.username: "user"
  31. #elasticsearch.password: "pass"
  32. # Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively.
  33. # These settings enable SSL for outgoing requests from the Kibana server to the browser.
  34. #server.ssl.enabled: false
  35. #server.ssl.certificate: /path/to/your/server.crt
  36. #server.ssl.key: /path/to/your/server.key
  37. # Optional settings that provide the paths to the PEM-format SSL certificate and key files.
  38. # These files validate that your Elasticsearch backend uses the same key files.
  39. #elasticsearch.ssl.certificate: /path/to/your/client.crt
  40. #elasticsearch.ssl.key: /path/to/your/client.key
  41. # Optional setting that enables you to specify a path to the PEM file for the certificate
  42. # authority for your Elasticsearch instance.
  43. #elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]
  44. # To disregard the validity of SSL certificates, change this setting's value to 'none'.
  45. #elasticsearch.ssl.verificationMode: full
  46. # Time in milliseconds to wait for Elasticsearch to respond to pings. Defaults to the value of
  47. # the elasticsearch.requestTimeout setting.
  48. #elasticsearch.pingTimeout: 1500
  49. # Time in milliseconds to wait for responses from the back end or Elasticsearch. This value
  50. # must be a positive integer.
  51. #elasticsearch.requestTimeout: 30000
  52. # List of Kibana client-side headers to send to Elasticsearch. To send *no* client-side
  53. # headers, set this value to [] (an empty list).
  54. #elasticsearch.requestHeadersWhitelist: [ authorization ]
  55. # Header names and values that are sent to Elasticsearch. Any custom headers cannot be overwritten
  56. # by client-side headers, regardless of the elasticsearch.requestHeadersWhitelist configuration.
  57. #elasticsearch.customHeaders: {}
  58. # Time in milliseconds for Elasticsearch to wait for responses from shards. Set to 0 to disable.
  59. #elasticsearch.shardTimeout: 0
  60. # Time in milliseconds to wait for Elasticsearch at Kibana startup before retrying.
  61. #elasticsearch.startupTimeout: 5000
  62. # Specifies the path where Kibana creates the process ID file.
  63. #pid.file: /var/run/kibana.pid
  64. # Enables you specify a file where Kibana stores log output.
  65. #logging.dest: stdout
  66. # Set the value of this setting to true to suppress all logging output.
  67. #logging.silent: false
  68. # Set the value of this setting to true to suppress all logging output other than error messages.
  69. #logging.quiet: false
  70. # Set the value of this setting to true to log all events, including system usage information
  71. # and all requests.
  72. #logging.verbose: false
  73. # Set the interval in milliseconds to sample system and process performance
  74. # metrics. Minimum is 100ms. Defaults to 5000.
  75. #ops.interval: 5000
  76. # The default locale. This locale can be used in certain circumstances to substitute any missing
  77. # translations.
  78. #i18n.defaultLocale: "en"

再导入kibana的安装包

kibana下目录结构

4、构建filebeat镜像

cd /root/elkf/

mkdir filebeat

cd filebeat

 创建Dockerfile文件并写入信息

vi Dockerfile

FROM centos:7.9.2009
MAINTAINER wzlu
RUN yum -y install java-1.8.0-openjdk vim telnet lsof
ADD filebeat-6.1.0-linux-x86_64.tar.gz /usr/local/
RUN cd /usr/local/filebeat-6.1.0-linux-x86_64
COPY filebeat.yml /usr/local/filebeat-6.1.0-linux-x86_64
ADD run.sh /run.sh
RUN chmod 755 /*.sh
CMD ["/run.sh"]

 编写执行的脚本文件

#!/bin/bash
/usr/local/filebeat-6.1.0-linux-x86_64/filebeat -e -c /usr/local/filebeat-6.1.0-linux-x86_64/filebeat.yml

 创建filebeat.yml文件  写入如下信息

   vi filebeat.yml  

  1. ###################### Filebeat Configuration Example #########################
  2. # This file is an example configuration file highlighting only the most common
  3. # options. The filebeat.reference.yml file from the same directory contains all the
  4. # supported options with more comments. You can use it as a reference.
  5. #
  6. # You can find the full configuration reference here:
  7. # https://www.elastic.co/guide/en/beats/filebeat/index.html
  8. # For more available modules and options, please see the filebeat.reference.yml sample
  9. # configuration file.
  10. #=========================== Filebeat prospectors =============================
  11. filebeat.prospectors:
  12. # Each - is a prospector. Most options can be set at the prospector level, so
  13. # you can use different prospectors for various configurations.
  14. # Below are the prospector specific configurations.
  15. - type: log
  16. # Change to true to enable this prospector configuration.
  17. enabled: true
  18. # Paths that should be crawled and fetched. Glob based paths.
  19. paths:
  20. - /var/log/nginx/access.log
  21. #- c:\programdata\elasticsearch\logs\*
  22. tags: ["nginx-access"]
  23. clean_*: true
  24. # Exclude lines. A list of regular expressions to match. It drops the lines that are
  25. # matching any regular expression from the list.
  26. #exclude_lines: ['^DBG']
  27. # Include lines. A list of regular expressions to match. It exports the lines that are
  28. # matching any regular expression from the list.
  29. #include_lines: ['^ERR', '^WARN']
  30. # Exclude files. A list of regular expressions to match. Filebeat drops the files that
  31. # are matching any regular expression from the list. By default, no files are dropped.
  32. #exclude_files: ['.gz$']
  33. # Optional additional fields. These fields can be freely picked
  34. # to add additional information to the crawled log files for filtering
  35. #fields:
  36. # level: debug
  37. # review: 1
  38. ### Multiline options
  39. # Mutiline can be used for log messages spanning multiple lines. This is common
  40. # for Java Stack Traces or C-Line Continuation
  41. # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
  42. #multiline.pattern: ^\[
  43. # Defines if the pattern set under pattern should be negated or not. Default is false.
  44. #multiline.negate: false
  45. # Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
  46. # that was (not) matched before or after or as long as a pattern is not matched based on negate.
  47. # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
  48. #multiline.match: after
  49. #============================= Filebeat modules ===============================
  50. filebeat.config.modules:
  51. # Glob pattern for configuration loading
  52. path: ${path.config}/modules.d/*.yml
  53. # Set to true to enable config reloading
  54. reload.enabled: false
  55. # Period on which files under path should be checked for changes
  56. #reload.period: 10s
  57. #==================== Elasticsearch template setting ==========================
  58. setup.template.settings:
  59. index.number_of_shards: 3
  60. #index.codec: best_compression
  61. #_source.enabled: false
  62. #================================ General =====================================
  63. # The name of the shipper that publishes the network data. It can be used to group
  64. # all the transactions sent by a single shipper in the web interface.
  65. #name:
  66. # The tags of the shipper are included in their own field with each
  67. # transaction published.
  68. #tags: ["nginx-access"]
  69. # Optional fields that you can specify to add additional information to the
  70. # output.
  71. #fields:
  72. # env: staging
  73. #============================== Dashboards =====================================
  74. # These settings control loading the sample dashboards to the Kibana index. Loading
  75. # the dashboards is disabled by default and can be enabled either by setting the
  76. # options here, or by using the `-setup` CLI flag or the `setup` command.
  77. #setup.dashboards.enabled: false
  78. # The URL from where to download the dashboards archive. By default this URL
  79. # has a value which is computed based on the Beat name and version. For released
  80. # versions, this URL points to the dashboard archive on the artifacts.elastic.co
  81. # website.
  82. #setup.dashboards.url:
  83. #============================== Kibana =====================================
  84. # Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
  85. # This requires a Kibana endpoint configuration.
  86. setup.kibana:
  87. # Kibana Host
  88. # Scheme and port can be left out and will be set to the default (http and 5601)
  89. # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  90. # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  91. #host: "localhost:5601"
  92. #============================= Elastic Cloud ==================================
  93. # These settings simplify using filebeat with the Elastic Cloud (https://cloud.elastic.co/).
  94. # The cloud.id setting overwrites the `output.elasticsearch.hosts` and
  95. # `setup.kibana.host` options.
  96. # You can find the `cloud.id` in the Elastic Cloud web UI.
  97. #cloud.id:
  98. # The cloud.auth setting overwrites the `output.elasticsearch.username` and
  99. # `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
  100. #cloud.auth:
  101. #================================ Outputs =====================================
  102. # Configure what output to use when sending the data collected by the beat.
  103. #-------------------------- Elasticsearch output ------------------------------
  104. #output.elasticsearch:
  105. # Array of hosts to connect to.
  106. #hosts: ["localhost:9200"]
  107. # Optional protocol and basic auth credentials.
  108. #protocol: "https"
  109. #username: "elastic"
  110. #password: "changeme"
  111. #----------------------------- Logstash output --------------------------------
  112. output.logstash:
  113. # The Logstash hosts
  114. hosts: ["logstash:5044"]
  115. # Optional SSL. By default is off.
  116. # List of root certificates for HTTPS server verifications
  117. #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]
  118. # Certificate for SSL client authentication
  119. #ssl.certificate: "/etc/pki/client/cert.pem"
  120. # Client Certificate Key
  121. #ssl.key: "/etc/pki/client/cert.key"
  122. #================================ Logging =====================================
  123. # Sets log level. The default log level is info.
  124. # Available log levels are: critical, error, warning, info, debug
  125. #logging.level: debug
  126. # At debug level, you can selectively enable logging only for some components.
  127. # To enable all selectors use ["*"]. Examples of other selectors are "beat",
  128. # "publish", "service".
  129. #logging.selectors: ["*"]

 导入filebeat离线安装包

filebeat目录下结构

5.一键部署启动

elkf目录结构

 先拉一个新的nginx镜像

docker pull nginx

然后使用docker-compose命令一键部署           (ps:注意检查容器端口是否被占用)

 docker-compose up -d

查看容器状态

docker-compose ps

获取日志信息

watch -n 2 curl -k 192.168.25.100(本机ip)

6.登录kibana查看日志

登录    http://本机IP地址:5601  查看

登录成功

查看日志

 ​​​

elkf日志系统搭建成功

如果您们发现里边有什么错误和问题可以联系作者欢迎指正,原创,谢谢支持!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/608089
推荐阅读
相关标签
  

闽ICP备14008679号