最新版本ELFK8.10部署实战

2023年 10月 15日 19.7k 0

一 背景

在现代软件系统中,日志是非常重要的组成部分。它们记录了系统的运行状态、错误消息、用户活动等信息。然而,当系统规模变大并且涉及多个组件时,日志管理变得非常复杂。传统的日志管理方法可能会面临一些挑战,包括集中化存储、检索和分析日志数据的困难。

ELK是一套流行的开源工具集,用于解决这些日志管理挑战。它由以下三个主要组件组成:
ELFK(Elasticsearch、Logstash、Filebeat、Kibana)是一套用于实时日志分析和监控的解决方案。它由一系列开源工具组成,各自担当不同的角色。Elasticsearch用于存储和索引日志数据,Logstash用于日志的收集、过滤和转发,Filebeat用于轻量级的日志收集,而Kibana用于可视化和分析日志数据。

二 相关概念

  • Elasticsearch:Elasticsearch是一个基于Lucene的分布式搜索和分析引擎。它被广泛用于存储、搜索和分析各种类型的数据,包括结构化、非结构化和地理位置数据。Elasticsearch具有高性能、可伸缩性和强大的搜索功能,以及实时数据索引和聚合的能力。
  • Logstash:Logstash是一个用于日志数据收集、过滤和转发的开源工具。它可以从各种来源(如文件、数据库、消息队列等)收集日志数据,并将其进行处理和转换,然后发送到目标存储或分析系统(如Elasticsearch)中。
  • Kibana:Kibana是一个基于Web的开源工具,用于实时分析和可视化存储在Elasticsearch中的数据。它提供了丰富的图表、图形和仪表板,使用户可以直观地探索和呈现数据,发现趋势、模式和异常情况,并生成自定义的可视化报告。
  • 三 前提条件

  • 硬件要求:ELK堆栈是资源密集型的应用程序,因此确保目标部署环境具备足够的计算资源和存储空间,以满足您的需求。这包括CPU、内存和存储设备。
  • 网络连接:确保目标部署环境具有稳定的网络连接,以便ELK组件之间能够相互通信,并且客户端可以访问Kibana进行日志分析和可视化。
  • 安全设置:根据您的安全要求,配置适当的防火墙规则和访问控制策略,以确保对ELK组件的访问受到限制并保护系统免受未经授权的访问。
  • 日志源配置:在部署ELK之前,确定您希望收集和处理的日志源。这可以是应用程序生成的日志文件、系统日志、网络设备日志等。确保您了解日志源的位置、格式和访问权限。
  • ELK版本选择:确定您希望部署的ELK版本。ELK的组件(Elasticsearch、Logstash和Kibana)有不同的版本和兼容性要求。选择适合您需求和环境的版本,并确保它们之间的兼容性。
  • 配置和定制化:根据您的需求,对ELK组件进行适当的配置和定制化。这可能包括修改Elasticsearch的索引设置、Logstash的数据处理管道和过滤规则,以及Kibana的仪表板和可视化设置。
  • 四 安装部署

    4.2 Linux系统配置

    • 调整进程最大打开文件数数量
    vim /etc/security/limits.conf
    # 直接末尾添加限制
    es soft nofile 65536
    es hard nofile 65536
    es soft nproc 65535
    es hard nproc 65535
    
    • 调整进程最大虚拟内存区域数量
    echo vm.max_map_count=262144>> /etc/sysctl.conf
    echo "fs.file-max = 1000000" >> /etc/sysctl.conf
    sysctl -p
    

    4.3 ELK安装部署

    在此安装ELK版本 8.10

    4.3.1 ElasticSearch安装

    Elasticsearch包含来自JDK维护者(GPLv2+CE)的捆绑版本的OpenJDK。因此可以不用提前自己安装JDK。

    • 下载并安装公共签名密钥:
    rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
    
    

    下载es rpm包并安装

    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.10.0-x86_64.rpm
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.10.0-x86_64.rpm.sha512
    shasum -a 512 -c elasticsearch-8.10.0-x86_64.rpm.sha512 
    sudo rpm --install elasticsearch-8.10.0-x86_64.rpm
    
    
    # 安装完成输出
    Creating elasticsearch group... OK
    Creating elasticsearch user... OK
    --------------------------- Security autoconfiguration information ------------------------------
    
    Authentication and authorization are enabled.
    TLS for the transport and HTTP layers is enabled and configured.
    
    The generated password for the elastic built-in superuser is : 6R-hxNMSxxxxxtxwW
    
    If this node should join an existing cluster, you can reconfigure this with
    '/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token '
    after creating an enrollment token on your existing cluster.
    
    You can complete the following actions at any time:
    
    Reset the password of the elastic built-in superuser with
    '/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.
    
    Generate an enrollment token for Kibana instances with
     '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.
    
    Generate an enrollment token for Elasticsearch nodes with
    '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.
    
    -------------------------------------------------------------------------------------------------
    ### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
     sudo systemctl daemon-reload
     sudo systemctl enable elasticsearch.service
    ### You can start elasticsearch service by executing
     sudo systemctl start elasticsearch.service
    [/usr/lib/tmpfiles.d/elasticsearch.conf:1] Line references path below legacy directory /var/run/, updating /var/run/elasticsearch → /run/elasticsearch; please update the tmpfiles.d/ drop-in file accordingly.
    
    • 启动

    安装Elasticsearch时,默认开启并配置了安全特性。安装Elasticsearch时,会自动进行如下安全配置:
    启用认证授权,并为弹性内置超级用户生成密码。
    TLS的证书和密钥是为传输层和HTTP层生成的,TLS是用这些密钥和证书启用和配置的。
    将密码、证书和密钥输出到您的终端。
    我们建议将弹性密码存储为shell中的环境变量。例子:

    export ELASTIC_PASSWORD="your_password"
    

    加入以有集群

    /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node
    
    /usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token 
    
    
    • 配置ES服务自启动

    配置Elasticsearch在系统启动时自动启动,使用如下命令:

    sudo /bin/systemctl daemon-reload
    sudo /bin/systemctl enable elasticsearch.service
    
    # 但节点ES配置 /etc/elasticsearch/elasticsearch.yml
    
    注释 cluster.initial_master_nodes: ["localhost"]
    
    并添加 discovery.type: single-node
    
    
    sudo systemctl start elasticsearch.service
    
    
    • 重制elastic密码
    [root@VM-48-14-centos ~]# /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
    This tool will reset the password of the [elastic] user to an autogenerated value.
    The password will be printed in the console.
    Please confirm that you would like to continue [y/N]y
    
    
    Password for the [elastic] user successfully reset.
    New value: 6MhAxxxxxxxxxtW
    

    如果您的Elasticsearch密钥存储库有密码保护,则需要使用本地文件和systemd环境变量向systemd提供密钥存储库密码。这个本地文件应该在它存在的时候受到保护,并且可以在Elasticsearch启动并运行后安全地删除。

    echo "keystore_password" > /path/to/my_pwd_file.tmp
    chmod 600 /path/to/my_pwd_file.tmp
    sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=/path/to/my_pwd_file.tmp
    sudo systemctl start elasticsearch.service
    

    查看日志

    sudo journalctl --unit elasticsearch
    
    • 检查服务状态
    [root@VM-48-14-centos ~]# curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic:6MhAuNxxxxxxdtW https://localhost:9200
    {
      "name" : "elk-host",
      "cluster_name" : "elasticsearch",
      "cluster_uuid" : "cLLsiBX1ToirQuBKwyA-1g",
      "version" : {
        "number" : "8.10.0",
        "build_flavor" : "default",
        "build_type" : "rpm",
        "build_hash" : "e338da74c79465dfdc204971e600342b0aa87b6b",
        "build_date" : "2023-09-07T08:16:21.960703010Z",
        "build_snapshot" : false,
        "lucene_version" : "9.7.0",
        "minimum_wire_compatibility_version" : "7.17.0",
        "minimum_index_compatibility_version" : "7.0.0"
      },
      "tagline" : "You Know, for Search"
    }
    
    • 查看ES配置
    [root@VM-48-14-centos ~]# egrep -v "^#|^$" /etc/elasticsearch/elasticsearch.yml
    path.data: /var/lib/elasticsearch
    path.logs: /var/log/elasticsearch
    discovery.type: single-node
    xpack.security.enabled: true
    xpack.security.enrollment.enabled: true
    xpack.security.http.ssl:
      enabled: true
      keystore.path: certs/http.p12
    xpack.security.transport.ssl:
      enabled: true
      verification_mode: certificate
      keystore.path: certs/transport.p12
      truststore.path: certs/transport.p12
    http.host: 0.0.0.0
    

    配置自动索引

    PUT _cluster/settings
    {
        "persistent": {
            "action.auto_create_index": "true" 
        }
    }
    
    
    GET _cluster/settings
    

    4.3.2 Kibana安装

    rpm安装kibana,版本为:8.10

    wget https://artifacts.elastic.co/downloads/kibana/kibana-8.10.2-x86_64.rpm
    wget https://artifacts.elastic.co/downloads/kibana/kibana-8.10.2-x86_64.rpm.sha512
    shasum -a 512 -c kibana-8.10.2-x86_64.rpm.sha512 
    sudo rpm --install kibana-8.10.2-x86_64.rpm
    
    # 设置kibana开机自启动
    sudo /bin/systemctl daemon-reload
    sudo /bin/systemctl enable kibana.service
    
    
    • 为kibana生成token
    [root@elk-host ~]# /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
    eyJ2ZXIiOiI4LjEwLjAiLCJhZHIiOlsiMTcyLjE3LjQ4LjE0OjkyMDAiXSwiZmdyIjoiMTk0MTM0MzQ4NTg5M2Y2Y2Y3xxxxxxxcyYmU4ZTM5NDNmMzg5Mzc0MjA2ZTE4MSIsImtleSI6Ikt5cTlEb3NCR20tYS1uXzNkQ0JEOmlMODEtNWNqU25Da0dkV0o3LXB2VkEifQ==
    
    • 启动kibana
    # 修改kibana 监听0.0.0.0
    server.host: 0.0.0.0
    

    输入token

    配置完成后,通过es的用户名密码登陆

    elastic:6MhAuNxxxxxxxxkdtW
    

    4.3.3 Logstash安装

    Logstash版本为8.10

    使用yum安装

    • 配置yum源并安装
    sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
    
    # 写入es yum仓库源
    cat > /etc/yum.repos.d/logstash.repo  "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
        index => "logstash-%{+YYYY.MM.dd}"
        ssl_certificate_verification => false
        user => "elastic"
        password => "6MxxxxxxxxdtW"
      }
    }
    EOF
    
    • 启动服务
     systemctl start logstash
    

    4.3.3 Filebeat安装

    安装8.10 rpm包安装方式

    • 安装
    curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.10.2-x86_64.rpm
    sudo rpm -vi filebeat-8.10.2-x86_64.rpm
    
    # 开机自启动
    systemctl enable filebeat
    
    • 配置filebeat

    先简单配置本地日志文件,输出到logstash

    filebeat.inputs:
    - type: filestream
    
      # Unique ID among all inputs, an ID is required.
      id: my-filestream-id
    
      # Change to true to enable this input configuration.
      enabled: true
    
      # Paths that should be crawled and fetched. Glob based paths.
      paths:
        - /var/log/*.log
    output.logstash:
      # The Logstash hosts
      hosts: ["localhost:5044"]
    

    查看filebeat日志

    journalctl -fu filebeat
    

    五 测试

    六 注意事项

    • kibana如果报错,可以进入es容器内执行reset kibana_system用户密码:elasticsearch-reset-password -i -u kibana_system

    参考链接

    • www.elastic.co/guide/en/el…
    • www.elastic.co/guide/en/lo…
    • www.elastic.co/guide/en/ki…

    相关文章

    JavaScript2024新功能:Object.groupBy、正则表达式v标志
    PHP trim 函数对多字节字符的使用和限制
    新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
    使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
    为React 19做准备:WordPress 6.6用户指南
    如何删除WordPress中的所有评论

    发布评论