CLUSTER ADMINISTRATION
集群管理
GOAL: Backup and cross-cluster search
目标:备份和跨集群搜索
REQUIRED SETUP:
建议docker-compose文件:
2e2k_two_clusters.yml
需求几步骤:
Let’s create a one-node cluster and index some data in it.
让我们先搞一个但节点的集群,然后存点数据进去
- Download the exam version of Elasticsearch
- 下载考试版本的ES
- Deploy the cluster
, with one node namedeoc-06-original-cluster
node-1
- 初始化一个单节点的集群,节点名
,集群名node-1
eoc-06-original-cluster
- 初始化一个单节点的集群,节点名
- Start the cluster
- 启动集群
- Create the index
and add some documents by running the following _bulk commandhamlet
- 创建一个叫
的索引,并用下面 ⬇️ 的语句存谢数据进去hamlet
- 创建一个叫
PUT hamlet/_bulk
{"index":{"_index":"hamlet","_id":0}}
{"line_number":"1","speaker":"BERNARDO","text_entry":"Whos there?"}
{"index":{"_index":"hamlet","_id":1}}
{"line_number":"2","speaker":"FRANCISCO","text_entry":"Nay, answer me: stand, and unfold yourself."}
{"index":{"_index":"hamlet","_id":2}}
{"line_number":"3","speaker":"BERNARDO","text_entry":"Long live the king!"}
{"index":{"_index":"hamlet","_id":3}}
{"line_number":"4","speaker":"FRANCISCO","text_entry":"Bernardo?"}
{"index":{"_index":"hamlet","_id":4}}
{"line_number":"5","speaker":"BERNARDO","text_entry":"He."}
或者
DELETE hamlet
PUT hamlet/_doc/1
{"line_number":"1","speaker":"BERNARDO","text_entry":"Whos there?"}
PUT hamlet/_doc/2
{"line_number":"2","speaker":"FRANCISCO","text_entry":"Nay, answer me: stand, and unfold yourself."}
第1题,创建备份存储空间
- Configure
to support a shared file system repository for backups located innode-1
- 修改
的节点配置,让它可以支持把文件存在以下目录node-1
- 修改
- “[home_folder]/repo” and
- “[home_folder]/elastic/repo” - e.g., “glc/elastic/repo”
- Create the
shared file system repository in “[home_folder]/elastic/repo”hamlet_backup
- 创建一个叫
存储空间在"[home_folder]/elastic/repo"hamlet_backup
- 创建一个叫
第1题,题解
(由于我是基于我的docker-compose文件启动的集群,所以会和题目不完全一样,比如题目里的node1我用的是esNode1,下同)
- 创建共享存储
- 在安装ES的机器的命令行里执行
mkdir ${dir}
- 设置成nfs共享存储,并挂载到集群的所有节点上
- 在安装ES的机器的命令行里执行
- 把这部分地址写进配置文件
$ES_HOME/config/elasticsearch.yml
-
echo 'path.repo: [${dir}]' >> /$ES_HOME/config/elasticsearch.yml
-
- 重启集群中的所有节点是这个配置生效
- 把共享存储注册为
的存储仓库hamlet_backup
- 执行以下命令
PUT _snapshot/hamlet_backup { "type": "fs", "settings": { "location": "/usr/share/elasticsearch/backup" } }
第1题,题解说明
- 这题主要考察snapshot仓库的创建,在生产中主要可能存在的问题是:
- 共享存储的路径应该是被集群中所有节点(包括源集群和目标集群)都能共享访问的(带读写权限)
- 如果是集群先存在,共享存储后挂载的话,需要对集群中的所有节点进行rolling restart
- 可以通过前两章中的
,"index.routing.allocation.exclude._name": "$node_name"
等设置将索引移出待重启节点,然后再对索引进行重启以使得配置生效"index.routing.allocation.exclude._ip_": "$node_ip",
- 可以通过前两章中的
- 参考链接-modules-snapshot,参考链接-create-snapshot
- 由于7.X早期版本的
页面不见了,这里贴的是7.9的相关页面,但其实从命令到操作方式上几乎没区别Create snapshot
- 由于7.X早期版本的
- 页面路径-modules-snapshot:Modules =》 Snapshot And Restore
- 页面路径-create-snapshot:Snapshot and restore =》 Register repository
第2题,创建snapshot,自体集群的数据备份和恢复
- Create a snapshot of the
index, so that the snapshothamlet
- 创建一个索引
的snapshothamlet
- is named
,hamlet_snapshot_1
- 取名叫
hamlet_snapshot_1
- 取名叫
- is stored into
hamlet_backup
- 并保存在
的仓库里hamlet_backup
- 并保存在
- 创建一个索引
- Delete the index
hamlet
- 把
这索引删了hamlet
- 把
- Restore the index
usinghamlet
hamlet_snapshot_1
- 再从
里恢复hamlet_snapshot_1
的数据hamlet
- 再从
第2题,题解
- 创建snapshot,运行以下命令
PUT /_snapshot/hamlet_backup/hamlet_snapshot_1?wait_for_completion=true
{
"indices": "hamlet",
"ignore_unavailable": true,
"include_global_state": false
}
- 可能返回值
{
"snapshot" : {
"snapshot" : "hamlet_snapshot_1",
"uuid" : "vv_unWlzTGyAKc8VJVA5aA",
"version_id" : 7020199,
"version" : "7.2.1",
"indices" : [
"hamlet"
],
"include_global_state" : false,
"state" : "SUCCESS",
"start_time" : "2020-11-12T02:53:42.547Z",
"start_time_in_millis" : 1605149622547,
"end_time" : "2020-11-12T02:53:42.631Z",
"end_time_in_millis" : 1605149622631,
"duration_in_millis" : 84,
"failures" : [ ],
"shards" : {
"total" : 1,
"failed" : 0,
"successful" : 1
}
}
}
- 如果多跑一次
{
"error": {
"root_cause": [
{
"type": "invalid_snapshot_name_exception",
"reason": "[hamlet_backup:hamlet_snapshot_1] Invalid snapshot name [hamlet_snapshot_1], snapshot with the same name already exists"
}
],
"type": "invalid_snapshot_name_exception",
"reason": "[hamlet_backup:hamlet_snapshot_1] Invalid snapshot name [hamlet_snapshot_1], snapshot with the same name already exists"
},
"status": 400
}
- 删除索引,
DELETE hamlet
- 从
恢复数据hamlet_snapshot_1
POST /_snapshot/hamlet_backup/hamlet_snapshot_1/_restore?wait_for_completion=true
{
"indices": "hamlet",
"include_global_state": false,
"ignore_unavailable": true
}
- 可能返回值(不加wait_for_completion=true)
{
"accepted": true
}
- wait_for_completion情况
{
"snapshot" : {
"snapshot" : "hamlet_snapshot_1",
"indices" : [
"hamlet"
],
"shards" : {
"total" : 1,
"failed" : 0,
"successful" : 1
}
}
}
- 如果多跑一次
{
"error": {
"root_cause": [
{
"type": "snapshot_restore_exception",
"reason": "[hamlet_backup:hamlet_snapshot_1/vv_unWlzTGyAKc8VJVA5aA] cannot restore index [hamlet] because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name"
}
],
"type": "snapshot_restore_exception",
"reason": "[hamlet_backup:hamlet_snapshot_1/vv_unWlzTGyAKc8VJVA5aA] cannot restore index [hamlet] because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name"
},
"status": 500
}
- 运行
检查一下索引是否还存在GET _cat/indices
- 运行
GET hamlet/_search
第2题,题解说明
- 这题主要考察的是数据的恢复,需要注意的
- 因为snapshot的id和index的名字是唯一的,所以重复运行会报错
- 多个索引可以备份在同一个snapshot里,同一个snapshot也可以恢复1个到多个索引,在备份和恢复的时候要做好规划
- 在备份和恢复的时候会有一些附加参数可以设置,比如:
-
用来跳过不可用的索引ignore_unavailable
-
用来决定是否包含集群的全局配置include_global_state
-
用来决定如果恢复数据的时候有些index出现了报错,同批次的其他索引的恢复是继续还是一同失败partial
-
- 测试里的数据量比较小,snapshot保存和恢复速度较快,可以添加
来等待流程完成,在真正的生产环境中进行操作时,建议不加这个参数,而是通过提交任务以及调用wait_for_completion=true
接口的方式来查看snapshot的创建和恢复的过程。GET /_snapshot/hamlet_snapshot_1/_current
- 参考链接-modules-snapshots,参考链接-restore-snapshots
- 页面路径-modules-snapshots:Modules =》 Snapshot And Restore
- 页面路径-restore-snapshots:Snapshot and restore =》 Restore a snapshot
- 可以直接用在生产上的,通过snapshot进行数据迁移的操作手顺:参考链接
第3题,多集群间通过snapshot进行数据迁移,以及跨集群的数据搜索
- Deploy a second cluster
, with one node namedeoc-06-adaptation-cluster
node-2
- 部署第二个集群叫
其中一个节点叫eoc-06-adaptation-cluster
node-2
- 部署第二个集群叫
- Start the cluster
- 启动集群
- (我加的)通过snapshot把第一个集群里的
恢复到这里来hamlet
- Create the index
onhamlet-pirate
and add documents using the _bulk commandnode-2
- 在
里创建一个索引叫node-2
hamlet-pirate
- 在
- Enable cross cluster search on
, so thateoc-06-adaptation-cluster
- 给集群
开启跨集群搜索功能eoc-06-adaptation-cluster
- the name of the remote cluster is
,original
- 远程集群设置为
original
- 远程集群设置为
- the seed is
, which is listening on the default transport port,node-1
- 连接地址是
的,监听它的默认端口node-1
- 连接地址是
- the cross cluster configuration persists across multiple restarts
- 让跨集群的配置生效可能需要重启几个集群
- Run the cross-cluster query below to check your setup
- 运行下面的跨集群搜索命令来校验你的配置是否成功
- 给集群
第3题,题解
- 创建集群大部分操作参考之前的章节,这里不再赘述
- 跨集群数据恢复:
- 在新集群里注册存储地址
PUT _snapshot/hamlet_backup { "type": "fs", "settings": { "location": "/usr/share/elasticsearch/backup" } }
- 使用和本集群自己恢复的命令一样(也可以根据需要稍作修改)
POST /_snapshot/hamlet_backup/hamlet_snapshot_1/_restore?wait_for_completion=true { "indices": "hamlet", "include_global_state": false, "ignore_unavailable": true }
- 开启跨集群搜索
- 有两种方式:
- 通过kibana的设置:
- 左边菜单栏里点
management
- 在
里点Elasticsearch
Remote Clusters
-
Add a remote cluster
- 表单里填:
-
:Name
original
-
:Seed Node
(因为我是在docker-compose集群里进行操作,所以写es721Node1:9300
这个内部域名,真正的生产环境换成实际的ip地址)es721Node1
-
-
Save
- 左边菜单栏里点
- 通过ES的api:
- 运行以下命令:
PUT _cluster/settings { "persistent": { "cluster": { "remote": { "original": { "seeds": [ "es721Node1:9300" ] } } } } }
- 通过kibana的设置:
- 运行跨集群搜索命令进行数据校验
POST /original:hamlet/_search { "query": { "match": { "speaker" : "BERNARDO" } } }
第3题,题解说明
- 这题前面一半考察的是数据恢复,和上一题一样,只是操作的平台从本集群换成了远程集群,那么需要注意的就是集群之间需要可以同时访问同一块共享存储的地址
- 后面一半考察的是跨集群搜索的设置,跨集群搜索要先注册远程集群,以一个或几个节点的域名/ip地址配合默认端口来相互通讯,但是这里要注意的是,集群间通讯的不是
(默认9200)而是http.port
(默认9300)transport.port
- 参考链接-modules-remote-clusters,参考链接-modules-cross-cluster-search
- 页面路径-modules-remote-clusters:Modules =》 Remote clusters
- 页面路径-modules-cross-cluster-search:Modules =》 Cross-cluster search