同福

使用Dockerfile基于6.8.15镜像搭建Elasticsearch微服务环境【20210522】

介绍

介绍

大家根福哥学会了使用Dockerfile创建Docker镜像的技巧了,那么我们现在搭建服务器环境就不需要再去下载软件的源代码了,也不需要编译安装了,更加不用操心软件和操作系统的各种兼容问题了。

今天福哥带着大家来安装es的环境,Elasticsearch是搜索引擎,和php、python不同之处在于Elasticsearch是不能简单地通过k8s进行负载均衡的,而且Elasticsearch的索引的数据会持续更新需要持久化保存起来,这些我们在本课都可以跟福哥学到。

镜像

tag

Elasticsearch的标签只有两个版本,福哥选择的是6.8.15版本。

home/topic/2021/0521/14/dcd4bd8b0238fc6615603f46c0e66c2c.png

结构

照例我们先把镜像拉取下来,然后启动一个容器,看看里面都有什么,弄明白了自己才好捣鼓啊!

拉取镜像

docker pull elasticsearch:6.8.15

创建数据卷

和前面的php和python不同,我们需要建立一个数据卷用来给Elasticsearch存储数据使用。

由于elasticsearch的数据和日志在不同位置,所以福哥建立了两个数据卷。

docker volume create es6.8.15-db
docker volume create es6.8.15-log

启动临时容器

启动容器的时候需要指定数据卷映射到容器内部。

docker run -tid --name es6.8.15 -h es6.8.15 -v es6.8.15-db:/var/lib/elasticsearch -v es6.8.15-log:/var/log/elasticsearch elasticsearch:6.8.15

切入临时容器

docker exec -ti es6.8.15 /bin/bash

查看结构

进入容器后,全局查找一番,发现原来elasticsearch部署在了/usr/share下面。

elasticsearch的配置文件在/usr/share/elasticsearch/config/elasticsearch.yml里面。

/usr/share/elasticsearch/config/elasticsearch.yml

[root@es6 elasticsearch]# find / -iname "*elasticsearch*"
/usr/share/elasticsearch
/usr/share/elasticsearch/config/elasticsearch.yml
/usr/share/elasticsearch/config/elasticsearch.keystore
/usr/share/elasticsearch/bin/elasticsearch-cli
/usr/share/elasticsearch/bin/elasticsearch-translog
/usr/share/elasticsearch/bin/elasticsearch-service-x64.exe
/usr/share/elasticsearch/bin/elasticsearch
/usr/share/elasticsearch/bin/elasticsearch-shard.bat
/usr/share/elasticsearch/bin/elasticsearch-syskeygen
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords.bat
/usr/share/elasticsearch/bin/elasticsearch-saml-metadata.bat
/usr/share/elasticsearch/bin/elasticsearch-keystore.bat
/usr/share/elasticsearch/bin/elasticsearch-croneval.bat
/usr/share/elasticsearch/bin/elasticsearch-certutil.bat
/usr/share/elasticsearch/bin/elasticsearch-cli.bat
/usr/share/elasticsearch/bin/elasticsearch-env.bat
/usr/share/elasticsearch/bin/elasticsearch-certgen.bat
/usr/share/elasticsearch/bin/elasticsearch-sql-cli-6.8.15.jar
/usr/share/elasticsearch/bin/elasticsearch-migrate
/usr/share/elasticsearch/bin/elasticsearch-sql-cli
/usr/share/elasticsearch/bin/elasticsearch-service-mgr.exe
/usr/share/elasticsearch/bin/elasticsearch-enve
/usr/share/elasticsearch/bin/elasticsearch-service.bat
/usr/share/elasticsearch/bin/elasticsearch-migrate.bat
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords
/usr/share/elasticsearch/bin/elasticsearch-plugin
/usr/share/elasticsearch/bin/elasticsearch-croneval
/usr/share/elasticsearch/bin/elasticsearch-users
/usr/share/elasticsearch/bin/elasticsearch-sql-cli.bat
/usr/share/elasticsearch/bin/elasticsearch-saml-metadata
/usr/share/elasticsearch/bin/elasticsearch-certutil
/usr/share/elasticsearch/bin/elasticsearch-plugin.bat
/usr/share/elasticsearch/bin/elasticsearch.bat
/usr/share/elasticsearch/bin/elasticsearch-keystore
/usr/share/elasticsearch/bin/elasticsearch-syskeygen.bat
/usr/share/elasticsearch/bin/elasticsearch-shard
/usr/share/elasticsearch/bin/elasticsearch-certgen
/usr/share/elasticsearch/bin/elasticsearch-env
/usr/share/elasticsearch/bin/elasticsearch-translog.bat
/usr/share/elasticsearch/bin/elasticsearch-users.bat
/usr/share/elasticsearch/modules/lang-painless/elasticsearch-scripting-painless-spi-6.8.15.jar
/usr/share/elasticsearch/modules/x-pack-ml/elasticsearch-grok-6.8.15.jar
/usr/share/elasticsearch/modules/reindex/elasticsearch-ssl-config-6.8.15.jar
/usr/share/elasticsearch/modules/reindex/elasticsearch-rest-client-6.8.15.jar
/usr/share/elasticsearch/modules/x-pack-monitoring/elasticsearch-rest-client-6.8.15.jar
/usr/share/elasticsearch/modules/x-pack-monitoring/elasticsearch-rest-client-sniffer-6.8.15.jar
/usr/share/elasticsearch/modules/ingest-common/elasticsearch-dissect-6.8.15.jar
/usr/share/elasticsearch/modules/ingest-common/elasticsearch-grok-6.8.15.jar
/usr/share/elasticsearch/lib/elasticsearch-x-content-6.8.15.jar
/usr/share/elasticsearch/lib/elasticsearch-6.8.15.jar
/usr/share/elasticsearch/lib/tools/security-cli/elasticsearch-security-cli-6.8.15.jar
/usr/share/elasticsearch/lib/tools/plugin-cli/elasticsearch-plugin-cli-6.8.15.jar
/usr/share/elasticsearch/lib/elasticsearch-secure-sm-6.8.15.jar
/usr/share/elasticsearch/lib/elasticsearch-cli-6.8.15.jar
/usr/share/elasticsearch/lib/elasticsearch-core-6.8.15.jar
/usr/share/elasticsearch/lib/elasticsearch-launchers-6.8.15.jar
/tmp/elasticsearch-9630971463160703342
/tmp/hsperfdata_elasticsearch
/var/spool/mail/elasticsearch

