ElasticSearch集群的部署
在分布式系統中,應用數量眾多,應用調用鏈復雜,常常使用ELK作為日志收集、分析和展示的組件。本篇文章將講講解如何部署ELK,然后講解如何 使用Filebeat采集Spring Boot的日志輸出到Logstash上,logstash再將日志輸出到Elasticsearch上,最后展示到kibana上面。整個日志采集流程如下圖:
在傳統的日志采集只會用ELK,那么為什么需要使用filebeat呢,因為 logstash是java應用,解析日志是非的消耗cpu和內存,logstash安裝在應用部署的機器上顯得非常的影響應用的性能。最常見的做法是用filebeat部署在應用的機器上,logstash單獨部署,然后由 filebeat將日志輸出給logstash解析,解析完由logstash再傳給elasticsearch。
1.安裝計划
本文主要講解如何部署ElasticSearch集群,部署的ElasticSearch的版本為7.3,計划用三台機器組成一個ElasticSearch集群,從而組成高可用,機器分配如下:
節點 | 規則 | 數量 |
---|---|---|
192.168.1.1 | 2核4G | 1 |
192.168.1.2 | 2核4G | 1 |
192.168.1.3 | 2核4G | 1 |
或者使用tar.gz包的方式直接解壓縮,多復制幾份,修改其中的端口號,從而達到再一台主機上構建多個node節點
2.安裝
下載安裝執行以下命令:
# 下載elasticsearch-7.2.0-x86_64的rpm包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.3.0-x86_64.rpm
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.3.0-x86_64.rpm.sha512
# shasum 檢查版本信息
shasum -a 512 -c elasticsearch-7.3.0-x86_64.rpm
# rpm本地安裝
rpm --install elasticsearch-7.3.0-x86_64.rpm
Creating elasticsearch group... OK
Creating elasticsearch user... OK
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
sudo systemctl start elasticsearch.service
Created elasticsearch keystore in /etc/elasticsearch
安裝成功ElasticSearch成功后,執行一下命令啟動elasticSearch,並設置為開啟自啟動:
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
elasticSearch的默認端口為9200,啟動成功后,執行以下命令:
curl -X GET "localhost:9200/"
如果返回以下的信息,則證明安裝成功:
{
"name" : "VM_0_5_centos",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "gst98AuET6a648YmAkXyMw",
"version" : {
"number" : "7.3.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "508c38a",
"build_date" : "2019-06-20T15:54:18.811730Z",
"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"
}
查看節點的健康狀態,執行命令 curl localhost:9200/_cluster/health
,如果返回以下信息,則Elasticsearch則為監控狀態。
{
"cluster_name" : "elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
可以執行以下的命令,查看es的 journal:
sudo journalctl --unit elasticsearch
3.配置文件和路徑
以下的路徑的配置文件可以配置es的java_home,es_config_home :
/etc/sysconfig/elasticsearch
es本身的一些配置在以下的路徑,在這里可以配置elasticsearch的堆內存,數據保留天數等信息:
/etc/elasticsearch
所有的配置文件描述和路徑如下表所示:
配置類型 | 描述 | 路徑 |
---|---|---|
home | elasticsearch的home目錄 | /usr/share/elasticsearch |
bin | elasticsearch的bin目錄 | /usr/share/elasticsearch/bin |
conf | elasticsearch的配置文件 | /etc/elasticsearch |
conf | elasticsearch的環境變量配置 | /etc/sysconfig/elasticsearch |
data | elasticsearch的數據目錄 | /var/lib/elasticsearch |
logs | elasticsearch的日志目錄 | /var/log/elasticsearch |
plugins | elasticsearch的插件目錄 | /usr/share/elasticsearch/plugins |
4.集群搭建
在三台機器上分別裝完elasticsearch,在主節點vim /etc/elasticsearch/
,配置一下的信息:
主節點的配置如下:
#三個集群需要同樣的集群名
cluster.name: my-application
# 每個node的名字需要唯一
node.name: node-1
# 開啟這個表示該節點能夠參與選舉主節點,並不表示這個就是主節點
node.master: true
#允許該節點存儲數據(默認開啟)
node.data: true
#注意一定要是路徑后面加上/var/lib/elasticsearch/nodes,要不然無法加入集群
path.data: /var/lib/elasticsearch/nodes
path.logs: /var/log/elasticsearch
# 有內網IP的話可以配置只監聽本機127.0.0.1的IP
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: false
# 注意:地址的話可以配置內網IP地址
discovery.seed_hosts: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
cluster.initial_master_nodes: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
同理從節點的配置:
cluster.name: my-application
# 每個node的名字需要唯一,兩個從節點的名字不能相同
node.name: node-2
node.master: true
#允許該節點存儲數據(默認開啟)
node.data: true
path.data: /var/lib/elasticsearch/nodes
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
xpack.security.enabled: false
http.cors.enabled: true
http.cors.allow-origin: "*"
discovery.seed_hosts: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
cluster.initial_master_nodes: ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
配置完主從節點,需要重啟三台elasticsearch,重啟命令如下:
systemctl restart elasticsearch.service
重啟三台Elasticsearch后,執行curl http://localhost:9200/_cat/master
,如果響應如下,則證明安裝成功
注意:需要防火牆放行9200和9300端口,或者關閉防火牆
查看主節點
# 表示的意思是從這三台節點選取node-1為主節點
SHBUDVUAQhi7FauSoI8bMg 192.168.1.1 192.168.1.1 node-1
也可執行命令curl -X GET http://127.0.0.1:9200/_cat/nodes?pretty'
,返回類型如下的數據則安裝成功:
查看集群所有節點信息
192.168.1.2 9 97 1 0.00 0.03 0.05 mdi - node-2
192.168.1.3 97 0 0.01 0.07 0.07 mdi - node-3
192.168.1.1 18 97 2 0.05 0.05 0.05 mdi * node-1 # 標星號的為主節點
也可以通過命令curl localhost:9200/_cluster/health?pretty
,查看集群的健康狀態:
{
"cluster_name" : "my-application",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 5,
"active_shards" : 10,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
Elasticsearch中信息很多,同時ES也有很多信息查看命令,可以幫助開發者快速查詢Elasticsearch的相關信息。
查看命令幫助信息
$ curl localhost:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
5.head安裝
在elasticsearch的高級版本之后,head不在是一個插件,而是一個獨立的應用來部署,源碼地址在https://github.com/mobz/elasticsearch-head。
注意,使用elasticsearch-head的話要求elasticsearch集群開啟外網訪問才行,否則無法連接到集群。
若是elasticsearch集群部署到內網使用,沒有使用外網IP,則無法使用elasticsearch-head連接集群進行管理
暫未找到解決辦法。。。
需要提前安裝node,安裝命令如下:
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install (yum install npm)
npm run start
安裝成功后,打開 http://localhost:9100/,瀏覽器顯示如下的界面,則安裝成功:
如上圖所示,在Head組件的界面上,可以很方便的查看集群的狀態和數據。