ElasticSearch教程FG015-ElasticSearch索引别名与滚动实战
Part01-基础概念与理论知识
1.1 索引别名概念
索引别名(Index Alias)是ElasticSearch中一个重要的功能,它允许为一个或多个索引创建一个别名,通过这个别名可以访问这些索引。索引别名提供了一种灵活的方式来管理索引,特别是在索引版本升级、数据迁移等场景中非常有用。更多视频教程www.fgedu.net.cn
索引别名的特点:
- 可以指向一个或多个索引
- 可以在运行时添加或移除索引
- 提供了一种透明的索引管理方式
- 支持路由和过滤条件
1.2 索引滚动概念
索引滚动(Index Rollover)是一种自动管理索引的机制,当索引达到一定条件(如大小、文档数、时间)时,自动创建新的索引,并将别名指向新索引。索引滚动非常适合日志、监控等时间序列数据的管理。
索引滚动的特点:
- 自动管理索引生命周期
- 基于条件触发滚动
- 无缝切换到新索引
- 减少索引大小,提高性能
1.3 生命周期管理
索引生命周期管理(ILM)是ElasticSearch提供的一种自动化索引管理功能,它可以根据预定义的策略自动执行索引的滚动、收缩、冻结和删除等操作。
生命周期管理的阶段:
- 热(Hot):活跃写入和查询的索引
- 温(Warm):不再写入但仍需查询的索引
- 冷(Cold):很少查询的索引
- 删除(Delete):不再需要的索引
学习交流加群风哥微信: itpux-com
Part02-生产环境规划与建议
2.1 索引管理策略
生产环境中的索引管理策略:
- 使用索引别名统一访问接口
- 实施索引滚动策略管理时间序列数据
- 合理设置索引大小和文档数阈值
- 制定索引生命周期管理策略
- 定期清理过期索引
2.2 滚动策略配置
滚动策略配置建议:
- 根据数据量设置合理的大小阈值
- 根据业务需求设置时间阈值
- 配置合适的索引模板
- 设置合理的分片和副本数
- 监控滚动操作的执行情况
2.3 生产环境最佳实践
生产环境中,索引别名和滚动应注意:
- 使用别名作为应用程序的访问点
- 实施自动化的索引管理
- 监控索引大小和性能
- 测试滚动操作的可靠性
- 制定应急恢复方案
学习交流加群风哥QQ113257174
Part03-生产环境项目实施方案
3.1 索引别名实战
索引别名的使用:
curl -X PUT “http://192.168.1.10:9200/fgedu-products-v1” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“product_id”: {
“type”: “keyword”
},
“product_name”: {
“type”: “text”,
“analyzer”: “ik_max_word”
},
“price”: {
“type”: “double”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products-v1”
}
# 创建别名
curl -X POST “http://192.168.1.10:9200/_aliases” -H “Content-Type: application/json” -d ‘{
“actions”: [
{
“add”: {
“index”: “fgedu-products-v1”,
“alias”: “fgedu-products”
}
}
]
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
# 查看别名
curl -X GET “http://192.168.1.10:9200/_alias/fgedu-products”
# 执行
# 输出日志
{
“fgedu-products-v1”: {
“aliases”: {
“fgedu-products”: {}
}
}
}
# 通过别名访问索引
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search”
# 执行
# 输出日志
{
“took”: 2,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 0,
“relation”: “eq”
},
“max_score”: null,
“hits”: []
}
}
3.2 索引滚动实战
索引滚动的使用:
curl -X PUT “http://192.168.1.10:9200/fgedu-logs-000001” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1,
“index.lifecycle.name”: “logs_policy”,
“index.lifecycle.rollover_alias”: “fgedu-logs”
},
“mappings”: {
“properties”: {
“timestamp”: {
“type”: “date”
},
“level”: {
“type”: “keyword”
},
“message”: {
“type”: “text”
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-logs-000001”
}
# 创建别名
curl -X POST “http://192.168.1.10:9200/_aliases” -H “Content-Type: application/json” -d ‘{
“actions”: [
{
“add”: {
“index”: “fgedu-logs-000001”,
“alias”: “fgedu-logs”,
“is_write_index”: true
}
}
]
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
# 手动触发滚动
curl -X POST “http://192.168.1.10:9200/fgedu-logs/_rollover” -H “Content-Type: application/json” -d ‘{
“conditions”: {
“max_age”: “7d”,
“max_size”: “30gb”,
“max_docs”: 1000000
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“old_index”: “fgedu-logs-000001”,
“new_index”: “fgedu-logs-000002”,
“rolled_over”: true,
“dry_run”: false,
“conditions”: {
“[max_age: 7d]”: false,
“[max_size: 30gb]”: false,
“[max_docs: 1000000]”: false
}
}
3.3 生命周期管理实战
生命周期管理的使用:
curl -X PUT “http://192.168.1.10:9200/_ilm/policy/logs_policy” -H “Content-Type: application/json” -d ‘{
“policy”: {
“phases”: {
“hot”: {
“actions”: {
“rollover”: {
“max_size”: “30gb”,
“max_age”: “7d”
}
}
},
“warm”: {
“min_age”: “30d”,
“actions”: {
“shrink”: {
“number_of_shards”: 1
},
“forcemerge”: {
“max_num_segments”: 1
}
}
},
“cold”: {
“min_age”: “60d”,
“actions”: {
“freeze”: {}
}
},
“delete”: {
“min_age”: “90d”,
“actions”: {
“delete”: {}
}
}
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
# 查看生命周期策略
curl -X GET “http://192.168.1.10:9200/_ilm/policy/logs_policy”
# 执行
# 输出日志
{
“logs_policy”: {
“version”: 1,
“modified_date”: “2024-01-01T00:00:00.000Z”,
“policy”: {
“phases”: {
“hot”: {
“actions”: {
“rollover”: {
“max_size”: “30gb”,
“max_age”: “7d”
}
}
},
“warm”: {
“min_age”: “30d”,
“actions”: {
“shrink”: {
“number_of_shards”: 1
},
“forcemerge”: {
“max_num_segments”: 1
}
}
},
“cold”: {
“min_age”: “60d”,
“actions”: {
“freeze”: {}
}
},
“delete”: {
“min_age”: “90d”,
“actions”: {
“delete”: {}
}
}
}
}
}
}
更多学习教程公众号风哥教程itpux_com
Part04-生产案例与实战讲解
4.1 日志索引管理
日志索引管理场景:
curl -X PUT “http://192.168.1.10:9200/_index_template/fgedu-logs-template” -H “Content-Type: application/json” -d ‘{
“index_patterns”: [“fgedu-logs-*”],
“template”: {
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1,
“index.lifecycle.name”: “logs_policy”,
“index.lifecycle.rollover_alias”: “fgedu-logs”
},
“mappings”: {
“properties”: {
“timestamp”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss.SSS”
},
“level”: {
“type”: “keyword”
},
“message”: {
“type”: “text”
},
“service”: {
“type”: “keyword”
},
“host”: {
“type”: “keyword”
}
}
}
},
“priority”: 100,
“version”: 1
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
# 创建初始日志索引
curl -X PUT “http://192.168.1.10:9200/fgedu-logs-000001” -H “Content-Type: application/json” -d ‘{
“aliases”: {
“fgedu-logs”: {
“is_write_index”: true
}
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-logs-000001”
}
# 写入日志数据
curl -X POST “http://192.168.1.10:9200/fgedu-logs/_doc” -H “Content-Type: application/json” -d ‘{
“timestamp”: “2024-01-01 10:00:00.123”,
“level”: “INFO”,
“message”: “Application started successfully”,
“service”: “fgedu-api”,
“host”: “server1”
}’
# 执行
# 输出日志
{
“_index”: “fgedu-logs-000001”,
“_id”: “_cQH044BMQz8e7k9eJ6-“,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 0,
“_primary_term”: 1
}
4.2 电商数据索引管理
电商数据索引管理场景:
curl -X PUT “http://192.168.1.10:9200/_index_template/fgedu-products-template” -H “Content-Type: application/json” -d ‘{
“index_patterns”: [“fgedu-products-v*”],
“template”: {
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“product_id”: {
“type”: “keyword”
},
“product_name”: {
“type”: “text”,
“analyzer”: “ik_max_word”
},
“price”: {
“type”: “double”
},
“category”: {
“type”: “keyword”
},
“stock”: {
“type”: “integer”
},
“created_at”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss”
}
}
}
},
“priority”: 100,
“version”: 1
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
# 创建v1版本索引
curl -X PUT “http://192.168.1.10:9200/fgedu-products-v1”
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products-v1”
}
# 创建别名
curl -X POST “http://192.168.1.10:9200/_aliases” -H “Content-Type: application/json” -d ‘{
“actions”: [
{
“add”: {
“index”: “fgedu-products-v1”,
“alias”: “fgedu-products”
}
}
]
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
# 当需要升级索引时,创建v2版本
curl -X PUT “http://192.168.1.10:9200/fgedu-products-v2”
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products-v2”
}
# 切换别名到v2版本
curl -X POST “http://192.168.1.10:9200/_aliases” -H “Content-Type: application/json” -d ‘{
“actions”: [
{
“remove”: {
“index”: “fgedu-products-v1”,
“alias”: “fgedu-products”
}
},
{
“add”: {
“index”: “fgedu-products-v2”,
“alias”: “fgedu-products”
}
}
]
}’
# 执行
# 输出日志
{
“acknowledged”: true
}
4.3 性能调优实战
索引别名和滚动的性能调优:
curl -X POST “http://192.168.1.10:9200/fgedu-logs/_rollover” -H “Content-Type: application/json” -d ‘{
“conditions”: {
“max_size”: “50gb”,
“max_age”: “14d”,
“max_docs”: 5000000
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“old_index”: “fgedu-logs-000002”,
“new_index”: “fgedu-logs-000003”,
“rolled_over”: true,
“dry_run”: false,
“conditions”: {
“[max_size: 50gb]”: false,
“[max_age: 14d]”: false,
“[max_docs: 5000000]”: false
}
}
# 性能调优 – 使用shrink操作减少分片
curl -X POST “http://192.168.1.10:9200/fgedu-logs-000001/_shrink/fgedu-logs-000001-shrunk” -H “Content-Type: application/json” -d ‘{
“settings”: {
“index.number_of_shards”: 1,
“index.number_of_replicas”: 1
}
}’
# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-logs-000001-shrunk”
}
风哥提示:合理设置滚动条件和分片数量可以显著提高索引管理的性能
Part05-风哥经验总结与分享
5.1 索引管理最佳实践
- 使用索引别名统一访问接口
- 实施自动化的索引滚动策略
- 合理设置生命周期管理策略
- 定期监控索引大小和性能
- 制定索引备份和恢复策略
5.2 常见问题与解决方案
- 索引大小过大:实施滚动策略
- 查询性能下降:优化分片设计
- 滚动操作失败:检查索引状态和权限
- 别名切换出错:确保新索引已就绪
- 生命周期策略不生效:检查策略配置和索引状态
5.3 生产环境调优建议
- 根据数据量设置合理的滚动条件
- 优化分片和副本配置
- 监控索引性能指标
- 实施自动化的索引管理
- 测试索引操作的可靠性
from ElasticSearch视频:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
