ELK適用場景
公司網站的訪問量有多大,訪問高峰期的時間段是多少,最經常訪問的熱點數據是什么?這一切的一切,雖然我們可以自己通過shell等手段截取出來,
但是如果網站多了,服務器多了,還是非常不方便,而且閱讀性也不好,因此ELK應運而生,不僅可以獲取訪問高峰期,還可以制作圖表,讓你的領導一目了然,
ELK已然成為各大互聯往公司必部署的項目,因此接下來我們就來部署一套ELK系統
安裝環境
192.168.41.142 ES,Kibana
192.168.41.143 logstash
ELK版本:7.5.1
操作系統:CentOS Linux release 7.6.1810 (Core)
注意:
- 請確保你的firewalld和selinux關閉
- 最好確保你的機器是2個cpu以上
- 最好確保你的機器是2G以上內存
- ES和logstash服務器需要java8以上
原理
logstash負責收集客戶端的日志信息發送給ES服務器,然后通過Kibana以web形式展現出來
kibana
Kibana是一個為Logstash和ElasticSearch提供的日志分析的Web接口。可使用它對日志進行高效的搜索、可視化、分析等各種操作。是一個開源的分析與可視化平台,設計出來用於和Elasticsearch一起使用的。你可以用kibana搜索、查看存放在Elasticsearch中的數據。Kibana與Elasticsearch的交互方式是各種不同的圖表、表格、地圖等,直觀的展示數據,從而達到高級的數據分析與可視化的目的。
部署kibana
1、上傳kibana包到192.168.41.142主機上,並解壓到/usr/local目錄下
tar -zxvf kibana-7.5.1-linux-x86_64.tar.gz -C /usr/local/
2、修改kibana的監聽端口,默認為5601,0.0.0.0代表全網監聽
root@bogon ~#vim /usr/local/kibana-7.5.1-linux-x86_64/config/kibana.yml server.port: 5601 server.host: "0.0.0.0
3、啟動kiabana
root@bogon ~#/usr/local/kibana-7.5.1-linux-x86_64/bin/kibana Kibana should not be run as root. Use --allow-root to continue.
當用root啟動kibana時會報錯,提示我們不能用root賬號運行,需要加--allow-root參數
root@bogon ~#nohup /usr/local/kibana/bin/kibana --allow-root &> /var/log/kibana.log & #剝離ssh終端后台運行kibana並把日志輸出到/var/log/kibana.log [1] 17854
root@bogon log#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:9000 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:5601 *:* LISTEN 0 128 *:10050 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 :::10050 :::*
ss -ntl 查看kibana的5601端口已經啟動
4、瀏覽器輸入Kibana服務器的ip地址加端口號驗證,出現以下內容代表kibana部署成功,由於連接不到ES服務器所以才會出現這種界面!
由於是kibana界面是不安全的,因為沒有任何的認證,誰都可以登錄到kibana界面,為了安全,我們可以部署一個nginx,利用反向代理到后端的kibana
5、部署nginx並配置
[root@localhost ~]# vim /etc/nginx/nginx.conf location / { proxy_pass http://127.0.0.1:5601; auth_basic "ELK ADMIN PAGE"; auth_basic_user_file /etc/nginx/.htpasswd; }
設置nginx認證賬戶密碼
root@bogon log#htpasswd -c -m /etc/nginx/.htpasswd admin1 New password: Re-type new password: Adding password for user admin1
重啟nginx服務
root@bogon log#systemctl restart nginx
重新登陸驗證
輸入用戶名密碼后得到如下界面
部署ES(elasticsearch)
1、上傳elasticsearch包並解壓
tar -zxvf elasticsearch-7.5.1-linux-x86_64.tar.gz -C /usr/local/
2、編輯配置文件
root@bogon ~#vim /usr/local/elasticsearch-7.5.1/config/elasticsearch.yml
network.host: 0.0.0.0 http.port: 9200 path.data: /usr/local/elasticsearch/data/ path.logs: /usr/local/elasticsearch/logs/
3、因為ES啟動文件不允許以root用戶執行,因此需要用一個普通用戶lizf,並且修改/usr/local/elasticsearch屬主和屬組為lizf
root@bogon ~#chown -R lizf.lizf /usr/local/elasticsearch-7.5.1/ root@bogon ~#ls -ld /usr/local/elasticsearch-7.5.1/ drwxr-xr-x. 10 lizf lizf 166 10月 24 12:22 /usr/local/elasticsearch-7.5.1/
4、切換到lizf用戶啟動
root@bogon ~#su - lizf 上一次登錄:日 10月 24 12:47:47 CST 2021pts/0 上 15:35:09 lizf@bogon ~$/usr/local/elasticsearch-7.5.1/bin/elasticsearch -d
可能會遇到的報錯
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535] [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] [3]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
解決辦法
1]: [root@localhost elasticsearch]# vim /etc/security/limits.conf * soft nofile 65536 * hard nofile 65536 [root@localhost local]# vim /etc/security/limits.d/20-nproc.conf * soft nproc unlimited [2]: [root@localhost elasticsearch]# vim /etc/sysctl.conf vm.max_map_count=262144 [root@localhost elasticsearch]#sysctl -p [3]: [root@localhost elasticsearch]# vim /usr/local/elasticsearch/config/elasticsearch.yml cluster.initial_master_nodes: ["node-1"] node.name: node-1
再次啟動ES時就不會報錯,端口正常啟動了
lizf@bogon ~$/usr/local/elasticsearch-7.5.1/bin/elasticsearch -d OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release. lizf@bogon ~$ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:9000 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:5601 *:* LISTEN 0 128 *:10050 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::9200 :::* LISTEN 0 128 :::9300 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 :::10050
瀏覽器驗證
部署logstash
1、在192.168.41.143主機上上傳logstash包並解壓到/usr/local/目錄下
2、編輯logstash配置文件
先將/usr/local/logstash-7.5.1/config/logstash-sample.conf 改成/usr/local/logstash-7.5.1/config/logstash.conf 再去編輯
[root@node2 config]# vim /usr/local/logstash-7.5.1/config/logstash.conf
3、由於logstash啟動非常慢,官方給了一個優化速度的包,咱們也安裝一下並啟動
[root@localhost config]# yum install epel-release -y && yum install haveged -y && systemctl enable haveged && systemctl start haveged
4、啟動logstash服務
[root@node2 config]# nohup /usr/local/logstash-7.5.1/bin/logstash -f /usr/local/logstash-7.5.1/config/logstash.conf &> /var/log/logstash.log & [1] 17916