Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route traffic to a service in a different namespace with Traefik and Kubernetes

Using Traefik as an ingress controller (on a kube cluster in GCP). Is it possible to create an ingress rule that uses a backend service from a different namespace?

We have a namespace for each of our "major" versions of code.

1-service.com -> 1-service.com ingress in the 1-service ns -> 1-service svc in the same ns

2-service.com -> 2-service.com ingress in the 2-service ns... and so on

I also would like another ingress rule in the "unversioned" namespace that will route traffic to one of the major releases.

service.com -> service.com ingress in the "service" ns -> X-service in the X-service namespace

I would like to keep major versions separate in k8s using versioned host names (1-service.com etc), but still have a "latest" that points to the latest of the releases.

I believe voyager can do cross namespace ingress -> svc. can Traefik do the same??

like image 662
Dan P Avatar asked Nov 17 '17 14:11

Dan P


1 Answers

You can use a workaround like this:

  1. Create a Service with type ExternalName in your namespace when you want to create an ingress:
apiVersion: v1
kind: Service
metadata:
  name: service-1
  namespace: unversioned
spec:
  type: ExternalName
  externalName: service-1.service-1-ns.svc.cluster.local
  ports:
  - name: http
    port: 8080
    protocol: TCP
  1. Create an ingress that point to this service:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
  name: ingress-to-other-ns
  namespace: service-1-ns
spec:
  rules:
  - host: latest.example.com
    http:
      paths:
      - backend:
          serviceName: service-1
          servicePort: 8080
        path: /
like image 173
Grigory Ignatyev Avatar answered Oct 17 '22 06:10

Grigory Ignatyev