如何使用 Ansible 設定具有叢集的生產 Elasticsearch 伺服器


在本文中,我們將學習如何使用 Ansible 在 CentOS7 上配置和安裝生產 Elasticsearch 叢集,以確保 Elasticsearch 節點免受外部網路的攻擊。我們將使用 VPN 服務連線到叢集。

Elasticsearch 是一款非常流行的開源搜尋伺服器,可用於即時分散式搜尋和資料分析,以提高效能、穩定性和可擴充套件性,我們需要將 Elasticsearch 作為叢集部署在多臺伺服器上。

先決條件

我們需要至少三個具有私有網路的 CentOS 7 伺服器環境,因為所需的 Elasticsearch 叢集為 3 個主節點,如果我們計劃使用一個主節點和一個數據節點,那麼我們還需要為資料節點新增額外的伺服器。請確保我們擁有唯一的 Ansible 清單主機名。

假設我們已經配置了 VPN 伺服器,並且能夠使用 VPN 連線到每臺伺服器,並且介面名稱為“tun0”,或者我們可以更改 Elasticsearch 在不同介面上的監聽,我們可以在 site.yml 檔案中進行相應的更改。

下載 Ansible-Elasticsearch Playbook

Elasticsearch 提供了一個 Ansible 角色,可用於設定 Elasticsearch 叢集,為此,我們需要將其新增到我們的 ansible-tinc playbook 中,並定義主機組並將適當的角色分配給這些組。

轉到伺服器中的 ansible-tinc 資料夾

# cd ~/ansible-tinc

我們需要克隆 Elastic 的 Github 儲存庫中的 ansible-elasticsearch 角色

# cd roles
# git clone https://github.com/elastic/ansible-elasticsearch
Initialized empty Git repository in /root/ansible-elasticsearch/.git/
remote: Counting objects: 1192, done.
remote: Total 1192 (delta 0), reused 0 (delta 0), pack-reused 1192
Receiving objects: 100% (1192/1192), 167.42 KiB | 86 KiB/s, done.
Resolving deltas: 100% (640/640), done.
# mv ansible-elasticsearch elasticsearch

更新 Ansible site.yml 中的主 playbook

我們需要將所有三個不同的 Elasticsearch 角色對映到三個不同的 Ansible 主機組,這將允許我們建立專用的主節點、專用的資料節點和主 Elasticsearch 節點。

對映組的 Elasticsearch 專用主角色

$ cd ~/ansible-playbook
$ vi site.yml
- hosts: elasticsearch_master_nodes
roles:
- { role: elasticsearch, es_instance_name: "node1", es_config: { discovery.zen.ping.unicast.hosts: "node01, node02, node03", network.host: "_tun0_, _local_", cluster.name: "production", discovery.zen.ping.multicast.enabled: false, http.port: 9200, transport.tcp.port: 9300, node.data: false, node.master: true, bootstrap.mlockall: true } }
vars:
es_major_version: "2.x"
es_version: "2.2.1"
es_heap_size: "2g"
es_cluster_name: "production"

此配置將在組中建立專用的主節點角色,其中值 **node.master: true** 和 **node.data: false** 定義相同。

現在,在 **elasticsearch_master_nodes** 中配置的主機被配置為專用的主 Elasticsearch 節點。

對映組中的 Elasticsearch 主/資料角色

在主 playbook site.yml 的底部,透過新增以下配置將主節點資格和資料 elasticsearch 角色對映到 elasticsearch_master-data_nodes 組。

- hosts: elasticsearch_master_data_nodes
roles:
- { role: elasticsearch, es_instance_name: "node1", es_config: { discovery.zen.ping.unicast.hosts: "node01, node02, node03", network.host: "_tun0_, _local_", cluster.name: "production", discovery.zen.ping.multicast.enabled: false, http.port: 9200, transport.tcp.port: 9300, node.data: true, node.master: true, bootstrap.mlockall: true } }
vars:
es_major_version: "2.x"
es_version: "2.2.1"
es_heap_size: "2g"
es_cluster_name: "production"

