Kubernetes ingress routes with url parameter

12/19/2018

As I will have no wifi tomorrow this is more some kind of theory crafting. I need to prepare the ingress files in "offline mode".

I want to route from ApplicationA to ApplicationB. These routes are hopefully able to carry url parameter. Both applications are using spring boot and REST. The cluster is (currently) set up by minikube.

So e.g. I got this url in ServiceA: http://url.com/customerapi/getCustomerById?id=5. This url should hit a method which is defined in ApplicationB. ApplicationB is reachable using customerservice and port 31001.

Is it as simple as the ingress below? Thats pretty much straight forward. Best regards.

I would define an kubernetes ingress like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: serviceA
spec:
  rules:
  - http:
      paths:
      - path: /customerapi
        backend:
          serviceName: customerservice
          servicePort: 31001
-- elp
kubernetes
spring-boot

1 Answer

12/21/2018

If I understand you correctly, you want to route traffic coming from web into two backends based on the url.

You can set your Ingress the following way:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: url.com
    http:
      paths:
      - path: /test1
        backend:
          serviceName: test1-svc
          servicePort: 80
      - path: /test2
        backend:
          serviceName: test2-svc
          servicePort: 80

This will route all from url.com/test1 to backend test1-svc and all from url.com/test2 to backend test2-svc.

If you need to use the parameter inside the Url, I think the following will work:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    ingress.kubernetes.io/query-routing: default/query-routing
spec:
  backend:
    serviceName: default-backend
    servicePort: 80
  rules:
  - host: url.com

---

kind:ConfigMap
apiVersion: v1
metadata:
  name: query-routing
data:
  mapping: |-
    [{
      "field": "getCustomerById",
      "value": "1",
      "path": "customerapi/",
      "service": "customerservice",
      "port": "31001"
    }]

But please test it on your example, as there are not enough details in your question.

There is a way of catching the parameter from Header using nginx.ingress.kubernetes.io/server-snippet Annotations. This particular one is being used by Shopify and usage is explained here. For more annotations please check Kubernetes NGINX Ingress Controller.

-- Crou
Source: StackOverflow