(實(shí)際應(yīng)用)ELK環(huán)境搭建

今天給大家?guī)淼氖情_源實(shí)時(shí)日志分析 ELK , ELK 由 ElasticSearch 、 Logstash 和 Kiabana 三個(gè)開源工具組成。官方網(wǎng)站:https://www.elastic.co

成都創(chuàng)新互聯(lián)公司是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷、企業(yè)網(wǎng)站建設(shè),友情鏈接,廣告投放為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。

其中的3個(gè)軟件是:

Elasticsearch 是個(gè)開源分布式搜索引擎,它的特點(diǎn)有:分布式,零配置,自動(dòng)發(fā)現(xiàn),索引自動(dòng)分片,索引副本機(jī)制, restful 風(fēng)格接口,多數(shù)據(jù)源,自動(dòng)搜索負(fù)載等。

Logstash 是一個(gè)完全開源的工具,他可以對(duì)你的日志進(jìn)行收集、分析,并將其存儲(chǔ)供以后使用(如,搜索)。

kibana 也是一個(gè)開源和免費(fèi)的工具,他 Kibana 可以為 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以幫助您匯總、分析和搜索重要數(shù)據(jù)日志

系統(tǒng)系統(tǒng)需要安裝的軟件ip描述
centos6.5Elasticsearch/test5192.168.253.210搜索存儲(chǔ)日志
centos6.5Elasticsearch/test4192.168.253.200搜索存儲(chǔ)日志
centos6.5Logstash/nginx/test1192.168.253.150用來收集日志給上面
centos6.5Kibana/nginx/test2192.168.253.100用來后端的展示

架構(gòu)原理圖:

(實(shí)際應(yīng)用)ELK環(huán)境搭建

一、先安裝elasticsearch集群,并測試通過再進(jìn)行其他軟件安裝。

在test5,test4上安裝分別安裝elasticsearch-2.3.3.rpm 前提要安裝java1.8 步驟如下:

yum remove java-1.7.0-openjdk

rpm -ivh jdk-8u51-linux-x64.rpm

java -version

yum localinstall elasticsearch-2.3.3.rpm -y

service elasticsearch start

cd /etc/elasticsearch/

vim elasticsearch.yml

(實(shí)際應(yīng)用)ELK環(huán)境搭建

(實(shí)際應(yīng)用)ELK環(huán)境搭建

修改如下配置

cluster.name: myelk  #設(shè)置集群的名稱,在一個(gè)集群里面都是這個(gè)名稱,必須相同

node.name: test5    #設(shè)置每一個(gè)節(jié)點(diǎn)的名,每個(gè)節(jié)點(diǎn)的名稱必須不一樣。

path.data: /path/to/data    #指定數(shù)據(jù)的存放位置,線上的機(jī)器這個(gè)要放到單一的大分區(qū)里面。

path.logs: /path/to/logs    #日志的目錄

bootstrap.mlockall: true  #啟動(dòng)最優(yōu)內(nèi)存配置,啟動(dòng)就分配了足夠的內(nèi)存,性能會(huì)好很多,測試我就不啟動(dòng)了。

network.host: 0.0.0.0  #監(jiān)聽的ip地址,這個(gè)表示所有的地址。

http.port: 9200      #監(jiān)聽的端口號(hào)

discovery.zen.ping.unicast.hosts: ["hostip", "hostip"]  #知道集群的ip有那些,沒有集群就會(huì)出現(xiàn)就一臺(tái)工作

mkdir -pv /path/to/{data,logs}

chown elasticsearch.elasticsearch /path -R

啟動(dòng)服務(wù)器  service elasticsearch start  并查看監(jiān)控端口啟動(dòng)

[root@test4 ~]# ss -tln

State       Recv-Q Send-Q                         Local Address:Port                           Peer Address:Port

LISTEN      0      128                                       :::54411                                                   :::*

LISTEN      0      128                                       :::111                                                       :::*

LISTEN      0      128                                        *:111                                                      *:*

LISTEN      0      50                                            :::9200                                                     :::*

LISTEN      0      50                                        :::9300                                                     :::*

LISTEN      0      128                                       :::22                                                           :::*

LISTEN      0      128                                        *:22                                                       *:*

LISTEN      0      128                                        *:51574                                                 *:*

LISTEN      0      128                                        127.0.0.1:631                                                *:*

LISTEN      0      128                                          ::1:631                                                      :::*

LISTEN      0      100                                          ::1:25                                                       :::*

LISTEN      0      100                                    127.0.0.1:25                                                    *:*

