【问题标题】:How to use ConfigMap configuration with Helm NginX Ingress controller - Kubernetes如何将 ConfigMap 配置与 Helm NginX Ingress 控制器一起使用 - Kubernetes
【发布时间】:2019-07-19 22:49:16
【问题描述】:

我找到了有关如何使用 ConfigMap 配置 NginX 入口控制器的文档:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/

不幸的是,我不知道也无法在任何地方找到如何从我的 Ingress 控制器加载该 ConfigMap。

我的入口控制器:

helm install --name ingress --namespace ingress-nginx --set rbac.create=true,controller.kind=DaemonSet,controller.service.type=ClusterIP,controller.hostNetwork=true stable/nginx-ingress

我的配置图:

kind: ConfigMap
apiVersion: v1
metadata:
  name: ingress-configmap
data:
  proxy-read-timeout: "86400s"
  client-max-body-size: "2g"
  use-http2: "false"

我的入口:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  tls:
    - hosts:
        - my.endpoint.net
      secretName: ingress-tls
  rules:
    - host: my.endpoint.net
      http:
        paths:
          - path: /
            backend:
              serviceName: web
              servicePort: 443
          - path: /api
            backend:
              serviceName: api
              servicePort: 443

如何让我的 Ingress 从 ConfigMap 加载配置?

