使用 kubernetes secret 使 nginx 可配置?

Make nginx configurable with kubernetes secret?

我有一个可用的 openresty,使用 lua-resty-openidc 作为入口控制器。 现在,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 秘密?

您需要在运行时从模板化版本呈现 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 或 secret 或您喜欢的方式。