Elastic Stack入門

Elastic Stack入門

是在尋找彈性棧(“ ELK”)指南,該指南顯示如何設置Elastic Stack並快速啓動和運行嗎?您來對地方了!首先,您安裝核心產品:

然後,您將學習如何實施一個系統監控解決方案,該解決方案使用Metricbeat收集服務器指標並將數據發送到Elasticsearch,在其中您可以使用Kibana搜索和可視化數據。完成基本設置後,添加Logstash進行其他分析。

首先,您可以在單個VM甚至筆記本電腦上安裝Elastic Stack。

實施安全性是設置彈性堆棧的關鍵步驟。要通過示例安裝快速啓動並運行,請立即跳過這些步驟。在通過網絡發送敏感數據之前,請確保您 保護彈性堆棧並啓用 加密的通信

開始之前

安裝Elasticsearch

Elasticsearch是一個實時的分佈式存儲,搜索和分析引擎。它可以用於許多目的,但是它擅長的一種環境是索引半結構化數據流,例如日誌或解碼的網絡數據包。

您可以在自己的硬件上運行Elasticsearch,或 在Elastic Cloud上使用我們 託管的Elasticsearch Service。Elasticsearch Service在AWS和GCP上均可用。 免費試用Elasticsearch Service

要下載並安裝Elasticsearch,請打開終端窗口並使用與您的系統配合使用的命令(Debian / Ubuntu 爲deb,Redhat / Centos / Fedora爲rpm,OS X爲macbrewLinux 爲Linux,Windows 爲win) :

deb:

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-amd64.deb
sudo dpkg -i elasticsearch-7.5.2-amd64.deb
sudo /etc/init.d/elasticsearch start

rpm:

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-x86_64.rpm
sudo rpm -i elasticsearch-7.5.2-x86_64.rpm
sudo service elasticsearch start

mac:

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-darwin-x86_64.tar.gz
tar -xzvf elasticsearch-7.5.2-darwin-x86_64.tar.gz
cd elasticsearch-7.5.2
./bin/elasticsearch

brew:

brew tap elastic/tap
brew install elastic/tap/elasticsearch-full
elasticsearch

linux:

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-linux-x86_64.tar.gz
tar -xzvf elasticsearch-7.5.2-linux-x86_64.tar.gz
cd elasticsearch-7.5.2
./bin/elasticsearch

win:

  1. Elasticsearch下載頁面下載Elasticsearch 7.5.2 Windows zip文件 。
  2. 將zip文件的內容提取到計算機上的目錄中,例如C:\Program Files
  3. 以管理員身份打開命令提示符,然後導航到包含解壓縮文件的目錄,例如:

cd C:\Program Files\elasticsearch-7.5.2

      4. 啓動Elasticsearch:

bin\elasticsearch.bat

對於其他操作系統,請轉到 Elasticsearch下載頁面。

默認的cluster.name和 node.name分別是elasticsearch和您的主機名。如果您打算繼續使用該羣集或添加更多節點,則最好將這些默認值更改爲唯一名稱。有關更改文件中這些設置和其他設置的詳細信息elasticsearch.yml,請參閱“ 配置Elasticsearch”

要了解有關安裝,配置和運行Elasticsearch的更多信息,請閱讀 Elasticsearch Reference


確保Elasticsearch已啓動並正在運行

要測試Elasticsearch守護程序是否已啓動並正在運行,請嘗試在端口9200上發送HTTP GET請求。

curl http://127.0.0.1:9200

在Windows上,如果未安裝cURL,請將瀏覽器指向該URL。

您應該看到類似於以下內容的響應:

