天天看點

将redis加入到elk日志系統裡

整個架構圖圖下:

部署redis

那就安裝新一點的tcl吧,方法如下:

1

2

3

4

5

<code>wget http:</code><code>//downloads</code><code>.sourceforge.net</code><code>/tcl/tcl8</code><code>.6.1-src.</code><code>tar</code><code>.gz</code>

<code>tar</code> <code>xzvf tcl8.6.1-src.</code><code>tar</code><code>.gz  -C </code><code>/usr/local/</code>

<code>cd</code>  <code>/usr/local/tcl8</code><code>.6.1</code><code>/unix/</code>

<code>.</code><code>/configure</code>

<code>make</code> <code>&amp;&amp; </code><code>make</code> <code>install</code>

然後重新去#make test就會看到成功的字樣,如圖:

現在redis的漏洞比較多,大多數就是因為密碼太簡單導緻的,是以把redis密碼改一下,在redis.conf裡,改成如下的樣子:

6

7

8

9

<code>bind 内網IP位址 127.0.0.1              </code><code>###僅允許内網和本機通路</code>

<code>protected-mode </code><code>yes</code>                   <code>###保護模式開啟</code>

<code>port 6379                          </code><code>###端口預設為6379,按需修改</code>

<code>daemonize </code><code>yes</code>                        <code>###守護模式開啟</code>

<code>pidfile </code><code>/usr/local/redis/redis</code><code>.pid               </code><code>###指定pid檔案路徑和檔案名</code>

<code>logfile </code><code>"/usr/local/redis/redis.log"</code>             <code>###指定日志檔案路徑和檔案名</code>

<code>dbfilename redis.rdb                     </code><code>###指定資料檔案RDB檔案名</code>

<code>dir</code> <code>/usr/local/redis/</code>                    <code>###指定資料檔案RDB檔案的存放路徑</code>

<code>requirepass 『YOURPASSWORD』              </code><code>###設定通路密碼,提升密碼強度</code>

儲存之後啟動redis即可。

如果redis是主從配置,若master配置了密碼則slave也要配置相應的密碼參數否則無法進行正常複制的。需要在slave的redis.conf裡找到“#masterauth  mstpassword”,去掉注釋,也改成跟master一樣的密碼,重新開機一下即可。 

安裝x-pack

x-pack是elk官方提供的認證授權插件,安裝方法很簡單,分别找到下面三個檔案,然後後面加上“install x-pack”即可:

<code>.</code><code>/elasticsearch-plugin</code> <code>install</code> <code>x-pack --batch </code>

<code>.</code><code>/logstash-plugin</code> <code>install</code> <code>x-pack </code>

<code>.</code><code>/kibana-plugin</code> <code>install</code> <code>x-pack</code>

如果要檢視已經安裝的插件,那就是:

<code>[root@chen-elk-001 bin]</code><code># ./elasticsearch-plugin list</code>

<code>x-pack</code>

<code>[root@chen-elk-001 bin]</code><code># ./kibana-plugin list</code>

<code>[email protected]</code>

如果kibana-plugin要解除安裝x-pack,那就是:

<code>.</code><code>/kibana-plugin</code> <code>remove x-pack</code>

重新開機服務即可登入,預設的登入使用者名: elastic 密碼:changeme。

這裡注意一下,#./logstash-plugin install x-pack 的時候可能是出現ruby源的錯誤,如圖:

<code>[root@chen-logstash-001 bin]</code><code># ./logstash-plugin install file:///root/x-pack-5.6.4.zip</code>

<code>Installing </code><code>file</code><code>: </code><code>/root/x-pack-5</code><code>.6.4.zip</code>

<code>Install successful</code>

配置filebeat

由于這個nginx我們需要先讓filebeat把nginx.log和error.log先推到redis存儲,然後再由redis推到logstash。配置filebeat.yml的具體資訊如下: 

10

11

<code>[root@iZbp10hw6wezxmrvrcjyhlZ filebeat]</code><code># grep -iv '#' /etc/filebeat/filebeat.yml | grep -iv '^$'</code>

<code>filebeat.prospectors:</code>

<code>- input_type: log</code>

<code>  </code><code>paths:</code>

<code>    </code><code>- </code><code>/usr/local/nginx/logs/</code><code>*.log    </code><code>#這裡是nginx的日志檔案夾</code>

<code>    </code> 

<code>output.redis:                </code><code>#以下這部分都是新加的</code>

<code>  </code><code>enabled: </code><code>true</code>

<code>  </code><code>hosts: [</code><code>"127.0.0.1:6379"</code><code>]</code>

<code>  </code><code>key: logindexer_list            </code><code>#與redis配置檔案裡的key遙相呼應</code>

<code>  </code><code>password: 『YOURPASSWORD』    </code><code>#跟上面的密碼遙相呼應</code>

配置完畢之後,啟動filebeat,指令語句:#/etc/init.d/filebeat start -c /etc/filebeat/filebeat.yml。

配置logstash

由于這台logstash已經開啟了一個logstash程序,那麼再收集nginx的日志需要新開一個logstash程序,也需要新寫一個conf檔案,假設新的conf檔案是nginx-logstash.conf,它的寫法如下:

12

13

14

15

16

17

18

19

20

21

22

23

<code>input {</code>

<code>    </code><code>redis {</code>

<code>        </code><code>host =&gt; </code><code>"10.168.173.181"</code>

<code>        </code><code>type</code> <code>=&gt; </code><code>"redis-input"</code>

<code>        </code><code>data_type =&gt; </code><code>"list"</code>

<code>        </code><code>key =&gt; </code><code>"logindexer_list"</code>

<code>        </code><code>port =&gt; 6379</code>

<code>        </code><code>password =&gt; </code><code>"ChenRedi$"</code>

<code>    </code><code>}</code>

<code>}</code>

<code># filter configration here</code>

<code>output {</code>

<code>     </code><code>elasticsearch {</code>

<code>        </code><code>hosts =&gt; [ </code><code>"10.162.80.192:9200"</code> <code>]</code>

<code>        </code><code>user =&gt; elastic</code>

<code>        </code><code>password =&gt; changeme</code>

<code>        </code><code>index =&gt; </code><code>"nginxlogstash-%{+YYYY.MM.dd}"</code>        <code>#這個是新的索引</code>

<code>stdout {</code>

<code>    </code><code>codec =&gt; rubydebug</code>

<code>       </code><code>}</code>

現在logstash不支援多個執行個體共享一個path.data,是以要在在啟動不同執行個體的時候,指令行裡增加"--path.data PATH " ,為不同執行個體指定不同的路徑。啟動logstash之後,看到顯示如下:

再到nginx的日志看一下,因為logstash裡沒有做日志的切割,是以是整個一個類似字元串的形式發送了過來:

果然有這樣的日志,可見logstash與nginx的redis已經正确連接配接。在elasticsearch裡,使用#curl -u 賬号密碼 'localhost:9200/_cat/indices?v' 查詢索引的時候,就會看到那個"nginxlogstash",如圖:

最後的最後,如果您覺得本文對您升職加薪有幫助,那麼請不吝贊助之手,刷一下下面的二維碼,贊助本人繼續寫更多的博文!

 本文轉自 蘇幕遮618 51CTO部落格,原文連結:http://blog.51cto.com/chenx1242/2054719

繼續閱讀