【问题标题】:nginx-ingress config map snippets being ignored by the nginx.confnginx.conf 忽略了 nginx-ingress 配置映射片段
【发布时间】:2019-03-17 20:32:04
【问题描述】:

我有一个 kubernetes 集群,我在其中使用 helm nginx-ingress chart 部署了一个 nginx 入口控制器。

我需要向 nginx-controller-pod 中生成的 nginx.conf 文件添加一些自定义配置,并且我看到一个问题,如果我添加一个单行选项,例如 proxy-buffer-size: "512k" 我可以看到这个反映在 nginx.conf 文件中,一切都按预期工作。

但是,如果我尝试添加一个 sn-p 来完成同样的事情:

location-snippet: |
  proxy_buffer_size "512k";

好像这被 nginx.conf 文件忽略了,proxy_buffer_size 设置保持在它的默认值。

我需要能够添加 http-snippetserver-snippetlocation-snippet 覆盖,但无论我尝试将它们添加到 ConfigMap 还是作为 Ingress.yaml 文件中的注释,它们总是被忽略。

我的 Ingress yaml 文件:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/ssl-redirect: "true" 
    ingress.kubernetes.io/secure-backends: "true"    
    ingress.kubernetes.io/force-ssl-redirect: "true"

    ingress.kubernetes.io/location-snippet: |
       proxy_buffer_size 512k;     --This does not update the nginx.conf
spec:
  tls:
  - hosts:
    - my.app.co.uk
    secretName: tls-secret

  rules:
  - host: my.app.co.uk
    http:
      paths:
      - path: /
        backend:
          serviceName: myappweb-service
          servicePort: 80

我的 nginx 配置图:

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: nginx-ingress
    chart: nginx-ingress-0.28.3
    component: controller
    heritage: Tiller
    release: nginx-ingress
  name: nginx-ingress-controller
  namespace: default
data:
  proxy-buffer-size: "512k" -- this works and updates the nginx.conf

  location-snippet: |
    proxy_buffers 4 512k; -- this does not update the nginx.conf

  server-snippet: |       -- this does not update the nginx.conf
    location /messagehub {
      proxy_set_header Upgrade $http_upgrade;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header Connection "upgrade";
      proxy_cache_bypass $http_upgrade;
   }

【问题讨论】:

    标签: nginx kubernetes yaml kubernetes-ingress nginx-ingress


    【解决方案1】:

    如果您想修改您的 Kubernetes Ingress,注释选项如下:

    • nginx.ingress.kubernetes.io/configuration-snippet 用于 nginx 位置块 sn-p
    • nginx.ingress.kubernetes.io/server-snippet 用于 nginx 配置服务块中的 sn-p

    看起来您正在使用 nginx.org/location-snippets: 处理这种情况。

    在 nginx 配置示例中还有一个 YAML 无效语法,您也应该根据 example 使用复数形式,如 server-snippets。在撰写本文时,docs 中有一个错字。开通this ticket跟进。

    应该是这样的:

      server-snippets: |
        location /messagehub {
          proxy_set_header Upgrade $http_upgrade;
          proxy_http_version 1.1;
          proxy_set_header X-Forwarded-Host $http_host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header Connection "upgrade";
          proxy_cache_bypass $http_upgrade;
          }
    

    而不是这个:

      server-snippet: |
        location /messagehub {
          proxy_set_header Upgrade $http_upgrade;
          proxy_http_version 1.1;
          proxy_set_header X-Forwarded-Host $http_host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header Connection "upgrade";
          proxy_cache_bypass $http_upgrade;
        }
    

    注意最后一个花括号的缩进。

    【讨论】:

    • 谢谢@Rico,大括号缩进的好地方。但是,如果我只使用 yaml 文件中的 location-snippet 行运行应用程序,这也会被忽略,并且不包括任何大括号
    • 我更新了答案,看来您需要使用location-snippets 并且文档中有错字
    • 谢谢@Rico,我已经在你的示例中使用服务器 sn-p 更新了我的 yaml,但没有看到它反映在生成的 nginx.conf 中
    • 您是否重新启动了入口控制器?我相信你在更改 ConfigMap 后需要重新启动它
    【解决方案2】:

    原来我的问题是由于我申请的 sn-p 的内容造成的。每次运行 kubectl apply -f myconfigmap.yaml 时,都会针对您尝试应用于 nginx.conf 的更改运行验证。当此验证失败时,它会静默失败,并且在终端中没有任何东西可以提醒您。

    实际上,您仍然会收到configmap/nginx-ingress-controller configured 消息。

    例如,当我将它添加到配置映射时,它会按预期更新 nginx.conf:

    http-snippet: |
      sendfile on;
    

    但是,当我添加这个时,什么都没有改变:

    http-snippet: |
      sendfile on;
      tcp_nopush on;
    

    原因是验证失败,但找到它的唯一方法是查看 nginx 入口控制器 pod 的日志。在这种情况下,我看到:

    Error: exit status 1
    2018/10/16 07:45:49 [emerg] 470#470: "tcp_nopush" directive is duplicate in 
    /tmp/nginx-cfg468835321:245
    nginx: [emerg] "tcp_nopush" directive is duplicate in /tmp/nginx-cfg468835321:245
    nginx: configuration file /tmp/nginx-cfg468835321 test failed
    

    所以我复制了一个已经存在的指令。

    【讨论】:

      【解决方案3】:

      我花了一天时间才知道 ingress-nginx(来自 Kubernetes)有 *-snippet,但是对于 nginx-ingress(来自 NGINX),@987654324 @ 与 s

      看这里:

      【讨论】:

      • 天哪...感谢您指出这一点!
      猜你喜欢
      • 2022-01-15
      • 2019-07-27
      • 2021-05-13
      • 2015-07-05
      • 2012-02-21
      • 2013-12-26
      • 2020-10-26
      • 2011-08-21
      • 1970-01-01
      相关资源
      最近更新 更多