Using nginx-ingress for cross-namespace services

Support for externalNames in nginx ingress has been asked for a while on nginx-ingress. Finally, it was released on nginx plus (https://github.com/nginxinc/kubernetes-ingress/blob/master/examples/externalname-services/README.md) and, AFAIK, id does not work on the standard version. I tried to set it up anyway, but the upstream always ends up on a 127.0.0.1:8181 endpoint if you try to configure an externalName as an upstream.

So I came up with this workaround. Probably not the most elegant solution, but it works, and the Service itself acts a load balancer.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-resource
  namespace: test-namespace
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.org/server-snippets: |
      location /exchangerates/ {
        proxy_set_header Host test.test.svc.cluster.local;
        proxy_pass http://test.test.svc.cluster.local:80/;
      }
spec:
  rules:
  - host: 8.8.8.8.xip.io
    http:
      paths:
      - backend:
          serviceName: test
          servicePort: 80
        path: /test

Here i used xip to build a valid hostname (http://xip.io/), it’s really useful for quick tests, give it a try!