Elasticsearch(es)相关操作记录-笔记

不是教程,随心记

分段存储,不允许修改:
删除只是标记,修改是先增加再删除
对于很少update是很友好的,但是如果频繁update,则会效率低下
先写请求日志再延迟分析和加载,可以提高建立索引和写磁盘的性能,但是查询会有时延

段合并:
Elasticsearch 通过在后台定期进行段合并来解决这个问题。小的段被合并到大的段,然后这些大的段再被合并到更大的段。
段合并的时候会将那些旧的已删除文档从文件系统中清除。被删除的文档不会被拷贝到新的大段中。合并的过程中不会中断索引和搜索。

先暂停集群的shard自动均衡
curl -XPUT -H 'Content-type':'application/json' http://10.9.2.51:9200/_cluster/settings -d'{"transient":{"cluster.routing.allocation.enable":"none"}}'
注意:每个节点重启都需要先关闭自动分片、再开启自动分片

elasticsearch集群配置:
cat /etc/elasticsearch/elasticsearch.yml

elasticsearch修改内存:
vim /etc/elasticsearch/jvm.options
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms35g
-Xmx35g

注意:
确保堆内存最小值( Xms )与最大值( Xmx )的大小是相同的,防止程序在运行时改变堆内存大小。
为linux预留一定的内存让其缓存频繁访问的文件,可以提高读写性能
建议留一半给os缓存,JVM堆内存大小并且尽量不要超过32G

JVM内存:
bin/elasticsearch.in.sh ES_HEAP_SIZE=???m

关闭多播配置集群节点:
/etc/elasticsearch/elasticsearch.yml
里面discovery.zen.ping.multicast:
	enabled
里面discovery.zen.ping.unicast.hosts

目录(需要分权限给elasticsearch启动用户):
chmod -R 777 /usr/share/elasticsearch
chmod -R 777 /var/log/elasticsearch
chmod -R 777 /var/lib/elasticsearch
chmod -R 777 /etc/sysconfig/elasticsearch
chmod -R 777 /etc/elasticsearch
chmod -R 777 /linkcld/data/
df -h看数据加载点,应该都要chmod -R 777 


添加用户:
adduser elsearch

需要设置句柄限制:
vim /etc/security/limits.conf
elsearch       soft    nofile  75535
elsearch       hard    nofile  75535
elsearch       soft    nproc  75535
elsearch       hard    nproc  75535

su elsearch
ulimit -a

停止elasticsearch应用:
exit
ps -ef|grep ela
kill -9 pid


启动:
su elsearch
cd /usr/share/elasticsearch/bin
./elasticsearch -d

日志:
tail -f /var/lib/elasticsearch/logs/hzjtj.log
[注意:启动会很慢,过很久才能看到侦听端口,特别是loaded plugin x-pack-watcher后面很慢]

查询参数:
curl http://10.9.2.51:9200/_cluster/settings?include_defaults&flat_settings&local&filter_path=defaults.indices*

查询节点状态:
curl '10.9.2.51:9200/_cat/health?v'

查询所有索引:
curl -X GET "10.9.2.51:9200/_cat/indices?v" > indices.txt

es集群:
discovery.zen.ping.unicast.hosts: ["172.18.109.243", "172.18.109.244", "172.18.109.245", "172.18.110.150", "172.18.110.151"]
7sCfN5gUTXOcl9XX_heb-Q node1 172.18.109.243
lSyWSAQwRQCBRTRe682MIg node2 172.18.109.244
WIboyqs7RUu0empb7zUv4Q node3 172.18.109.245
8tAHpcWsRQ-GW9w3N7i9hA node5 172.18.110.151

shutdown你要升级的节点(2.x被删除)
curl -XPOST  http://127.0.0.1:9200/_cluster/nodes/_local/_shutdown

升级重启节点,并确认该节点重新加入集群中
cd /usr/share/elasticsearch/bin
./elasticsearch -d

重复2-3步,升级重启其它要升级的节点。

通过多播,自动发现其它集群节点

1. elasticsearch启动后查看是否启动成功:
curl -XGET "http://10.9.2.51:9200/_cluster/health?pretty=true"

2. 检查集群状态:
curl http://10.9.2.51:9200/_cluster/stats?pretty

3. 查看集群健康:
curl http://10.9.2.51:9200/_cluster/health?pretty

4. 查看参数设置:
curl http://10.9.2.51:9200/_cluster/settings?pretty

5. 节点状态:
curl http://10.9.2.51:9200/_nodes/process?pretty

6. 当你不知道有那些属性可以查看时:
curl http://10.9.2.51:9200/_cat/,会返回可以查看的属性

7. 查看集群节点
curl http://10.9.2.51:9200/_nodes?pretty

8. 查看分片
curl http://10.9.2.51:9200/_cat/shards?help
curl http://10.9.2.51:9200/_cat/shards?h=index,shard,prirep,state,docs,store,ip,node,unassigned.reason,unassigned.details

9. 查看mapping
curl -XGET "http://10.9.2.51:9200/chargestation-index/_mapping?pretty"

CAT显示更易读:
curl '10.9.2.51:9200/_cat/health?v'
allocation--分配到每个节点的分片数量
count--统计文档数量
health--集群的健康状况
indices--索引信息
master--集群主节点
nodes--集群中所有节点
recovery--集群中正在进行的分片恢复
shards--集群分片信息
plugins--已安装的插件


查看指定索引库的健康状态
http://10.9.2.51:9200/_cluster/health/index_name?pretty
http://10.9.2.51:9200/_cluster/health/index_name,index_name2?pretty

重启启动集群的shard均衡
curl -XPUT http://10.9.2.51:9200/_cluster/settings -d'{"transient":{"cluster.routing.allocation.enable":"all"}}’
注意:每个节点重启都需要先关闭自动分片、再开启自动分片

