老版本的elasticsearch 因为某些原因无法使用快照snapshot进行数据数据备份,然后在到新版进行恢复, 所以使用的策略是使用elasticsearch-dump工具(更慢)将数据导入本地单机的elasticsearch中, 导入完成后使用快照snapshot将数据备份到本地(snapshot恢复时数据更快). 随后将老版本es那几台服务器从ubuntu18升级到ubuntu20, 使用docker swarm 进行分布式部署. 部署完成后将本机备份的快照放到新版本elasticsearch的备份目录下进行数据恢复
https://zhoujinl.github.io/2018/10/19/docker-swarm-manager-ha/
https://www.cnblogs.com/guoxiangyue/p/12455972.html
https://ox0spy.github.io/post/elasticsearch/elasticsearch-snapshot-restore/
https://www.cnblogs.com/sanduzxcvbnm/p/12090809.html
安装
sudo apt install docker.io
设置开机自启
sudo systemctl enable --now docker
启动docker
sudo systemctl start docker
添加组(不使用sudo运行docker)
sudo usermod -aG docker \$username
查看docker信息
docker --version
防止启动docker elasticsearch时jvm报错
永久修改: echo vm.max_map_count=524288 >> /etc/sysctl.conf
临时修改(重启失效):sudo sysctl :w-w vm.max_map_count=524288
sysctl -p 使得配置生效
添加docker的清华镜像
echo "{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}" >> /etc/docker/daemon.json
pull必要镜像
docker pull elasticsearch:7.6.2
docker pull kibana:7.6.3
初始化docker swarm
docker swarm init
(默认本台主机为manager, 集群leader)获得如下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pThg44av-1591886970074)(pic/init.png)]
在其他主机上使用docker swarm join --token token ip:port
加入docker swarm 集群中, 作为 worker
将其他主机加入为 manager
在manager主机上, 使用docker swarm join-token manager
获取到加入集群manager的token
格式相同只是token不同
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yy79okm4-1591886970077)(pic/join_manager.png)]
查看swarm中节点
sudo docker node ls
将elasticsearch中需要映射出的文件进行本地创建, 并给予权限
mkdir -m 777 -p /soft/elasticsearch/{data,logs,plugins}
将elasticsearch映射的文件创建在根目录下, 防止误删
挂载elasticsearch备份目录
创建挂载目录:mkdir /mnt/es_backups
挂载共享文件目录: sshfs -o allow_other {username}@{ip}:{path} /mnt/es_backups/
elasticsearch 做数据快照,恢复时所用到的目录必须指向同一目录, 所以使用共享文件系统实现.-o allow_othoer
其他用户也能访问挂载目录
创建共享配置文件
mkdir /mnt/es_backups/config/{master,node}
为了方便管理配置文件,将所有eleaticsearch的配置文件,都写在共享文件系统中,
由于在每台机器上都需要进行以上操作,将以上写成脚本
#!/bin/bash
apt install docker.io sshfs -y && \
systemctl enable --now docker && \
systemctl start docker && \
echo "{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}" >> /etc/docker/daemon.json && \
sysctl -w vm.max_map_count=524288
sysctl -p
docker pull elasticsearch:7.6.2 && \
docker pull kibana:7.6.2 && \
mkdir -p /mnt/es_backups/config/{master,node} && \
mkdir -m 777 -p /soft/elasticsearch/{data,logs,plugins} && \
# 挂载目录
sshfs -o allow_other {远程文件系统} /mnt/es_backups/ && \
# 加入集群, 作为woker
docker swarm join --token {token} && \
echo "fish"
https://docs.docker.com/compose/compose-file/
version: '3.3'
services:
kibana:
image: docker.elastic.co/kibana/kibana:7.6.2
environment:
- ELASTICSEARCH_URL=http://es_master:9200
- ELASTICSEARCH_HOSTS=http://es_master:9200
ports:
- 5601:5601
networks:
- elastic
deploy:
mode: replicated
replicas: 1
resources:
limits:
memory: 800M
placement:
constraints:
- node.role==manager
es_master:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
environment:
- node.name=master
- node.master=true
- node.data=true
#- bootstrap.memory_lock=true
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es_node
- cluster.initial_master_nodes=es_master,es_node
- network.host=0
- network.publish_host=_eth0_
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- path.repo=/usr/share/elasticsearch/backups
volumes:
- /soft/elasticsearch/data/:/usr/share/elasticsearch/data
- /soft/elasticsearch/logs/:/usr/share/elasticsearch/logs
- /soft/elasticsearch/plugins/:/usr/share/elasticsearch/plugins
- /mnt/es_backups/config/master:/usr/share/elasticsearch/config
- /mnt/es_backups:/usr/share/elasticsearch/backups
ports:
- 9200:9200
- 9300:9300
networks:
- elastic
deploy:
mode: replicated
replicas: 1
resources:
limits: # es_maseter 资源使用上限
cpus: "0.50"
memory: 2G
reservations: # es_maseter 随时可以使用的资源
cpus: "0.25"
memory: 1G
placement:
constraints:
- node.role==manager # 部署位置
es_node:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
environment:
- node.name=node
- node.master=true
- node.data=true
#- bootstrap.memory_lock=true
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es_master
- cluster.initial_master_nodes=es_master,es_node
#- network.host=0
#- network.publish_host=_eth0_
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- path.repo=/usr/share/elasticsearch/backups
volumes:
- /soft/elasticsearch/data/:/usr/share/elasticsearch/data
- /soft/elasticsearch/logs/:/usr/share/elasticsearch/logs
- /soft/elasticsearch/plugins/:/usr/share/elasticsearch/plugins
- /mnt/es_backups/config/node:/usr/share/elasticsearch/config
- /mnt/es_backups:/usr/share/elasticsearch/backups
# ports:
# - 9200:9200
# - 9300:9300
networks:
- elastic
deploy:
mode: replicated
replicas: 4
resources:
limits:
cpus: "0.80"
memory: 2G
reservations:
cpus: "0.40"
memory: 1G
placement:
constraints:
- node.role==worker
networks:
elastic:
driver: overlay # 必须使用swarm类型的网络
使用以上配置文件进行部署
docker stack deploy -c docker-swarm.yml es
其中es表示service名字
查看所有服务
docker service ls
查看所有服务
docker service ps {service_name} --no-trunc
–no-trunc 打印完整信息
查看服务日志
docker service logs {service_name}
将worker提升到manager
docker node promote {node_name}
将manager降为woker
docker node demote {node_name}
*/mnt/es_backups/config/{node, master}中需要先有配置文件, 可以先docker run --name es eleasticsearch
, 然后使用docker cp es:/user/share/elasticsearch/config /mnt/es_backups/{master,node}
, 后面更改elasticsearch.yml文件
https://www.cnblogs.com/resn/p/9082663.html
https://github.com/elasticsearch-dump/elasticsearch-dump
# 导映射
elasticdump \
--input=http://old.es:9200/my_index \
# 导入到本地
--output=/data/my_index_mapping.json \
# 导入新的es中
#--output=http://new.es:9200/new_inde
--type=mapping
# 导数据
elasticdump \
--input=http://production.es.com:9200/my_index \
# 导入到本地
--output=/data/my_index.json \
# 导入新的es中
#--output=http://new.es:9200/new_inde
--type=data
快照的备份,恢复均可以在kibana中完成
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/snapshots-register-repository.html
使用快照, 先要在本机es的配置elasticsearch.yml文件中配配置
path.repo: ["/mount/backups", "/mount/longterm_backups"]
用于指定备份保存的位置
注册一个快照保存位置, 如果location没有位置前缀, 模式使用path.repo中的地一个作为前缀/mount/backups/my_backup
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "my_backup"
}
}
开始备份, 备份所有数据, 备份名为all_backups
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/snapshots-take-snapshot.html
PUT /_snapshot/my_backup/all_backups?wait_for_completion=true
wait_for_completion
等待完成后返回
得到目录结构如下
└── all_backups
├── index-0
├── index.latest
├── indices
│ ├── 1LBcsNpHQnyfjEdUPAEjew
│ ├── aEpJjlcHTNyDCEdt_TmFEw
│ ├── f6miB7pJRlCkXzEPjngjOw
│ ├── KqDxG2WdRk6fhymDv433aA
│ └── unxAE2DiRsGiGVFllFzp7g
├── meta--EWxAnkFSZ2PPTNPijE6Og.dat
└── snap--EWxAnkFSZ2PPTNPijE6Og.dat
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/snapshots-restore-snapshot.html
将all_backups文件移动到共享文件系统, 即eleaticsearch集群的备份目录下
es集群中建立同名备份库
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "my_backup"
}
}
验证es能否访问到all_backups, 如果不保错则正确, 报错的话在次确认每台服务器上是正确挂载共享文件系统以及权限
GET /_snapshot/backups/all_backups
恢复数据
POST /_snapshot/my_backup/all_backups/_restore
待恢复完成
老版本的elasticsearch 因为某些原因无法使用快照snapshot进行数据数据备份,然后在到新版进行恢复, 所以使用的策略是使用elasticsearch-dump工具(更慢)将数据导入本地单机的elasticsearch中, 导入完成后使用快照snapshot将数据备份到本地(snapshot恢复时数据更快). 随后将老版本es那几台服务器从ubuntu18升级到ubuntu20, 使用docker swarm 进行分布式部署. 部署完成后将本机备份的快照放到新版本elasticsearch的备份目录下进行数据恢复
https://zhoujinl.github.io/2018/10/19/docker-swarm-manager-ha/
https://www.cnblogs.com/guoxiangyue/p/12455972.html
https://ox0spy.github.io/post/elasticsearch/elasticsearch-snapshot-restore/
https://www.cnblogs.com/sanduzxcvbnm/p/12090809.html
安装
sudo apt install docker.io
设置开机自启
sudo systemctl enable --now docker
启动docker
sudo systemctl start docker
添加组(不使用sudo运行docker)
sudo usermod -aG docker \$username
查看docker信息
docker --version
防止启动docker elasticsearch时jvm报错
永久修改: echo vm.max_map_count=524288 >> /etc/sysctl.conf
临时修改(重启失效):sudo sysctl :w-w vm.max_map_count=524288
sysctl -p 使得配置生效
添加docker的清华镜像
echo "{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}" >> /etc/docker/daemon.json
pull必要镜像
docker pull elasticsearch:7.6.2
docker pull kibana:7.6.3
初始化docker swarm
docker swarm init
(默认本台主机为manager, 集群leader)获得如下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1o021tte-1591886970949)(pic/init.png)]
在其他主机上使用docker swarm join --token token ip:port
加入docker swarm 集群中, 作为 worker
将其他主机加入为 manager
在manager主机上, 使用docker swarm join-token manager
获取到加入集群manager的token
格式相同只是token不同
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NwFVKmlj-1591886970952)(pic/join_manager.png)]
查看swarm中节点
sudo docker node ls
将elasticsearch中需要映射出的文件进行本地创建, 并给予权限
mkdir -m 777 -p /soft/elasticsearch/{data,logs,plugins}
将elasticsearch映射的文件创建在根目录下, 防止误删
挂载elasticsearch备份目录
创建挂载目录:mkdir /mnt/es_backups
挂载共享文件目录: sshfs -o allow_other {username}@{ip}:{path} /mnt/es_backups/
elasticsearch 做数据快照,恢复时所用到的目录必须指向同一目录, 所以使用共享文件系统实现.-o allow_othoer
其他用户也能访问挂载目录
创建共享配置文件
mkdir /mnt/es_backups/config/{master,node}
为了方便管理配置文件,将所有eleaticsearch的配置文件,都写在共享文件系统中,
由于在每台机器上都需要进行以上操作,将以上写成脚本
#!/bin/bash
apt install docker.io sshfs -y && \
systemctl enable --now docker && \
systemctl start docker && \
echo "{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}" >> /etc/docker/daemon.json && \
sysctl -w vm.max_map_count=524288
sysctl -p
docker pull elasticsearch:7.6.2 && \
docker pull kibana:7.6.2 && \
mkdir -p /mnt/es_backups/config/{master,node} && \
mkdir -m 777 -p /soft/elasticsearch/{data,logs,plugins} && \
# 挂载目录
sshfs -o allow_other {远程文件系统} /mnt/es_backups/ && \
# 加入集群, 作为woker
docker swarm join --token {token} && \
echo "fish"
https://docs.docker.com/compose/compose-file/
version: '3.3'
services:
kibana:
image: docker.elastic.co/kibana/kibana:7.6.2
environment:
- ELASTICSEARCH_URL=http://es_master:9200
- ELASTICSEARCH_HOSTS=http://es_master:9200
ports:
- 5601:5601
networks:
- elastic
deploy:
mode: replicated
replicas: 1
resources:
limits:
memory: 800M
placement:
constraints:
- node.role==manager
es_master:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
environment:
- node.name=master
- node.master=true
- node.data=true
#- bootstrap.memory_lock=true
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es_node
- cluster.initial_master_nodes=es_master,es_node
- network.host=0
- network.publish_host=_eth0_
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- path.repo=/usr/share/elasticsearch/backups
volumes:
- /soft/elasticsearch/data/:/usr/share/elasticsearch/data
- /soft/elasticsearch/logs/:/usr/share/elasticsearch/logs
- /soft/elasticsearch/plugins/:/usr/share/elasticsearch/plugins
- /mnt/es_backups/config/master:/usr/share/elasticsearch/config
- /mnt/es_backups:/usr/share/elasticsearch/backups
ports:
- 9200:9200
- 9300:9300
networks:
- elastic
deploy:
mode: replicated
replicas: 1
resources:
limits: # es_maseter 资源使用上限
cpus: "0.50"
memory: 2G
reservations: # es_maseter 随时可以使用的资源
cpus: "0.25"
memory: 1G
placement:
constraints:
- node.role==manager # 部署位置
es_node:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
environment:
- node.name=node
- node.master=true
- node.data=true
#- bootstrap.memory_lock=true
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es_master
- cluster.initial_master_nodes=es_master,es_node
#- network.host=0
#- network.publish_host=_eth0_
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- path.repo=/usr/share/elasticsearch/backups
volumes:
- /soft/elasticsearch/data/:/usr/share/elasticsearch/data
- /soft/elasticsearch/logs/:/usr/share/elasticsearch/logs
- /soft/elasticsearch/plugins/:/usr/share/elasticsearch/plugins
- /mnt/es_backups/config/node:/usr/share/elasticsearch/config
- /mnt/es_backups:/usr/share/elasticsearch/backups
# ports:
# - 9200:9200
# - 9300:9300
networks:
- elastic
deploy:
mode: replicated
replicas: 4
resources:
limits:
cpus: "0.80"
memory: 2G
reservations:
cpus: "0.40"
memory: 1G
placement:
constraints:
- node.role==worker
networks:
elastic:
driver: overlay # 必须使用swarm类型的网络
使用以上配置文件进行部署
docker stack deploy -c docker-swarm.yml es
其中es表示service名字
查看所有服务
docker service ls
查看所有服务
docker service ps {service_name} --no-trunc
–no-trunc 打印完整信息
查看服务日志
docker service logs {service_name}
将worker提升到manager
docker node promote {node_name}
将manager降为woker
docker node demote {node_name}
*/mnt/es_backups/config/{node, master}中需要先有配置文件, 可以先docker run --name es eleasticsearch
, 然后使用docker cp es:/user/share/elasticsearch/config /mnt/es_backups/{master,node}
, 后面更改elasticsearch.yml文件
https://www.cnblogs.com/resn/p/9082663.html
https://github.com/elasticsearch-dump/elasticsearch-dump
# 导映射
elasticdump \
--input=http://old.es:9200/my_index \
# 导入到本地
--output=/data/my_index_mapping.json \
# 导入新的es中
#--output=http://new.es:9200/new_inde
--type=mapping
# 导数据
elasticdump \
--input=http://production.es.com:9200/my_index \
# 导入到本地
--output=/data/my_index.json \
# 导入新的es中
#--output=http://new.es:9200/new_inde
--type=data
快照的备份,恢复均可以在kibana中完成
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/snapshots-register-repository.html
使用快照, 先要在本机es的配置elasticsearch.yml文件中配配置
path.repo: ["/mount/backups", "/mount/longterm_backups"]
用于指定备份保存的位置
注册一个快照保存位置, 如果location没有位置前缀, 模式使用path.repo中的地一个作为前缀/mount/backups/my_backup
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "my_backup"
}
}
开始备份, 备份所有数据, 备份名为all_backups
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/snapshots-take-snapshot.html
PUT /_snapshot/my_backup/all_backups?wait_for_completion=true
wait_for_completion
等待完成后返回
得到目录结构如下
└── all_backups
├── index-0
├── index.latest
├── indices
│ ├── 1LBcsNpHQnyfjEdUPAEjew
│ ├── aEpJjlcHTNyDCEdt_TmFEw
│ ├── f6miB7pJRlCkXzEPjngjOw
│ ├── KqDxG2WdRk6fhymDv433aA
│ └── unxAE2DiRsGiGVFllFzp7g
├── meta--EWxAnkFSZ2PPTNPijE6Og.dat
└── snap--EWxAnkFSZ2PPTNPijE6Og.dat
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/snapshots-restore-snapshot.html
将all_backups文件移动到共享文件系统, 即eleaticsearch集群的备份目录下
es集群中建立同名备份库
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "my_backup"
}
}
验证es能否访问到all_backups, 如果不保错则正确, 报错的话在次确认每台服务器上是正确挂载共享文件系统以及权限
GET /_snapshot/backups/all_backups
恢复数据
POST /_snapshot/my_backup/all_backups/_restore
待恢复完成