Kong Ingress controller route based on request header

Hi,

I’am having trouble configuring kong ingress controller in kubernetes. I would like to route traffic based on a request header to a service in my kubernetes cluster. The host of these request should be the same.

The request curl -X POST http://myhost.com -H 'x-custom-header: some-value' should be routed to serviceA and curl -X POST http://myhost.com -H 'x-custom-header: some-other-value' should be routed to serviceB.

Ingress only support path and host based routing and I can’t figure out how I should configure my ingress. This is my current configuration.

Ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: api-gateway-ingress
spec:
    - host: myhost.com
      http:
        paths:
          - path: /
            backend:
              serviceName: serviceA
              servicePort: 80
          - path: /b
            backend:
              serviceName: serviceB
              servicePort: 80

Service:

apiVersion: v1
kind: Service
metadata:
  name: serviceA
  annotations:
    configuration.konghq.com: service-a-routing
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
---
apiVersion: v1
kind: Service
metadata:
  name: serviceB
  annotations:
    configuration.konghq.com: service-b-routing
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http

KongIngress:

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
    name: service-a-routing
route:
  hosts:
    - myhost.com
  headers:
    x-custom-header: 
      - some-value
---
apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
    name: service-b-routing
route:
  hosts:
    - myhost.com
  headers:
    x-custom-header: 
      - some-other-value

When I make the request curl -X POST http://myhost.com -H 'x-custom-header: some-other-value' it is routed to serviceA instead of serviceB.

Any recommendation?

Thanks,
Tom

To start off, you need two Ingress resources with one rule each one with / as the rule (exact same Ingress resources, except with different names and backends).

Now to one of those, attach the service-a-routing KongIngress resouce.
Please note that you need to associate the KongIngresss resource with Ingress and not with Service resource for this configuration.

Do the same with the other Ingress resource with service-b-routing KongIngress resource.

Also, you can safely remove the hosts section of the route in KongIngress, that gets populated via the Ingress definition (since, Ingress supports path and hosts). KongIngress is an extension to Ingress resource.

It works perfectly now. Thanks a lot!


© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