redirect everything with ingress-nginx

1/8/2022

i've create a yaml file that its only job is: It should immediately redirect to google.com

but its just doesn't work...

my localhost still returns 404-nginx

i'm on docker-desktop and my clusterversion is v1.21.5

here is my 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

here is my 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

NOTE: the other ingress sevices e.g. cheddar.127.0.0.1.nip.io is working perfectly...

-- Ali
kubectl
kubernetes
kubernetes-ingress
nginx-ingress

1 Answer

1/8/2022

I guess you forgot the ingress class name.

spec:
  ingressClassName: nginx
  ...

Apart from that, you can create an 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

Note, that the cert from your ingress controller is not the cert of google. So there can be some issues around that. One setting that may help with those kind of issues is the annotation nginx.ingress.kubernetes.io/upstream-vhost like shown above.

-- The Fool
Source: StackOverflow