文章目录
温故知新,以为师!
前言
最近项目上突然用的ES,记得上次使用还是在上家公司的时候,都过去了两三年了,概念、命令、DSL统统忘了,模模糊糊的记录,让本身一个简单的开发花了很长时间,最后还是在边复习、边实践的基础上做完,有些惭愧了,所以不得不把ES相关知识看一遍,最后发现好多知识点,想想还是踏踏实实写下来,温故知新!!!
环境配置
硬件环境
阿里云乞丐版,程序猿你懂得,舍得花时间,空间换时间,SWAP,将白嫖坚持到底!!!
[root@Genterator ~]# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
1 Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
[root@Genterator ~]# cat /proc/meminfo | grep MemTotal
MemTotal: 1790344 kB
[root@Genterator ~]# free -h
total used free shared buff/cache available
Mem: 1.7Gi 1.4Gi 134Mi 0.0Ki 212Mi 204Mi
Swap: 2.5Gi 114Mi 2.4Gi
软件环境
- Elasticsearch
ES选择上根据个人需要把,我选择的7.17.1, 个人建议跟公司一致,或者比公司高几个版本的,因为每个版本都会有一些变动,比如前公司用6版的,里面有doc
概念,结果在7版本就取消了,一切根据个人需要吧,如果喜欢尝鲜的,可以使用最新版!
[root@Genterator single]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.elastic.co/elasticsearch/elasticsearch 7.17.1 515ab4fba870 15 months ago 618MB
- elasticsearch-head
这个工具看个人吧,图形化界面,建议使用Kibana
,这个是ELK之一,也是elastic的又一大利器,咱们后面慢慢说,第一次玩es的建议安装个elasticsearch-head,随便玩玩,大概熟悉一下就ok了
[root@Genterator single]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mobz/elasticsearch-head 5 b19a5c98e43b 6 years ago 824MB
ES部署
创建路径
根据个人爱好吧,我喜欢放到/opt/volume
目录下,这个根据自己需要决定
[root@Genterator volume]# mkdir -p /opt/volume/es/single/data
[root@Genterator volume]# mkdir -p /opt/volume/es/single/logs
[root@Genterator volume]# mkdir -p /opt/volume/es/single/plugins
分别对应:
- 数据文件夹
- 日志文件夹
- 插件文件夹
编写docker-compose.yml
version: '3.6'
services:
elasticsearch:
image: elasticsearch:7.17.1
container_name: es
environment:
node.name: es-single
cluster.name: es-cluster
discovery.type: single-node
ES_JAVA_OPTS: "-Xms512m -Xmx512m"
# 开启es跨域
http.cors.enabled: "true"
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
# 安全控制(根据个人需要选择打开或关闭)
# xpack.security.enabled: "true"
# xpack.security.transport.ssl.enabled: "true"
# ELASTIC_PASSWORD: "123456"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /opt/volume/es/single/data:/usr/share/elasticsearch/data
- /opt/volume/es/single/plugins:/usr/share/elasticsearch/plugins
- /opt/volume/es/single/logs:/usr/share/elasticsearch/logs
ports:
- 9200:9200
- 9300:9300
networks:
- elastic
elasticsearch-head:
image: mobz/elasticsearch-head:5
container_name: es-head
ports:
- 9100:9100
networks:
- elastic
networks:
name:
elastic:
driver: bridge
name: elastic
ipam:
driver: default
config:
- subnet: 172.30.0.0/25
gateway: 172.30.0.1
说明:
- 里面包含es和es-head,根据需要选择
- 给es设置了单独的网管,根据需要自行选择
启动命令
[root@Genterator single]# docker-compose up -d
首次启动建议去掉-d
, 此为后台启动,第一次启动肯定会遇到各种各样的问题,建议先在控制台打印成功的标志后,再使用-d
在后台启动
验证
es验证
访问自己的域名,端口9200
,如果自己设置了端口映射,根据自己的设置的进行调整
http://127.0.0.1:9200
成功标志:
{
"name" : "es-single",
"cluster_name" : "es-cluster",
"cluster_uuid" : "_na_",
"version" : {
"number" : "7.17.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "e5acb99f822233d62d6444ce45a4543dc1c8059a",
"build_date" : "2022-02-23T22:20:54.153567231Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
es-head验证
访问自己部署的ip地址和端口,我设置的映射端口为9100
:
http://127.0.0.1:9100/
成功标志:
- 输入
1
处地址,页面返回则表示es-head正常 - 输入
2
处地址(es访问地址),点击连接
后3
处显示green
则表示正常 - es-head连接需要跨域,有以下解决方式:
- 配置文件中添加以下内容来允许跨域访问 Elasticsearch
在生产环境中,不建议使用 http.cors.allow-origin=* 开启所有来源的跨域请求。您可以根据需要配置具体的允许来源,以增强安全性。- http.cors.enabled=true - http.cors.allow-origin=*
- 在 es-head 配置文件中,将 es_host 设置为 Elasticsearch 的容器名称或 IP 地址:
至此,单节点ES配置结束!!!
问题总结
我在部署中遇到问题比较少,主要有两方面:
权限问题
[root@Genterator single]# docker-compose up
WARNING: Some networks were defined but are not used by any service: name
Pulling elasticsearch (docker.elastic.co/elasticsearch/elasticsearch:7.17.1)...
7.17.1: Pulling from elasticsearch/elasticsearch
……
elasticsearch | Exception in thread "main" java.lang.RuntimeException: starting java failed with [1]
elasticsearch | output:
elasticsearch | [0.000s][error][logging] Error opening log file 'logs/gc.log': Permission denied
elasticsearch | [0.000s][error][logging] Initialization of output 'file=logs/gc.log' using options 'filecount=32,filesize=64m' failed.
elasticsearch | error:
elasticsearch | Invalid -Xlog option '-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m', see error log for details.
elasticsearch | Error: Could not create the Java Virtual Machine.
……
这个主要问题在于我们映射本地数据卷时,只创建了文件夹,没有给予权限
# 给需要写入文件的文件夹赋予权限
[root@Genterator single]# chmod 777 -R /opt/volume/es
最大虚拟内存区数量
elasticsearch | ERROR: [1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
elasticsearch | bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
elasticsearch | ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/es-cluster.log
max_map_count文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值是65536。
[root@Genterator single]# cat /proc/sys/vm/max_map_count
65530
[root@Genterator single]# sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
[root@Genterator single]# cat /proc/sys/vm/max_map_count
262144
后期更新
增加kinbana支持
在上面的docker-compose中增加下列内容,可以替换掉Elasticsearch-head部分,二者选其一即可!
kibana:
image: kibana:7.17.1
container_name: es-kibana
environment:
#设置连接到es
ELASTICSEARCH_URL: http://es:9200
ELASTICSEARCH_HOSTS: http://es:9200
#配置访问密码
ELASTICSEARCH_USERNAME: "elastic"
ELASTICSEARCH_PASSWORD: "Admin!123"
#设置中文
I18N_LOCALE: zh-CN
I18N_COOKIE_LANG_ENABLED: 'true'
ulimits:
memlock:
soft: -1
hard: -1
mem_limit: 1g
ports:
- 5601:5601
networks:
- elastic
注:
内存不足者,或者丐版阿里云的,一定一定要设置mem_limit,限制内存哦,elastic的三大组件都很消耗内存!!