Skip to main content

elasticsearch

ElasticSearch 安装配置

创建 docker-compse.yml 文件
version: '3' 

networks:
es:

services:

elasticsearch:
image: elasticsearch:7.10.1
container_name: elasticsearch
restart: always
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- ./data:/usr/share/elasticsearch/data
- ./logs:/usr/share/elasticsearch/logs
- ./config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
- 9300:9300
privileged: true
environment:
# 开启内存锁定
- bootstrap.memory_lock=true
# 修改jvm内存
- ES_JAVA_OPTS=-Xms512m -Xmx512m
- TAKE_FILE_OWNERSHIP=true
# 指定单节点启动
- discovery.type=single-node
ulimits:
# 取消内存相关限制 用于开启内存锁定
memlock:
soft: -1
hard: -1
networks:
- es
在对应的文件目录下,创建elasticsearch.yml 文件
cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.port: 9200
# 开启es跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
# 开启安全控制
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

ES和Kibana同时启动

version: '3'

networks:
es:

services:

elasticsearch:
image: elasticsearch:7.10.1
container_name: elasticsearch
restart: always
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- /mnt/data/efk/es/data:/usr/share/elasticsearch/data
- /mnt/data/efk/es/logs:/usr/share/elasticsearch/logs
- /mnt/data/efk/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /mnt/data/efk/es/plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
- 9300:9300
privileged: true
environment:
# 开启内存锁定
- bootstrap.memory_lock=true
# 修改jvm内存
- ES_JAVA_OPTS=-Xms2g -Xmx2g
- TAKE_FILE_OWNERSHIP=true
# 指定单节点启动
- discovery.type=single-node
ulimits:
# 取消内存相关限制 用于开启内存锁定
memlock:
soft: -1
hard: -1
networks:
- es

kibana:
image: kibana:7.10.1
container_name: kibana
privileged: true
restart: always
volumes:
- /mnt/data/efk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
ports:
- 5601:5601
networks:
- es
运行 启动命令
 docker-compose up -d
修改用户密码/进入容器
  docker exec -it elasticsearch sh 
进入密码设置
 ./bin/elasticsearch-setup-passwords -h
设置密码 (自动生成模式,定义配置 输入 ./bin/elasticsearch-setup-passwords interactive )
  ./bin/elasticsearch-setup-passwords auto

验证

在浏览器输入 http://xxxx.xxx.xxx.xxx:9200/

命令行简单验证查询命令
//查询es是否正常运行
curl -XGET 'http://localhost:9200'

// 查看所有索引及基础信息
curl -XGET 'http://localhost:9200/_cat/indices'

// 查询日志
curl -XGET 'http://localhost:9200/_search'

// 查询指定索引名日志
curl -XGET 'http://localhost:9200/sillegalaccess/_search'

// 查看索引内的映射关系
curl -XGET 'http://localhost:9200/索引名/_mapping'

// 查看集群状态
curl -XGET 'http://localhost:9200/_cluster/health'

// 查看集群详细状态
curl -XGET 'http://localhost:9200/_cluster/stats'

// 查看集群配置
curl -XGET 'http://localhost:9200/_cluster/settings'

// 查看索引存放的shards
curl -XGET 'http://localhost:9200/_cat/shards'

// 查看master机器
curl -XGET 'http://localhost:9200/_cat/master?v'

// 查看所有nodes
curl -XGET 'http://localhost:9200/_cat/nodes?v'

// 删除索引及索引中的数据
curl -X DELETE "http://localhost:9200/索引名"

// 删除索引中的数据,保留索引
curl -X POST "http://localhost:9200/索引名/_delete_by_query" -H 'Content-Type: application/json' -d'{"query": {"match_all": {}}}'

// 需要登陆的查询索引
curl -u elastic:JQs4lKgbXKnJjn9G+Mgn http://localhost:9200/_cat/indices

// 刷新索引数据
curl -X POST "http://localhost:9200/<索引名>/_refresh?pretty"