【问题讨论】:

    标签: kubernetes kubernetes-helm kubernetes-ingress nginx-ingress


    【解决方案1】:

    通过terraform安装图表时,配置值可以设置如下:

    resource "helm_release" "ingress_nginx" {
      name       = "nginx"
      repository = "https://kubernetes.github.io/ingress-nginx/"
      chart      = "ingress-nginx"
    
      set {
        name  = "version"
        value = "v4.0.2"
      }
      set {
        name  = "controller.config.proxy-read-timeout"
        value = "86400s"
      }
      set {
        name  = "controller.config.client-max-body-size"
        value = "2g"
      }
      set {
        name  = "controller.config.use-http2"
        value = "false"
      }
    }
    

    【讨论】:

      【解决方案2】:

      我设法通过 configmap 更新了 nginx 中的“large-client-header-buffers”。这是我遵循的步骤..

      1. 在 nginx 入口控制器 pod 描述中找到 configmap 名称
      kubectl -n utility describe pods/test-nginx-ingress-controller-584dd58494-d8fqr |grep configmap
            --configmap=test-namespace/test-nginx-ingress-controller
      

      注意:在我的例子中,命名空间是“test-namespace”,配置映射名称是“test-nginx-ingress-controller”

      1. 创建一个 configmap yaml
      cat << EOF > test-nginx-ingress-controller-configmap.yaml 
      
      kind: ConfigMap
      apiVersion: v1
      metadata:
        name: test-nginx-ingress-controller
        namespace: test-namespace
      data:
        large-client-header-buffers: "4 16k"
      EOF
      

      注意:请根据步骤 1

      中的发现替换 namespaceconfigmap name >
      1. 部署 configmap yaml
      kubectl apply -f test-nginx-ingress-controller-configmap.yaml
      

      然后你会看到更改在几分钟后更新到 nginx 控制器 pod

      i.g.
      kubectl -n test-namespace exec -it test-nginx-ingress-controller-584dd58494-d8fqr -- cat /etc/nginx/nginx.conf|grep large
          large_client_header_buffers     4 16k;
      
      

      【讨论】:

        【解决方案3】:

        使用 enable-underscores-in-headers=true 对我有用,但 enable-underscores-in-headers='"true"'

        helm install nginx-ingress ingress-nginx/ingress-nginx
        --namespace 入口基础
        --set controller.config.enable-underscores-in-headers=true

        【讨论】:

          【解决方案4】:

          只是为了确认上面的@NeverEndingQueue 答案,配置映射的名称存在于 nginx-controller pod 规范本身中,因此如果您检查 nginx-controller pod 的 yaml:kubectl get po release-name-nginx-ingress-controller-random-sequence -o yaml,在spec.containers 下,你会发现类似的东西:

            - args:
              - /nginx-ingress-controller
              - --default-backend-service=default/release-name-nginx-ingress-default-backend
              - --election-id=ingress-controller-leader
              - --ingress-class=nginx
              - --configmap=default/release-name-nginx-ingress-controller
          

          例如这里,需要在命名空间default 中创建一个名为release-name-nginx-ingress-controller 的配置映射。

          完成后,您可以通过检查日志来验证更改是否已发生。通常,您会看到如下内容:

          I1116 10:35:45.174127       6 event.go:278] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"default", Name:"release-name-nginx-ingress-controller", UID:"76819abf-4df0-41e3-a3fe-25445e754f32", APIVersion:"v1", ResourceVersion:"62559702", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap default/release-name-nginx-ingress-controller
          I1116 10:35:45.184627       6 controller.go:141] Configuration changes detected, backend reload required.
          I1116 10:35:45.396920       6 controller.go:157] Backend successfully reloaded.
          

          【讨论】:

            【解决方案5】:

            更简单的方法是修改通过 helm 部署的值。进入 ConfigMap 需要更改的值现在位于 controller.config.entries 中。显示最新值:helm show values nginx-stable/nginx-ingress,并在您正在运行的版本上查找格式。

            我有很多问题,因为所有在线参考都说controller.config,直到我检查了上面的命令。

            输入值升级后:

            helm upgrade -f <PATH_TO_FILE>.yaml <NAME> nginx-stable/nginx-ingress
            

            【讨论】:

              【解决方案6】:

              如果您想在部署 nginx-ingress-controller 时提供自己的配置,您可以在原始 nginx-ingress Helm 图表上创建一个包装 Helm 图表,并提供您自己的 values.yaml 可以进行自定义配置。

              在这里使用 Helm 3。

              创建图表:

              $ helm create custom-nginx
              $ tree custom-nginx
              

              所以我的图表结构是这样的:

              custom-nginx/
              ├── Chart.yaml
              ├── charts
              ├── templates
              │   ├── NOTES.txt
              │   ├── _helpers.tpl
              │   ├── deployment.yaml
              │   ├── hpa.yaml
              │   ├── ingress.yaml
              │   ├── service.yaml
              │   ├── serviceaccount.yaml
              │   └── tests
              │       └── test-connection.yaml
              └── values.yaml
              

              这里还有一些额外的东西。具体来说,我不需要完整的 templates/ 目录及其内容,因此我将删除这些内容:

              $ rm custom-nginx/templates/*
              $ rmdir custom-nginx/templates
              

              现在,图表结构应如下所示:

              custom-nginx/
              ├── Chart.yaml
              ├── charts
              └── values.yaml
              

              因为,我们必须将原始 nginx-ingress 图表作为依赖项包含在内,所以我的 Chart.yaml 看起来像这样:

               $ cat custom-nginx/Chart.yaml 
              apiVersion: v2
              name: custom-nginx
              description: A Helm chart for Kubernetes
              
              # A chart can be either an 'application' or a 'library' chart.
              #
              # Application charts are a collection of templates that can be packaged into versioned archives
              # to be deployed.
              #
              # Library charts provide useful utilities or functions for the chart developer. They're included as
              # a dependency of application charts to inject those utilities and functions into the rendering
              # pipeline. Library charts do not define any templates and therefore cannot be deployed.
              type: application
              
              # This is the chart version. This version number should be incremented each time you make changes
              # to the chart and its templates, including the app version.
              # Versions are expected to follow Semantic Versioning (https://semver.org/)
              version: 1.39.1
              
              # This is the version number of the application being deployed. This version number should be
              # incremented each time you make changes to the application. Versions are not expected to
              # follow Semantic Versioning. They should reflect the version the application is using.
              appVersion: 0.32.0
              
              dependencies:
              - name: nginx-ingress
                version: 1.39.1
                repository: https://kubernetes-charts.storage.googleapis.com/ 
              

              这里,appVersionnginx-controller docker 映像版本,version 与我正在使用的 nginx-ingress 图表版本匹配。

              唯一剩下的就是提供您的自定义配置。这是我的自定义配置的精简版:

              $ cat custom-nginx/values.yaml 
              # Default values for custom-nginx.
              # This is a YAML-formatted file.
              # Declare variables to be passed into your templates.
              
              nginx-ingress:
                controller:
                  ingressClass: internal-nginx
                  replicaCount: 1
                  service:
                    externalTrafficPolicy: Local
                  publishService:
                    enabled: true
                  autoscaling:
                    enabled: true
                    minReplicas: 1
                    maxReplicas: 3
                    targetCPUUtilizationPercentage: "80"
                    targetMemoryUtilizationPercentage: "80"
                  resources:
                    requests:
                      cpu: 1
                      memory: 2Gi
                    limits:
                      cpu: 1
                      memory : 2Gi
                  metrics:
                    enabled: true
                  config:
                    compute-full-forwarded-for: "true"
              

              我们可以在https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/ 中检查可用作配置的密钥(values.yaml 中的config 部分)

              其余的配置可以在这里找到:https://github.com/helm/charts/tree/master/stable/nginx-ingress#configuration

              配置完成后,只需下载图表的依赖项:

              $ helm dependency update <path/to/chart>
              

              最好在部署图表之前对其进行基本检查:

              $ helm lint <path/to/chart>
              $ helm install --debug --dry-run --namespace <namespace> <release-name> <path/to/chart>
              

              然后部署您的图表(它将使用您自己的自定义配置部署您的 nginx-ingress-controller)。

              另外,既然你现在有一个图表,你可以升级和回滚你的图表。

              【讨论】:

                【解决方案7】:

                我阅读了上述答案,但无法正常工作。

                对我有用的是:

                release_name=tcp-udp-ic
                
                # add the helm repo from NginX and update the chart
                helm repo add nginx-stable https://helm.nginx.com/stable
                helm repo update
                
                echo "- Installing -${release_name}- into cluster ..."
                
                #delete the config map if already exists
                kubectl delete cm tcp-udp-ic-cm
                
                helm del --purge ${release_name}
                helm upgrade --install ${release_name} \
                --set controller.image.tag=1.6.0 \
                --set controller.config.name=tcp-udp-ic-cm \
                nginx-stable/nginx-ingress --version 0.4.0 #--dry-run --debug
                
                # update the /etc/nginx/nginx.conf file with my attributes, via the config map
                kubectl apply -f tcp-udp-ic-cm.yaml
                

                tcp-udp-ic-cm.yaml 是:

                kind: ConfigMap
                apiVersion: v1
                metadata:
                  name: tcp-udp-ic-cm
                  namespace: default
                data:
                  worker-connections : "10000"
                

                基本上我需要使用 helm 部署版本并设置将要使用的 config-map 的名称。 Helm 创建配置映射但为空。然后我应用 config-map 文件以使用我的值更新 config-map 资源。这个序列是我唯一能做的。

                【讨论】:

                  【解决方案8】:

                  也可以在安装时传递 config mag 属性:

                  helm install stable/nginx-ingress --name nginx-ingress --set controller.config.use-forwarded-headers='"true"'
                  

                  注意:对于非字符串值,必须在双引号周围使用单引号才能使其正常工作。

                  【讨论】:

                  • 也感谢这个有效的答案。但是想知道如何将 http-sn-p 作为参数传递给 helm 图表?例如,“http-sn-p”:“proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=mycache:32m use_temp_path=off max_size=4g inactive=1h;”。谢谢
                  【解决方案9】:

                  如果您使用helm install 安装 ingress-nginx,如果没有传递 nginx 控制器应查看的 ConfigMap 的显式值,则默认值似乎是 {namespace}/{release-name}-nginx -入口控制器。这是由https://github.com/helm/charts/blob/1e074fc79d0f2ee085ea75bf9bacca9115633fa9/stable/nginx-ingress/templates/controller-deployment.yaml#L67 生成的。 (如果是死链接,请参见类似内容)。

                  要自己验证,请尝试找到用于安装 ingress-nginx 图表的命令,并将 --dry-run --debug 添加到命令中。这将向您展示 Tiller 生成的要应用于集群的 yaml 文件。 # Source: nginx-ingress/templates/controller-deployment.yaml 行开始控制器部署,其具有arg--configmap=。这个arg 的值是控制器感知的ConfigMap 的名称,并用于更新它自己的.conf 文件。这可以显式传递,但如果不是,它将有一个默认值。

                  如果使用 RIGHT 名称创建 ConfigMap,控制器的日志将显示它获取了配置更改并自行重新加载。

                  这可以通过kubectl logs &lt;pod-name-of-controller&gt; -n &lt;namespace-arg-if-not-in-default-namespace&gt; 进行验证。我的日志消息包含文本 Configuration changes detected, backend reload required. 如果 ConfigMap 名称错误,这些日志消息将不会出现。

                  我认为这方面的官方文档是不必要的,但也许我不正确?我将尝试提交包含这些详细信息的 PR。了解更多的人应该帮助充实它们,这样人们就不必不必要地偶然发现这一点。

                  干杯,感谢您的帖子。

                  【讨论】:

                    【解决方案10】:

                    我已经设法在 helm install 命令末尾使用 --dry-run --debug 选项显示 Helm 执行的 YAML 内容。然后我注意到那里的控制器是用:--configmap={namespace-where-the-nginx-ingress-is-deployed}/{name-of-the-helm-chart}-nginx-ingress-controller 执行的。 为了加载您的 ConfigMap,您需要用自己的方式覆盖它(查看命名空间)。

                    kind: ConfigMap
                    apiVersion: v1
                    metadata:
                      name: {name-of-the-helm-chart}-nginx-ingress-controller
                      namespace: {namespace-where-the-nginx-ingress-is-deployed}
                    data:
                      proxy-read-timeout: "86400"
                      proxy-body-size: "2g"
                      use-http2: "false"
                    

                    配置属性列表可以在here找到。

                    【讨论】:

                    • --configmap 在某个地方的 yaml 中吗?您如何查看正在运行的部署中的 --configmap 是什么?
                    • --configmap 不是公认的 helm 标志。虽然创建配置映射和 nginx 入口没有问题,但我仍然不知道如何将两者链接在一起。入口没有从配置映射中获取属性。
                    • 不要使用:--configmap 选项,以与 Helm 内部调用 configmap 相同的方式命名您的 configmap。如果您再次阅读我的答案,您将能够发现它。
                    • 应用的配置映射的名称是{name-of-the-helm-chart}-ingress-nginx-ingress-controller,将从部署图表的命名空间中获取。添加评论以防答案中的编辑被拒绝。非常感谢您的帮助@NeverEndingQueue!干杯!!!
                    • 很高兴我能帮上忙。感谢您的编辑,我稍微调整了一下。我认为不是:{name-of-the-helm-chart}-ingress-nginx-ingress-controller,而是:{name-of-the-helm-chart}-nginx-ingress-controller。对吗?
                    【解决方案11】:

                    您拥有的是入口 yaml 而不是入口控制器部署 yaml ,入口控制器是实际执行工作的 Pod,通常是 nginx 容器本身。可以在您共享的文档中找到此类配置的示例 here

                    更新

                    使用提供的示例,您还可以使用以下方式使用 config map 将配置加载到 nginx 中

                         volumeMounts:
                          - name: nginx-config
                            mountPath: /etc/nginx/nginx.conf
                           subPath: nginx.conf
                        volumes:
                         - name: nginx-config
                           configMap:
                           name: nginx-config 
                    

                    nginx-config 包含您的 nginx 配置作为配置映射的一部分

                    【讨论】:

                    • 正如您所指出的,自定义模板是配置 NginX 控制器的一种方式:custom-template 但这里有自己的密钥约定的 ConfigMap:configmap 是另一种方式。请注意configmap 直接在data: 中提供配置。我看的不是如何从 ConfigMap 加载自定义模板,而是如何直接从 ConfigMap 加载配置。
                    【解决方案12】:

                    当你应用 ConfigMap 配置需要的 key-value 数据时,Ingress 控制器会拾取这些信息并将其插入到嵌套的 nginx-ingress-controller Pod 的原始配置文件 /etc/nginx/nginx.conf 中,因此之后很容易验证 ConfigMap 的值是否有是否成功反映,通过检查对应 Pod 中的实际nginx.conf

                    您还可以查看相关nginx-ingress-controller Pod 的日志,以检查ConfigMap 数据是否已经重新加载到后端nginx.conf,或者如果不调查原因。

                    【讨论】:

                    • 谢谢。是的,ConfigMap 更改很好地影响了内部的nginx.conf。如果有人想检查 NginX 配置是否在外部受到影响(不进入 pod),您可以设置:server_tokens offserver_tokens on 并注意 NginX 是否在 HTTP 标头中宣传自己。
                    • 如果检测到配置映射,我应该在控制器中看到什么样的日志?因为似乎我在这里关注了所有内容,我不确定我的 .conf 是否正在更新
                    • kubectl exec -ndefault nginx-ingress-controller-b545558d8-829dz -- cat /etc/nginx/nginx.conf | grep tokens 例如。
                    【解决方案13】:

                    您应该在入口控制器部署清单中使用它

                    【讨论】:

                    猜你喜欢
                    • 2019-12-18
                    • 2021-06-17
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-11-06
                    • 2019-02-20
                    • 2019-03-24
                    • 1970-01-01
                    相关资源
                    最近更新 更多