Elasticsearch Cluster 搭建教程

教程所使用的搭建環(huán)境

  • Mac OS 10.14.6
  • VirtualBOx 6.0.12
  • Ubuntu image: ubuntu-18.04.3-live-server-amd64.iso
  • Elasticsearch 7.5.2
  • Logstash 7.5.0
  • Kibana 7.5.0
  • Curator 5.8.1
  • NFS

搭建步驟概要

  • 創(chuàng)建虛擬機
  • 安裝并配置Elassticsearch集群
  • 安裝Logstash并配置pipeline
  • 安裝Kibana并配置
  • 安裝NFS和Curator并配置日志備份

Mac OS上安裝 Virtualbox 的步驟比較簡單,這里省略了

創(chuàng)建虛擬機

我一共創(chuàng)建了4臺虛擬機,Elasticsearch 集群三臺(一臺master node, 兩臺data node),最后一臺作為Logstash, Kibana, NFS 的載體。

Ubuntu 虛擬機下載地址: http://releases.ubuntu.com/18.04/

創(chuàng)建虛擬機教程: https://hibbard.eu/install-ubuntu-virtual-box/

這個過程中唯一要注意的是,在中國大陸,在安裝過程中Mirror adress請?zhí)顚?code>http://mirrors.aliyun.com/ubuntu,這樣在安裝過程中或者之后的一些使用中才能有較好的網(wǎng)絡速度下載文件,否則很可能安裝系統(tǒng)失敗。

接著是配置虛擬機網(wǎng)絡以保證幾臺虛擬機之間能相互訪問,請參考一下文檔:

https://www.thomas-krenn.com/en/wiki/Network_Configuration_in_VirtualBox

https://www.nakivo.com/blog/virtualbox-network-setting-guide/

基本上在安裝ELK集群過程中,你最好使用NAT Network,它能保證各個虛擬機之間既能相互訪問又可以擁有最好的網(wǎng)絡下載速度。并且在下載完成以后,將所有節(jié)點的網(wǎng)絡切換成 Bridge Adapter,并選擇 en0: Wifi(Wireless),這樣在Mac OS上才能通過 ssh 訪問虛擬機網(wǎng)絡?。?!

創(chuàng)建好以后使用ifconfig 查看IP地址, 并用ping檢查幾臺虛擬機之間的連通性。這里我的四臺機器的地址分別是:

192.168.0.106 //master node
192.168.0.110 //data node 1
192.168.0.111 //data node 2
192.168.0.112 //logstash and kibana node

為了方便遠程登錄操作,請安裝 openssh apt-get install openssh-server

安裝并配置Elassticsearch集群

yum install java-1.8.0-openjdk -y      //最好java 8, logstash跟高版本的 Java 之間存在一些兼容性問題
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-x86_64.rpm
rpm --install elasticsearch-7.5.2-x86_64.rpm --nodeps
systemctl daemon-reload
systemctl enable elastcisearch
systemctl start elastcisearch

配置 master 節(jié)點 /etc/elasticsearch/elasticsearch.yml

cluster.name: my-cluster
node.name: master-node
node.data: false
node.master: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
path.repo: /var/nfs/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.zen.minimum_master_nodes: 1
discovery.seed_hosts: ["192.168.0.106", "192.168.0.110", "192.168.0.111"]
cluster.initial_master_nodes: ["192.168.0.106"]

xpack.monitoring.enabled: true
xpack.monitoring.collection.enabled: true
xpack.monitoring.collection.interval: 10m
xpack.monitoring.elasticsearch.collection.enabled: true

配置 data 節(jié)點 /etc/elasticsearch/elasticsearch.yml

cluster.name: my-cluster
node.name: data-node-1
node.master: false
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
path.repo: /var/nfs/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: [ "192.168.0.106" ]

xpack.monitoring.enabled: true
xpack.monitoring.collection.enabled: true
xpack.monitoring.collection.interval: 10m
xpack.monitoring.elasticsearch.collection.enabled: true

重啟三臺elasticsearch節(jié)點,訪問master節(jié)點檢查集群是否健康:

curl 192.168.0.106:9200/_cluster/health?pretty

會得到如下結(jié)果:

{
  "cluster_name" : "my-cluster",
  "status" : "green",  
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 2,
  "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
}

查看 elasticsearch 日志: tail -F /usr/share/elasticsearch/my-cluster.log

設置 elasticsearch 賬號密碼( 每個賬號用處不一樣,elastic 賬號權(quán)限最高):

/usr/share/elasticsearch/bin/elasticsearch-setup-password  interactive

在所有節(jié)點的 /etc/elasticsearch/elasticsearch.yml中添加以下配置啟用 security :

xpack.security.enabled: true

如果在集群內(nèi)要啟用 https 傳輸,需要為每個節(jié)點生成證書,過程中所有選項可以保持默認直接按回車鍵

/usr/share/elasticsearch/bin/elasticsearch-certutil  --multiple

解壓生成的證書文件放到各個節(jié)點的自定義目錄中,并配置到 elasticsearch.yml中:

xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/master-node.p12
xpack.security.transport.ssl.truststore.path: certs/master-node.p12

此時,你需要使用賬號密碼才能訪問API:

curl -u username:password 192.168.0.106:9200/_cluster/health?pretty

安裝Logstash并配置pipeline

wget https://artifacts.elastic.co/downloads/logstash/logstash-7.5.0.rpm
rpm --install logstash-7.5.0.rpm --nodeps
systemctl daemon-reload
systemctl enable logstash

配置 /etc/logstash/conf.d/pipeline.conf:

input {
  udp {
    port => 12200
    codec => json
  }
}
filter {
# 可以暫時不做任何配置
}
output {
  elasticsearch {
    hosts => "192.168.0.106"
    index => "logstash-%{+YYYY.MM.dd}"
    user => elastic
    password => password
  }
}

然后啟動 log stash : systemctl start logstash

如果要查看logstash的日志,請使用命令 journalctl -fu log stash

安裝Kibana并配置

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.6.0-x86_64.rpm
rpm --install kibana-7.6.0-x86_64.rpm --nodeps
systemctl daemon-reload
systemctl enable kibana

配置 /etc/kibana/kibana.yml:

elasticsearch.hosts: ["http://192.168.0.106:9200"]
elasticsearch.username: "elastic"
elasticsearch.password: "password"
xpack.monitoring.enabled: true
xpack.monitoring.kibana.collection.enabled: true
xpack.monitoring.min_interval_seconds: 600   (單位是秒)
xpack.monitoring.kibana.collection.interval: 600000  (單位是毫秒)
xpack.monitoring.elasticsearch.hosts: ["http://192.168.0.106:9200"]
xpack.monitoring.elasticsearch.username: "elastic"
xpack.monitoring.elasticsearch.password: "password"

啟動 kibana 并查看日志:

systemctl start kibana
tail -F /var/log/kibana/kibana.log

到這里為止,基本上集群已經(jīng)搭建完畢!

但這里我們所有的東西都是安裝在虛擬機內(nèi)部,如何才能在Mac OS上訪問 Kibana呢?

在Mac OS上打開終端:

ssh username@192.168.0.112 -L 5601:localhost:5601

這樣在Mac OS 上打開瀏覽器,訪問 localhost:5601 輸入賬號密碼即可訪問 Kibana! 階段性成功,恭喜~

image-20200214130558898.png

安裝NFS和Curator并配置日志備份

在生產(chǎn)環(huán)境,往往我們的系統(tǒng)會產(chǎn)生大量的日志,所以定期備份清理日志是非常有必要的,而讓這個過程自動化也是值得去做的一件事情。

備份日志我們可以通過為index創(chuàng)建snapshot, 然后刪除index即可。但是我們需要一個地方存儲 snapshot,這里我使用 NFS server,備份工具我使用 elasticsearch-curator。

192.168.0.112節(jié)點安裝配置NFS,作為 snapshot 備份服務器:

apt-get update
apt-get install nfs-kernel-server
mkdir /var/nfs/elasticsearch
chown -R nobody:nogroup /var/nfs/elasticsearch