停止临时容器

docker stop es6.8.15

删除临时容器

docker rm es6.8.15

Dockerfile

最后福哥把前面的设置命令整理到一起写成Dockerfile,这样大家就可以通过Dockerfile安装环境了。

elasticsearch.yml

首先福哥建立了一个配置文件elasticsearch.yml,把自定义的参数写到里面去,导入到镜像里面。
福哥只把path.data和path.logs改了一下,其他没有动!

# elasticsearch.yml

cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.port: 9200

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch

Dockerfile

创建Dockerfile,将elasticsearch.yml直接覆盖默认配置文件/usr/share/elasticsearch/config/elasticsearch.yml。

FROM elasticsearch:6.8.15

MAINTAINER Andy Bogate
MAINTAINER tongfu@tongfu.net
MAINTAINER http://docker.tongfu.net

EXPOSE 9200

# config
COPY elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml

/var/lib/elasticsearch不可写

和MongoDB一样的问题,Elasticsearch也需要给数据卷目标授权,否则就会报错了。

home/topic/2021/0521/15/5cdcf296307de9ba28690cc69cf753e9.png

使用docker valume inspect查看数据卷es6.8.15-db和es6.8.15-log的宿主机路径,授权777权限。

home/topic/2021/0521/15/c306c455d96ec2823593e8197cf8985c.png

然后重新创建容器,这下终于成功了!

home/topic/2021/0521/15/5e633b9f2e5cbf8ed39aca5d1ede7056.png

其他

vm.max_map_count

如果服务器没有设置过vm.max_map_count会导致Elasticsearch启动失败,默认vm.max_map_count是65530,需要加大到262144,可以使用下面的方法增大vm.max_map_count的数量。

[root@tongfunet]# vi /etc/sysctl.conf

vm.max_map_count=262144

使sysctl的配置改动立即生效(不需要重启服务器)

sysctl -p

总结

今天福哥带着大家使用Dockerfile搭建了Elasticsearch微服务环境了,可以发现使用Dockerfile方式搭建环境我们真的只需要关心我们需要关心的部分,繁琐的编译参数、依赖库、环境参数等等一系列的问题基础镜像都给我们解决好了。

下一课,福哥会带着搭建学习使用Dockerfile搭建Nginx环境,敬请期待~~