ELK日志收集系統介紹
一 簡單介紹
ELK部署搭建有很多成型的方案,這里推薦一種比較中規中矩的方案,它整合了logstash比較消耗資源以及當服務端臨時宕機的時候出現數據丟失的問題,主要由filebeat+redis+logstash+elasticsearch+kibana構成,在每個需要收集日志的機器上面下發filebeat作為日志收集端,redis作為消息隊列,並且設置集群,設置高可用,logstatsh作為所有收集到的日志篩選,清洗端,而elasticsearch作為所有的日志的存儲端,kibana作為日志的展示平台,展示所有的收集日志並提供圖標等。
二 搭建介紹
這里准備了三台本地虛擬機,他們的信息以及將要部署的軟件如下所示。
名稱 | ip地址 | 角色 | 部署應用 | 備注 |
devops1 | 172.16.173.141 | 日志客戶端,消息隊列緩存端,日志集中處理端,日志分布存儲端,日志展示端 | filebeat,redis,logstash,elasticsearch,kibana | 服務端 |
devops2 | 172.16.173.142 | 日志客戶端,消息隊列緩存端,日志分布存儲端 | filebeat,redis,elasticsearch | 客戶端 |
devops3 | 172.16.173.143 | 日志客戶端,消息隊列緩存端,日志分布存儲端 | filebeat,redis,elasticsearch | 客戶端 |
注意: 所有部署的應用都是以systemctl的形式進行管理
三 在每台服務器上面開始部署日志收集端,這里使用ansible的腳本形式對服務器進行批量部署(ansible相關的知識自己補充學習)
3.1 安裝filebeat並將其使用systemctl進行部署
3.1.1 提前下載好需要安裝的filebeat的文件以及service,這里是rpm包為 filebeat-7.7.0-x86_64.rpm
3.1.2 service文件是
[Unit] Description=Filebeat sends log files to Logstash or directly to Elasticsearch. Documentation=https://www.elastic.co/products/beats/filebeat Wants=network-online.target After=network-online.target [Service] Environment="BEAT_LOG_OPTS=" Environment="BEAT_CONFIG_OPTS=-c /etc/filebeat/filebeat.yml" Environment="BEAT_PATH_OPTS=-path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat" ExecStart=/usr/share/filebeat/bin/filebeat -environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS Restart=always [Install] WantedBy=multi-user.target
3.1.3 按照ansible工程組織如下所示
3.1.4 最后就是filebeat的安裝playbook
- name: copy filebeat rpm to remote copy: src=filebeat-7.7.0-x86_64.rpm dest=/root mode=755 force=yes - name: rpm install filebeat rpm shell: rpm -ivh filebeat-7.7.0-x86_64.rpm args: chdir: /root - name: copy service to remote copy: src=filebeat.service dest=/usr/lib/systemd/system/filebeat.service force=yes mode=755 - name: start filebeat service systemd: name=filebeat state=restarted enabled=yes
3.2 在每台服務器安裝redis中間件作為消息隊列,防止在logstatsh在意外中斷的時候出現日志文件丟失,前面的工程結構以及ansible不再追述,這里只介紹如何使用ansible安裝redis消息中間件
- name: install wget yum: name=wget state=present - name: install redis shell: "wget http://download.redis.io/releases/redis-4.0.6.tar.gz" args: chdir: /usr/local ignore_errors: false - name: tar redis shell: "tar -zxvf redis-4.0.6.tar.gz" args: chdir: /usr/local - name: install gcc yum: name=gcc state=present - name: make shell: "make MALLOC=libc" args: chdir: /usr/local/redis-4.0.6 - name: make install shell: "make install" args: chdir: /usr/local/redis-4.0.6/src - name: remove old redis config lineinfile: path: /usr/local/redis-4.0.6/redis.conf regexp: '^#*?daemonize no|^#*?daemonize yes' state: absent - name: add new redis config lineinfile: path: /usr/local/redis-4.0.6/redis.conf line: "daemonize yes" state: present - name: start redis shell: "/usr/local/bin/redis-server /usr/local/redis-4.0.6/redis.conf" args: chdir: /root ignore_errors: true
3.3 在devops1上面部署logstash