写在前面:原文再续,书接上一回。上一篇主要介绍Elasticsearch的特点与应用,也分享了分布式集群的特性,以及数据读取、写入、更新、删除的原理,这些都能够作为很好的基础知识储备指引进一步的学习。而面对生产环境,需要考虑的因素会更加复杂,下面从集群搭建开始,谈谈我在生产环境下常常用到的运维操作。
一、从零搭建Elasticsearch集群
1、下载Elasticsearch
https://www.elastic.co/downloads/elasticsearch
2、部署依赖环境
- 服务器清单:es1、es2、es3(2G内存)
- 所有主机安装java环境(jdk8)
[
- 所有主机配置主机映射、创建用户appuser,配置免密互信
#
- 所有主机创建相关目录
[
- 所有主机修改内核参数
#
3、安装Elasticsearch
- 主机es1解压Elasticsearch安装包
[
- 主机es1修改配置文件
[
- 同步Elasticsearch安装目录到所有主机,修改个性配置
#
- 所有主机后台启动Elasticsearch进程
[
4、验证Elasticsearch集群状态
[
二、Elasticsearch配置文件
1、elasticsearch.yml配置项
1
2、jvm.options配置项
#
3、log4j2.properties配置项
Elasticsearch公开三个属性
三、Elasticsearch冷热分离
1、配置冷热数据节点
1)热节点存储新数据(读写),冷节点存储历史数据(只读);
2)有利于节省硬件成本,热节点采用更好的硬件。
- 热节点配置:
# elasticsearch.yml
node.name: "es4"
cluster.name: "cluster1"
network.host: 0.0.0.0
node.master: false
node.data: true
discovery.zen.ping.unicast.hosts: ["es1", "es2", "es3"]
node.attr.box_type: "hot" # 标识为热数据节点
- 冷节点配置:
# elasticsearch.yml
node.name: "es5"
cluster.name: "cluster1"
network.host: 0.0.0.0
node.master: false
node.data: true
discovery.zen.ping.unicast.hosts: ["es1", "es2", "es3"]
node.attr.box_type: "cool" # 标识为冷数据节点
2、创建Index冷热模板
PUT /_template/hot_template
{
"index_patterns" : "*", # 匹配所有的索引
"order" : 0, # 多个模板同时匹配,以order顺序倒排,order越大,优先级越高
"settings" : {
"number_of_shards" : 2,
"index.routing.allocation.require.box_type": "hot", # 指定默认为热数据节点
"number_of_replicas": 1
}
}
提示:如果不想创建index模板,可以在创建index时在setting中指定 "index.routing.allocation.require.box_type": "hot" 配置,效果相同。
3、冷热数据迁移
PUT test_index/_settings
{
"settings": {
"index.routing.allocation.require.box_type": "hot" # 指定数据存放到热数据节点
}
}
PUT test_index/_settings
{
"settings": {
"index.routing.allocation.require.box_type": "cool" # 指定数据存放到冷数据节点
}
}
四、Elasticsearch数据迁移
1、手工移动分片
- 关闭集群已有分片的自平衡
curl -XPUT 192.168.0.1:9200/_cluster/settings -d '{
"transient": {
"cluster.routing.rebalance.enable": "none"
}
}'
- 移动分片
curl -XPOST '192.168.0.1:9200/_cluster/reroute' -d '{
"commands": [{
"move": {
"index": "test_index_20190905", # 索引名
"shard": 3, # 分片号
"from_node": "es01", # 分片所在原节点
"to_node": "es02" # 分片移动的目标节点
}
}]
}'
- 查看分片迁移状态
curl '192.168.0.1:9200/_cat/shards/test_index_20190905'
#带所有分片都为START状态即迁移成功
- 打开集群自平衡
curl -XPUT 192.168.0.1:9200/_cluster/settings -d '{
"transient": {
"cluster.routing.rebalance.enable": "all"
}
}'
2、索引跨集群迁移
- 快照导出
1)检查确认原ES状态
curl 'http://192.168.0.1:9200/_cluster/health?pretty'
2)创建原集群备份仓库
curl -XPOST "http://192.168.0.1:9200/_snapshot/mybackup" -d '{
"type": "fs",
"settings": {
"location": "/esbackup/restore",
"compress": "true",
"max_snapshot_bytes_per_sec": "300mb",
"max_restore_bytes_per_sec": "300mb"
}
}'
3)查看仓库
curl -XGET "http://192.168.0.1:9200/_snapshot/mybackup?pretty"
curl -XGET "http://192.168.0.1:9200/_snapshot/_all?pretty"
4)检查索引
curl "http://192.168.0.1:9200/_cat/indices/alert_2019.07.2*" | sort -k2
5)创建快照
curl -XPUT "http://192.168.0.1:9200/_snapshot/mybackup/alert_backup_20190801?wait_for_completion=true" -d '{
"indices": "alert_2019.07.2*",
"ignore_unavaliable": "true",
"include_global_state": "false"
}'
6)查看快照
curl -XGET "http://192.168.0.1:9200/_snapshot/mybackup/_all?pretty"
curl -XGET "http://192.168.0.1:9200/_snapshot/mybackup/alert_backup_20190801?pretty"
curl -XGET "http://192.168.0.1:9200/_snapshot/mybackup/alert_backup_20190801/_status?pretty"
curl -XGET "http://192.168.0.1:9200/_snapshot/mybackup/_current?pretty"
- 快照恢复
1)检查确认新集群ES状态
curl 'http://192.168.126.1:9202/_cluster/health?pretty'
2)创建新集群备份仓库,与原集群一致
curl -XPUT "http://192.168.126.1:9202/_snapshot/mybackup" -d '{
"type": "fs",
"settings": {
"location": "/esbackup/restore",
"compress": "true",
"max_snapshot_bytes_per_sec": "300mb",
"max_restore_bytes_per_sec": "300mb"
}
}'
3)查看仓库
curl -XGET "http://192.168.126.1:9202/_snapshot/mybackup?pretty"
curl -XGET "http://192.168.126.1:9202/_snapshot/_all?pretty"
4)查看快照
curl -XGET "http://192.168.126.1:9202/_snapshot/mybackup/_all?pretty"
curl -XGET "http://192.168.126.1:9202/_snapshot/mybackup/alert_backup_20190801?pretty"
curl -XGET "http://192.168.126.1:9202/_snapshot/mybackup/alert_backup_20190801/_status?pretty"
curl -XGET "http://192.168.126.1:9202/_snapshot/mybackup/_current?pretty"
5)快照恢复
curl -XPOST "http://192.168.126.1:9202/_snapshot/mybackup/alert_backup_20190801/_restore" -d '{
"indices": "alert_2019.07.2*"
"index_settings": {
"index.number_of_replicas": 1
}
}'
6)监控恢复进度
curl -XGET "http://192.168.126.1:9202/alert_2019.07.2*/_recovery?pretty"
7)检查索引
curl -XGET "http://192.168.126.1:9202/_cat/indices/alert_2019.07.2*?v" | sort -k3
- 删除快照和仓库
#新集群
curl -XDELETE "http://192.168.126.1:9202/_snapshot/mybackup/alert_backup_20190801"
curl -XDELETE "http://192.168.126.1:9202/_snapshot/mybackup"
#旧集群
curl -XDELETE "http://192.168.0.1:9200/_snapshot/mybackup/alert_backup_20190801"
curl -XDELETE "http://192.168.0.1:9200/_snapshot/mybackup"
#清快照库目录
rm -rf /esbackup/restore/*
五、Elasticsearch节点滚动重启
- 停止集群自平衡
curl -XPUT '192.168.0.1:9200/_cluster/settings' -d '{
"transient": {
"cluster.routing.allocation.enable": "none", # 主要影响集群中新创建的索引无法进行分片分配
"cluster.routing.rebalance.enable": "none" # 主要影响集群中已有的分片不会平衡到其他节点
}
}'
- 重启ES实例
ps -ef | grep 'elasticsearch01' | grep -v grep | awk '{system("kill "$2"")}'
sleep 5
cd /app/elasticsearch01/bin
./elasticsearch -d
- 恢复集群自平衡
curl -XPUT '192.168.0.1:9200/_cluster/settings' -d '{
"transient": {
"cluster.routing.allocation.enable": "all",
"cluster.routing.rebalance.enable": "all"
}
}'
- 临时提高分片恢复参数
curl -XPUT '192.168.0.1:9200/_cluster/settings' -d '{
"transient": {
"indices.recovery.max_bytes_per_sec": "500mb", # 数据在节点之间传输的最大带宽
"cluster.routing.allocation.node_concurrent_recoveries": "50", # 同时recovery的并发数
"cluster.routing.allocation.node_initial_primaries_recoveries": "10", # 并发恢复的分片数
"indices.recovery.concurrent_streams": "5" # 同时读取数据文件流的线程数
}
}'
- 检查集群状态
curl '192.168.0.1:9200/_cluster/health?pretty' # 集群状态为green后执行下一步
- 恢复参数到缺省值
curl -XPUT '192.168.0.1:9200/_cluster/settings' -d '{
"transient": {
"indices.recovery.max_bytes_per_sec": "40mb",
"cluster.routing.allocation.node_concurrent_recoveries": "2",
"cluster.routing.allocation.node_initial_primaries_recoveries": "4",
"indices.recovery.concurrent_streams": "2"
}
}'
参考链接:
https://www.cnblogs.com/kaynet/p/5861926.html —— Elasticsearch常用配置及性能参数