1. 程式人生 > 其它 >ELK部署文件--elasticsearch

ELK部署文件--elasticsearch

1 背景

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

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

filebeat ==> logstash ==> elasticsearch ==> kibana

  • 資源準備

    下載部署包地址: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

本文來自部落格園,作者:liu_kx,轉載請註明原文連結:https://www.cnblogs.com/liukx/p/15716358.html