K8s service LB to external services w/ nginx-ingress controller

8/26/2020

Is it possible to configure k8s nginx-ingress as a LB to have a K8s Service actively connect to external backend hosted on external hosts/ports (where one will be enabled at a time, connecting back to the cluster service)?

Similar to envoy proxy? This is on vanilla K8s, on-prem.

So rather than balance load from

client -> cluster -> service.

I am looking for

service -> nginx-ingress -> external-backend.
-- jj2424
kubernetes
nginx-ingress

2 Answers

9/3/2020

While defining ingress use nginx.ingress.kubernetes.io/configuration-snippet annotation. Enable also proxy protocol using use-proxy-protocol: "true".

Using this annotation you can add additional configuration to the NGINX location.

Please take a look: ingress-nginx-issue, advanced-configuration-with-annotations.

-- Malgorzata
Source: StackOverflow

9/3/2020

Define a Kubernetes Service with no selector. Then you need to define a Endpoint. You can put the IP and port in the Endpoint. Normally you do not define Endpoints for Services but because the Service will not have a Selector you will need to provide an Endpoint with the same name as the Service.

Then you point the Ingress to the Service.

Here's an example that exposes an Ingress on the cluster and sends the traffic to 192.168.88.1 on TCP 8081.

apiVersion: v1
kind: Service
metadata:
  name: router
  namespace: default
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8081
---
apiVersion: v1
kind: Endpoints
metadata:
  name: router
  namespace: default
subsets:
  - addresses:
      - ip: 192.168.88.1
      - ip: 192.168.88.2 # As per question below
    ports:
      - port: 8081
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: router
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - host: my-router.domain.com
      http:
        paths:
          - path: /
            backend:
              serviceName: router
              servicePort: 80
-- Justin Tamblyn
Source: StackOverflow