入口 - 简单的扇出配置不起作用

Ingress - simple fanout configuration not working

我正在使用 Ubuntu 20.04.2 LTS。我安装了 microk8s 1.20.6 rev 2143 并尝试使用入口。我一定是遗漏了一些东西——但它并没有像我期望的那样工作。我追踪到以下配置的奇怪行为:

ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ubuntu
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: my-ubuntu
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
      - path: /nginx
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80

nginx-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
    - port: 80
      name: http
    - port: 443
      name: https
  type: ClusterIP
  selector:
    app: nginx

现在,

curl my-ubuntu/                     # this returns Welcome page, as expected
curl my-ubuntu/nginx                # this returns Welcome page, as expected
curl my-ubuntu/bad-page.html        # this returns 404 Not Found, as expected
curl my-ubuntu/nginx/bad-page.html  # this returns Welcome page. WHY?

my-ubuntu/nginx/* returns 欢迎页面下的任何请求,即使 url 是正确的并且应该返回不同的内容。我是不是配置有误?

我能够使用 Docker 为 Windows + WSL2 + Ubuntu + 安装的入口重现相同的奇怪行为:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.41.2/deploy/static/provider/cloud/deploy.yaml

编辑

nginx-deployment.yaml 我用过:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  revisionHistoryLimit: 0
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx

当我像@HarshManvar 建议的那样尝试 /nginx/ 而不是 /nginx 时,我得到了这个行为:

curl my-ubuntu/                     # this returns Welcome page, as expected
curl my-ubuntu/bad-page.html        # this returns 404 Not Found, as expected
curl my-ubuntu/nginx                # this returns 404 Not Found
curl my-ubuntu/nginx/               # this returns Welcome page
curl my-ubuntu/nginx/bad-page.html  # this returns Welcome page

Kubernetes Ingress documentation about Simple fanout 也确实使用了 /nginx 模式,但没有像上面描述的那样工作。

https://kubernetes.github.io/ingress-nginx/examples/rewrite/ 解释了如何使用 rewrite-target 注释。我能够使用以下 ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ubuntu
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /(.*)
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
      - path: /nginx($|/.*)
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80

每个 path 都用 ( ) 定义正则表达式,这会产生 </code>、<code> 等,又名。正则表达式捕获组变量。现在您使用这些变量放置 rewrite-target,这将是传递给处理请求的服务容器的实际 URL。

也许还有另一种方法,但这是我能够让它工作的唯一方法。