目录
描述
最近研究了一下docker-compose发布elasticsearch7.6,虽然网上有一些教程,但是根据教程操作,最后根本跑不起来或者有三个节点的集群,配置密码后只有一个节点是活的,其他节点无法跟这个节点通信。踩了不少坑,最后还是看官方文档学习。
ES官网docker配置文档
ES官网证书配置文档
如果您只是简单的玩一玩,不需要配置证书、密码,只需参照ES官网docker配置文档即可
制作自定义elasticsearch7.6镜像
ES_Dockerfile配置,包含了ik分词器、生成证书。
ik分词器下载地址
ik下载之后是zip包,需要将zip解压后,压缩成tar.gz格式的
#官方镜像
FROM elasticsearch:7.6.2
USER root
##添加ik分词器
ADD elasticsearch-analysis-ik-7.6.2.tar.gz /usr/share/elasticsearch/plugins/
RUN mv /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-7.6.2 /usr/share/elasticsearch/plugins/ik
RUN chmod 777 /usr/share/elasticsearch/plugins/ik -R
#生成证书,密码可自己配置
RUN bin/elasticsearch-certutil ca --out config/elastic-stack-ca.p12 --pass 123456
#生成证书,密码可自己配置
RUN bin/elasticsearch-certutil cert --ca config/elastic-stack-ca.p12 --ca-pass 123456 --out config/elastic-certificates.p12 --pass 123456
#创建keystore
RUN bin/elasticsearch-keystore create
#将密码添加至keystore
RUN sh -c '/bin/echo -e "123456" | sh bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password'
RUN sh -c '/bin/echo -e "123456" | sh bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password'
#文件赋权限
RUN chmod 777 /usr/share/elasticsearch/config/elastic-certificates.p12
RUN chmod 777 /usr/share/elasticsearch/config/elastic-stack-ca.p12
构建镜像
注:centos7docker:443是我自己搭建的harbor镜像仓库,如果您没有镜像仓库您也可以使用阿里云的容器镜像服务。如果您只是在本地做测试,也可以不用镜像仓库。
# docker build -t centos7docker:443/aliang-xyl/elasticsearch:7.6.2 . -f ES_DockerFile
生成的镜像
[root@centos7docker elasticsearch]# docker images | grep '7.6.2'
centos7docker:443/aliang-xyl/elasticsearch 7.6.2 66d1054960ee 46 minutes ago 820MB
kibana 7.6.2 f70986bc5191 5 months ago 1.01GB
elasticsearch 7.6.2 f29a1ee41030 5 months ago 791MB
推送至镜像仓库
# docker push centos7docker:443/aliang-xyl/elasticsearch:7.6.2
制作自定义elasticsearch-head镜像
如果使用原版elasticsearch-head镜像会出现无法使用的情况,报错如下:
{
"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
出现这种错误是因为Content-Type不支持,支持的格式是application/json;charset=UTF-8
启动elasticsearch-head容器,将容器中的/usr/src/app/_site/vendor.js拷贝出来,然后将vendor.js里面的application/x-www-form-urlencoded替换成application/json;charset=UTF-8。
ES_Head_DockerFile配置:
#原版镜像
FROM mobz/elasticsearch-head:5
USER root
#删除原本的vendor.js
RUN rm -f /usr/src/app/_site/vendor.js
#将修改后的vendor.js添加进来
ADD vendor.js /usr/src/app/_site/
RUN chmod 777 /usr/src/app/_site/vendor.js
构建镜像
# docker build -t centos7docker:443/aliang-xyl/elasticsearch-head:5 . -f ES_Head_DockerFile
推送镜像
# docker push centos7docker:443/aliang-xyl/elasticsearch-head:5
elasticsearch.yml配置
注意证书的配置要和自定义镜像中的证书信息一致
network.host: 0.0.0.0
#master节点es01
cluster.initial_master_nodes: ["es01"]
discovery.seed_hosts: ["es01","es02","es03"]
cluster.name: "es-docker-cluster"
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
#开启kibana监控配置,如果不开启,也可以在kibana监控界面开启
xpack.monitoring.collection.enabled: true
#开启安全认证相关配置
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: