使用 ingress-nginx 重定向所有内容

redirect everything with ingress-nginx

我创建了一个 yaml 文件,它唯一的工作是:It should immediately redirect to google.com

但这就是行不通...

我的localhost还是returns404-nginx

我在 docker-桌面上,我的集群版本是 v1.21.5

这是我的 redirect.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-google
  annotations:
    nginx.ingress.kubernetes.io/permanent-redirect: https://www.google.com
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: doesntmatter
            port:
              number: 80

这是我的 kubectl get ingress

NAME          CLASS    HOSTS                          ADDRESS     PORTS   AGE
cheddar       nginx    cheddar.127.0.0.1.nip.io       localhost   80      31m
my-google     <none>   *                                          80      26m
stilton       nginx    stilton.127.0.0.1.nip.io       localhost   80      31m
wensleydale   nginx    wensleydale.127.0.0.1.nip.io   localhost   80      31m

注意:其他入口服务,例如cheddar.127.0.0.1.nip.io 运行良好...

我猜你忘记了 ingress class 的名字。

spec:
  ingressClassName: nginx
  ...

除此之外,您还可以创建 external service

---
apiVersion: v1
kind: Service
metadata:
  name: google
spec:
  type: ExternalName
  externalName: www.google.com
  ports:
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: google
  labels:
    name: google
  annotations:
    nginx.ingress.kubernetes.io/backend-protocol: HTTPS
    nginx.ingress.kubernetes.io/upstream-vhost: www.google.com
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: google
            port:
              name: https

请注意,来自您的入口控制器的证书不是 google 的证书。因此可能存在一些问题。一种可能有助于解决此类问题的设置是注释 nginx.ingress.kubernetes.io/upstream-vhost,如上所示。