centos7下安裝配置elk 7.2.1版本教程

ELK簡介

官網地址:https://www.elastic.co/cn/

官網權威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

 

ELKElasticsearchLogstashKibana的簡稱,這三者是核心套件,但並非全部。

Elasticsearch是實時全文搜索和分析引擎,提供蒐集、分析、存儲數據三大功能;是一套開放RESTJAVA API等結構提供高效搜索功能,可擴展的分佈式系統。它構建於Apache Lucene搜索引擎庫之上。

Logstash是一個用來蒐集、分析、過濾日誌的工具。它支持幾乎任何類型的日誌,包括系統日誌、錯誤日誌和自定義應用程序日誌。它可以從許多來源接收日誌,這些來源包括 syslog、消息傳遞(例如 RabbitMQ)和JMX,它能夠以多種方式輸出數據,包括電子郵件、websocketsElasticsearch

Kibana是一個基於Web的圖形界面,用於搜索、分析和可視化存儲在 Elasticsearch指標中的日誌數據。它利用ElasticsearchREST接口來檢索數據,不僅允許用戶創建他們自己的數據的定製儀表板視圖,還允許他們以特殊的方式查詢和過濾數據

部署規劃

計劃在三臺CentOS7機器上部署ELK,其中一臺機器作爲ELK的服務節點,IP192.168.1.185;另外兩臺作爲客戶節點,IP192.168.1.186/187
其中服務節點部署ElasticsearchLogstashKibana三個組件,客戶節點部署Logstash
https://images2015.cnblogs.com/blog/1181869/201706/1181869-20170615170755587-1541111347.jpg

服務節點部署

環境準備

默認root用戶下操作,其他用戶請在命令前添加sudo

  1. 安裝JDK
 

yum -y install java-1.8.0-openjdk

2、關閉防火牆。

 

systemctl stop firewalld
systemctl disable firewalld

或者設置防火牆規則:



 

firewall-cmd --add-port=9200/tcp --permanent
firewall-cmd --add-port=9300/tcp --permanent
firewall-cmd --add-port=5601/tcp --permanent
firewall-cmd --reload

3、添加ELK倉庫









 

cat > /etc/yum.repos.d/elasticsearch.repo <<EOF
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=0
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF

5、更新yum

 

yum clean all
yum makecache

Elasticsearch

  1. 安裝Elasticsearch
    yum install -y elasticsearch

如果yum 下載慢,使用下面安裝方法上傳完成後,執行安裝:
wget –c  https://mirrors.huaweicloud.com/elasticsearch/7.2.1/elasticsearch-7.2.1-x86_64.rpm

rpm -ivh elasticsearch-7.2.1-x86_64.rpm
2、確認Elasticsearch的安裝信息
# rpm -qi elasticsearch

Name        : elasticsearch
Epoch       : 0
Version     : 7.2.1
Release     : 1
Architecture: x86_64
Install Date: Thu 11 Jun 2020 10:16:45 AM CST
Group       : Application/Internet
Size        : 535521434
License     : Elastic License
Signature   : RSA/SHA512, Thu 25 Jul 2019 03:39:33 AM CST, Key ID d27d666cd88e42b4
Source RPM  : elasticsearch-7.2.1-1-src.rpm
Build Date  : Thu 25 Jul 2019 02:08:59 AM CST
Build Host  : packer-virtualbox-iso-1559162487
Relocations : /usr 
Packager    : Elasticsearch
Vendor      : Elasticsearch
URL         : https://www.elastic.co/
Summary     : Distributed RESTful search engine built for the cloud
Description :
Reference documentation can be found at
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
and the 'Elasticsearch: The Definitive Guide' book can be found at
https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html

3、啓動Elasticsearch並設置開機啓動

systemctl daemon-reload
systemctl start elasticsearch
systemctl enable elasticsearch.service


4、查看Elasticsearch運行狀態
 

systemctl status elasticsearch
ps -ef | grep elasticsearch
netstat -nlpt

5、嘗試請求
# curl localhost:9200

 