(實(shí)際應(yīng)用)ELK環(huán)境搭建

兩臺(tái)的配置都一樣就是上面的IP和note名稱要配置不一樣就行

(實(shí)際應(yīng)用)ELK環(huán)境搭建

(實(shí)際應(yīng)用)ELK環(huán)境搭建

[root@test5 ~]# ss -tln

State       Recv-Q Send-Q                         Local Address:Port                           Peer Address:Port

LISTEN      0      128                                        *:45822                                     *:*

LISTEN      0      128                                       :::39620                                    :::*

LISTEN      0      128                                       :::111                                      :::*

LISTEN      0      128                                        *:111                                       *:*

LISTEN      0      50                                        :::9200                                     :::*

LISTEN      0      50                                        :::9300                                     :::*

LISTEN      0      128                                       :::22                                       :::*

LISTEN      0      128                                        *:22                                        *:*

LISTEN      0      128                                127.0.0.1:631                                       *:*

LISTEN      0      128                                      ::1:631                                      :::*

LISTEN      0      100                                      ::1:25                                       :::*

LISTEN      0      100                                127.0.0.1:25                                        *:*

安裝插件 head和kopf 之后訪問 ip:9200/_plugin/head 和ip:9200/_plugin/kopf (插件可以圖形查看elasticsearch的狀態(tài)和刪除創(chuàng)建索引)

/usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf

/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

[root@test5]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf

-> Installing lmenezes/elasticsearch-kopf...

Trying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip ...

Downloading ......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE

Verifying https://github.com/lmenezes/elasticsearch-kopf/archive/master.zip checksums if available ...

NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)

Installed kopf into /usr/share/elasticsearch/plugins/kopf

[root@test5 ]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

-> Installing mobz/elasticsearch-head...

Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...

Downloading ........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE

Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...

NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)

Installed head into /usr/share/elasticsearch/plugins/head

(實(shí)際應(yīng)用)ELK環(huán)境搭建

(實(shí)際應(yīng)用)ELK環(huán)境搭建

二、安裝nginx和logstash軟件

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

nginx-1.8.1-1.el6.ngx.x86_64.rpm

logstash-2.3.3-1.noarch.rpm

jdk-8u51-linux-x64.rpm

在test1上安裝好nginx服務(wù) 就是收集它的日志呢

日志在/var/log/nginx/access.log

然后在test1上安裝logstash-2.3.3-1.noarch.rpm

yum remove java-1.7.0-openjdk

rpm -ivh jdk-8u91-linux-x64.rpm

rpm -ivh logstash-2.3.3-1.noarch.rpm  

/etc/init.d/logstash start  #啟動(dòng)服務(wù)

/opt/logstash/bin/logstash -e "input {stdin{}} output{stdout{ codec=>"rubydebug"}}"  #檢測環(huán)境  執(zhí)行這個(gè)命令檢測環(huán)境正常否,啟動(dòng)完成后 直接輸入東西就會(huì)出現(xiàn)

Settings: Default pipeline workers: 1

Pipeline main started

hello world

{

"message" => "hello world",

"@version" => "1",

"@timestamp" => "2017-05-24T08:04:46.993Z",

"host" => "0.0.0.0"

}

之后輸入/opt/logstash/bin/logstash -e 'input {stdin{}} output{ elasticsearch { hosts => ["192.168.253.200:9200"] index => "test"}}' 

 就是輸入東西到253.200的elasticsearch上 會(huì)在/path/to/data/myelk/nodes/0/indices 生成你名稱test索引文件目錄 可以多輸入幾個(gè)到253.200的目錄看看有沒有文件有就證明正常。

[root@test4 ~]# ls /path/to/data/myelk/nodes/0/indices/

test

之后在test1的/etc/logstash/conf.d 建立以.conf結(jié)尾的配置文件,我收集nginx就叫nginx.conf了內(nèi)容如下

[root@test1 nginx]# cd /etc/logstash/conf.d/

[root@test1 conf.d]# ls

nginx.conf

[root@test1 conf.d]# cat nginx.conf

input {

file {

type => "accesslog"

path => "/var/log/nginx/access.log"

start_position => "beginning"

}

}

output {

if [type] == "accesslog" {

elasticsearch  {

hosts => ["192.168.253.200"]

index => "nginx-access-%{+YYYY.MM.dd}"

}

}

}

/etc/init.d/logstash configtest

ps -ef |grep java

/opt/logstash/bin/logstash -f nginx.conf

之后在elasticearch查看有沒有索引生成。多訪問下nginx服務(wù)

如果沒有就修改這個(gè)文件

