1. 程式人生 > >【ELK】Centos7 安裝 ELK 7.6.2 和 UI 管理介面以及測試例子

【ELK】Centos7 安裝 ELK 7.6.2 和 UI 管理介面以及測試例子

# 1. 初始化環境 ## 1.0 初始化環境官網參考 ## 1.1 架構 | 主機 ip | hostname | 安裝的服務 | | ------ | --------- | -------- | | 192.168.110.245 | node01 | es、kibana | | 192.168.110.246 | node02 | es、logstash、filebeat、auditbeat | | 192.168.110.247 | node03 | es、filebeat、auditbeat | ## 1.2 配置 hosts ```shell cat << EOF >> /etc/hosts 192.168.110.245 node01 192.168.110.246 node02 192.168.110.247 node03 EOF ``` ## 1.3 修改 hostname ```shell hostnamectl set-hostname node01 hostnamectl set-hostname node02 hostnamectl set-hostname node03 ``` ## 1.4 時間同步(聯網狀態) ```shell yum -y install ntpdate /bin/echo "* */1 * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root ``` ## 1.5 關閉 selinux ```shell setenforce 0 #臨時,馬上生效 sed -i 's/enforcing/disabled/' /etc/selinux/config #永久,重啟生效 ``` ## 1.6 關閉防火牆 ```shell iptables -F systemctl stop firewalld systemctl disable firewalld ``` ## 1.7 記憶體解鎖和檔案限制 ```shell #臨時修改,重啟失效 ulimit -n 65535 #永久修改,重啟生效 cat << EOF >>/etc/security/limits.conf elasticsearch soft memlock unlimited elasticsearch hard memlock unlimited * soft nofile 65535 * hard nofile 65535 EOF ``` ## 1.8 關閉 swap 快取 ```shell swapoff -a #臨時 sed -i '/swap/s/^/#/' /etc/fstab #永久 ``` ## 1.9 設定 es 使用者記憶體限制大小 ```shell sysctl -w vm.max_map_count=262144 echo 'vm.max_map_count=262144' >> /etc/sysctl.conf ``` ## 1.10 不能 root 使用者啟動 ## 1.11 java版本 > es 7.*之後的版本不需要安裝環境 java,預設自帶 jdk 環境,但是其他的服務還是需要 jdk 環境的,所以還是要統一安裝,另外 jdk 版本要為 8 或者 11,我這裡預設 8 就好了,直接 yum 安裝 ```shell yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel ``` # 2. 安裝 es 叢集 ## 2.1 官網下載 ``` wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-x86_64.rpm wget https://artifacts.elastic.co/downloads/kibana/kibana-7.6.2-x86_64.rpm wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.6.2-x86_64.rpm wget https://artifacts.elastic.co/downloads/logstash/logstash-7.6.2.rpm wget https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-7.6.2-x86_64.rpm ``` ## 2.2 yum 直接安裝則可 ```shell yum -y install elasticsearch yum -y install kibana yum -y install filebeat yum -y install auditbeat ``` ## 2.3 es 配置修改,3 臺機都要,其中 node.name 配置不同,其他一樣 ``` cd /etc/elasticsearch/ && cp elasticsearch.yml elasticsearch.yml.orig cat << EOF > elasticsearch.yml cluster.name: es-cluster node.name: node01 path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch bootstrap.memory_lock: true network.host: 0.0.0.0 http.port: 9200 discovery.seed_hosts: ["192.168.110.245", "192.168.110.246", "192.168.110.247"] cluster.initial_master_nodes: ["node01"] 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 xpack.security.audit.enabled: true xpack.security.audit.logfile.events.emit_request_body: true EOF #node02: sed -i 's#node\.name\:\ node01#node.name: node02#' /etc/elasticsearch/elasticsearch.yml #node03: sed -i 's#node\.name\:\ node01#node.name: node03#' /etc/elasticsearch/elasticsearch.yml ``` ``` 解釋說明: cluster.name 叢集名字,同一個叢集中使用相同名字,單機就隨意 node.name: es-cluster 節點名字,每臺機都不一樣 bootstrap.memory_lock: true 將程序地址所在 RAM 空間中,以防止 es 記憶體被換出,交換對效能,節點穩定性非常不利,應不惜一切代價避免交換。它可能導致垃圾收集持續數分鐘而不是毫秒,並且可能導致節點響應緩慢甚至斷開與群集的連線。在彈性分散式系統中,讓作業系統殺死該節點更為有效。 1、如果你的機器已經全域性進位制swap,那麼不開啟這個配置也無所謂。詳情檢視 1.8 配置 2、或者配置了/etc/sysctl.conf 的 vm.swappiness = 1 ,所以也不需要配置 true 3、如果以上兩個你都沒做,那你還是乖乖把 true 配置上。我這裡都做了,也不影響。 network.host: 0.0.0.0 需要外網 ip 可以這麼配置,不然配置內網也行 http.port: 9200 埠號,不配置的話預設9200 discovery.seed_hosts: ["192.168.110.245", "192.168.110.246", "192.168.110.247"] 這裡可以不指定,不過避免 es 迴環查詢,直接指定方便 cluster.initial_master_nodes: ["node-1"] 首次啟動 es 叢集指定 master 機器,之後重啟和新增新伺服器都無需更改 xpack.security.enabled: true 啟用 xpack 安全模組 xpack.security.transport.ssl.enabled: true TLS 功能,可對通訊進行加密 ``` ## 2.4 如果是 yum 安裝的方式需要修改es的啟動檔案 {{< link "https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-system-settings.html#systemd" "參考地址" >
}} ```shell cat /usr/lib/systemd/system/elasticsearch.service [Service] LimitMEMLOCK=infinity 修改命令: sed -i '/\[Service\]/a\LimitMEMLOCK=infinity' /usr/lib/systemd/system/elasticsearch.service 修改完之後要reload 一下 sudo systemctl daemon-reload ``` ## 2.5 如果你要設定 es 的記憶體大小,有兩個地方 ``` 1、jvm.options cat /etc/elasticsearch/jvm.options -Xms512m -Xmx512m 2、這裡不做介紹,以上一種為常用方法,如果你設定的 java PATH 有問題,可以在這裡面指定 java 環境。 /etc/sysconfig/elasticsearch ``` ## 2.6 在 node01 配置證書,我這裡設定的空密碼,直接回車便可。 ``` /usr/share/elasticsearch/bin/elasticsearch-certutil ca /usr/share/elasticsearch/bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12 cp /usr/share/elasticsearch/{elastic-stack-ca.p12,elastic-certificates.p12} /etc/elasticsearch/ chmod 640 /etc/elasticsearch/elastic-certificates.p12 #scp /etc/elasticsearch/elastic-certificates.p12 root@node02:/etc/elasticsearch/ #scp /etc/elasticsearch/elastic-certificates.p12 root@node03:/etc/elasticsearch/ ``` ``` 生成CA證書 bin/elasticsearch-certutil ca,將產生新檔案 elastic-stack-ca.p12。該 elasticsearch-certutil 命令還會提示你輸入密碼以保護檔案和金鑰,請保留該檔案的副本並記住其密碼。 為叢集中的每個節點生成證書和私鑰 bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12,將產生新檔案 elastic-certificates.p12。系統還會提示你輸入密碼,你可以輸入證書和金鑰的密碼,也可以按Enter鍵將密碼留空。預設情況下 elasticsearch-certutil 生成沒有主機名資訊的證書,這意味著你可以將證書用於叢集中的每個節點,另外要關閉主機名驗證。 注意:如果你在建立證書時輸入了密碼,那可以通過下面的方法設定。 /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password ``` ## 2.7 node01啟動 es ``` systemctl start elasticsearch systemctl enable elasticsearch systemctl status elasticsearch ``` ## 2.8 node01,配置賬號密碼完成tls通訊 命令: ``` /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive ``` 結果: ``` [root@node01 elasticsearch]# /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive Initiating the setup of passwords for reserved users elastic,apm_system,kibana,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 Enter password for [elastic]: Reenter password for [elastic]: Passwords do not match. Try again. Enter password for [elastic]: Reenter password for [elastic]: Enter password for [apm_system]: Reenter password for [apm_system]: Enter password for [kibana]: Reenter password for [kibana]: Enter password for [logstash_system]: Reenter password for [logstash_system]: Enter password for [beats_system]: Reenter password for [beats_system]: Enter password for [remote_monitoring_user]: Reenter password for [remote_monitoring_user]: Changed password for user [apm_system] Changed password for user [kibana] Changed password for user [logstash_system] Changed password for user [beats_system] Changed password for user [remote_monitoring_user] Changed password for user [elastic] ``` 檢查:檢查 node01是否啟動並且密碼訪問正常 ``` [root@node01 elasticsearch]# curl http://192.168.110.245:9200/_cat/nodes?v -u elastic:123456 ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 192.168.110.245 12 90 3 0.07 0.08 0.08 dilm * node01 ``` >
注意的問題: > 1. 證書許可權沒設定,建立好預設為 600,手動修改為 640 > ``` 啟動報錯相關: org.elasticsearch.ElasticsearchSecurityException: failed to load SSL configuration [xpack.security.transport.ssl] Caused by: org.elasticsearch.ElasticsearchException: failed to initialize SSL TrustManager - not permitted to read truststore file [/etc/elasticsearch/elastic-certificates.p12] Caused by: java.nio.file.AccessDeniedException: /etc/elasticsearch/elastic-certificates.p12 ``` >
2. 許可權檢視及修改 ``` [root@node01 elasticsearch]# ll /etc/elasticsearch/elastic-certificates.p12 -rw------- 1 640 elasticsearch 3451 Apr 16 18:57 elastic-certificates.p12 [root@node01 elasticsearch]# chmod 640 /etc/elasticsearch/elastic-certificates.p12 ``` ## 2.9 將 node01 的證書 scp 到 02 和 03,並啟動 02 和 03的 es > 這裡有三個證書 >> elasticsearch.keystore:這裡存放的是 elastic和 123456 的賬號密碼,給 es 叢集使用,預設讀取 >> elastic-stack-ca.p12:這個是 ca 初始證書,儲存好就行,生成完 cert 之後用處不大 >> elastic-certificates.p12:這個是 cert 驗證證書,es 叢集就是通過這個證書完成 ``` scp /etc/elasticsearch/{elasticsearch.keystore,elastic-stack-ca.p12,elastic-certificates.p12} root@node02:/etc/elasticsearch/ scp /etc/elasticsearch/{elasticsearch.keystore,elastic-stack-ca.p12,elastic-certificates.p12} root@node03:/etc/elasticsearch/ systemctl start elasticsearch systemctl enable elasticsearch systemctl status elasticsearch ``` ## 2.10 基於 api 的檢查,叢集是否搭建成功。 > #檢視叢集健康狀態 ``` curl http://192.168.110.245:9200/_cat/health?v -u elastic:123456 ``` > #檢視叢集節點 ``` curl http://192.168.110.245:9200/_cat/nodes?v -u elastic:123456 ``` > 可以發現 es 叢集 ok 了!我們可以發現 node01 的 master 欄位值為*,沒錯,它就是 master 節點了。 ``` [root@node01 elasticsearch]# curl http://192.168.110.245:9200/_cat/health?v -u elastic:123456 epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1591345666 08:27:46 es-cluster green 3 3 2 1 0 0 0 0 - 100.0% [root@node01 elasticsearch]# curl http://192.168.110.245:9200/_cat/nodes?v -u elastic:123456 ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 192.168.110.245 20 91 0 0.00 0.01 0.05 dilm * node01 192.168.110.246 21 78 63 0.42 0.14 0.08 dilm - node02 192.168.110.247 29 76 13 0.89 0.29 0.13 dilm - node03 ``` # 3. 安裝 es 的 web UI ## 3.0 有哪些 web ui | | | | | ---- | --- | --- | | ElasticHD | 我喜歡用這個,因為臨時使用可以直接./ElasticHD 啟動,方便快捷。 有 sql 轉 json 的小工具,很好用 | https://github.com/360EntSecGroup-Skylar/ElasticHD/ | | elasticsearch-HQ | 要安裝 python3.6 有 es 預設的介面查詢,還有 node 節點的監控資料 | https://github.com/ElasticHQ/elasticsearch-HQ/ | | dejavu | 支援從 json 和 csv 匯入匯出資料 | https://github.com/appbaseio/dejavu/ | | cerebro | | https://github.com/lmenezes/cerebro | | elasticsearch-head | | https://github.com/mobz/elasticsearch-head | | elasticsearch-browser | 不支援 5.0 之後 | https://github.com/OlegKunitsyn/elasticsearch-browser | | elasticsearch-kopf | 支援 2.0之前 | https://github.com/lmenezes/elasticsearch-kopf | ## 3.1 如果帶賬號密碼訪問的話 url ``` http://elastic:[email protected]:9200 ``` ## 3.2 安裝web UI管理工具ElasticHD: > 普通安裝: (1)安裝supervisord ``` yum -y install supervisor echo_supervisord_conf > supervisord.conf systemctl start supervisord systemctl enable supervisord ``` (2)安裝 elasticHD ``` yum -y install xdg-utils cd /usr/local/src/ wget https://github.com/360EntSecGroup-Skylar/ElasticHD/releases/download/1.4/elasticHD_linux_amd64.zip unzip elasticHD_linux_amd64.zip chmod 0777 ElasticHD mv ElasticHD /usr/local/bin/ cat < /etc/supervisord.d/ElasticHD.ini [program:ElasticHD] command=/usr/local/bin/ElasticHD -p 0.0.0.0:9800 autostart=true autorestart=true user=elasticsearch redirect_stderr=true stdout_logfile=/tmp/ElasticHD.log EOF supervisorctl update supervisorctl reload supervisorctl status ``` > docker 安裝: ``` docker run -p 9800:9800 -d --link elasticsearch:demo containerize/elastichd ``` web端訪問結果:http://192.168.110.245:9800/ ## 3.3 安裝 elasticsearch -HQ > 普通安裝: (1)安裝python3.6 ``` yum install python3 python3-pip python3-devel -y ``` (2) 安裝 elasticsearch-HQ ``` cd /usr/local/src/ wget -O elasticsearch-HQ.zip https://github.com/ElasticHQ/elasticsearch-HQ/archive/master.zip unzip elasticsearch-HQ.zip mv elasticsearch-HQ-master /usr/local/elasticsearch-HQ cd /usr/local/elasticsearch-HQ pip3 install -r requirements.txt cat < /etc/supervisord.d/ElasticHQ.ini [program:ElasticHQ] command=/usr/bin/python3 /usr/local/elasticsearch-HQ/application.py --host 0.0.0.0 --port 5000 autostart=true autorestart=true #user=elasticsearch redirect_stderr=true stdout_logfile=/tmp/ElasticHQ.log EOF supervisorctl update supervisorctl reload supervisorctl status ``` > docker 安裝 ``` docker run -p 5000:5000 elastichq/elasticsearch-hq ``` web端訪問結果:http://192.168.110.245:5000/ # 4. 安裝 Kibana ## 4.1 引用: > Kibana是一個針對Elasticsearch的開源分析及視覺化平臺,用來搜尋、檢視互動儲存在Elasticsearch索引中的資料。使用Kibana,可以通過各種圖表進行高階資料分析及展示。 > Kibana讓海量資料更容易理解。它操作簡單,基於瀏覽器的使用者介面可以快速建立儀表板(dashboard)實時顯示Elasticsearch查詢動態。 ## 4.2 安裝: ``` yum -y install kibana-7.6.2-x86_64.rpm #建立日誌路徑 mkdir /var/log/kibana touch /var/log/kibana/kibana.log chmod -R 755 /var/log/kibana/ chown -R kibana /var/log/kibana/ ``` ## 4.3 修改配置檔案 ``` [root@node01 elk]# cat /etc/kibana/kibana.yml |grep -Ev '^#|^$' #kibana 埠號 server.port: 5601 #kibana 內網 ip server.host: "192.168.110.245" #es 的地址 elasticsearch.hosts: ["http://192.168.110.245:9200"] #es 的賬號密碼,在配置 tls 通訊的時候那裡就設定了的 elasticsearch.username: "kibana" elasticsearch.password: "123456" #記錄傳送到 es 的查詢 elasticsearch.logQueries: true # 日誌路徑 logging.dest: /var/log/kibana/kibana.log # 禁止顯示除錯誤訊息以外的所有日誌記錄輸出。 logging.quiet: true # 記錄所有日誌事件,包括系統使用情況資訊和所有請求 logging.verbose: false # 開啟 xpack xpack.security.enabled: true ``` ## 4.4 啟動 ``` systemctl restart kibana systemctl status kibana ``` ## 4.5 open kibana url http://192.168.110.245:5601/ ,這裡要用 elastic 賬號登入,使用 kibana 登入是無法,會報錯 403 沒許可權 ## 4.6 記憶體限制 參考資料: 預設記憶體限制 1.4GB,如果過需要可以通過環境變數進行替換,一般不需要更改。 # 5. node02 安裝 logstash ## 5.1 安裝 logstash ``` yum -y install logstash-7.6.2.rpm cp /etc/logstash/logstash.yml /etc/logstash/logstash.yml.orig ``` ## 5.2 配置檔案 ``` [root@node02 elk]# cat /etc/logstash/logstash.yml|grep -Ev '#|^$' path.data: /var/lib/logstash # 配置自動載入配置,這樣就無須在更改配置時手動重啟 logstash 引起資料丟失 config.reload.automatic: true # 自動載入的時間 config.reload.interval: 10s path.logs: /var/log/logstash ``` ## 5.3 修改記憶體地址 ``` vim /etc/logstash/jvm.options -Xms1g -Xmx1g ``` ## 5.4 手動測試,輸入 hello ``` cd /usr/share/logstash ./bin/logstash -e 'input { stdin { } } output { stdout { } }' hello ``` # 6. node02、03安裝 filebeat ## 6.1 安裝 ``` yum -y install filebeat-7.6.2-x86_64.rpm cd /etc/filebeat cp filebeat.yml filebeat.yml.orig ``` # 7. 日誌處理 ## 案例一:官方例子 ## 參考: https://www.elastic.co/guide/en/logstash/7.7/plugins-inputs-beats.html https://www.elastic.co/guide/en/logstash/current/advanced-pipeline.html ## 7.1.1在 node02 調整 logstash 配置 ``` cd /etc/logstash/conf.d/ cat < /etc/logstash/conf.d/example.conf input { beats { port => 5044 } } output { elasticsearch { hosts => ["http://192.168.110.245:9200"] index => "%{[@metadata][beat]}-%{[@metadata][version]}" user => "elastic" password => "123456" } } EOF systemctl restart logstash ``` ## 7.1.2檢查埠是否啟動: ``` [root@node02 elk]# netstat -nltp|grep 5044 tcp6 0 0 :::5044 :::* LISTEN 31422/java ``` ## 7.1.3 在 node02 配置 filebeat ``` cd /root/src/elk wget https://download.elastic.co/demos/logstash/gettingstarted/logstash-tutorial.log.gz gzip -d logstash-tutorial.log.gz cat < filebeat.yml filebeat.inputs: - type: log paths: - /root/src/elk/logstash-tutorial.log output.logstash: hosts: ["192.168.110.246:5044"] EOF ``` ## 7.1.4 啟動 filebeat ``` /usr/share/filebeat/bin/filebeat -e -c filebeat.yml -d "publish" ``` 啟動結果部分截圖: ## 7.1.5 在 kibana 開啟“Management”->>“Elasticsearch”->>“IndexManagement”,可以看到 filebeat 的索引 然後我們到 kiabana 那裡新增 index 試試。 建立成功 最後我們到 Discover 這裡就可以看到相關試圖了