先赞后看,养成习惯 🌹
欢迎微信关注:Java编程之道
每天进步一点点,沉淀技术分享知识。
Docker 搭建ES+Head+Kibana
纯手搭建,绝不留坑,一键启动,屡试不爽!😀
后面自己在学习ELK、EFK的知识,所以搭建一套稳定的测试环境是必不可少的环节。考虑到市面上很多ES环境搭建都是针对tar包等,搭建过程繁琐并且因为版本还会出现很多兼容性问题以及大家没有那么多cpu资源的窘境,所以为大家整理一份自己的搭建笔记,助你走上ES学习快车道。
特此说明:仅限于测试环境,生产环境慎用!
前置条件
- Linux+Docker+手
- ES7.6.2+Kibana7.6.2+Head5
ES硬通货
兵马未动,粮草先行,不要问我那么多,改了再说。
# es需要修改linux的一些参数。
设置vm.max_map_count=262144
sudo vim /etc/sysctl.conf
vm.max_map_count=262144
# 或不重启, 直接生效当前的命令
sysctl -w vm.max_map_count=262144
Docker网卡
自定义桥接网卡,用于容器间通信。
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynetwork
拉取ES镜像
docker pull elasticsearch:7.6.2
启动ES
- 创建挂载目录,主要是因为懒!(其中config和logs下面分别需要创建elasticsearch.yml,jvm.options,gc.log)否则启动会报错
-
启动时默认设置一个较小的内存,防止启动起来就很卡
-
挂载ES配置、Jvm配置、日志目录、数据目录、插件目录到宿主机
相关配置文件核心信息如下
- elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0
- jvm.options
## JVM configuration
################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms256m
-Xmx512m
################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################
## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly
## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30
## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}
## heap dumps
# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError
# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data
# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log
## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m
# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
搞到以上目录创建和配置文件创建后,一句话启动ES(不要忘了--net mynetwork )
docker run --name my_elasticsearch -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -e "discovery.type=single-node" -v 、/home/xianglei/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/xianglei/data/:/data -v /home/xianglei/config/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /home/xianglei/logs/:/usr/share/elasticsearch/logs -v /home/xianglei/plugins/:/usr/share/elasticsearch/plugins -d --net mynetwork elasticsearch:7.6.2
访问9200查看是否成功
Kibana安装
- Kibana配置信息 这里的kibana.yml需要大家在挂载目录下自己创建,否则起不来哦!
# 挂载配置目录 /xianglei/kibana/kibana.yml
# Default Kibana configuration for docker target
server.name: "kibana"
server.host: "0.0.0.0"
# 因为实在容器内部,容器内部通信是通过自定义网桥模式进行通信,直接使用容器名字即可
elasticsearch.hosts: [ "http://my_elasticsearch:9200" ]
i18n.locale: "zh-CN"
- kibana启动
注意:一定要使用自定义网卡,否则无法ping到ES容器(--net mynetwork)
docker run -p 5601:5601 --name kibana -v /home/xianglei/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml --net mynetwork -d kibana:7.6.2
- 网卡信息
[root@localhost kibana]# docker network list
NETWORK ID NAME DRIVER SCOPE
fc722f5a22ae bridge bridge local
d1bd2ce47aa8 host host local
692d4bd9fbfd mynetwork bridge local
478f5a96a153 none null local
[root@localhost kibana]#
- mynetwork网卡
[root@localhost kibana]# docker network inspect 69
[
{
"Name": "mynetwork",
"Id": "692d4bd9fbfd3e1278fc00708560fae879a2ead2803c344dcbcd05e93f298f72",
"Created": "2020-08-30T17:24:02.485634925+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"660882dddee268f39d3cc8593e45a71faae2c9b16e67eb4ab538d29f429f5f53": {
"Name": "my_elasticsearch",
"EndpointID": "1e19c28713bb308a17a9fff5247fd28142a653d5d11029bf879b3281a1fd5186",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"d7ba01b77d290d5385d61c4fecd3b1d3c0dcd1820ed2ee5a77a5ee693016d739": {
"Name": "kibana",
"EndpointID": "7949cb16c43f05c4ebaca2b5f08adf9fc2df71a6312087f461bfa7b8fe898250",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
安装Head
这里mobz/elasticsearch-head最新的版本也只有5,不要去尝试7,拉不到镜像。
docker pull docker.io/mobz/elasticsearch-head:5
- 解决跨域问题
cd /home/xianglei/config
vim elasticsearch.yml
# 添加跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
- 启动Head
docker run --name elasticsearch-head -d -p 9100:9100 --net mynetwork docker.io/mobz/elasticsearch-head:5
访问9100
相信我你会出现406的问题
- 处理一下406的问题 需要修改一下head容器中的js信息
因为容器里没有vi、vim工具,同时在容器里面安装工具也是很慢的,所以我这里用docker cp 解决问题
# 从容器中拷贝出vendor.js 路径为/usr/src/app/_site/vendor.js
docker cp 0bb839d4f4d7:/usr/src/app/_site/vendor.js /home/xianglei/head
# 修改完后再拷贝进去
docker cp /home/xianglei/head/vendor.js a16bf81af69d:/usr/src/app/_site/
第6886行
contentType: "application/x-www-form-urlencoded
# 改成
contentType: "application/json;charset=UTF-8"
第7573行
var inspectData = s.contentType === "application/x-www-form-urlencoded" &&
# 改成
var inspectData = s.contentType === "application/json;charset=UTF-8" &&
重启head即可,406问题完美解决!Nice
IK分词器
下载解压到plugins目录重启es,因为我已经挂载了数据卷,所以我直接解压到plugins重启ES即可
看到已经启动了Ik分词器
至此,小工搞成,分分钟的事情。这下你没有放弃ES的理由了吧!