本页内容

  • 先决条件
  • 我们要做什么
  • 验证 Elasticsearch 集群配置 节点1上的配置 节点2上的配置 节点3上的配置
  • 注册用于备份的仓库
  • Elasticsearch 集群的备份与还原 执行备份 还原备份
  • 节点1上的配置
  • 节点2上的配置
  • 节点3上的配置
  • 执行备份
  • 还原备份

在 Elasticsearch 中,进行备份的机制称为 Snapshot(快照)。快照是从处于运行状态的 Elasticsearch 集群中获取的备份。无需关闭集群,这有助于避免应用程序出现维护窗口。可以对单个索引或整个集群创建快照,并将其存储在共享文件系统上的仓库中。

Elasticsearch 中的快照是以增量方式创建的。这意味着在创建某个索引的快照时,Elasticsearch 会避免复制已作为该索引较早快照的一部分存储的任何数据。因此,定期为集群创建快照可能是高效的。

正如我们可以在集群处于运行状态时对其进行备份一样,我们也可以将快照还原到正在运行的集群中。还原索引时,我们甚至可以更改所还原索引的名称及其某些设置。

要进行备份,必须先注册一个快照仓库,然后才能执行快照和还原操作。为了给集群注册共享文件系统仓库,必须在所有主节点和数据节点上将同一共享文件系统挂载到相同的位置。该位置必须在所有主节点和数据节点的配置文件中注册。

在本文中,我们将验证 NFS 共享仓库,并了解创建快照及还原快照的步骤。

先决条件

  • NFS 共享目录可用,并在所有 3 个 Elasticsearch 节点上挂载到相同位置
  • 在 3 台 Ubuntu 服务器上搭建的 3 节点 Elasticsearch 集群。

我们要做什么

  • 验证 NFS 服务器设置。
  • 验证 Elasticsearch 集群配置
  • 注册用于备份的仓库。
  • 执行备份与还原。

验证 NFS 服务器/客户端设置。

在本文中,我们不会讨论 NFS 的设置,因为它不在本文的讨论范围之内。但要对 Elasticsearch 进行备份,我们需要具备以下设置。

es-node-1(10.11.10.61) 
 
: NFS Client
es-node-2(10.11.10.62)
 
: NFS Client
es-node-3(10.11.10.63)
 
: NFS Client
NFS Server(10.11.10.64)
 
: NFS Server

这里,

NFS 服务器已将其“/home/ubuntu/shared/”目录共享给 Elasticsearch 节点。

每个 Elasticsearch 都有其本地目录“/home/ubuntu/mounted”,挂载到 NFS 的共享目录“/home/ubuntu/shared/”上。我们需要确保所有目录的所有权都属于启动 Elasticsearch 时使用的同一用户。

一旦完成此设置,我们就可以继续往下进行。

验证 Elasticsearch 集群配置

执行以下配置,使 Elasticsearch 以集群模式工作:

在这里,如果你已经搭建了 Elasticsearch 集群,那么一定熟悉以下配置。

除了现有的 Elasticsearch 集群配置之外,我们要进行 Elasticsearch 集群备份所需做的唯一配置是 " path.repo: ["/home/ubuntu/mounted"] ":

vim config/elasticsearch.yml
path.repo: ["/home/ubuntu/mounted"]
 

在每个节点上都保持相同设置。

节点1上的配置

#give your cluster a name.
cluster.name: my-cluster
#give your nodes a name (change node number from node to node).
node.name: "es-node-1"
#define node 1 as master-eligible:
node.master: true
#define nodes 2 and 3 as data nodes:
node.data: true
#enter the private IP and port of your node:
network.host: 10.11.10.61
http.port: 9200
#detail the private IPs of your nodes:
discovery.zen.ping.unicast.hosts: ["10.11.10.61", "10.11.10.62", "10.11.10.63"]
cluster.initial_master_nodes:
- 10.11.10.61
path.repo: ["/home/ubuntu/mounted"]

节点2上的配置

#give your cluster a name.
cluster.name: my-cluster
#give your nodes a name (change node number from node to node).
node.name: "es-node-2"
#define node 2 as master-eligible:
node.master: false
#define nodes 2 and 3 as data nodes:
node.data: true 
#enter the private IP and port of your node:
network.host: 10.11.10.62
http.port: 9200 
#detail the private IPs of your nodes:
discovery.zen.ping.unicast.hosts: ["10.11.10.61", "10.11.10.62", "10.11.10.63"
path.repo: ["/home/ubuntu/mounted"]

节点3上的配置

#give your cluster a name.
cluster.name: my-cluster
#give your nodes a name (change node number from node to node).
node.name: "es-node-3"
#define node 3 as master-eligible:
node.master: false
#define nodes 2 and 3 as data nodes:
node.data: true
#enter the private IP and port of your node:
network.host: 10.11.10.63
http.port: 9200
#detail the private IPs of your nodes:
discovery.zen.ping.unicast.hosts: ["10.11.10.61","10.11.10.62","10.11.10.63""]
path.repo: ["/home/ubuntu/mounted"]

如何备份 Elasticsearch 集群数据完整教程指南

完成所有这些配置后,启动所有 Elasticsearch 节点,首先启动初始主节点。

注册用于备份的仓库

使用以下命令检查现有的仓库。

curl -XGET 
'http://IP_Of_Elasticsearch_Node_Or_Master:9200/_snapshot/_all?pretty=true'

如果得到空白响应,则表明我们尚未设置任何仓库

要设置仓库,请执行以下命令。

curl -XPUT 
'http://IP_Of_Elasticsearch_Node_Or_Master:9200/_snapshot/my_backup'
 -d {
"type": "fs",
"settings": {
"location": "
/home/ubuntu/mounted
",
"compress": true
}
}'

这里,上述命令中的“my_backup”是仓库的名称。

我们可以使用以下命令检查已注册的仓库

curl -XGET 
'http://IP_Of_Elasticsearch_Node_Or_Master:9200/_snapshot/_all?pretty=true'

Elasticsearch 集群的备份与还原

执行备份

创建仓库之后,我们就可以进行备份了。

使用以下命令创建一个名为“snapshot_name”的备份

curl -XPUT 
"https://IP_Of_Elasticsearch_Node_Or_Master:9200/_snapshot/my_backup/snapshot_name?wait_for_completion=true"

还原备份

我们可以使用以下命令还原已创建的快照。

使用以下命令还原名为“snapshot_name”的备份

curl -XPOST 
"http://IP_Of_Elasticsearch_Node_Or_Master:9200/_snapshot/my_backup/snapshot_name/_restore?wait_for_completion=true"

结论

在本文中,我们了解了注册仓库、创建备份以及还原备份的步骤。