{
  "name" : "node-1",
  "cluster_name" : "my-application",
  "cluster_uuid" : "zxkahIszSWahBps7ozbGEg",
  "version" : {
    "number" : "7.2.1",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "fe6cb20",
    "build_date" : "2019-07-24T17:58:29.979462Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

6、設置允許其他機器訪問
當前只能響應本機的請求,想要其他機器也能訪問的話,那麼需要修改elasticsearch的配置。

vim /etc/elasticsearch/elasticsearch.yml,如下修改:









 

# line 17, uncomment
cluster.name: my-application
# line 22, uncomment and change
node.name: node-1
# line 55, uncomment and change
network.host: 0.0.0.0
# line 59, uncomment
http.port: 9200
# line 72, uncomment and change
cluster.initial_master_nodes: ["node-1", "node-2"]

7、重啓Elasticsearch



 

systemctl stop elasticsearch
systemctl start elasticsearch
# 啓動後稍等一會
netstat -nlpt

如果可以看到:::9200,就可以通過外部瀏覽器訪問Elasticsearch服務了,至此Elasticsearch安裝配置完成。

Kibana

1、安裝Kibana
yum install -y kibana
同樣的,如果下載速度緩慢,那麼可以通過瀏覽器下載 https://mirrors.huaweicloud.com/kibana/7.2.1/kibana-7.2.1-x86_64.rpm ,然後上傳到服務節點,上傳後執行安裝:
rpm -ivh kibana-7.2.1-x86_64.rpm

2、確認Kibana的安裝信息
rpm -qi kibana

Name        : kibana
Version     : 7.2.1
Release     : 1
Architecture: x86_64
Install Date: Thu 11 Jun 2020 10:44:13 AM CST
Group       : default
Size        : 557832921
License     : Elastic License
Signature   : RSA/SHA512, Thu 25 Jul 2019 03:42:35 AM CST, Key ID d27d666cd88e42b4
Source RPM  : kibana-7.2.1-1.src.rpm
Build Date  : Thu 25 Jul 2019 02:35:36 AM CST
Build Host  : packer-virtualbox-iso-1559162487
Relocations : / 
Packager    : Kibana Team <[email protected]>
Vendor      : Elasticsearch, Inc.
URL         : https://www.elastic.co
Summary     : Explore and visualize your Elasticsearch data
Description :
Explore and visualize your Elasticsearch data

3、啓動Kibana並設置開機啓動

 

systemctl start kibana
systemctl enable kibana

4、查看Kibana運行狀態


 

systemctl status kibana
ps -ef | grep kibana
netstat -nlpt

5、測試訪問
# curl localhost:5601 -L

 

6、設置允許其他機器訪問
# vim /etc/kibana/kibana.yml,如下修改:





 

# line 2, uncomment and change
server.port: 5601
# line 7, uncomment and change
server.host: "0.0.0.0"
# line 28, uncomment
elasticsearch.hosts: ["http://localhost:9200"]

7、重啓Kibana



 

systemctl stop kibana
systemctl start kibana
# 啓動後稍等一會
netstat -nlpt

如果可以看到0.0.0.0:5601,就可以通過外部瀏覽器訪問Kibana服務了,至此Kibana安裝配置完成。
http://cdn.voidking.com/@/imgs/centos7-install-elk/welcome.jpg?imageView2/0/w/800

Logstash

1、安裝Logstash
yum install -y logstash
也可以通過瀏覽器下載 https://mirrors.huaweicloud.com/logstash/7.2.1/logstash-7.2.1.rpm ,然後上傳到服務節點,上傳後執行安裝:
rpm -ivh logstash-7.2.1.rpm

2、確認Logstash的安裝信息
# rpm -qi logstash

Name        : logstash
Epoch       : 1
Version     : 7.2.1
Release     : 1
Architecture: noarch
Install Date: Thu 11 Jun 2020 10:50:36 AM CST
Group       : default
Size        : 299656320
License     : Elastic License
Signature   : RSA/SHA512, Thu 25 Jul 2019 03:43:37 AM CST, Key ID d27d666cd88e42b4
Source RPM  : logstash-7.2.1-1.src.rpm
Build Date  : Thu 25 Jul 2019 03:26:03 AM CST
Build Host  : packer-virtualbox-iso-1559162487
Relocations : / 
Packager    : <vagrant@packer-virtualbox-iso-1559162487>
Vendor      : Elasticsearch
URL         : http://www.elasticsearch.org/overview/logstash/
Summary     : An extensible logging pipeline
Description :
An extensible logging pipeline

3、啓動Logstash

 

systemctl start logstash

4、查看Logstash運行狀態


 

systemctl status logstash
ps -ef | grep logstash
netstat -nlpt

啓動成功了,然後呢?怎麼使用?

5Logstash hello world

systemctl stop logstash
/usr/share/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'

輸入啓動命令後,耐心等待服務啓動,直到出現Successfully started Logstash API endpoint。輸入“hello world”回車,即可打印出輸出。


logstash通過管道來處理數據,標準的管道包含inputfilteroutput。以上命令,指定了一個管道的參數,沒有filterinput是控制檯標準輸入,output是控制檯標準輸出。

6、管道配置寫入文件
在當前用戶目錄,新建配置文件std.conf,內容爲:





 

input {
    stdin { }
}
output {
    stdout {}
}

7、測試配置文件並啓動

 

/usr/share/logstash/bin/logstash --path.settings /etc/logstash -f ~/std.conf

8、從文件中讀取信息
1)新建/usr/local/test.log文件,內容爲:

 

hello logstash!

2)新建test.conf,內容爲:













 

