ELK部署

ELK適用場景

公司網站的訪問量有多大,訪問高峯期的時間段是多少,最常常訪問的熱點數據是什麼?這一切的一切,雖然咱們能夠本身經過shell等手段截取出來,
可是若是網站多了,服務器多了,仍是很是不方便,並且閱讀性也很差,所以ELK應運而生,不只能夠獲取訪問高峯期,還能夠製做圖表,讓你的領導一目瞭然,
ELK已然成爲各大互聯往公司必部署的項目,所以接下來咱們就來部署一套ELK系統java

實驗環境

192.168.254.13 ES,Kibana
192.168.254.11 logstash
ELK版本:7.5.1
操做系統:CentOS Linux release 7.6.1810 (Core)node

note:linux

  • 請確保你的firewalld和selinux關閉
  • 最好確保你的機器是2個cpu以上
  • 最好確保你的機器是2G以上內存

原理

logstash負責收集客戶端的日誌信息發送給ES服務器,而後經過Kibana以web形式展示出來
note:ES和logstash服務器須要java8以上nginx

部署kibana

1.解壓ES和kibana包,並移動到/usr/local下web

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.啓動服務shell

nohup /usr/local/kibana/bin/kibana --allow-root & 剝離ssh終端後臺運行kibana

4.瀏覽器輸入Kibana服務器的ip地址加端口號驗證,出現如下內容表明kibana部署成功,因爲鏈接不到ES服務器因此纔會出現這種界面!apache

 

 

 note:可是kibana界面是不安全的,由於沒有任何的認證,誰均可以登陸到kibana界面,爲了安全,咱們能夠部署一個nginx,利用反向代理到後端的kibanavim

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搭建完畢!!!

 

補充

對於ELK而言雖然能夠收集到我們想要的日誌,可是logstash比較重型,所以消耗服務器資源較大,在企業當中有可能比較影響業務,所以,咱們能夠用filebeat搭建日誌收集,可是filebeat不支持正則表達式,咱們能夠用filebeat收集日誌交給logstash進行過濾,在發送給ES服務器

對於EFK的搭建往後更新......

相關文章
相關標籤/搜索