這將為主節點資格建立資料節點,值 **node.master: true** 和 **node.data: true** 定義相同。

將 **es_version** 設定為為專用主角色配置的相同值。

將 Elasticsearch 專用資料角色對映到組

我們需要將以下配置新增到主 playbook site.yml 中,以便為 elasticsearch_data_nodes 組中的專用資料 elasticsearch 角色。

- hosts: elasticsearch_data_nodes
roles:
- { role: elasticsearch, es_instance_name: "node1", es_config: { discovery.zen.ping.unicast.hosts: "node01, node02, node03", network.host: "_tun0_, _local_", cluster.name: "production", discovery.zen.ping.multicast.enabled: false, http.port: 9200, transport.tcp.port: 9300, node.data: true, node.master: false, bootstrap.mlockall: true } }
vars:
es_major_version: "2.x"
es_version: "2.2.1"
es_heap_size: "2g"
es_cluster_name: "production"

此角色將建立專用資料節點,因為它配置為節點的值 **node.master: false** 和 **node.data: true**。

儲存配置檔案並退出

更新主機清單檔案

要建立 Elasticsearch 節點,只需將主機新增到 Ansible 清單檔案中的主機檔案中

# vi hosts
[vpn]
node01 vpn_ip=10.0.0.1 ansible_host=10.2.2.1
node02 vpn_ip=10.0.0.2 ansible_host=192.168.100.100
node03 vpn_ip=10.0.0.3 ansible_host=192.168.100.101
node04 vpn_ip=10.0.0.4 ansible_host=192.168.100.102
[removevpn]

現在我們必須新增三個在 site.yml 檔案中對映的組

[elasticsearch_master_nodes]
[elasticsearch_master_data_nodes]
[elasticsearch_data_nodes]

Ansible 清單中的總主機檔案應如下所示

[vpn] node01 vpn_ip=10.0.0.1 ansible_host=10.2.2.1 node02 vpn_ip=10.0.0.2 ansible_host=192.168.100.100 node03 vpn_ip=10.0.0.3 ansible_host=192.168.100.101 node04 vpn_ip=10.0.0.4 ansible_host=192.168.100.102 [removevpn] [elasticsearch_master_nodes] node01 node02 node03 [elasticsearch_master_data_nodes] [elasticsearch_data_nodes] node04

建立 Elasticseach 叢集

由於我們已經配置了 Elasticsearch 叢集伺服器所需的所有設定,因此以下是建立 Elasticsearch 叢集的命令

# ansible-playbook site.yml

Playbook 執行完成後,elasticsearch 叢集應該已經啟動並執行。

驗證 Elasticsearch 叢集狀態

在任何三個伺服器中,我們需要執行以下命令

# curl -XGET 'https://:9200/_cluster/state?pretty'
Output:
Cluster State:
{
   "cluster_name" : "production",
   "version" : 8,
   "state_uuid" : "SgTyb0vNTTu2rdKPrc6tkQ",
   "master_node" : "OzqNzte9RYWSXS6OkGhveA",
   "blocks" : { },
   "nodes" : {
      "OzqMzte9RYWDXS6OkGhveA" : {
         "name" : "node02-node1",
         "transport_address" : "192.168.100.2:9300",
         "attributes" : {
            "data" : "false",
            "master" : "true"
         }
      },
      "7bohaaYVTee$HvSgBFp-2g" : {
         "name" : "node04-node1",
         "transport_address" : "192.168.100.4:9300",
         "attributes" : {
            "master" : "false"
         }
      },
      "cBat9IgPQwKU_GPF8L3Y1g" : {
         "name" : "node03-node1",
         "transport_address" : "192.168.100..3:9300",
         "attributes" : {
            "master" : "false"
         }
      },
...

如果我們能夠看到上述輸出,則 Elasticsearch 叢集伺服器工作正常並正在執行。

由於我們已經完成了配置和設定,因此我們可以配置 Elasticsearch 叢集以執行在健康狀態下,因為 Elasticsearch 還有許多其他功能和配置選項未涵蓋,有關高階功能的更多資訊,您可以參考官方文件。

更新於: 2019年10月18日

287 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.