// 刷新全部索引数据
curl -X POST "http://localhost:9200/_refresh?pretty"

// 需要登陆的刷新
curl -X POST -u elastic:JQs4lKgbXKnJjn9G+Mgn http://localhost:9200/_refresh?pretty

// 查询某一个索引最近10条记录
curl -s -X GET -u elastic:JQs4lKgbXKnJjn9G+Mgn "http://localhost:9200/illegalaccess-linux-http.192.168.8.196.8020/_search" -H "Content-Type: application/json" -d '{"size":10,"sort":[{"timestamp":{"order":"desc"}}],"query":{"bool":{"must":[{"exists":{"field":"log"}},{"exists":{"field":"url"}}]}}}' | jq '.hits.hits[] | {id: ._id, timestamp: ._source.timestamp, log: ._source.log, url: ._source.url}'

// 复制一个索引的全部数据
curl -XPOST -u elastic:JQs4lKgbXKnJjn9G+Mgn "http://localhost:9200/_reindex?pretty" -H "Content-Type: application/json" -d'{"source":{"index":"blackmail-test"},"dest":{"index":"test_delete"}}'

// 清除scroll镜像
curl -X DELETE "http://localhost:9200/_search/scroll/_all" -H "Content-Type: application/js+Mgn"u "elastic:JQs4lKgbXKnJjn9G+

// 查看ELK用户权限
curl -X GET "http://localhost:9200/_security/role/superuser" -u elastic:JQs4lKgbXKnJjn9G+Mgn

增删改查命令

1、新增索引
curl -H "Content-Type:application/json;charset=utf-8"  -XPUT 'http://127.0.0.1:9200/test_0527'
2、新增mapping
type为"text"类型的字段:

text:会被分词,然后进行索引,支持模糊查询;不支持聚合(适用于全文搜索)

keyword:不进行分词,直接索引;精确查询;支持聚合(适用于关键词搜索)

curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/doc/_mapping?' -d '
{
"doc": {
"date_detection": false,
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"mobile_phone": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}'
3、查询mapping
curl -H "Content-Type:application/json;charset=utf-8" -XGET 'http://127.0.0.1:9200/test_0527/_mapping?pretty'
4、插入文档
curl -H "Content-Type:application/json;charset=utf-8"  -XPOST  'http://127.0.0.1:9200/test_0527/doc?' -d 
'{"name":"张三","mobile_phone":"13522335566"}'
5、查询文档
// 全文查询
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty'

//分页查询
curl -H "Content-Type:application/json;charset=utf-8" -XGET 'http://127.0.0.1:9200/fluentd.test01/_search?from=20&size=10'

// 全文查询返回限定字段
curl -H "Content-Type:application/json;charset=utf-8" -POST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"_source": ["mobile_phone"]}'

// 条件查询,短语精准匹配
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"query":{"match_phrase":{"name":"王五"}}}'

// 条件查询,term精准查询
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/doc/_search?' -d '{"query":{"term":{"mobile_phone.keyword":"13522335566"}}}'

// 条件查询,分词匹配
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"query":{"match":{"name":"王"}}}'
6、删除文档
curl -H "Content-Type:application/json;charset=utf-8"  -XDELETE  'http://127.0.0.1:9200/test_0527/doc/zo30-GoByqlpGcLNsU9t?'
7、给索引添加别名
curl -H "Content-Type:application/json;charset=utf-8"  -XPOST 'http://127.0.0.1:9200/_aliases' -d '{"actions": [{"add":{"index":"test_0527", "alias":"test"}}]}'
8、查询索引某个字段存在的文档
curl -H "Content-Type:application/json;charset=utf-8"  -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"query":{"exists" : {"field": "mobile_phone"}}}'

常用命令

