• Elastic Stack 环境配置与框架简介


    目录

    简介

    什么是Elastic Stack

    Elasticasearch

    Logstash

    Kibana

    Beats

    框架图

    下载

    配置

    一、安装java环境

    启动

    Elasticsearch

    Kibana

    FileBeat

    Logstash

    测验


    简介

    什么是Elastic Stack

    Elastic Stack缩写为elk,它由三个软件组成:ElasticsearchLogstashKibana,因此缩写为elk,随着版本的变化,添加了一个新软件Beats,因此其全名现在为ELKB

    Elasticasearch

    Elasticasearch是一个基于java的开源分布式搜索引擎。其特点如下:

    1. 分布式
    2. 无需配置
    3. 自动发现
    4. 索引复制机制
    5. Restful风格界面
    6. 多个数据源
    7. 自动搜索加载

    Elasticasearch为所有类型的数据提供近乎实时的搜索和分析。无论您拥有结构化或非结构化文本、数字数据或地理空间数据,Elasticsearch都可以以支持快速搜索的方式高效地存储和索引。

    Logstash

    Logstash是一个轻量级的日志收集系统。它以管道的形式工作。它可以过滤从管道输入的数据,并在输出时将结果传输到任何服务器

    Kibana

    KibanaElasticsearch的可视化服务。它可以直观地显示Elasticsearch生成的数据,并支持多样化的分析和演示

    Beats

    BeatsElastic开发的客户端程序,用于存储在监控服务器中。它主要用于数据收集,可以将收集的数据发送到ElasticsearchLogstash

    框架图

     Beats(FileBeats)

    Beats->Logstash->Elasticsearch->Kibana

    下载

    本文中使用的beats功能:filebeat

    Name

    Link

    1Elasticasearchdownload
    2

    Logstash

    download
    3

    Kibana

    download
    4

    beats

    download

    配置

    一、安装java环境

    • 安装JDK
    sudo apt-get install openjdk-8-jdk
    • 检查Java
    1. $ java -version
    2. openjdk version "1.8.0_352"
    3. OpenJDK Runtime Environment (build 1.8.0_352-8u352-ga-1~22.04-b08)
    4. OpenJDK 64-Bit Server VM (build 25.352-b08, mixed mode)
    • 关闭防火墙
    sudo ufw disable

    启动

    Elasticsearch

    打开配置文件

    vim config/elasticsearch.yml

    打开以下两个字段并设置网络。主机设置为0.0.0.0,以便Internet可以访问此计算机

    1. # ---------------------------------- Network -----------------------------------
    2. #
    3. # By default Elasticsearch is only accessible on localhost. Set a different
    4. # address here to expose this node on the network:
    5. #
    6. network.host: 0.0.0.0
    7. #
    8. # By default Elasticsearch listens for HTTP traffic on the first free port it
    9. # finds starting at 9200. Set a specific HTTP port here:
    10. #
    11. http.port: 9200

    关闭GeoIp采集

    ingest.geoip.downloader.enabled: false

    关闭SSH身份验证。你可以检查自己的情况。如果您在没有证书的情况下通过HTTPS访问浏览器,将被阻止

    1. xpack.security.enabled: false
    2. xpack.security.http.ssl:
    3. enabled: false
    4. keystore.path: certs/http.p12

    添加跨域访问

    1. http.cors.enabled: true
    2. http.cors.allow-origin: "*"

    修改jvm虚拟机的映射内存以提高文件处理速度

    vim config/jvm.options

    修改如下:

    如果使用Java 19,可以添加--enable-preview参数来改进MMapDirectory

    1. ################################################################
    2. ## IMPORTANT: JVM heap size
    3. ################################################################
    4. ##
    5. ## The heap size is automatically configured by Elasticsearch
    6. ## based on the available memory in your system and the roles
    7. ## each node is configured to fulfill. If specifying heap is
    8. ## required, it should be done through a file in jvm.options.d,
    9. ## which should be named with .options suffix, and the min and
    10. ## max should be set to the same value. For example, to set the
    11. ## heap to 4 GB, create a new file in the jvm.options.d
    12. ## directory containing these lines:
    13. ##
    14. -Xms4g
    15. -Xmx4g
    16. ##
    17. ## See https://www.elastic.co/guide/en/elasticsearch/reference/8.5/heap-size.html
    18. ## for more information
    19. ##
    20. ################################################################

    修改虚拟MAP大小

    sudo vim /etc/sysctl.conf

    将此内容添加到文件并保存

    vm.max_map_count=262144

    重置sysctl

    sysctl -p

    启动

    ./bin/elasticsearch

    在浏览器中打开以下地址,并将IP更改为服务器的IP(非环回地址)

    http://x.x.x.x:9200/

    如果成功,浏览器将返回以下输出:

    1. {
    2. "name" : "xxxx",
    3. "cluster_name" : "elasticsearch",
    4. "cluster_uuid" : "mkUeEpucQEWlPVQpJXwOjw",
    5. "version" : {
    6. "number" : "8.5.2",
    7. "build_flavor" : "default",
    8. "build_type" : "tar",
    9. "build_hash" : "a846182fa16b4ebfcc89aa3c11a11fd5adf3de04",
    10. "build_date" : "2022-11-17T18:56:17.538630285Z",
    11. "build_snapshot" : false,
    12. "lucene_version" : "9.4.1",
    13. "minimum_wire_compatibility_version" : "7.17.0",
    14. "minimum_index_compatibility_version" : "7.0.0"
    15. },
    16. "tagline" : "You Know, for Search"
    17. }

    Kibana

    配置端口和地址。使用0.0.0.0作为通用地址

    vim config/kibana.yaml

    添加以下内容:

    1. # =================== System: Kibana Server ===================
    2. # Kibana is served by a back end server. This setting specifies the port to use.
    3. server.port: 5601
    4. # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
    5. # The default is 'localhost', which usually means remote machines will not be able to connect.
    6. # To allow connections from remote users, set this parameter to a non-loopback address.
    7. server.host: "0.0.0.0"

    启动

    ./bin/kinaba

    通过浏览器访问以下地址:

    http://x.x.x.x:5601/

    如果启动正常,你可以看到以下页面:

     

    FileBeat

    首先,创建测试日志文件并输入测试内容:

    1. touch test.log
    2. echo test message > test.log

    打开filebeat。yml并修改以下内容
    测验log是刚刚创建的文件,你可以根据你的要求更改:

    1. # filestream is an input for collecting log messages from files.
    2. - type: filestream
    3. # Unique ID among all inputs, an ID is required.
    4. id: my-filestream-id
    5. # Change to true to enable this input configuration.
    6. enabled: true
    7. # Paths that should be crawled and fetched. Glob based paths.
    8. paths:
    9. - /home/logserver_bzm/test.log

    屏蔽elastic search输出

    1. #output.elasticsearch:
    2. # Array of hosts to connect to.
    3. # hosts: ["localhost:9200"]

    打开logstash

    1. output.logstash:
    2. # The Logstash hosts
    3. hosts: ["localhost:5044"]

    启动

    ./filebeat -e -c filebeat.yml -d "publish"

    Logstash

    首先创建一个配置文件来配置logstash

    vim ./config/logstash.conf

    写下以下内容
    "logstash的简单配置。输入部分用于配置filebeat,过滤部分用于设置过滤屏幕以过滤filebeat的输入,输出部分将消息发送到弹性搜索"

    1. input {
    2. beats {
    3. port => 5044
    4. codec => "json"
    5. }
    6. }
    7. filter {
    8. grok {
    9. match => { "message" => "(?(.*))" }
    10. }
    11. }
    12. output {
    13. stdout { codec => rubydebug }
    14. elasticsearch {
    15. hosts => ["localhost:9200"]
    16. index => "test1-nginx-access-%{+YYYY.MM.dd}"
    17. }
    18. }

    启动

    ./bin/logstash -f ./config/logstash.conf

    测验

    完成所有配置并启动四项服务后,在浏览器中输入Kibana的地址
    在打开的页面中选择菜单图标

    下拉并找到“Dev Tools”

    运行控制台代码。如果一切正常,你可以看到以下输出:

    可以从输出中看到你在filebeat里配置的日志文件里的内容

    1. {
    2. "took": 58,
    3. "timed_out": false,
    4. "_shards": {
    5. "total": 1,
    6. "successful": 1,
    7. "skipped": 0,
    8. "failed": 0
    9. },
    10. "hits": {
    11. "total": {
    12. "value": 1,
    13. "relation": "eq"
    14. },
    15. "max_score": 1,
    16. "hits": [
    17. {
    18. "_index": "test1-nginx-access-2022.11.25",
    19. "_id": "P93orIQBWUNz5yg551ec",
    20. "_score": 1,
    21. "_source": {
    22. "@version": "1",
    23. "input": {
    24. "type": "filestream"
    25. },
    26. "@timestamp": "2022-11-25T03:49:18.038Z",
    27. "log": {
    28. "file": {
    29. "path": "/home/logserver_bzm/test.log"
    30. },
    31. "offset": 0
    32. },
    33. "text": "test message",
    34. "tags": [
    35. "_jsonparsefailure",
    36. "beats_input_codec_json_applied"
    37. ],
    38. "message": "test message",
    39. "ecs": {
    40. "version": "8.0.0"
    41. }
    42. }
    43. }
    44. ]
    45. }
    46. }

    很明显,输出中的消息部分与日志中的内容一致

    你还可以看到logstash的输出

     

  • 相关阅读:
    带你深度解析虚幻引擎4的照明和阴影知识
    一些ECharts配置
    笔记:在Entity Framework Core中使用乐观并发控制来处理数据更新的冲突
    农产品直销平台/农场品销售系统
    双重循环及break,continue,return的运用
    讲述CBO And Database Statistics
    机器学习之机器如何学习
    秋招每日一题------剑指offer最大连续子数组
    微服务分布式开源架构是什么?
    大模型Tuning分类
  • 原文地址:https://blog.csdn.net/bjbz_cxy/article/details/128036014