編輯 /etc/exports, 并加入以下這一行代碼:

/var/nfs/elasticsearch   192.168.0.106(rw,sync,all_squash,no_subtree_check) 192.168.0.110(rw,sync,all_squash,no_subtree_check) 192.168.0.111(rw,sync,all_squash,no_subtree_check)

然后

exportfs -a
service nfs-kernel-server start

在所有 elasticsearch 節(jié)點安裝 NFS client

apt-get update
apt-get install nfs-common
mkdir /var/nfs/elasticsearch
mount 192.168.0.112:/var/nfs/elasticsearch /var/nfs/elasticsearch

編輯 /etc/fstab 加入這一行代碼(這一步似乎可以省略):

192.168.0.112:/var/nfs/elasticsearch /var/nfs/elasticsearch nfs auto,noatime,nolock,bg,nfsvers=4,sec=krb5p,intr,tcp,actimeo=1800 0 0

編輯所有 elasticsearch節(jié)點的 /etc/elasticsearch/elasticsearch.yml加入:

path.repo: /var/nfs/elasticsearch

此時重啟所有節(jié)點即可!

你還可以使用 Samba服務器或者Azure Blob或者 AWS S3等選項作為備份存儲空間。

在創(chuàng)建 snapshot 之前,先創(chuàng)建 repository 了:

curl -XPUT 'http://192.168.0.106:9200/_snapshot/my_backup' -d
'{
  "type": "fs",
  "settings": {
    "location": "/var/nfs/elasticsearch",
    "compress": true
  }
}'

接著可以為你的 index創(chuàng)建 snapshot了:

curl -XPUT 'http://192.168.0.106:9200/_snapshot/my_backup/logstash-2020.02.07?wait_for_completion=true' -d
'{
  "indices": "logstash-2020.02.07",
  "ignore_unavailable": true,
  "include_global_state": false
}'

安裝并配置 elasticsearch-curator

這里為了安裝最新版本 curator,我使用 pip 作為安裝工具!

apt-get install python-pip
pip install elasticsearch-curator

創(chuàng)建配置文件 /etc/curator/config.yml:

---
client:
  hosts:
    - localhost
  port: 9200
  master_only: true
  url_prefix:
  use_ssl: false
  certificate:
  client_cert:
  client_key:
  ssl_no_validate: False
  http_auth: elastic:password
  timeout: 60
logging:
  loglevel: INFO

創(chuàng)建配置文件 /etc/curator/action_file.yml:

---
actions:
  1:
    action: snapshot
    description: create snapshot for indices older than 30 days
    options:
      repository: my_backup
      name: logstash-%Y.%m.%d
      ignore_unavailable: False
      include_global_state: True
      partial: False
      continue_if_exception: false
      wait_for_completion: True
      skip_repo_fs_check: False
      disable_action: false
    filters:
    - filtertype: pattern
      kind: prefix
      value: logstash-
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 0
  2:
    action: delete_indices
    description: Delete indices older than 30 days.
    options:
      ignore_empty_list: true
      continue_if_exception: false
      disable_action: false
    filters:
    - filtertype: pattern
      kind: prefix
      value: logstash-
      exclude:
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 0

設置定時任務 crontab -e,每天凌晨3點啟動任務:

0 3 * * * /usr/local/bin/curator --config /etc/curator/config.yml /etc/curator/action_file.yml > /home/yanghai/crontab.log 2>&1

大功告成,一個 ELK 集群就搭建完成了!

你可以使用echo -n '{"message": "Hello World"}' | nc -4u -w1 192.168.0.112 12200 向 logstash 發(fā)送消息,很快你就可以在 logstash 里面看到日志!

后記:

ELK 集群搭建有很多細節(jié)地方容易出錯,如果在搭建過程中遇到任何問題,請聯(lián)系微信 jiangyanghai!
elasticsearch 提供了API用于對所有資源進行操作配置,數(shù)量龐大,請多翻閱官方文檔或者下載 《Elasticsearch cookbook》一書。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容