【问题标题】:Make nginx configurable with kubernetes secret?使用 kubernetes 秘密使 nginx 可配置?
【发布时间】:2020-04-09 07:56:22
【问题描述】:

我有一个使用 lua-resty-openidc 作为入口控制器的 openresty。 现在,nginx.conf 在我的图像中被硬编码,如下所示:

    server {
        server_name  _;
        listen       80;

        location /OAuth2Client {
            access_by_lua_block {
                local opts = {
                    discovery = "/.well-known/openid-configuration",
                    redirect_uri = "/authorization-code/callback",
                    client_id = "clientID",
                    client_secret = "clientSecret",
                    scope = "openid profile somethingElse",
                }
    ...
            }
            proxy_pass http://clusterIp/OAuth2Client;
        }
    }

由于 Nginx 不接受环境变量,有没有一种简单的方法可以让我的 nginx.conf 可配置,例如

    server {
        server_name  ${myServerName};
        listen       ${myServerPort};

        location /${specificProjectRoot} {
            access_by_lua_block {
                local opts = {
                    discovery = "${oidc-provider-dev-url}/.well-known/openid-configuration",
                    redirect_uri = "${specificProjectRoot}/authorization-code/callback",
                    client_id = "${myClientId}",
                    client_secret = "${myClientSecret}",
                    scope = "${myScopes}",
                }
    ...
            }
            proxy_pass http://${myClusterIP}/${specificProjectRoot};
        }
    }

这样任何命名空间中的任何团队都可以重用我的图像,并且只需提供一个包含其项目特定配置的 kubernetes 机密?

【问题讨论】:

  • 如果你创建一个“start_script.sh”来在你的 nginx 启动时运行呢?在脚本中,您可以使用环境变量并使用sed 您可以替换 nginx.conf 中的值。类似的东西:sed -i -e "s,\${myServerName},$SERVER_NAME," /etc/nginx/nginx.conf

标签: nginx kubernetes openresty


【解决方案1】:

您需要在运行时从模板版本呈现nginx.conf(正如 Juliano 的评论所提到的)。为此,您的 Dockerfile 可能如下所示:

FROM nginx
COPY nginx.conf.template /etc/nginx/
CMD ["/bin/bash", "-c", "envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && exec nginx -g 'daemon off;'"]

请注意,它会将nginx.conf.template 复制到您的映像中,这将是您的模板化配置,其变量格式为${MY_SERVER_NAME},其中MY_SERVER_NAMEinjected into your pod as an environment variable,通过您的Kubernetes 清单、您的configmap 或秘密或您喜欢的任何方式.

【讨论】:

    猜你喜欢
    • 2016-09-07
    • 2020-12-27
    • 2018-08-13
    • 2020-07-27
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多