天天看點

Elasticsearch 叢集搭建實戰與踩坑指南(包括Elasticsearch安裝、開啟認證、開啟SSL安全連接配接、叢集通信配置)Elasticsearch 叢集搭建實戰與踩坑指南

Elasticsearch 叢集搭建實戰與踩坑指南

  • 産品:Elasticsearch
  • 版本:7.14.0
  • 環境:Centos7

文章目錄

  • Elasticsearch 叢集搭建實戰與踩坑指南
    • 前期準備
      • 版本選擇
      • 主機規劃
      • 安裝包下載下傳
      • 伺服器配置
    • 配置啟動
      • 解壓
      • 生成 Elastic 安全證書
      • 建立使用者
      • 修改配置檔案
      • 啟動主節點
      • 啟動資料節點
      • 檢查叢集狀态
    • 圖形化界面cerebro安裝
    • 附錄A:啟動失敗解決目錄
    • 關注公衆号,學習更多運維實戰案例!

前期準備

版本選擇

  • Elasticsearch v7.14.0

主機規劃

主機名 主機IP 角色 部署路徑
esmaster1 100.253.1.49 master /xswork
esnode1 100.253.232.24 data /xswork

安裝包下載下傳

# 進入工作目錄
cd /work
# 拉取安裝包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-linux-x86_64.tar.gz
           

伺服器配置

  • /etc/hosts

說明:配置後可用主機名代替IP位址,友善後續的配置與維護

# 最後一行添加
100.253.1.49 esmaster1
100.253.232.24 esnode1
           
  • /etc/security/limits.conf

說明:

1.啟動elasticsearch nofile 最少需要 65536

2.memlock 過小會導緻 elasticsearch 鎖定記憶體失敗,導緻無法啟動

# 修改最大打開檔案數
* soft nofile 65536
* hard nofile 65536

# 修改最大鎖定記憶體位址空間
* soft memlock unlimited
* hard memlock unlimited
           
- **/etc/sysctl.conf**
           

說明:最大虛拟記憶體大小預設為65530,elasticsearch 啟動需要 262144

配置啟動

解壓

# 進入工作目錄
cd /work
# 解壓
tar -zxvf elasticsearch-7.14.0-linux-x86_64.tar.gz
           

生成 Elastic 安全證書

在主節點執行如下語句生成證書

cd elasticsearch-7.14.0
./bin/elasticsearch-certutil ca

This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.

Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority

By default the 'ca' mode produces a single PKCS#12 output file which holds:
    * The CA certificate
    * The CA's private key

If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key

Please enter the desired output file [elastic-stack-ca.p12]:  # 回車即可
Enter password for elastic-stack-ca.p12 :                     # 回車即可
           

在上面我們接受預設的檔案名,并輸入一個自己熟悉的密碼(針對我的情況,我接受空)。我們在 Elasticsearch 的安裝目錄下,我們可以看見一個生産的證書檔案:elastic-stack-ca.p12

drwxr-xr-x.  2 elk elk   4096 7月  30 04:52 bin
drwxr-xr-x.  3 elk elk    260 8月  31 17:36 config
drwxrwxr-x.  4 elk elk     30 8月  25 18:51 data
-rw-------.  1 elk elk   2672 9月   1 14:21 elastic-stack-ca.p12
drwxr-xr-x.  9 elk elk    107 7月  30 04:52 jdk
drwxr-xr-x.  3 elk elk   4096 7月  30 04:52 lib
-rw-r--r--.  1 elk elk   3860 7月  30 04:47 LICENSE.txt
drwxr-xr-x.  2 elk elk   4096 8月  31 19:09 logs
drwxr-xr-x. 59 elk elk   4096 7月  30 04:53 modules
-rw-rw-r--.  1 elk elk   1414 8月  26 09:33 newfile.crt.pem
-rw-r--r--.  1 elk elk 615722 7月  30 04:51 NOTICE.txt
drwxr-xr-x.  2 elk elk      6 7月  30 04:51 plugins
-rw-r--r--.  1 elk elk   2710 7月  30 04:47 README.asciidoc
           

我們接着運作如下的指令來生成一個證書:

./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