input {
    file {
        path => ["/usr/local/test.log"]
        sincedb_path => "/dev/null"
        start_position => "beginning"
    }
}
filter {
}
output {
    stdout {
        codec => rubydebug
    }
}

3)啓動Logstash
/usr/share/logstash/bin/logstash --path.settings /etc/logstash -f ~/test.conf
 

9、寫入信息到文件
1)修改test.conf爲:













 

input {
    file {
        path => ["/usr/local/test.log"]
        sincedb_path => "/dev/null"
        start_position => "beginning"
    }
}
filter {
}
output {
    file {
        path => ["/usr/local/test.log.out"]
    }
}

2)啓動Logstash
/usr/share/logstash/bin/logstash --path.settings  -f ~/test.conf
啓動後,/usr/local/目錄下多了test.log.out文件。

數據傳遞

Logstash收集到了數據,怎樣傳遞給Elasticsearch顯示?怎樣傳遞給Kibana顯示?
1、修改test.conf爲:













 

input {
    file {
        path => ["/usr/local/test.log"]
        sincedb_path => "/dev/null"
        start_position => "beginning"
    }
}
filter {
}
output {
    elasticsearch {
        hosts => ["http://localhost:9200"]
    }
}

2、啓動Logstash
/usr/share/logstash/bin/logstash -f ~/test.conf

3、查看索引
瀏覽器訪問 http://192.168.1.185:9200/_cat/indices?v
 

其中有個索引是logstash的,這就是我們想要查看的數據索引。

4、查看數據
瀏覽器訪問 http://192.168.1.185:9200/logstash-2020.06.11-000001/_search
 
看到了hello logstash!,說明數據已經成功傳遞到了Elasticsearch。鏈接後添加?pretty參數,可以進行格式化顯示。

5Kibana添加indices
瀏覽器訪問Kibana http://192.168.1.185:5601
 

點擊頁面上的Logs,---》數據源按鈕。

 

然後填寫indices相關信息 , 最後點擊Update Source,即可在頁面上看到Logstash傳遞的信息。


  

6、修改test.log
修改test.log爲:

 

hello logstash!
the log has been updated.

再次查看Kibana的日誌顯示,可以看到更新了一條數據。
 

 

客戶節點部署

環境準備

默認root用戶下操作,其他用戶請添加sudo

  1. 安裝JDK

yum -y install java-1.8.0-openjdk

2、關閉防火牆。

 

systemctl stop firewalld
systemctl disable firewalld

3、添加ELK倉庫









 

cat > /etc/yum.repos.d/elasticsearch.repo <<EOF
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF

4、更新yum

 

yum clean all
yum makecache

Logstash

1、安裝Logstash
yum install -y logstash
也可以通過瀏覽器下載 wget https://mirrors.huaweicloud.com/logstash/7.2.1/logstash-7.2.1.rpm ,然後上傳到服務節點,上傳後執行安裝:
rpm -ivh logstash-7.2.1.rpm

2、確認Logstash的安裝信息
rpm -qi logstash
 

3、創建測試文件
新建配置文件test.conf,內容爲:













 

input {
    file {
        path => ["/usr/local/test.log"]
        sincedb_path => "/dev/null"
        start_position => "beginning"
    }
}
filter {
}
output {
    elasticsearch {
        hosts => ["http://192.168.1.185:9200"]
    }
}

新建/usr/local/test.log,內容爲:

 

this is a log recorded by 186

4、啓動logstash

 

/usr/share/logstash/bin/logstash -f ~/test.conf

5、在Kibana查看日誌
刷新Kibana,即可看到客戶節點的日誌。
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章