How to Route Outside of Kubernetes using IngressRoute

Thanks for your help @dduportal.

As i mentioned in https://github.com/containous/traefik/issues/5751. I am using IngressRoutes to define rules to send traffic to several services within k8s.
Most of the services exist within k8s but a few are outside of k8s. So normally we are route to them from k8s services by using ExternalName services, however it appears these dont work with traefik. So, Im trying to find a way to route outside of k8s for a few rules.

I created a file based provider

    [http.services]
      [http.services.gateway-legacy.loadBalancer]
        [[http.services.gateway-legacy.loadBalancer.servers]]
          url = "http://gateway-lagacy.mydomain.internal/"

and then trying to references that in an IngressRoute

  - match: PathPrefix(`/legacy`)
    kind: Rule
    services:
    - name: gateway-legacy
      port: 8000

but i get the following in the logs

error msg="Cannot create service: service not found default/gateway-legacy" ingress=entry-www-de namespace=default servicePort=8001 serviceName=gateway-legacy providerName=kubernetescrd

How can I route outside of the k8s cluster using IngressRoutes.

@dduportal I think you said you had 2 workarounds?

Hi @crhuber, thanks for this description. Let's start with the first solution, continuing on the direction you took with the Traefik file provider.

In this case, Kubernetes is not used for routing to the external service

  • If the web service is running in Kubernetes, then IngressRoute + (Kubernetes) Service with annotations.
  • If the web service is outside Kubernetes, then Traefik Router + Traefik Service (as described in https://docs.traefik.io/v2.0/routing/overview/).

Following the file provider documentation example (https://docs.traefik.io/v2.0/routing/overview/#example-with-a-file-provider), then you need to define, in the TOML for the file provider dynamic config, the http routers objects equivalent to the IngressRoute you wanted to create + specify the port 8000 in the http.service.xxx.url field.

Please note the following:

  • I've named the service gateway-legacy-svc and the router gateway-legacy to avoid confusion.
  • I assume that the Traefik pod is able to contact the URL http://gateway-lagacy.mydomain.internal:8000/. You can verify it by spawning an interactive shell inside traefik's pod with kubectl exec -ti --namespace=<traefik namespace> <traefik pod id> -- sh and curl-ing the URL from this shell: # apk add --no-cache curl && curl -v http://gateway-lagacy.mydomain.internal:8000/
[http]
  [http.routers]
     [http.routers.gateway-legacy]
      rule = "PathPrefix(`/legacy`)"
      service = "gateway-legacy-svc" # Traefik Service Reference
  
  [http.services]
    [http.services.gateway-legacy-svc.loadBalancer]
      [[http.services.gateway-legacy-svc.loadBalancer.servers]]
      url = "http://gateway-lagacy.mydomain.internal:8000/"

Can you start by trying this solution please?

Yes that worked perfectly, thanks @dduportal!

1 Like