1. 程式人生 > >ELk(Elasticsearch, Logstash, Kibana)的安裝配置

ELk(Elasticsearch, Logstash, Kibana)的安裝配置

目錄

ELk(Elasticsearch, Logstash, Kibana)的安裝配置

本文中所有的版本都是基於5.2.0,因為公司es(Elasticsearch)的環境是5.2.0。

1. Elasticsearch的安裝-官網

關於Elasticsearch的安裝在之前的文章中已經寫過了,這裡不再贅述。

[elasticsearch及head外掛安裝與配置](https://www.cnblogs.com/chaos-x/p/9446250.html)

2. Kibana的安裝配置-官網

Kibana是一個能把你es中資料進行視覺化顯示的工具,包括實時統計和分析,基本上是零配置。

1. Kibana下載地址

Kibana 5.2.0 linux 64-bit tar.gz

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.2.0-linux-x86_64.tar.gz

2.解壓

tar -zxvf kibana-5.2.0-linux-x86_64.tar.gz

3. 配置

vim kibana-5.2.0-linux-x86_64/config/kibana.yml

# Kibana is served by a back end server. This setting specifies the port to use.
server.port: 30000

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
server.host: "0.0.0.0"

# The URL of the Elasticsearch instance to use for all your queries.
# es的訪問地址和商品號
elasticsearch.url: "http://localhost:19200"
  • 啟動

sh kibana-5.2.0-linux-x86_64/bin/kibana

在瀏覽器中訪問Kibana的伺服器加埠號就可以看到Kibana的頁面了。

3. Logstash的安裝配置-官網

Logstash是日誌的收集工具,可以對日誌進行收集,分析,解碼,過濾,輸出。一般使用Filebeat收集日誌到Logstash,由Logstash處理後儲存到es。關於Filebeat後面再說。

1. 下載

https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz

wget https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz

2. 解壓

tar -zxvf logstash-5.2.0.tar.gz

3. 啟動

sh logstash-5.2.0/bin/logstash -e 'input{stdin{}}output{stdout{codec=>rubydebug}}'

啟動後輸入'hello word',回車。會輸出如下結果

{
       "message" => "Hello World",
      "@version" => "1",
    "@timestamp" => "2014-08-07T10:30:59.937Z",
          "host" => "raochenlindeMacBook-Air.local",
}

Logstash 會給事件新增一些額外資訊。最重要的就是 @timestamp,用來標記事件的發生時間。因為這個欄位涉及到 Logstash 的內部流轉,所以必須是一個 joda 物件,如果你嘗試自己給一個字串欄位重新命名為 @timestamp 的話,Logstash 會直接報錯。所以,請使用 filters/date 外掛 來管理這個特殊欄位。

4. 配置

可以把把配置寫到一個檔案中,來啟動Logstash。

  • 在Logstash的配置檔案目錄中建立test.yml檔案
cd logstash-5.2.0/config/
vim test.yml
  • 配置檔案內容
input{
    stdin{}
}
ouput{
    stdout{
        codec=>rubydebug
    }
}
  • 啟動

    sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/test.yml

可以達步驟3的效果。

4. 使用ELK收集nginx的訪問日誌

1.配置Logstash

  • 在Logstash的配置檔案目錄中建立nginx-log.yml檔案
cd logstash-5.2.0/config/
vim nginx-log.yml
  • 配置檔案內容
input {
  file {  # 指定一個檔案作為輸入源
    path => "/usr/local/nginx/logs/access.log"  # 指定檔案的路徑
    start_position => "beginning"  # 指定何時開始收集,這時設定從檔案的最開頭收集
    type => "nginx"  # 定義日誌型別,可自定義
  }
}
filter {  # 配置過濾器
    grok {
        match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"}  # 定義日誌的輸出格式
    }
    geoip {
        source => "clientip"
    }
}
output {
    # 標準輸出,是輸出到終端
    stdout { codec => rubydebug }
    # 輸出到es
    elasticsearch {
        hosts => ["127.0.0.1:19200"]
        index => "nginx-log-%{+YYYY.MM.dd}"
  }
}
  • 檢查配置檔案

sh logstash --path.settings /etc/logstash/ -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit

2. 配置nginx的配置檔案

  • 開啟nginx的配置檔案

vim /usr/local/nginx/conf/nginx.conf

  • 在http中增加如下內容
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$upstream_addr" $request_time';
}
  • 在要格式化日誌的 server中增加如下配置
        #access_log  logs/host.access.log  main;
        # 增加如下內容, 日誌格式化main2要在上面定義,不然這裡無法引用
        access_log  logs/elk_access.log  main2;

        location / {
            root   html;
            index  index.html index.htm;
            # 增加如下三行內容,分別是攜帶訪問host,遠端地址和各層代理地址
            proxy_set_header Host   $host;
            proxy_set_header X-Real-IP      $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
  • 重啟nginx

sh /usr/local/nginx/sbin/nginx -s reload

3. 檢查啟動

  • 檢查

    sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit

  • 啟動

    sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml
    當終端輸出如下內容就成功了

{
         "@timestamp" => 2018-12-18T08:44:56.361Z,
    "plugin_instance" => "vda",
               "read" => 467266,
             "plugin" => "disk",
               "host" => "172.24.121.18",
           "@version" => "1",
      "collectd_type" => "disk_ops",
               "type" => "collectd",
              "write" => 12204609
}
{
         "longterm" => 0.08,
       "@timestamp" => 2018-12-18T08:44:46.362Z,
           "plugin" => "load",
        "shortterm" => 0.06,
             "host" => "172.24.121.18",
         "@version" => "1",
    "collectd_type" => "load",
             "type" => "collectd",
          "midterm" => 0.04
}

5. Kibana展示nginx訪問日誌

進入Kibana頁面如下

  • 配置Index Patterns

  • 進入後點擊左上角的 ‘add new’ 進行建立索引模板

在Kibana中,要分析展示資料時,要先建立Index Patterns

選擇index的時候可以用萬用字元 ‘*’ 來把所有的nginx-log的訪問日誌分成一個組。

Time-field name 是要指定一個日期格式的欄位,以便於後面統計使用。

然後就可以在這裡選擇配置好的Index patterns了。

其中x軸的時間就是建立Index Patterns的時候選擇的那個日期欄位。

參考:

本文中所有的版本都是基於5.2.0,因為公司es(Elasticsearch)的環境是5.2.0。

1. Elasticsearch的安裝-官網

關於Elasticsearch的安裝在之前的文章中已經寫過了,這裡不再贅述。

[elasticsearch及head外掛安裝與配置](https://www.cnblogs.com/chaos-x/p/9446250.html)

2. Kibana的安裝配置-官網

Kibana是一個能把你es中資料進行視覺化顯示的工具,包括實時統計和分析,基本上是零配置。

1. Kibana下載地址

Kibana 5.2.0 linux 64-bit tar.gz

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.2.0-linux-x86_64.tar.gz

2.解壓

tar -zxvf kibana-5.2.0-linux-x86_64.tar.gz

3. 配置

vim kibana-5.2.0-linux-x86_64/config/kibana.yml

# Kibana is served by a back end server. This setting specifies the port to use.
server.port: 30000

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
server.host: "0.0.0.0"

# The URL of the Elasticsearch instance to use for all your queries.
# es的訪問地址和商品號
elasticsearch.url: "http://localhost:19200"
  • 啟動

sh kibana-5.2.0-linux-x86_64/bin/kibana

在瀏覽器中訪問Kibana的伺服器加埠號就可以看到Kibana的頁面了。

3. Logstash的安裝配置-官網

Logstash是日誌的收集工具,可以對日誌進行收集,分析,解碼,過濾,輸出。一般使用Filebeat收集日誌到Logstash,由Logstash處理後儲存到es。關於Filebeat後面再說。

1. 下載

https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz

wget https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz

2. 解壓

tar -zxvf logstash-5.2.0.tar.gz

3. 啟動

sh logstash-5.2.0/bin/logstash -e 'input{stdin{}}output{stdout{codec=>rubydebug}}'

啟動後輸入'hello word',回車。會輸出如下結果

{
       "message" => "Hello World",
      "@version" => "1",
    "@timestamp" => "2014-08-07T10:30:59.937Z",
          "host" => "raochenlindeMacBook-Air.local",
}

Logstash 會給事件新增一些額外資訊。最重要的就是 @timestamp,用來標記事件的發生時間。因為這個欄位涉及到 Logstash 的內部流轉,所以必須是一個 joda 物件,如果你嘗試自己給一個字串欄位重新命名為 @timestamp 的話,Logstash 會直接報錯。所以,請使用 filters/date 外掛 來管理這個特殊欄位。

4. 配置

可以把把配置寫到一個檔案中,來啟動Logstash。

  • 在Logstash的配置檔案目錄中建立test.yml檔案
cd logstash-5.2.0/config/
vim test.yml
  • 配置檔案內容
input{
    stdin{}
}
ouput{
    stdout{
        codec=>rubydebug
    }
}
  • 啟動

    sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/test.yml

可以達步驟3的效果。

4. 使用ELK收集nginx的訪問日誌

1.配置Logstash

  • 在Logstash的配置檔案目錄中建立nginx-log.yml檔案
cd logstash-5.2.0/config/
vim nginx-log.yml
  • 配置檔案內容
input {
  file {  # 指定一個檔案作為輸入源
    path => "/usr/local/nginx/logs/access.log"  # 指定檔案的路徑
    start_position => "beginning"  # 指定何時開始收集,這時設定從檔案的最開頭收集
    type => "nginx"  # 定義日誌型別,可自定義
  }
}
filter {  # 配置過濾器
    grok {
        match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"}  # 定義日誌的輸出格式
    }
    geoip {
        source => "clientip"
    }
}
output {
    # 標準輸出,是輸出到終端
    stdout { codec => rubydebug }
    # 輸出到es
    elasticsearch {
        hosts => ["127.0.0.1:19200"]
        index => "nginx-log-%{+YYYY.MM.dd}"
  }
}
  • 檢查配置檔案

sh logstash --path.settings /etc/logstash/ -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit

2. 配置nginx的配置檔案

  • 開啟nginx的配置檔案

vim /usr/local/nginx/conf/nginx.conf

  • 在http中增加如下內容
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$upstream_addr" $request_time';
}
  • 在要格式化日誌的 server中增加如下配置
        #access_log  logs/host.access.log  main;
        # 增加如下內容, 日誌格式化main2要在上面定義,不然這裡無法引用
        access_log  logs/elk_access.log  main2;

        location / {
            root   html;
            index  index.html index.htm;
            # 增加如下三行內容,分別是攜帶訪問host,遠端地址和各層代理地址
            proxy_set_header Host   $host;
            proxy_set_header X-Real-IP      $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
  • 重啟nginx

sh /usr/local/nginx/sbin/nginx -s reload

3. 檢查啟動

  • 檢查

    sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml --config.test_and_exit

  • 啟動

    sh logstash-5.2.0/bin/logstash -f logstash-5.2.0/config/nginx-log.yml
    當終端輸出如下內容就成功了

{
         "@timestamp" => 2018-12-18T08:44:56.361Z,
    "plugin_instance" => "vda",
               "read" => 467266,
             "plugin" => "disk",
               "host" => "172.24.121.18",
           "@version" => "1",
      "collectd_type" => "disk_ops",
               "type" => "collectd",
              "write" => 12204609
}
{
         "longterm" => 0.08,
       "@timestamp" => 2018-12-18T08:44:46.362Z,
           "plugin" => "load",
        "shortterm" => 0.06,
             "host" => "172.24.121.18",
         "@version" => "1",
    "collectd_type" => "load",
             "type" => "collectd",
          "midterm" => 0.04
}

5. Kibana展示nginx訪問日誌

進入Kibana頁面如下

  • 配置Index Patterns

  • 進入後點擊左上角的 ‘add new’ 進行建立索引模板

在Kibana中,要分析展示資料時,要先建立Index Patterns

選擇index的時候可以用萬用字元 ‘*’ 來把所有的nginx-log的訪問日誌分成一個組。

Time-field name 是要指定一個日期格式的欄位,以便於後面統計使用。

然後就可以在這裡選擇配置好的Index patterns了。

其中x軸的時間就是建立Index Patterns的時候選擇的那個日期欄位。

參考: