ELK部署
ELK適用場景
公司網站的訪問量有多大,訪問高峰期的時間段是多少,最經常訪問的熱點數據是什么?這一切的一切,雖然我們可以自己通過shell等手段截取出來,
但是如果網站多了,服務器多了,還是非常不方便,而且閱讀性也不好,因此ELK應運而生,不僅可以獲取訪問高峰期,還可以制作圖表,讓你的領導一目了然,
ELK已然成為各大互聯往公司必部署的項目,因此接下來我們就來部署一套ELK系統
實驗環境
192.168.254.13 ES,Kibana
192.168.254.11 logstash
ELK版本:7.5.1
操作系統:CentOS Linux release 7.6.1810 (Core)
note:
- 請確保你的firewalld和selinux關閉
- 最好確保你的機器是2個cpu以上
- 最好確保你的機器是2G以上內存
原理
logstash負責收集客戶端的日志信息發送給ES服務器,然后通過Kibana以web形式展現出來
note:ES和logstash服務器需要java8以上
部署kibana
1.解壓ES和kibana包,並移動到/usr/local下
tar -zxvf kibana-7.5.1-linux-x86_64.tar.gz tar -zxvf elasticsearch-7.5.1-linux-x86_64.tar.gz mv elasticsearch-7.5.1 /usr/local/elasticsearch mv kibana-7.5.1-linux-x86_64 /usr/local/kibana
2.修改kibana的監聽端口,默認為5601,0.0.0.0代表
vim config/kibana.yml
server.port: 5601
server.host: "0.0.0.0
3.啟動服務
nohup /usr/local/kibana/bin/kibana --allow-root & 剝離ssh終端后台運行kibana
4.瀏覽器輸入Kibana服務器的ip地址加端口號驗證,出現以下內容代表kibana部署成功,由於連接不到ES服務器所以才會出現這種界面!

note:但是kibana界面是不安全的,因為沒有任何的認證,誰都可以登錄到kibana界面,為了安全,我們可以部署一個nginx,利用反向代理到后端的kibana
5.安裝部署nginx並配置
1)[root@localhost ~]# yum install epel-release -y && yum install nginx -y
2)[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;
}
3)[root@localhost ~]# service nginx restart
4)[root@localhost ~]# htpasswd -c -m /etc/nginx/.htpasswd admin1
New password:
Re-type new password:
Adding password for user admin1
5)瀏覽器輸入192.168.254.14,輸入用戶名密碼驗證

重啟nginx服務后驗證

輸入用戶名密碼后得到如下界面

這樣我們就可以把我們的ELK管理界面保護起來了,有木有點意思?
部署ES
1.編輯配置文件
vim config/elasticsearch.yml
network.host: 0.0.0.0
http.port: 9200
path.data: /usr/local/elasticsearch/data/
path.logs: /usr/local/elasticsearch/logs/
[liwang@localhost ~]$ vim /usr/local/elasticsearch-7.5.1/config/jvm.options
-Xms200m #按需要配置
-Xmx200m #按需要配置
2.因為ES啟動文件不允許以root用戶執行,因此需要創建一個普通用戶,並且修改/usr/local/elasticsearch屬主和屬組為liwang
[root@localhost elasticsearch]# useradd liwang
[root@localhost elasticsearch]# chown -R liwang.liwang /usr/local/elasticsearch/
[root@localhost ~]$ su - liwang
[liwang@localhost ~]$ /usr/local/elasticsearch/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
[3]: [root@localhost elasticsearch]# vim /usr/local/elasticsearch/config/elasticsearch.yml
cluster.initial_master_nodes: ["node-1"]
node.name: node-1
再次啟動ES:
此時已經不報錯,並且端口也正常啟動了
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::9200 :::*
LISTEN 0 128 :::9300 :::*
....
瀏覽器驗證

部署logstash
1.解壓文件,編輯配置文件
[root@localhost config]# tar -zxvf logstash-7.5.1.tar.gz -C /usr/local/
[root@localhost config]# vim /usr/local/logstash-7.5.1/config/logstash.conf
input{
stdin{}
}
output{
stdout{
codec=>rubydebug
}
}
2.由於logstash啟動非常慢,官方給了一個優化速度的包,咱們也安裝一下並啟動
[root@localhost config]# yum install epel-release -y && yum install haveged -y && systemctl enable haveged && systemctl start haveged
3.啟動logstash服務並測試
[root@localhost config]# /usr/local/logstash-7.5.1/bin/logstash -f /usr/local/logstash-7.5.1/config/logstash.conf
4.查看端口是否啟動

5.驗證,隨便輸入一些信息如果有如下返回單表OK

5.沒有問題就可已修改一下配置文件,監控apache訪問日志

6.重新啟動logstash
[root@localhost config]# /usr/local/logstash-7.5.1/bin/logstash -f /usr/local/logstash-7.5.1/config/logstash.conf
7.訪問一下192.168.254.14的apache服務器,看是否有日志發送到ES服務器,可以看到已經有日志了

8.可以看到日志就是原生的apache日志,接下來咱們可以根據需求定義,以后就可以按照咱們自定義的格式顯示了

9.修改配置文件,把剛才咱們的正則表達式添加進來

10.重啟logstash服務再次查看kibana,已經出現咱們自己定義的字段了

ELK搭建完畢!!!