为了便于聚合和范围查询,es里面的fielddata所有不同值都会放到内存,
可能出现内存不够被熔断的错误:
1)调整内存门限
curl -XPUT '10.9.2.51:9200/_cluster/settings' -d '
{
	"transient":{
		"indices.breaker.fielddata.limit":"??mb or 60%"
	}
}'
并且通过 vim /etc/elasticsearch/jvm.options
	-Xms35g
	-Xmx35g
或vim bin/elasticsearch.in.sh
	ES_HEAP_SIZE=???m
增加JVM内存堆大小
2)如果内存不足,
就需要单独将用于聚合或范围查询且值很多的字段单独写磁盘(比如日志系统的日期字段)
curl -XPOST '10.9.2.51:9200/myindex' -d '
{
	"mappings":{
		"document":{
			"properties":{
				"title":{
					"doc_value":true
				}
			}
		}
	}
}'
(注:如果索引已经存在,可能需要导出索引,删除索引,然后修改定义并导入以重建索引)

可以修改elasticsearch.yml,也可以动态调整门限:
curl -XPUT 10.9.2.51:9200/_cluster/settings -d '{
  "persistent" : {
    "indices.breaker.request.limit" : "41%" 
  }
}'
indices.breaker.total.limit--默认是堆内存的70%,控制总的缓存大小
indices.breaker.fieldata.limit--默认是堆内存的60%,控制字段缓存大小,可通过调整门限、堆内存大小、设置"doc_value":true单独保存字段到磁盘解决
indices.breaker.request.limit--默认是堆内存的40%,控制分配给聚合桶创建这种操作的堆大小,可通过调整门限、堆内存大小、修改查询请求解决
参见:
https://www.elastic.co/guide/en/elasticsearch/reference/current/circuit-breaker.html

提升性能:
批量索引、更新、删除,一次请求一批插入、更新、删除
对于es这种查询为主的存储,写入是有一定时延的,
更新也是通过增删实现,调整flush和建立映射的间隔、以及调整index合并时机,可以提高写入性能
频繁的相同条件查询且内容很少变化时,过滤器缓存可以加快后续查询速度
elasticsearch.yml中定义的indices.cache.filter.size、indices.fielddata.cache.size,
百分比是相对于最大堆内存总数
可以通过下面方式查看:
curl 10.9.2.51:9200/_nodes/stats/indices/fielddata?fields=*&pretty
curl 10.9.2.51:9200/_nodes/stats/fielddata?fields=*&pretty
curl 10.9.2.51:9200/_nodes/stats/indices/fielddata?level=indices&fields=*&pretty
evictions过大表明缓存太小

根Redis类似,处理es集群的问题,需要熟练通过查询接口了解集群状态,根据结果分析出问题所在

elasticsearch处理文档间关系:
1)对象映射:处理1对1关系,对象数组拆成多个属性数组保存,多条件查询会不准确
2)嵌套文档:处理1对多关系,保存到同一个磁盘块,适合子对象不太多的情况
3)父子结构:保存关系指针的多个文档,存在同一个分片中。但是插入和查询时需维护文档间关系
4)反范式:保存重复数据,可以应付多对多关系,缺点是修改不方便,而且占多余磁盘空间
5)应用层连接:存储和读取时,类似关系型数据库的外键,通过id关联,分开存储和读取

一个索引的分片数默认是5,一旦创建不能修改
如果索引太大,又想分布到更多的节点,只能是垂直或水平切分成多个索引,类似Mysql分表方案
可以进行索引收缩和拆分
POST my_source_index/_shrink/my_target_index
{
"settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1,
    "index.codec": "best_compression"
}}

提高shard数量、减少副本数可提高构造索引的速度
提高shard数量、增加副本数可提高查询的速度

别名很有用:
为索引创建别名的好处,类似linux里面的软链接,创建新的索引扩容时不用改应用层的代码
然后还可以一个别名包含多个索引,比如7-days的别名,指向7个以day为单位的索引


模版很有用:
将配置应用到一批索引,不用逐个索引单独配置
curl 10.9.2.51:9200/_template
添加模版:
curl -XPUT '10.9.2.51:9200/_template/loggin_index' -d '{}'
删除模版:
curl -XDELETE '10.9.2.51:9200/_template/loggin_index'

动态映射:
自动确定新加入字段的类型,可以通过-XPUT调整mapping下面各字段的dynamic属性来配置

监控集群健康状况:
curl 10.9.2.51:9200/_cluster/health?pretty
relocating_shards--大于0表示正在集群你移动分片,比如添加新节点、重启失效节点、删除节点
initializing_shards--大于0表示添加新索引或重启节点
unassigned_shards--大于0表示节点不足,有未分片的副本分片
绿色--主分片和副分片都已分发且运行正常
黄色--副本分片丢失
红色--主分片丢失,可能是节点从集群中消失

为了进一步定位问题:
curl 10.9.2.51:9200/_cluster/health?level=indices&pretty
查看每个索引的分片状况


可以通过慢日志来定位慢在哪里,默认是关闭的
可以通过'{index_name}/_settings'设置时间门限
在logging.yml文件中,配置日志输出文件及输出格式

CPU使用率定位:
curl 10.9.2.51:9200/_nodes/hot_threads

导出索引:
elasticdump --input=http://10.9.2.51:9200/chargestation-index  --output=chargestation-index.json --limit=500
elasticdump --input=/tmp/evcarrealdata-index.json    --output=http://10.9.2.234:9200/

本地es:
10.9.2.234

设置max_result_window:
PUT indexName/_settings
{
  "index":{
    "max_result_window":100000000
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值