我想通过 docker-compose.yml 使用官方 Elasticsearch docker 镜像,如官方文档所示:
我的简化版 docker-compose.yml 如下所示:
version: '2'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.5.2
environment:
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9200:9200
运行后默认情况下
docker-compose up
我有用户elastic
使用默认密码changeme
创建。正如 documentation 建议我可以通过致电更改用户密码:
curl -XPUT -u elastic 'localhost:9200/_xpack/security/user/elastic/_password' -H "Content-Type: application/json" -d '{
"password" : "elasticpassword"
}'
但这在运行 Docker 镜像时需要额外的步骤。
有没有办法在
elastic
命令期间配置默认的docker-compose up
用户密码?也许通过环境变量以某种方式或通过 elasticsearch.yml
配置文件?
我可以创建自己的图像作为
docker.elastic.co/elasticsearch/elasticsearch:5.5.2
图像和 RUN
curl ...
命令之上的包装器作为相关 Dockerfile 的一部分,但对我来说,创建我自己的 Elasticsearch 图像版本只是为了配置 似乎是开销elastic
用户密码...
对我有用的解决方案是将具有基本身份验证的 nginx 代理容器放在 elasticsearch 容器前面。 Nginx 配置可能类似于:
upstream elasticsearch {
server elasticsearch:9200;
}
server {
listen 80;
server_name server.name.com;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
location / {
proxy_pass http://elasticsearch;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
}
其中
.htpasswd
包含用户名和加密的用户密码(您甚至可以使用在线服务来生成它,例如 http://www.htaccesstools.com/htpasswd-generator/)。
除此之外,您可以购买 X-pack/Shield 的许可证,然后根据需要使用它。