Ubuntu18安裝ELK
前期准備
Elasticsearch和Logstash需要運行在Java8環境下,所以要先在機器上安裝好jdk1.8。
本人博客里提供了centos環境下的jdk安裝教程,僅供參考:https://www.cnblogs.com/helios-fz/p/12623038.html 。
注意:目前ES暫不支持Java 9。
安裝和配置Elasticsearch
Ubuntu的默認包存儲庫中不提供Elastic Stack組件。 但是,在添加Elastic的包源列表后,它們可以與APT一起安裝。
所有Elastic Stack的軟件包都使用Elasticsearch簽名密鑰進行簽名,以保護您的系統免受軟件包欺騙。 使用密鑰進行身份驗證的軟件包將被包管理器視為信任。
首先,運行以下命令將Elasticsearch公共GPG密鑰導入APT:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
接下來,將Elastic源列表添加到sources.list.d
目錄,其中APT將查找新的源:
echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
接下來,更新您的包列表,以便APT讀取新的Elastic源:
sudo apt update
然后使用以下命令安裝Elasticsearch:
sudo apt install elasticsearch
默認安裝路徑為:/usr/share/elasticsearch
默認配置文件路徑為:/etc/elasticsearch
完成Elasticsearch安裝后,我們需要修改/etc/elasticsearch下的主配置文件elasticsearch.yml
。注意: Elasticsearch的配置文件采用YAML格式,這意味着縮進非常重要! 編輯此文件時,請確保不添加任何額外的空格。
配置文件說明如下:
#服務名[集群名] cluster.name: my-application #節點名 node.name: node-1 #設置此節點具備成為主節點的資格 node.master: true #為節點添加自定義屬性 node.attr.rack: r1 #數據文件存放位置 官方建議自定義 path.data: /var/lib/elasticsearch #日志文件存放路徑 官方建議自定義 path.logs: /var/log/elasticsearch #啟動時鎖定內存,默認為true。 #因為當jvm開始swapping時es的效率 會降低,所以要保證它不swap,可以把ES_MIN_MEM和ES_MAX_MEM兩個環境變量設置成同一個值,並且保證機器有足夠的內存分配給es。 #同時也要允許elasticsearch的進程可以鎖住內存,linux下可以通過ulimit -l unlimited命令來實現。 bootstrap.memory_lock: false #禁止swapping交換 bootstrap.system_call_filter: false #為es實例綁定特定的IP地址 network.host: localhost #es實例設置特定的端口,默認為9200端口 http.port: 9200 #es集群間通信的tcp端口 transport.tcp.port: 8081 #跨域設置 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-credentials: true
啟動elasticsearch:
# 切換到命令文件夾 cd /usr/share/elasticsearch/bin/ # 后台啟動es ./elasticsearch -d
現在Elasticsearch已經啟動並運行,讓我們安裝Kibana,它是Elastic Stack的下一個組件。
Q&A
Elasticsearch啟動報錯:-Pack is not supported and Machine Learning is not available
在elasticsearch.yml中加入配置項:
xpack.ml.enabled: false
xpack.ml.enabled設置為false禁用X-Pack機器學習功能
安裝和配置Kibana
根據官方文檔 ,Kibana應該在Elasticsearch之后安裝。 按此順序安裝可確保每個產品所依賴的組件正確到位。
因為在上一步中已經添加了Elastic包源,所以您可以使用apt安裝Elastic Stack的其余組件:
sudo apt install kibana
默認安裝路徑為:/usr/share/kibana
默認配置文件路徑為:/etc/kibana
修改/etc/kibana下的主配置文件kibana.ym
l:
# 默認端口是5601,如需要改端口在此修改 server.port: 5601 # es的地址和端口 elasticsearch.hosts: ["http://localhost:9200"] # 本機地址,訪問此地址+端口顯示kibana界面 server.host: "localhost" # 漢化kibana i18n.locale: "zh-CN"
后台啟動Kibana:
# 切換到命令文件夾 cd /usr/share/kibana/bin/ # 啟動 ./kibana &
Q&A
配置外部服務器訪問kibana
1.查看kibana端口號是否對外暴露
2.修改kibaba.yml文件
server.host: "localhost" 修改為 server.host: "本機ip"
安裝和配置Logstash
雖然Beats可以將數據直接發送到Elasticsearch數據庫,但還是建議使用Logstash來處理數據。 這樣可以從不同的源收集數據,將其轉換為通用格式,並將其導出到另一個數據庫。
使用以下命令安裝Logstash:
sudo apt install logstash
默認安裝路徑為:/usr/share/logstash
默認配置文件路徑為:/etc/logstash
Logstash可以視為一個管道,從一端接收數據,以某種方式處理它,然后將其發送到目的地(在這里是指Elasticsearch)。 Logstash管道有兩個必需元素, input
和output
,以及一個可選元素filter
。 輸入插件使用來自源的數據,過濾器插件處理數據,輸出插件將數據寫入目標。
創建一個名為02-beats-input.conf
的配置文件,在其中設置Filebeat輸入:
# 進入目錄 cd /etc/logstash/conf.d/ # 創建文件 touch 02-beats-input.conf
插入以下input
配置, 這將在TCP端口5044
上監聽beats
輸入:
input { beats { port => 5044 } }
保存並關閉文件。 接下來,在當前目錄下創建一個名為10-syslog-filter.conf
的配置文件,在其中添加系統日志過濾器,也稱為syslogs :
# 創建文件 touch 10-syslog-filter.conf
插入以下syslog過濾器配置。 此示例系統日志配置取自官方Elastic文檔 。 此過濾器用於解析傳入的系統日志,使其可以被預定義的Kibana儀表板構建和使用:
filter { if [fileset][module] == "system" { if [fileset][name] == "auth" { grok { match => { "message" => ["%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:\[%{POSINT:[system][auth][pid]}\])?: %{DATA:[system][auth][ssh][event]} %{DATA:[system][auth][ssh][method]} for (invalid user )?%{DATA:[system][auth][user]} from %{IPORHOST:[system][auth][ssh][ip]} port %{NUMBER:[system][auth][ssh][port]} ssh2(: %{GREEDYDATA:[system][auth][ssh][signature]})?", "%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:\[%{POSINT:[system][auth][pid]}\])?: %{DATA:[system][auth][ssh][event]} user %{DATA:[system][auth][user]} from %{IPORHOST:[system][auth][ssh][ip]}", "%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:\[%{POSINT:[system][auth][pid]}\])?: Did not receive identification string from %{IPORHOST:[system][auth][ssh][dropped_ip]}", "%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sudo(?:\[%{POSINT:[system][auth][pid]}\])?: \s*%{DATA:[system][auth][user]} :( %{DATA:[system][auth][sudo][error]} ;)? TTY=%{DATA:[system][auth][sudo][tty]} ; PWD=%{DATA:[system][auth][sudo][pwd]} ; USER=%{DATA:[system][auth][sudo][user]} ; COMMAND=%{GREEDYDATA:[system][auth][sudo][command]}", "%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} groupadd(?:\[%{POSINT:[system][auth][pid]}\])?: new group: name=%{DATA:system.auth.groupadd.name}, GID=%{NUMBER:system.auth.groupadd.gid}", "%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} useradd(?:\[%{POSINT:[system][auth][pid]}\])?: new user: name=%{DATA:[system][auth][user][add][name]}, UID=%{NUMBER:[system][auth][user][add][uid]}, GID=%{NUMBER:[system][auth][user][add][gid]}, home=%{DATA:[system][auth][user][add][home]}, shell=%{DATA:[system][auth][user][add][shell]}$", "%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} %{DATA:[system][auth][program]}(?:\[%{POSINT:[system][auth][pid]}\])?: %{GREEDYMULTILINE:[system][auth][message]}"] } pattern_definitions => { "GREEDYMULTILINE"=> "(.|\n)*" } remove_field => "message" } date { match => [ "[system][auth][timestamp]", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] } geoip { source => "[system][auth][ssh][ip]" target => "[system][auth][ssh][geoip]" } } else if [fileset][name] == "syslog" { grok { match => { "message" => ["%{SYSLOGTIMESTAMP:[system][syslog][timestamp]} %{SYSLOGHOST:[system][syslog][hostname]} %{DATA:[system][syslog][program]}(?:\[%{POSINT:[system][syslog][pid]}\])?: %{GREEDYMULTILINE:[system][syslog][message]}"] } pattern_definitions => { "GREEDYMULTILINE" => "(.|\n)*" } remove_field => "message" } date { match => [ "[system][syslog][timestamp]", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] } } } }
完成后保存並關閉文件。
最后,創建一個名為30-elasticsearch-output.conf
的配置文件:
# 創建文件 touch 30-elasticsearch-output.conf
插入以下output
配置。 本質上,此輸出將Logstash配置為將Beats數據存儲在Elasticsearch中,該數據在localhost:9200
運行,位於以Beat使用的名稱命名的索引中。 這里使用的Beat是Filebeat:
output { elasticsearch { hosts => ["localhost:9200"] manage_template => false index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}" } }
保存並關閉文件。
如果要為使用Filebeat輸入的其他應用程序添加過濾器,請確保將文件命名為在輸入和輸出配置之間的數字,在這里就應該是02到
之間的兩位數 。30
使用以下命令測試Logstash配置:
sudo -u logstash /usr/share/logstash/bin/logstash --path.settings /etc/logstash -t
如果沒有語法錯誤,幾秒鍾后輸出將顯示Configruation OK
。 如果在輸出中沒有看到此信息,請檢查輸出中出現的任何錯誤並更新配置以更正它們。
如果配置測試成功,請啟動並啟用Logstash以使配置更改生效:
sudo -u logstash /usr/share/logstash/bin/logstash --path.settings /etc/logstash
sudo systemctl enable logstash
啟動成功后,可以通過 http://127.0.0.1:9600/ 來查看logstash運行狀態。
注意:直接使用 ./logstash & 會報錯,錯誤是 Failed to read pipelines yaml file. Location: /usr/share/logstash/config/pipelines.yml,所以啟動的時候要加上 --path.setting 參數,指定配置文件地址。
安裝和配置Filebeat
Elastic Stack使用幾個名為Beats的輕量級數據發送器來收集各種來源的數據,並將它們傳輸到Logstash或Elasticsearch。 以下是目前Elastic提供的Beats:
- Filebeat :收集並發送日志文件。
- Metricbeat :從您的系統和服務中收集指標。
- Packetbeat :收集和分析網絡數據。
- Winlogbeat :收集Windows事件日志。
- Auditbeat :收集Linux審計框架數據並監視文件完整性。
- 心跳 :通過主動探測監控服務的可用性。
這里我們使用Filebeat將本地日志轉發到我們的Elastic Stack。
使用apt
安裝Filebeat:
sudo apt install filebeat
默認安裝路徑為:/usr/share/filebeat
默認配置文件路徑為:/etc/filebeat
接下來,配置Filebeat以連接到Logstash。打開Filebeat配置文件filebeat.yml。
與Elasticsearch一樣,Filebeat的配置文件采用YAML格式。 這意味着正確的縮進至關重要,因此請務必使用這些說明中指示的相同數量的空格。
Filebeat支持多種輸出,但通常只將事件直接發送到Elasticsearch或Logstash以進行其他處理。 在這里將使用Logstash對Filebeat收集的數據執行其他處理。 Filebeat不需要直接向Elasticsearch發送任何數據,所以在這里找到文件的output.elasticsearch
部分,在行首加 # 注釋掉相關配置
:
#output.elasticsearch: # Array of hosts to connect to. #hosts: ["localhost:9200"]
然后配置output.logstash
部分。 刪除#
以激活logstash配置
。 以下配置將Filebeat連接到
5044端口上
的Logstash:
output.logstash: # The Logstash hosts hosts: ["localhost:5044"]
保存並關閉文件。
Filebeat的功能可以使用Filebeat模塊進行擴展。 在這里我們將使用系統模塊,該模塊收集和解析由常見Linux發行版的系統日志記錄服務創建的日志。
輸入以下命令啟用:
sudo filebeat modules enable system
通過運行以下命令查看已啟用和已禁用模塊的列表:
sudo filebeat modules list
列表內容大致如下:
Enabled:
system
Disabled:
apache2
auditd
elasticsearch
icinga
iis
kafka
kibana
logstash
mongodb
mysql
nginx
osquery
postgresql
redis
traefik
默認情況下,Filebeat使用syslog和授權日志的默認路徑。 在這里我們無需更改配置中的任何內容。模塊參數可以在/etc/filebeat/modules.d/system.yml
配置文件中查看。
接下來,將索引模板加載到Elasticsearch中。 Elasticsearch索引是具有類似特征的文檔集合。 索引用名稱標識,用於在其中執行各種操作時引用索引。 創建新索引時,將自動應用索引模板。
使用以下命令加載模板:
sudo filebeat setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
成功之后輸出:
Loaded index template
Filebeat附帶了示例Kibana儀表板,可以在Kibana中可視化Filebeat數據。 在使用儀表板之前,需要創建索引模式並將儀表板加載到Kibana中。
在儀表板加載時,Filebeat連接到Elasticsearch以檢查版本信息。 要在啟用Logstash時加載儀表板,需要禁用Logstash輸出並啟用Elasticsearch輸出:
sudo filebeat setup -e -E output.logstash.enabled=false -E output.elasticsearch.hosts=['localhost:9200'] -E setup.kibana.host=localhost:5601
輸出大致如下:
2020-07-08T16:24:52.197+0800 INFO instance/beat.go:611 Home path: [/usr/share/filebeat] Config path: [/etc/filebeat] Data path: [/var/lib/filebeat] Logs path: [/var/log/filebeat] 2020-07-08T16:24:52.197+0800 INFO instance/beat.go:618 Beat UUID: 6dd3cc49-28df-4fbf-99f4-8d78c43fda6c 2020-07-08T16:24:52.197+0800 INFO [beat] instance/beat.go:931 Beat info {"system_info": {"beat": {"path": {"config": "/etc/filebeat", "data": "/var/lib/filebeat", "home": "/usr/share/filebeat", "logs": "/var/log/filebeat"}, "type": "filebeat", "uuid": "6dd3cc49-28df-4fbf-99f4-8d78c43fda6c"}}} 2020-07-08T16:24:52.197+0800 INFO [beat] instance/beat.go:940 Build info {"system_info": {"build": {"commit": "4e10965a54359738b64b9ea4b141831affbb8242", "libbeat": "6.8.10", "time": "2020-05-28T13:47:20.000Z", "version": "6.8.10"}}} 2020-07-08T16:24:52.197+0800 INFO [beat] instance/beat.go:943 Go runtime info {"system_info": {"go": {"os":"linux","arch":"amd64","max_procs":8,"version":"go1.10.8"}}} 2020-07-08T16:24:52.198+0800 INFO [beat] instance/beat.go:947 Host info {"system_info": {"host": {"architecture":"x86_64","boot_time":"2020-07-07T09:20:33+08:00","containerized":false,"name":"fanzhen","ip":["127.0.0.1/8","::1/128","192.168.1.128/24","fe80::4ab2:967c:4254:4ffe/64"],"kernel_version":"5.3.0-28-generic","mac":["60:f2:62:57:89:8d"],"os":{"family":"debian","platform":"ubuntu","name":"Ubuntu","version":"18.04.4 LTS (Bionic Beaver)","major":18,"minor":4,"patch":4,"codename":"bionic"},"timezone":"CST","timezone_offset_sec":28800,"id":"8f613c4d695c4642890b0e636e121222"}}} 2020-07-08T16:24:52.198+0800 INFO [beat] instance/beat.go:976 Process info {"system_info": {"process": {"capabilities": {"inheritable":null,"permitted":["chown","dac_override","dac_read_search","fowner","fsetid","kill","setgid","setuid","setpcap","linux_immutable","net_bind_service","net_broadcast","net_admin","net_raw","ipc_lock","ipc_owner","sys_module","sys_rawio","sys_chroot","sys_ptrace","sys_pacct","sys_admin","sys_boot","sys_nice","sys_resource","sys_time","sys_tty_config","mknod","lease","audit_write","audit_control","setfcap","mac_override","mac_admin","syslog","wake_alarm","block_suspend","audit_read"],"effective":["chown","dac_override","dac_read_search","fowner","fsetid","kill","setgid","setuid","setpcap","linux_immutable","net_bind_service","net_broadcast","net_admin","net_raw","ipc_lock","ipc_owner","sys_module","sys_rawio","sys_chroot","sys_ptrace","sys_pacct","sys_admin","sys_boot","sys_nice","sys_resource","sys_time","sys_tty_config","mknod","lease","audit_write","audit_control","setfcap","mac_override","mac_admin","syslog","wake_alarm","block_suspend","audit_read"],"bounding":["chown","dac_override","dac_read_search","fowner","fsetid","kill","setgid","setuid","setpcap","linux_immutable","net_bind_service","net_broadcast","net_admin","net_raw","ipc_lock","ipc_owner","sys_module","sys_rawio","sys_chroot","sys_ptrace","sys_pacct","sys_admin","sys_boot","sys_nice","sys_resource","sys_time","sys_tty_config","mknod","lease","audit_write","audit_control","setfcap","mac_override","mac_admin","syslog","wake_alarm","block_suspend","audit_read"],"ambient":null}, "cwd": "/root", "exe": "/usr/share/filebeat/bin/filebeat", "name": "filebeat", "pid": 24600, "ppid": 24599, "seccomp": {"mode":"disabled","no_new_privs":false}, "start_time": "2020-07-08T16:24:51.210+0800"}}} 2020-07-08T16:24:52.198+0800 INFO instance/beat.go:280 Setup Beat: filebeat; Version: 6.8.10 2020-07-08T16:24:52.198+0800 INFO elasticsearch/client.go:164 Elasticsearch url: http://localhost:9200 2020-07-08T16:24:52.198+0800 INFO [publisher] pipeline/module.go:110 Beat name: fanzhen 2020-07-08T16:24:52.199+0800 INFO elasticsearch/client.go:164 Elasticsearch url: http://localhost:9200 2020-07-08T16:24:52.200+0800 INFO elasticsearch/client.go:739 Attempting to connect to Elasticsearch version 6.8.10 2020-07-08T16:24:52.269+0800 INFO template/load.go:128 Template already exists and will not be overwritten. 2020-07-08T16:24:52.269+0800 INFO instance/beat.go:889 Template successfully loaded. Loaded index template Loading dashboards (Kibana must be running and reachable) 2020-07-08T16:24:52.269+0800 INFO elasticsearch/client.go:164 Elasticsearch url: http://localhost:9200 2020-07-08T16:24:52.270+0800 INFO elasticsearch/client.go:739 Attempting to connect to Elasticsearch version 6.8.10 2020-07-08T16:24:52.333+0800 INFO kibana/client.go:118 Kibana url: http://localhost:5601 2020-07-08T16:24:55.199+0800 INFO add_cloud_metadata/add_cloud_metadata.go:340 add_cloud_metadata: hosting provider type not detected. 2020-07-08T16:25:25.165+0800 INFO instance/beat.go:736 Kibana dashboards successfully loaded. Loaded dashboards 2020-07-08T16:25:25.166+0800 INFO elasticsearch/client.go:164 Elasticsearch url: http://localhost:9200 2020-07-08T16:25:25.168+0800 INFO elasticsearch/client.go:739 Attempting to connect to Elasticsearch version 6.8.10 2020-07-08T16:25:25.249+0800 INFO kibana/client.go:118 Kibana url: http://localhost:5601 2020-07-08T16:25:25.321+0800 WARN fileset/modules.go:388 X-Pack Machine Learning is not enabled 2020-07-08T16:25:25.384+0800 WARN fileset/modules.go:388 X-Pack Machine Learning is not enabled Loaded machine learning job configurations
之后,啟動並啟用Filebeat:
sudo systemctl start filebeat sudo systemctl enable filebeat
如果已經正確設置了Elastic Stack,Filebeat將開始把系統日志和授權日志發送到Logstash,Logstash會將該數據加載到Elasticsearch中。
要驗證Elasticsearch是否確實正在接收此數據,請使用以下命令查詢Filebeat索引:
curl -XGET 'http://localhost:9200/filebeat-*/_search?pretty'
輸出大致如下:
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 12, "successful" : 12, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 11408, "max_score" : 1.0, "hits" : [ { "_index" : "filebeat-6.8.10-2020.07.05", "_type" : "doc", "_id" : "ZumJLXMBSsiTvi4Uh_J1", "_score" : 1.0, "_source" : { "@timestamp" : "2020-07-05T05:53:50.000Z", "system" : { "auth" : { "program" : "systemd-logind", "message" : "New seat seat0.", "timestamp" : "Jul 5 13:53:50", "pid" : "638", "hostname" : "fanzhen" } }, "@version" : "1", "source" : "/var/log/auth.log", "log" : { "file" : { "path" : "/var/log/auth.log" } }, "offset" : 0, "event" : { "dataset" : "system.auth" }, "prospector" : { "type" : "log" }, "host" : { "id" : "8f613c4d695c4642890b0e636e121222", "name" : "fanzhen", "containerized" : false, "os" : { "name" : "Ubuntu", "platform" : "ubuntu", "family" : "debian", "codename" : "bionic", "version" : "18.04.4 LTS (Bionic Beaver)" }, "architecture" : "x86_64" }, "beat" : { "name" : "fanzhen", "version" : "6.8.10", "hostname" : "fanzhen" }, "tags" : [ "beats_input_codec_plain_applied", "_geoip_lookup_failure" ], "fileset" : { "module" : "system", "name" : "auth" }, "input" : { "type" : "log" } } }, ... ] } }
如果輸出顯示總命中數為0,則Elasticsearch不會在搜索的索引下加載任何日志。這時候需要檢查設置是否有錯誤。