Carbonman
Carbonman

Reputation: 89

Mirror traffic using Traefik 2

We are using Traefik v2 running in kubernetes in a shared namespace (called shared), with multiple namespaces for different projects/services. We are utilising the IngressRoute CRD along with middlewares.

We need to mirror (duplicate) all incoming traffic to a specific URL (blah.example.com/newservice) and forward it to 2 backend services in 2 different namespaces. Because they are separated between 2 namespaces, they are running as the same name, with the same port.

I've looked at the following link, but don't seem to understand it: https://doc.traefik.io/traefik/v2.3/routing/providers/kubernetes-crd/#mirroring

This is my config:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
  name: shared-ingressroute
  namespace: shared
spec:
  entryPoints: []
  routes:
  - kind: Rule
    match: Host(`blah.example.com`) && PathPrefix(`/newservice/`)
    middlewares:
    - name: shared-middleware-testing-middleware
      namespace: shared
    priority: 0
    services:
    - kind: Service
      name: customer-mirror
      namespace: namespace1
      port: TraefikService


---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: shared-middleware-testing-middleware
  namespace: shared
spec:
  stripPrefix:
    prefixes:
    - /newservice/

---
apiVersion: traefik.containo.us/v1alpha1
kind: TraefikService
metadata:
  name: customer-mirror
  namespace: namespace1

spec:
  mirroring:
    name: newservice
    port: 8011
    namespace: namespace1
    mirrors:
      - name: newservice
        port: 8011
        percent: 100
        namespace: namespace2

What am I doing wrong?

Upvotes: 2

Views: 1869

Answers (2)

neoakris
neoakris

Reputation: 5075

@Carbonman Traefik mirroring was previously poorly documented and really hard to figured out. (I'm not a traefik expert, just a tinkerer who wanted to learn the basics of it.)

I made a how-to guide at this permanent location that explains traefik mirroring in good depth, It might not answer your question directly, but it should help you understand it well enough to figure out the rest yourself. (It's far too much to put in an SO answer, so I'm linking to it, using a link that won't rot.)
https://gist.github.com/neoakris/8ce77dab88868de0f5206bc9c482cfab



Side note: they're coming out with traefik 3.0 that has a goal of improving Kube UX(user experience)
https://traefik.io/blog/traefik-proxy-3-0-scope-beta-program-and-the-first-feature-drop/

Upvotes: 1

Vlad
Vlad

Reputation: 320

based on docs, for Your case kind should be TraefikService

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
  name: shared-ingressroute
  namespace: shared
spec:
  entryPoints: []
  routes:
  - kind: Rule
    match: Host(`blah.example.com`) && PathPrefix(`/newservice/`)
    middlewares:
    - name: shared-middleware-testing-middleware
      namespace: shared
    services:
    - kind: TraefikService
      name: customer-mirror
      namespace: namespace1

Upvotes: 0

Related Questions