1.cat 命令是监控 es 的节点,内存,索引,分片,集群状态等一些基本信息。
curl -H "content-tpye:application/json" -XGET 'http://127.0.0.1:9200/_cat/'
2.cat 命令快速使用——命令查询参数。
curl -H "content-tpye:application/json" -XGET 'http://127.0.0.1:9200/_cat/master?v'
3.【help】—— 帮助了解cat 相关指令支持哪些功能,返回参数第一列显示完整的名称,第二列显示缩写,第三列提供了关于这个参数的简介。
curl -H "content-tpye:application/json" -XGET 'http://127.0.0.1:9200/_cat/master?help'
4.【h】—— 指定字段输出。
curl -H "content-tpye:application/json" -XGET 'http://127.0.0.1:9200/_cat/master?h=ip,host'
5.查询集群内所有节点信息。
 curl -H "content-tpye:application/json" -XGET 'http://127.0.0.1:9200/_cat/nodes?v'

// 返回结果:堆内存,内存,cpu百分比, 最近1,5,15分钟 节点的负载,显示主节点( * 标记主节点),节点名等信息。
6.查询各节点机器存储信息。
curl -H "Content-Type:application/json;charset=utf-8"  -XGET 'http://127.0.0.1:9200/_cat/allocation?v'

// 返回结果:节点分片数,索引占用磁盘大小,磁盘已使用容量大小,磁盘可用容量大小,磁盘总容量大小,磁盘使用率等节点信息。
7.查询索引信息。
 curl -H "Content-Type:application/json;charset=utf-8"  -XGET 'http://127.0.0.1:9200/_cat/indices?v'

// 返回结果:索引的健康状态,索引名,索引主分片,副本大小,文档数,被删除文档数,索引主分片,副本 总占用存储空间。
8.查询分片信息
curl -H "Content-Type:application/json;charset=utf-8"  -XGET 'http://127.0.0.1:9200/_cat/shards?v'

// 返回结果:索引名称,分片序号,主副分片标志,该分片存储空间,分片存储的文档数,分片所属节点ip,节点名。
9.查询集群健康状态。
curl -H "content-tpye:application/json" -XGET 'http://127.0.0.1:9200/_cat/health?v'

// 返回结果:集群名称,集群状态,节点数,数据节点数,分片数,主分片数,激活的分片百分比(active_shards_percent)。
10.查询集群所有的别名索引。
curl -H "Content-Type:application/json;charset=utf-8"  -XGET 'http://127.0.0.1:9200/_cat/aliases?v'
11.查询主节点信息。
curl -H "Content-Type:application/json;charset=utf-8"  -XGET 'http://127.0.0.1:9200/_cat/master?v'
12.快速查询当前整个集群或者指定索引的document的数量(不包括删除的但是还没有清理掉的document)。
curl -H "Content-Type:application/json;charset=utf-8"  -XGET 'http://127.0.0.1:9200/_cat/count'

配置文件详解

#ES集群名称,同一个集群内的所有节点集群名称必须保持一致
cluster.name: ES-Cluster

#ES集群内的节点名称,同一个集群内的节点名称要具备唯一性
​node.name: ES-master-01

#允许节点是否可以成为一个master节点,ES是默认集群中的第一台机器成为master,如果这台机器停止就会重新选举
node.master: true

#允许该节点存储索引数据(默认开启)
node.data: true

#数据存储路径(path可以指定多个存储位置,分散存储,有助于性能提升)
#目录还要对elasticsearch的运行用户有写入权限
path.data: /usr/local/elasticsearch-cluster/elasticsearch-a/data
path.logs: /usr/local/elasticsearch-cluster/elasticsearch-a/logs

#在ES运行起来后锁定ES所能使用的堆内存大小,锁定内存大小一般为可用内存的一半左右;锁定内存后就不会使用交换分区
#如果不打开此项,当系统物理内存空间不足,ES将使用交换分区,ES如果使用交换分区,那么ES的性能将会变得很差
bootstrap.memory_lock: true

#es绑定地址,支持IPv4及IPv6,默认绑定127.0.0.1;es的HTTP端口和集群通信端口就会监听在此地址上
network.host: 192.168.3.21​

