如何使用Ansible通过集群设置生产Elasticsearch服务器

在本文中,我们将学习如何使用Anisble在CentOS7上配置和安装生产Elastic Search集群,以确保Elasticsearch节点不受外部网络的影响。我们将使用VPN服务连接到集群。

Elasticsearch是一种非常流行的开源搜索服务器,可用于实时分布式搜索和数据分析,以实现更好的性能,稳定性和可伸缩性,我们需要将Elasticsearch作为一个集群部署在多个服务器上。

先决条件

我们至少需要三个具有专用网络的CentOS 7服务器环境,因为所需的Elasticsearch集群是3个主节点,并且如果我们计划一个主节点和一个数据节点,那么我们还需要用于该数据节点的其他服务器。请确保我们具有唯一的Ansible库存主机名称。

假设我们已经配置了VPN服务器,并且能够使用VPN连接每个服务器,并且接口名称为“ tun0”,或者我们可以在其他接口上更改Elasticsearch侦听,则可以在site.yml文件中进行适当的更改。

下载Ansible-Elasticsearch手册

Elasticsearch提供了一个Ansible角色,可用于设置Elasticsearch集群,为此,我们需要将其添加到ansible-tinc剧本中,并定义主机组,并将它们分配给那些组适当的角色。

转到服务器中的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中更新主剧本

我们需要将所有三个不同的Elasticsearch角色映射到三个不同的Anisble主机组,这将允许我们创建专用的master,专用数据和master 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:truenode.data:false将定义相同的角色。

现在,在elasticsearch_master_nodes中配置的主机被配置为专用的主Elasticsearch节点。

在组中映射Elasticsearch主数据角色

在主手册book.yml的底部,通过添加以下配置,将符合主条件的数据弹性搜索角色映射到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"

这将为符合master资格的values节点创建数据节点master:true nodedate:true将定义相同的日期

 es_version设置为为专用主角色配置的相同值。

将Elasticsearch专用数据角色映射到组

我们需要将以下配置添加到主剧本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"

该角色将创建专用的数据节点,因为它被配置为带有值节点的节点-错误和节点。数据:正确。

保存配置文件并退出

更新主机列表文件

要创建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]

Anisble列表中的主机总数文件应如下所示

[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

剧本完全运行后,elasticsearch群集应已启动并正在运行。

验证Elasticsearch集群状态

在这三台服务器中的任何一台中,我们需要运行以下命令

# curl -XGET 'http://localhost: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具有许多其他功能和配置选项,有关高级功能的更多信息未涵盖,您可以参考官方文档。