Enter password for CA (elastic-stack-ca.p12) :                    # 回車即可
Please enter the desired output file [elastic-certificates.p12]:  # 回車即可
Enter password for elastic-certificates.p12 :                     # 使用生成elastic-stack-ca.p12時設定的密碼
           

上面的指令将使用我們的 CA 來生成一個證書:elastic-certificates.p12

drwxr-xr-x.  2 elk elk   4096 7月  30 04:52 bin
drwxr-xr-x.  3 elk elk    260 8月  31 17:36 config
drwxrwxr-x.  4 elk elk     30 8月  25 18:51 data
-rw-------.  1 elk elk   3596 9月   1 14:25 elastic-certificates.p12
-rw-------.  1 elk elk   2672 9月   1 14:21 elastic-stack-ca.p12
drwxr-xr-x.  9 elk elk    107 7月  30 04:52 jdk
drwxr-xr-x.  3 elk elk   4096 7月  30 04:52 lib
-rw-r--r--.  1 elk elk   3860 7月  30 04:47 LICENSE.txt
drwxr-xr-x.  2 elk elk   4096 8月  31 19:09 logs
drwxr-xr-x. 59 elk elk   4096 7月  30 04:53 modules
-rw-rw-r--.  1 elk elk   1414 8月  26 09:33 newfile.crt.pem
-rw-r--r--.  1 elk elk 615722 7月  30 04:51 NOTICE.txt
drwxr-xr-x.  2 elk elk      6 7月  30 04:51 plugins
-rw-r--r--.  1 elk elk   2710 7月  30 04:47 README.asciidoc
           

把上面的 elastic-certificates.p12 證書分别拷入到主、資料節點 Elasticsearch 安裝目錄下的 config 子目錄。

建立.pem 密鑰,供kibana、Beta使用

openssl pkcs12 -in elastic-stack-ca.p12 -out newfile.crt.pem -clcerts -nokeys
           

建立使用者

修改主節點配置檔案

vi ./config/elasticsearch.yml
#esmaster
cluster.name: ITcls
node.name: esmaster1

network.host: 0.0.0.0

discovery.seed_hosts: ["esmaster1", "esnode1"]
cluster.initial_master_nodes: ["esmaster1"]

path.data: /xswork/elasticsearch-7.14.0/data/data
path.logs: /xswork/elasticsearch-7.14.0/data/logs

node.master: true
node.data: true
node.ingest: true
node.ml: true
cluster.remote.connect: false

http.port: 29210
transport.tcp.port: 29310

http.cors.enabled: true
http.cors.allow-origin: "*" 

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 使用 elastic-agent時開啟
# xpack.security.authc.api_key.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
           

臨時啟動主節點

# 建立 data 目錄
mkdir data
./bin/elasticsearch
           

在使用之前,我們必須為它們設定密碼。在主節點 Elasticsearch 的目錄裡安裝打入如下的指令,按照提示設定内置使用者密碼,在這個過程中選擇你喜歡的密碼來設定:

./bin/elasticsearch-setup-passwords interactive
           

如果你喜歡一個随機的密碼,那麼你可以使用如下的方式來建立你自己密碼:

./bin/elasticsearch-setup-passwords auto
           

通過設定密碼後,就可以使用内置使用者登入 Elasticsearch ,内置使用者清單如下:

elastic :A built-in superuser. See Built-in roles. 
kibana  :The user Kibana uses to connect and communicate with Elasticsearch. 
logstash_system :The user Logstash uses when storing monitoring information in Elasticsearch. 
beats_system :The user the Beats use when storing monitoring information in Elasticsearch. 
apm_system :The user the APM server uses when storing monitoring information in Elasticsearch. 
remote_monitoring_user:The user Metricbeat uses when collecting and storing monitoring information in Elasticsearch. It has the remote_monitoring_agent and remote_monitoring_collector built-in roles. 
           

建立使用者成功之後測試登入成功後關閉主節點,進行下一步。

修改配置檔案

主節點

vi ./config/elasticsearch.yml
#esmaster
cluster.name: ITcls
node.name: esmaster1

network.host: 0.0.0.0

discovery.seed_hosts: ["esmaster1", "esnode1"]
cluster.initial_master_nodes: ["esmaster1"]

path.data: /xswork/elasticsearch-7.14.0/data/data
path.logs: /xswork/elasticsearch-7.14.0/data/logs