{
  "name" : "QtI5dUu",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "DMXhqzzjTGqEtDlkaMOzlA",
  "version" : {
    "number" : "7.5.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "00d8bc1",
    "build_date" : "2018-06-06T16:48:02.249996Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}


安裝Kibana

Kibana是一個旨在與Elasticsearch一起使用的開源分析和可視化平臺。您可以使用Kibana搜索,查看和與Elasticsearch索引中存儲的數據進行交互。您可以輕鬆地執行高級數據分析,並在各種圖表,表格和地圖中可視化數據。

 

如果您在Elastic Cloud上運行我們的託管Elasticsearch Service ,則只需輕按一下即可啓用Kibana

我們建議您將Kibana與Elasticsearch安裝在同一服務器上,但這不是必需的。如果將產品安裝在其他服務器上,則需要在Kibana配置文件中更改Elasticsearch服務器的URL(IP:PORT)kibana.yml,然後再啓動Kibana。

要下載並安裝Kibana,請打開一個終端窗口,然後使用適用於您系統的命令:

deb,rpm或linux:

curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-7.5.2-linux-x86_64.tar.gz
tar xzvf kibana-7.5.2-linux-x86_64.tar.gz
cd kibana-7.5.2-linux-x86_64/
./bin/kibana

mac:

curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-7.5.2-darwin-x86_64.tar.gz
tar xzvf kibana-7.5.2-darwin-x86_64.tar.gz
cd kibana-7.5.2-darwin-x86_64/
./bin/kibana

brew:

brew tap elastic/tap
brew install elastic/tap/kibana-full
kibana

win:

  1. Kibana下載頁面下載Kibana 7.5.2 Windows zip文件 。
  2. 將zip文件的內容提取到計算機上的目錄中,例如C:\Program Files
  3. 以管理員身份打開命令提示符,然後導航到包含解壓縮文件的目錄,例如:

cd C:\Program Files\kibana-7.5.2-windows

     4. 啓動Kibana:

bin\kibana.bat

對於其他操作系統,請轉到 Kibana下載頁面。

要了解有關安裝,配置和運行Kibana的更多信息,請閱讀 Kibana Reference


啓動Kibana Web界面

要啓動Kibana Web界面,將瀏覽器指向端口5601。例如,http://127.0.0.1: 5601 。

安裝Beats

Beats是您作爲代理安裝在服務器上以將運營數據發送到Elasticsearch的開源數據託運人。Beats可以將數據直接發送到Elasticsearch或通過Logstash發送,您可以在其中進一步處理和增強數據。

每個Beat是單獨安裝的產品。在本指南中,您將學習如何在system啓用了收集系統指標的模塊的情況下安裝和運行Metricbeat 。

要了解有關安裝和配置其他Beats的更多信息,請參閱《入門指南》文檔:

Elastic Beats To capture

Auditbeat

Audit data

Filebeat

Log files

Functionbeat

Cloud data

Heartbeat

Availability monitoring

Journalbeat

Systemd journals

Metricbeat

Metrics

Packetbeat

Network traffic

Winlogbeat

Windows event logs

安裝Metricbeat

要下載並安裝Metricbeat,請打開一個終端窗口,然後使用適用於您的系統的命令:

deb:

curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.5.2-amd64.deb
sudo dpkg -i metricbeat-7.5.2-amd64.deb

rpm:

curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.5.2-x86_64.rpm
sudo rpm -vi metricbeat-7.5.2-x86_64.rpm

mac:

curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.5.2-darwin-x86_64.tar.gz
tar xzvf metricbeat-7.5.2-darwin-x86_64.tar.gz

brew:

brew tap elastic/tap
brew install elastic/tap/metricbeat-full

linux:

curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.5.2-linux-x86_64.tar.gz
tar xzvf metricbeat-7.5.2-linux-x86_64.tar.gz

win:

  1. Metricbeat下載頁面下載Metricbeat Windows zip文件 。
  2. 將zip文件的內容提取到中C:\Program Files
  3. metricbeat-7.5.2-windows目錄重命名爲Metricbeat
  4. 以管理員身份打開PowerShell提示符(右鍵單擊PowerShell圖標,然後選擇以管理員身份運行”)。
  5. 在PowerShell提示符下,運行以下命令以將Metricbeat安裝爲Windows服務:

PS > cd 'C:\Program Files\Metricbeat'
PS C:\Program Files\Metricbeat> .\install-service-metricbeat.ps1

如果在系統上禁用了腳本執行,請設置當前會話的執行策略以允許腳本運行。例如:PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-metricbeat.ps1

對於其他操作系統,請轉到 Beats下載頁面。


將系統指標發送到Elasticsearch

Metricbeat提供了預構建的模塊,可用於在約5分鐘內快速實現和部署系統監視解決方案,並帶有示例儀表板和數據可視化。

在本節中,您將學習如何運行該system模塊以從服務器上運行的操作系統和服務中收集指標。系統模塊收集系統級別的度量標準,例如CPU使用率,內存,文件系統,磁盤IO和網絡IO統計信息,以及系統上運行的每個進程的頂級統計信息。

開始之前:確認Elasticsearch和Kibana正在運行,並且Elasticsearch準備好從Metricbeat接收數據。

要設置system模塊並開始收集系統指標:

  1. 從Metricbeat安裝目錄中,啓用system模塊:

    deb和rpm:

    sudo metricbeat modules enable system

    Mac和Linux:

    ./metricbeat modules enable system

    brew:

    metricbeat modules enable system

    win:

    PS C:\Program Files\Metricbeat> .\metricbeat.exe modules enable system

設置初始環境:

deb和rpm:

sudo metricbeat setup -e

Mac和Linux:

./metricbeat setup -e

brew:

metricbeat setup -e

win:

PS C:\Program Files\Metricbeat> metricbeat.exe setup -e

setup命令將加載Kibana儀表板。如果已經設置了儀表板,請忽略此命令。該-e標誌是可選的,並將輸出發送到標準錯誤而不是syslog。

  1. 啓動Metricbeat:

    deb和rpm:

    sudo service metricbeat start

    Mac和Linux:

    ./metricbeat -e

    brew:

    metricbeat -e

    win:

    PS C:\Program Files\Metricbeat> Start-Service metricbeat

Metricbeat運行並開始將系統指標發送到Elasticsearch。


可視化系統指標在Kibana

要可視化系統指標,請打開瀏覽器並導航到Metricbeat系統概述儀表板:http:// localhost:5601 / app / kibana#/ dashboard / Metricbeat-system-overview-ecs

 

如果您在Kibana中看不到數據,請嘗試將日期範圍更改爲更大的範圍。默認情況下,Kibana顯示最後15分鐘。如果看到錯誤,請確保Metricbeat正在運行,然後刷新頁面。

單擊“ 主機概述”以查看有關所選主機的詳細指標。

現在您已經知道如何爲Elastic Stack設置最簡單的架構-一個或多個Beats將事件數據直接發送到在同一服務器上運行的Elasticsearch實例-讓我們添加Logstash。

安裝Logstash(可選)編輯

Logstash是一個功能強大的工具,可與各種部署集成。它提供了大量的插件,可幫助您解析,豐富,轉換和緩衝來自各種來源的數據。如果您的數據需要Beats中沒有的其他處理,則需要將Logstash添加到您的部署中。

要下載並安裝Logstash,請打開一個終端窗口並使用與您的系統配合使用的命令:

deb:

curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.5.2.deb
sudo dpkg -i logstash-7.5.2.deb

rpm:

curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.5.2.rpm
sudo rpm -i logstash-7.5.2.rpm

mac and linux:

curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.5.2.tar.gz
tar -xzvf logstash-7.5.2.tar.gz

brew:

brew tap elastic/tap
brew install elastic/tap/logstash-full

win:

  1. Logstash下載頁面下載Logstash 7.5.2 Windows zip文件 。
  2. 將zip文件的內容提取到計算機上的目錄中,例如C:\Program Files。使用短路徑(少於30個字符),以避免遇到Windows上文件路徑長度的限制。

對於其他操作系統,請轉到 Logstash下載頁面。

要了解有關安裝,配置和運行Logstash的更多信息,請閱讀 Logstash參考

配置Logstash的Beats輸入

Logstash提供了用於從多種輸入中讀取的輸入插件。在本指南中,您將創建一個Logstash管道配置,該配置偵聽Beats輸入並將接收到的事件發送到Elasticsearch輸出。

要配置Logstash:

  1. 創建一個名爲的新Logstash管道配置文件demo-metrics-pipeline.conf。如果您將Logstash作爲deb或rpm軟件包安裝,請在Logstash config目錄中創建文件 。該文件必須包含:

    • 一個輸入階段,將Logstash配置爲在端口5044上偵聽傳入的Beats連接。
    • 將事件編入Elasticsearch的輸出階段。輸出階段還配置Logstash以寫入Metricbeat索引。

    例如:

input {
  beats {
    port => 5044
  }
}

# The filter part of this file is commented out to indicate that it
# is optional.
# filter {
#
# }

output {
  elasticsearch {
    hosts => "localhost:9200"
    manage_template => false
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
  }
}

使用此管道配置啓動Logstash時,Beats事件將通過Logstash進行路由,您可以在其中完全訪問Logstash功能以收集,充實和轉換數據。

啓動Logstash編輯

使用適合您的系統的命令。如果將Logstash作爲deb或rpm軟件包安裝,請確保配置文件位於config目錄中。對於其他平臺,config不需要目錄,但這是保持一致的最佳實踐。

deb:

sudo /etc/init.d/logstash start

rpm:

sudo service logstash start

mac and linux:

cd logstash-7.5.2
./bin/logstash -f path/to/config/demo-metrics-pipeline.conf

brew:

logstash -f path/to/config/demo-metrics-pipeline.conf

win:

bin\logstash.bat -f path\to\config\demo-metrics-pipeline.conf

如果收到JVM錯誤消息,請檢查Java版本,如安裝Logstash中所示 。

Logstash開始偵聽Beats輸入中的事件。接下來,您需要配置Metricbeat以將事件發送到Logstash。

配置Metricbeat以將事件發送到Logstash

Metricbeat默認將事件發送到Elasticsearch。要將事件發送到Logstash,請修改Metricbeat配置文件metricbeat.yml。您可以在Metricbeat安裝目錄下找到該文件,/etc/metricbeat用於rpm和deb或 /usr/local/etc/metricbeatbrew。

output.elasticsearch通過註釋掉它來禁用該部分,然後output.logstash通過取消註釋來啓用該部分:

#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
  #hosts: ["localhost:9200"]
.
.
.
#----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["localhost:5044"]

保存文件,然後重新啓動Metricbeat以應用配置更改。

Logstash從Beats輸入讀取,並將事件編入Elasticsearch。您尚未定義過濾器部分,因此Logstash只是將事件轉發到Elasticsearch,而無需其他處理。接下來,您將學習如何定義過濾器階段。

定義過濾器以從字段提取數據

Metricbeat收集的系統指標包括一個名爲的字段cmdline ,其中包含用於啓動系統進程的完整命令行參數。例如:

"cmdline": "/Applications/Firefox.app/Contents/MacOS/plugin-container.app/Contents/MacOS/plugin-container -childID 3
-isForBrowser -boolPrefs 36:1|299:0| -stringPrefs 285:38;{b77ae304-9f53-a248-8bd4-a243dbf2cab1}| -schedulerPrefs
0001,2 -greomni /Applications/Firefox.app/Contents/Resources/omni.ja -appomni
/Applications/Firefox.app/Contents/Resources/browser/omni.ja -appdir
/Applications/Firefox.app/Contents/Resources/browser -profile
/Users/dedemorton/Library/Application Support/Firefox/Profiles/mftvzeod.default-1468353066634
99468 gecko-crash-server-pipe.99468 org.mozilla.machname.1911848630 tab"

您可能只想發送命令路徑,而不是將整個命令行參數發送給Elasticsearch。一種方法是使用Grok過濾器。學習Grok不在本入門指南的範圍內,但是如果您想了解更多信息,請參閱 Grok過濾器插件文檔。

要提取路徑,請在您之前創建的Logstash配置文件的輸入和輸出部分之間添加以下Grok過濾器:

filter {
  if [system][process] {
    if [system][process][cmdline] {
      grok {
        match => { 
          "[system][process][cmdline]" => "^%{PATH:[system][process][cmdline_path]}"
        }
        remove_field => "[system][process][cmdline]" 
      }
    }
  }
}
  1. 使用模式匹配路徑,然後將路徑存儲在名爲的字段中 cmdline_path
  2. 刪除原始字段,cmdline因此不會在Elasticsearch中建立索引。

完成後,完整的配置文件應如下所示:

input {
  beats {
    port => 5044
  }
}

filter {
  if [system][process] {
    if [system][process][cmdline] {
      grok {
        match => {
          "[system][process][cmdline]" => "^%{PATH:[system][process][cmdline_path]}"
        }
        remove_field => "[system][process][cmdline]"
      }
    }
  }
}

output {
  elasticsearch {
    hosts => "localhost:9200"
    manage_template => false
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
  }
}

重新啓動Logstash以獲取更改。現在,該事件包括一個名爲的字段 cmdline_path,其中包含命令路徑:

"cmdline_path": "/Applications/Firefox.app/Contents/MacOS/plugin-container.app/Contents/MacOS/plugin-container"


下一步是什麼?

恭喜你!您已經成功設置了彈性堆棧。您學習瞭如何將系統指標傳輸到Elasticsearch並可視化Kibana中的數據。您還學習瞭如何使用Logstash過濾Metricbeat收集的事件。

接下來,您將要設置Elastic Stack安全功能並激活您的試用許可證,以便可以解鎖Elastic Stack的全部功能。要了解操作方法,請閱讀:

是否想通過指標監視和集中式日誌分析快速啓動並運行?在Kibana中試用Metrics應用程序和Logs應用程序。有關更多詳細信息,請參閱《度量標準監視指南》 和《日誌監視指南》

稍後,當您準備設置生產環境時,另請參閱《 Elastic Stack安裝和升級指南》

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章