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!

Hi guys

I am beginner with k8s
I am trying to do same thing routing my request based on request header can you please send me example if you have any what I need to create in K8s. We are using rancher kubernetes.
We will have version number in request header based on version number we want to route request.

Appreciate your help

Thanks
Chirag

If i want to proxy the traffic based on header regex matching not exact matching, what i should do?