vi /etc/init.d/logstash

LS_USER=root    ###把這里換成root或者把訪問的日志加個(gè)權(quán)限可以讓logstash可以讀取它 重啟服務(wù)就會(huì)生成索引了

LS_GROUP=root

LS_HOME=/var/lib/logstash

LS_HEAP_SIZE="1g"

LS_LOG_DIR=/var/log/logstash

LS_LOG_FILE="${LS_LOG_DIR}/$name.log"

LS_CONF_DIR=/etc/logstash/conf.d

LS_OPEN_FILES=16384

LS_NICE=19

KILL_ON_STOP_TIMEOUT=${KILL_ON_STOP_TIMEOUT-0} #default value is zero to this variable but could be updated by user request

LS_OPTS=""

test4查看:

[root@test4 ~]# ls /path/to/data/myelk/nodes/0/indices/

nginx-access-2017.05.23  test

[root@test1 logstash]# cat logstash.log

{:timestamp=>"2017-05-24T16:05:19.659000+0800", :message=>"Pipeline main started"}

三、安裝kibana軟件

上面的都安裝完成后在test2上面安裝kibana

rpm -ivh kibana-4.5.1-1.x86_64.rpm

編輯配置文件在這里/opt/kibana/config/kibana.yml 就修改下面幾項(xiàng)就行

server.port: 5601  端口

server.host: "0.0.0.0"  監(jiān)聽

elasticsearch.url: "http://192.168.48.200:9200" elasticsearch地址

(實(shí)際應(yīng)用)ELK環(huán)境搭建

/etc/init.d/kibana start 啟動(dòng)服務(wù)

訪問kibana  http://ip:5601

(實(shí)際應(yīng)用)ELK環(huán)境搭建

添加展示的索引,就是在上面定義的  nginx-access-2016.07.03

(實(shí)際應(yīng)用)ELK環(huán)境搭建

(實(shí)際應(yīng)用)ELK環(huán)境搭建

(實(shí)際應(yīng)用)ELK環(huán)境搭建

(實(shí)際應(yīng)用)ELK環(huán)境搭建

配置kibana上面的收集Nginx日志的logstash

在kibana那臺(tái)服務(wù)器上面安裝logstash(按照之前的步驟安裝)

然后再logstash的/etc/logstash/conf.d/下面

寫一個(gè)配置文件:

[root@test2 conf.d]# vim nginx.conf

input {

file {

type => "accesslog"

path => "/var/log/nginx/access.log"

start_position => "beginning"

}

}

output {

if [type] == "accesslog" {

elasticsearch  {

hosts => ["192.168.253.200"]

index => "nginx-access-%{+MM.dd.YYYY}"

}

}

}

/opt/logstash/bin/logstash -f nginx.conf

查看Elasticsearch中多出了一個(gè)以月-日-年的Nginx訪問日志索引

[root@test4 ~]# ls /path/to/data/myelk/nodes/0/indices/

nginx-access-05.23.2017  nginx-access-2017.05.23  test

然后在kibana,瀏覽器上面按照之前的創(chuàng)建,生成一個(gè)新的日志文件

(實(shí)際應(yīng)用)ELK環(huán)境搭建

四、其他的一些配置。

kibana是直接訪問的比較不安全,我們需要用nginx訪問代理,并設(shè)置權(quán)限用戶名和密碼訪問

先在kibana服務(wù)器上安裝nginx 不介紹了

在nginx里面配置

#############################################################################

server

    {

            listen            80;

            server_name    localhost;

    auth_basic "Restricted Access";

            auth_basic_user_file /usr/local/nginx/conf/htpasswd.users;  #密碼和用戶

 

        location / {

            proxy_pass    http://localhost:5601;  #代理kibana的5601之后就可以直接80訪問了

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header REMOTE-HOST $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           

            }

}

#############################################################################

創(chuàng)建密碼和用戶文件:htpasswd.users 

需要安裝httpd-tool包先安裝它

 htpasswd -bc /usr/local/nginx/conf/htpasswd.users admin paswdadmin  #前面是用戶后面是密碼

(實(shí)際應(yīng)用)ELK環(huán)境搭建

之后通過訪問需要密碼和用戶并且是80端口了

(實(shí)際應(yīng)用)ELK環(huán)境搭建

本文標(biāo)題:(實(shí)際應(yīng)用)ELK環(huán)境搭建
轉(zhuǎn)載來源:http://bm7419.com/article8/ipoeop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、Google、自適應(yīng)網(wǎng)站網(wǎng)站制作、面包屑導(dǎo)航網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)