select a service/pod by its label from Ingress

5/20/2019

Is it possible to select a service/pod via its label from Ingress (to direct the traffic to)?

Let's say I have 2 similar pods/services with different labels, but I want to direct the traffic to only one of them

I'm looking for something similar to this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: Ingress-name
  labels:
    owner: me
selector:
  matchLabels:
    podlabel: pod-label
spec:
  rules:
  - host: ${INGRESS_HOST}
    http:
      paths:
        - path: /api
          backend:
            serviceName: <something>
            servicePort: <something>

how should I support this part:

selector:
  matchLabels:
    podlabel: pod-label
-- Mahyar
kubernetes
kubernetes-ingress
kubernetes-pod

1 Answer

5/21/2019

If you want to select a service name from ingress then you can use

apiVersion: extensions/v1beta1
kind: Ingress
spec:
  rules:
  - host: ${INGRESS_HOST}
    http:
      paths:
        - path: /api
          backend:
            serviceName: <service name>
            servicePort: <service name>

If you want to manage the traffic to redirect to particular pod then you can achieve this at the service layer.

If you want to blue/green deployment etc you can manage and divert traffic to particular pod from service only.

So ingress will be pointing to the same service but service will be changing pointing of pod.

Check this blue-green deployment: https://www.ianlewis.org/en/bluegreen-deployments-kubernetes you can find out how service manages traffic routes based on lables blue and green.

-- Harsh Manvar
Source: StackOverflow