ELK部署文檔--elasticsearch


1 背景

ELK是三個開源軟件的縮寫,分別表示:Elasticsearch , Logstash, Kibana , 它們都是開源軟件。新增了一個FileBeat,它是一個輕量級的日志收集處理工具(Agent),Filebeat占用資源少,適合於在各個服務器上搜集日志后傳輸給Logstash。

這是一個最簡單的架構圖:

filebeat ==> logstash ==> elasticsearch ==> kibana
本文主要講述的是elasticsearch,filebeat可參考ELK部署文檔--filebeat,logstash 可參考ELK部署文檔--logstash

  • 資源准備

    下載部署包地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch

    注:elk全家桶各個應用版本需一致

  • java版本

    java版本要求jdk1.8+,查看版本:
    java -version

  • 創建用戶

    elk需要用非root用戶部署,因此需要創建一個賬號用於部署
    useradd elk

    修改目錄的權限,給elk賦權
    chown elk:elk /data/software/elk -R

2 elasticsearch

Elasticsearch是一個開源的分布式、RESTful 風格的搜索和數據分析引擎

elasticsearch默認端口為9200

2.1 下載安裝

  • 解壓安裝包到指定目錄/data/software/elk
    tar -vxf elasticsearch-7.15.2-linux-x86_64.tar.gz -C /data/software/elk

2.2 配置文件

  • 修改conf下elasticsearch.yml配置文件

    • 設置ip地址,任意網絡均可訪問

      取消注釋:network.host: 0.0.0.0

    • 修改節點名稱,每個節點必須都是唯一

      node.name: appops-ykm-es1

    • 是否為集群主節點

      node.master: true

    • 配置集群成員

      discovery.zen.ping.unicast.hosts: ["173.16.11.45","173.16.11.18"]

  • 修改conf下jvm.options配置文件

    • 修改啟動配置要求

      調整Xms和Xmx為1g:-Xms1g -Xmx1g

  • 修改系統配置/etc/sysctl.conf

    • 添加:vm.max_map_count = 655360

      修改完后重啟使配置生效:sysctl -p

2.3 服務部署啟用

在bin目錄下啟動elasticsearch(需在非root用戶下啟動)
./elasticsearch -d
-d為后台啟動

檢查服務:
ps -ef | grep elasticsearch

啟用成功后可以訪問 http://localhost:9200 查看信息,出現如下信息則啟用成功

2.4 設置CA認證

  • 停止應用
    ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}' | xargs kill -9

  • 在elasticsearch目錄下生成證書
    ./bin/elasticsearch-certutil ca
    ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

    ./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
    ./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

  • 把bin下生成的兩個文件復制到config目錄下,並修改權限
    cp ./bin/elastic-certificates.p12 ./config
    cp ./bin/elastic-stack-ca.p12 ./config

    chmod 600 ./config/elastic-certificates.p12
    chmod 600 ./config/elastic-stack-ca.p12

  • 同步ca證書到elasticsearch集群其他節點並變更配置(集群)

    vim ./config/elasticsearch.yml

    xpack.security.enabled: true
    xpack.security.transport.ssl.enabled: true
    xpack.security.transport.ssl.verification_mode: certificate
    xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
    xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
    
  • 啟動elasticsearch集群所有節點

    ./bin/elasticsearch -d

  • 密碼初始化

    bin/elasticsearch-setup-passwords interactive

    future versions of Elasticsearch will require Java 11; your Java version from [/data/sdk/jdk1.8.0_201/jre] does not meet this requirement
    Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
    You will be prompted to enter passwords as the process progresses.
    Please confirm that you would like to continue [y/N]y # 輸入y
    
    Enter password for [elastic]: # 輸入設置密碼
    Reenter password for [elastic]: # 再次輸入重新設置密碼
    # 其它不設置項目直接按Enter跳過
    
  • 驗證密碼是否設置成功

    curl -XGET -u elastic 'localhost:9200/_xpack/security/user?pretty'

    輸入密碼后可以正常返回數據則配置成功,配置后賬號為elastic

2.5 常用命令

  • 查看集群狀態
    curl -XGET --user elastic:password http://localhost:9200/_cluster/health?pretty

  • 查看node整體信息
    curl -XGET --user elastic:password localhost:9200/_cat/nodes?v

  • 集群node詳細信息
    curl -XGET --user elastic:password localhost:9200/_nodes/process?pretty

  • 空間檢查

    curl --user elastic:password http://localhost:9200/_cat/allocation?v

  • 查看索引

    curl -XGET --user elastic:password "http://127.0.0.1:9200/_cat/indices?v&pretty"

2.6 索引管理

可以通過設置定時任務定時刪除已過期索引

  • 編寫shell腳本
    vim del_index.sh

    #!/bin/bash
    
    deletetime=$(date -d "30 days ago" +%Y.%m.%d) # 刪除30天以前的日志
    for i in `curl -XGET --user elastic:password "http://127.0.0.1:9200/_cat/indices?v&pretty" | awk '{print $3}' | grep $deletetime`
    do
        echo $deletetime
        curl -XDELETE --user elastic:password "http://127.0.0.1:9200/$i?pretty"
    done
    
  • Linux添加定時任務
    crontab -e

    10 9 */1 * * sh /data/software/elk/elasticsearch/del_index.sh
    # 每天9:10定時執行刪除
    
  • 查看定時任務
    crontab -l

2.7 報錯處理

2.7.1 bootstrp checks failed

報錯信息:

解決方案:
修改elasticsearch.yml
vim ./config/elasticsearch.yml
取消注釋:cluster.initial_master_nodes: ["node-1", "node-2"]

2.7.2 GeoIp

報錯信息:

[ERROR][o.e.i.g.GeoIpDownloader  ] [node_elastic] exception during geoip databases update
java.net.UnknownHostException: geoip.elastic.co
        at sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567) ~[?:?]

解決方案:
此版本將GeoIp功能默認開啟了采集。在默認的啟動下是會去官網的默認地址下獲取最新的Ip的GEO信息
在elasticsearch.yml中添加配置 ingest.geoip.downloader.enabled: false

2.7.3 fatal error

報錯信息:

fatal error in thread [elasticsearch[node_elastic]]

解決方案:
內存設置過小,需修改config目錄下的jvm.options,把參數調大

-Xms2g
-Xmx2g
2.7.4 uncaught exception

報錯信息:

uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException

解決方案:
不能用root用戶啟動,切換成其它非root用戶

su elk


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM