nginx.ingress.kubernetes.io/server-snippet 注释包含无效的单词位置

nginx.ingress.kubernetes.io/server-snippet annotation contains invalid word location

我是 kubernetes 新手,使用的是 AWS EKS 集群 1.21。我正在尝试为我的 k8s 集群编写 nginx 入口配置并使用 server-snippet 阻止一些请求。我的入口配置如下

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: abc-ingress-external
  namespace: backend
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx-external
    nginx.ingress.kubernetes.io/server-snippet: |
       location = /ping {
         deny all;
         return 403;
       }
spec:
  rules:
  - host: dev-abc.example.com
    http:
      paths:
      - backend:
          service:
              name: miller
              port:
                number: 80
        path: /
        pathType: Prefix

当我应用这个配置时,我得到这个错误:

for: "ingress.yml": admission webhook "validate.nginx.ingress.kubernetes.io" denied the request: nginx.ingress.kubernetes.io/server-snippet annotation contains invalid word location

我调查了一下,发现这与 annotation-value-word-blocklist 有关。但是我不知道如何解决这个问题。任何帮助将不胜感激。

似乎 issue 在某些版本中使用 location。以下在 EKS 集群上测试成功。

在 EKS 上安装基本的 ingress-nginx:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/aws/deploy.yaml

注意:如果你的集群版本<1.21,需要在service spec中注释掉ipFamilyPolicyipFamilies

运行 一个 http 服务:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/docs/examples/http-svc.yaml

为服务创建入口:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: http-svc
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/server-snippet: |
       location = /ping {
         deny all;
         return 403;
       }
spec:
  rules:
  - host: test.domain.com
    http:
      paths:
      - path: /
        pathType: ImplementationSpecific
        backend:
          service:
            name: http-svc
            port:
              number: 8080

Return 200 符合预期: curl -H 'HOST: test.domain.com' http://<get your nlb address from the console>

Return 200 符合预期: curl -H 'HOST: test.domain.com' -k https://<get your nlb address from the console>

Return 403 正如预期的那样,该代码段正在运行: curl -H 'HOST: test.domain.com' -k https://<get your nlb address from the console>/ping

使用最新版本以避免“注释包含无效词位置”问题。

或者,将 nginx.ingress.kubernetes.io/server-snippet 替换为 nginx.org/server-snippets 解决了我的问题。例如参见 [​​=12=]