#是否启用tcp无延迟,true为启用tcp不延迟,默认为false启用tcp延迟
network.tcp.no_delay: true
#是否启用TCP保持活动状态,默认为true
network.tcp.keep_alive: true
#是否应该重复使用地址。默认true,在Windows机器上默认为false
network.tcp.reuse_address: true
#tcp发送缓冲区大小,默认不设置
network.tcp.send_buffer_size: 128mb
#tcp接收缓冲区大小,默认不设置
network.tcp.receive_buffer_size: 128mb

#设置集群节点通信的TCP端口,默认是9300
transport.tcp.port: 9301
#设置是否压缩TCP传输时的数据,默认为false
transport.tcp.compress: true
#设置http请求内容的最大容量,默认是100mb
http.max_content_length: 200mb

#是否开启跨域访问
http.cors.enabled: true
#开启跨域访问后的地址限制,*表示无限制
http.cors.allow-origin: "*"
#定义ES对外调用的http端口,默认是9200
http.port: 9201

#Elasticsearch7.x新增参数,写入候选主节点的设备地址,来开启服务时就可以被选为主节点,由discovery.zen.ping.unicast.hosts:参数改变而来
discovery.seed_hosts: ["192.168.3.21:9301", "192.168.3.22:9301","192.168.3.23​:9301"]
#Elasticsearch7新增参数,写入候选主节点的设备地址,来开启服务时就可以被选为主节点
cluster.initial_master_nodes: ["192.168.3.21​:9301", "192.168.3.22:9301","192.168.3.23:9301"]

#Elasticsearch7新增参数,设置每个节点在选中的主节点的检查之间等待的时间。默认为1秒
cluster.fault_detection.leader_check.interval: 2s
#Elasticsearch7新增参数,启动后30秒内,如果集群未形成,那么将会记录一条警告信息,警告信息未master not fount开始,默认为10秒
discovery.cluster_formation_warning_timeout: 30s
#Elasticsearch7新增参数,节点发送请求加入集群后,在认为请求失败后,再次发送请求的等待时间,默认为60秒
cluster.join.timeout: 30s
#Elasticsearch7新增参数,设置主节点等待每个集群状态完全更新后发布到所有节点的时间,默认为30秒
cluster.publish.timeout: 90s
#集群内同时启动的数据任务个数,默认是2个
cluster.routing.allocation.cluster_concurrent_rebalance: 32
#添加或删除节点及负载均衡时并发恢复的线程个数,默认4个
cluster.routing.allocation.node_concurrent_recoveries: 32
#初始化数据恢复时,并发恢复线程的个数,默认4个
cluster.routing.allocation.node_initial_primaries_recoveries: 32

# 开启xpack安全验证
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
# 证书配置
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

JVM配置
#堆内存配置
#Xms表示ES堆内存初始大小
-Xms16g
#Xmx表示ES堆内存的最大可用空间
-Xmx16g
#GC配置
-XX:+UseConcMarkSweepGC
#使用CMS内存收集
-XX:CMSInitiatingOccupancyFraction=75
#使用CMS作为垃圾回收使用,75%后开始CMS收集
-XX:+UseCMSInitiatingOccupancyOnly
#使用手动定义初始化开始CMS收集
tip
  • jvm配置在conf目录下 vim jvm.options 。
  • ES默认JVM配置 Xms(minimum heap size)=1g,Xmx(maxmimum heap size)=1g
  • 生产环境,这个配置就比较重要了,需要确保 ES 有足够堆空间可用。
  • 注意:Xms 和 Xmx 不能大于你物理机内存的 50%。
  • 此文件一般在安装目录下,或者使用 whereis elasticsearch 命令,列出es相关的文件路径,都找一找。

安装包安装elk

Docker 启动命令

docker run \
--name elasticsearch -d \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-p 9200:9200 \
-p 9300:9300 \
registry.cn-beijing.aliyuncs.com/yyb-ricky/elasticsearch:7.10.1 \