Dynamic redirect using ingress

9/5/2018

I have 2 questions:

1) I have a kubernetes cluster with multiple services and I want to use ingress to dynamically redirect the traffic to the cluster.

I expect the configuration to look something like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /service1/*
        backend:
          serviceName: service1
          servicePort: 80
        path: /*
      - path: /service2/*
        backend:
          serviceName: service2
          servicePort: 80
        path:/*

So basically I want all the traffic to /service1/endpoint to be redirected to s1:80/endpoint dynamically.

2) Let's say I have 2 web services - service1 & service2.

I want the users to work with the following URL in their browser:

kube/serviceN/endpoint

Is there a way to do that without having my users redirected to service1/endpoint?

Thanks!

-- Shachar Ashkenazi
containers
kubernetes
kubernetes-ingress
routing
service

2 Answers

9/5/2018

I hope I have understood your question properly but if so, what you have provided as an example is pretty close to the mark. The below config should work as described.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /service1/
        backend:
          serviceName: service1
          servicePort: 80
      - path: /service2/
        backend:
          serviceName: service2
          servicePort: 80

Good luck :)

-- Karrier
Source: StackOverflow

9/5/2018

I believe your ingress definition to be almost correct:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /service1
        backend:
          serviceName: service1
          servicePort: 80
      - path: /service2
        backend:
          serviceName: service2
          servicePort: 80

This should work if you have an ingress correctly deployed!

-- enzian
Source: StackOverflow