【问题标题】:How to set up default 'elastic' user password while running official Elasticsearch docker image?如何在运行官方 Elasticsearch docker 映像时设置默认的“弹性”用户密码?
【发布时间】:2023-03-15 20:35:02
【问题描述】:

我想通过 docker-compose.yml 使用官方的Elasticsearch docker image 官方文档建议:

我的简化 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 后,我使用默认密码changeme 创建了用户elastic。正如documentation 建议的那样,我可以通过调用来更改用户密码:

curl -XPUT -u elastic 'localhost:9200/_xpack/security/user/elastic/_password' -H "Content-Type: application/json" -d '{
  "password" : "elasticpassword"
}'

但这在运行 Docker 映像时需要额外的步骤。

有没有办法在docker-compose up 命令期间配置默认的elastic 用户密码?也许通过环境变量以某种方式或通过elasticsearch.yml 配置文件?

我可以在 docker.elastic.co/elasticsearch/elasticsearch:5.5.2 图像和 RUN curl ... 命令之上创建自己的图像作为包装器,作为相关 Dockerfile 的一部分,但创建自己的 Elasticsearch 图像版本对我来说似乎是开销配置elastic用户密码...

【问题讨论】:

  • 如果你只使用 image: docker.elastic.co/elasticsearch/elasticsearch:5.5.2 用户名/密码安全将不起作用。您将需要一个安装了 X-pack/Shield 的 docker 映像作为插件,这可以使用 dockerfile 完成。但是请注意,除非您在 X-Pack 插件中添加许可证密钥,否则它不会启动。如果您使用任何 Web 服务器,如 nginx 或 Traefik,它们可以扩展为使用基本身份验证。这会是更好的选择吗?

标签: elasticsearch docker docker-compose elasticsearch-x-pack


【解决方案1】:

对我有用的解决方案是将具有基本身份验证的 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 的许可证,然后根据需要使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-18
    • 2020-11-09
    • 2022-12-11
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2014-11-27
    相关资源
    最近更新 更多