简体   繁体   中英

Ingress to redirect to service from LB ip

I'm trying to create an Ingress rule to redirect requests from LoadBalancer to Service.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: hostlessendpoint
spec:
  rules:
  - http:
      paths:
      - path: /hostless
        backend:
          serviceName: node-red
          servicePort: 1880

The above yaml script is supposed to redirect all requests from https://LOAD_BALANCER_IP/hostless to node-red on port 1880

If I try to request the above URL, I got an error Cannot GET /hostless . Requesting the root gives 404 page not found .

I can access my services with the direct URL (eg. http://LOAD_BALANCER_IP:1880 will redirect to Node-red service).

Service yaml looks like that:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: node-red
  name: node-red
spec:
  ports:
    - name: "1880"
      port: 1880
      protocol: TCP
      targetPort: 1880
  selector:
    app: node-red
  sessionAffinity: None
  type: LoadBalancer

How to use ingress to reach a service whereas a custom port?

Try setting a rewrite-target annotation, which will effectively rewrite /hostless requests to / , ie http://LOAD_BALANCER_IP:1880/hostless externally to http://node-red:1880 internally.

For example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: hostlessendpoint
spec:
  rules:
  - http:
      paths:
      - path: /hostless(/|$)(.*)
        backend:
          serviceName: node-red
          servicePort: 1880

See this link , assuming you are using Nginx ingress controller.

I've found a way to make it works !

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
    traefik.ingress.kubernetes.io/redirect-entry-point: https
    kubernetes.io/ingress.class: "traefik"
    traefik.ingress.kubernetes.io/router.middlewares: "kube-system-traefik-stripprefix@kubernetescrd"
  name: traefik-all
  namespace: partner
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: nginx-demo
          servicePort: 2222
      - path: /node
        backend:
          serviceName: node-red
          servicePort: 1880
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: traefik-stripprefix
  namespace: kube-system
spec:
  stripPrefixRegex:
    regex:
      - "/[^/]+"

I use a middleware to remove the prefix as K3S Traefik does not support the traefik.ingress.kubernetes.io/rewrite-target annotation.

With the above code, all trafic from LOAD_BALANCER_IP/* will be redirected to nginx-demo:2222 service. All trafic from LOAD_BALANCER_IP/node/* will be redicted to node-red:1880 service.

I hope this could help someone !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM