在路径级别应用 nginx-ingress 注释

Apply nginx-ingress annotations at path level

我们正在从传统的 nginx 部署迁移到 kubernetes nginx-ingress 控制器。我正在尝试在 location 级别应用设置,但看不到如何使用注释来实现。

例如,我们有:

server {
  listen 80;
  server_name example.com;

  location /allow-big-uploads {
    client_max_body_size 100M;
    ...
  }
}

然后我们翻译成这样:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-ingress
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 100m <-- this now applies globally
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /allow-big-uploads
            backend:
              serviceName: example-svc
              servicePort: 5009

path 部分下添加该注释似乎不起作用。我错过了什么吗?

Annotations can only be set on the whole kubernetes resource, as they are part of the resource metadata. The ingress spec 不包含较低级别的功能。

如果您正在寻找更复杂的设置,traefik have built a custom resource definition for their ingress controller that allows more configuration per service。缺点是定义与其他入口控制器不兼容。

如果您在同一台主机上有 2 个位置,并且只想在一个位置上应用设置,则可以使用相同的主机创建 2 个入口,并在您感兴趣的入口上应用配置片段注释:

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#configuration-snippet

nginx.ingress.kubernetes.io/configuration-snippet: |

  more_set_headers "Request-Id: $req_id";

我已经尝试过这个例子并且它有效。

但是,当我尝试通过配置片段更改 client_max_body_size 时,出现错误:

"client_max_body_size" directive is duplicate