node.master: true
node.data: true
node.ingest: true
node.ml: true
cluster.remote.connect: false

http.port: 29210
transport.tcp.port: 29310

http.cors.enabled: true
http.cors.allow-origin: "*" 

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 使用 elastic-agent時開啟
# xpack.security.authc.api_key.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

xpack.security.http.ssl.enabled: true
xpack.security.authc.api_key.enabled: true
xpack.security.http.ssl.keystore.path: elastic-certificates.p12
xpack.security.http.ssl.truststore.path: elastic-certificates.p12
           

資料節點

cluster.name: ITcls
node.name: esnode1

network.host: 0.0.0.0

discovery.seed_hosts: ["esmaster1", "esnode1"]
cluster.initial_master_nodes: ["esmaster1"]

path.data: /xswork/elasticsearch-7.14.0/data/data
path.logs: /xswork/elasticsearch-7.14.0/data/logs

node.master: false
node.data: true
node.ingest: true
node.ml: true
cluster.remote.connect: false

http.port: 29210
transport.tcp.port: 29310

http.cors.enabled: true
http.cors.allow-origin: "*" 

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

xpack.security.http.ssl.enabled: true
xpack.security.authc.api_key.enabled: true
xpack.security.http.ssl.keystore.path: elastic-certificates.p12
xpack.security.http.ssl.truststore.path: elastic-certificates.p12
           

啟動主節點

./bin/elasticsearch -d
           

我們已經在配置檔案中配置https模式,在通路時需使用https,例如:https://100.253.1.49:29210。

啟動資料節點

# 建立 data 目錄
mkdir data
./bin/elasticsearch -d
           

檢查叢集狀态

curl --user youruser:yourpasswd -XGET 'https://100.253.1.49:29210/_cluster/health?pretty' -k

{
  "cluster_name" : "ITcls",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 31,
  "active_shards" : 61,
  "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
}
           

圖形化界面cerebro安裝

拉取壓縮檔案

wget https://github.com/lmenezes/cerebro/releases/download/v0.9.2/cerebro‐0.9.2.tgz
           

解壓

tar ‐zxvf cerebro‐0.9.2.tgz
           

修改配置

# 将端口修改為自定義端口
vi cerebro-0.9.4/conf/application.conf 
play {
  # Cerebro port, by default it's 9000 (play's default)
  server.http.port = 29220
}

           

啟動

浏覽器通路 http://ip位址:端口 測試,例如 http://100.253.1.49:29220

附錄A:啟動失敗解決目錄

#錯誤彙總以及對應的解決方法,生産環境下參數按實際情況而定
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
解決:修改檔案描述符大小
vi /etc/security/limits.conf 檔案後面加上 esuser為使用者,也可以使用*
# 任何使用者可以打開的最大的檔案描述符數量,預設1024,這裡的數值會限制tcp連接配接
# soft是一個警告值,而hard則是一個真正意義的閥值,超過就會報錯
# soft的限制不能比hard限制高
* soft nofile 65536
* hard nofile 65536

[2]: memory locking requested for elasticsearch process but memory is not locked
解決:鎖定記憶體失敗
vi /etc/security/limits.conf 檔案後面加上 *表示系統下的所有使用者
#最大鎖定記憶體位址空間,機關(KB)
* soft memlock unlimited
* hard memlock unlimited

[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解決:最大虛拟記憶體太小
vi /etc/sysctl.conf 添加
vm.max_map_count=888888
最後執行指令 sysctl ‐p

[4]: max ERROR: bootstrap checks failed
修改elasticsearch.yml配置檔案,允許外網通路。
vim config/elasticsearch.yml
增加 network.host: 0.0.0.0

[5]: other 啟動使用者不能是root使用者,且關閉機器節點間的防火牆
防火牆關閉方式:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
firewall‐cmd ‐‐state #檢視預設防火牆狀态(關閉後顯示notrunning,開啟後顯示running)
           

關注公衆号,學習更多運維實戰案例!

Elasticsearch 叢集搭建實戰與踩坑指南(包括Elasticsearch安裝、開啟認證、開啟SSL安全連接配接、叢集通信配置)Elasticsearch 叢集搭建實戰與踩坑指南

繼續閱讀