Home > Software engineering >  How to map only specific services to a specific listener port in ALB (k8s ingress)
How to map only specific services to a specific listener port in ALB (k8s ingress)

Time:02-18

For example, guestbook-ui service and bbs-ui service are installed in k8s.

And I want to map guestbook-ui only to the 8080 listener port and bbs-ui service to the 8081 listener port to the pre-generated k8s ALB ingress.

However, if you write and store the following in spec, all guestbook-ui and bbs-ui services are deployed to all ports of 8080, 8081, and routing is twisted.

# skip

metadata:
  annotations:
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":8080}, {"HTTP":8081}]'

# skip

spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: bbs-ui
            port:
              number: 80
        path: /*
        pathType: ImplementationSpecific
  - http:
      paths:
      - backend:
          service:
            name: guestbook-ui
            port:
              number: 80
        path: /*
        pathType: ImplementationSpecific

How can I deploy the service to the listener port I want?

CodePudding user response:

There is a feature to automatically merge multiple ingress rules for all ingresses in the same ingress group. The AWS ALB ingress controller supports them with a single ALB.

metadata:
  annotations:
    alb.ingress.kubernetes.io/group.name: my-group

In the AWS ALB ingress controller, prior to version 2.0, each ingress object you created in Kubernetes would get its own ALB. Customers wanted a way to lower their cost and duplicate configuration by sharing the same ALB for multiple services and namespaces. By sharing an ALB, you can still use annotations for advanced routing but share a single load balancer for a team, or any combination of apps by specifying the alb.ingress.kubernetes.io/group.name annotation. All services with the same group.name will use the same load balancer.

So, you can create ingress like this:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress1
  namespace: mynamespace
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/tags: mytag=tag
    alb.ingress.kubernetes.io/target-type: ip
    alb.ingress.kubernetes.io/group.name: my-group
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 8080}]'
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: bbs-ui
            port:
              number: 80
        path: /*
        pathType: ImplementationSpecific

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress2
  namespace: mynamespace
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/tags: mytag=tag
    alb.ingress.kubernetes.io/target-type: ip
    alb.ingress.kubernetes.io/group.name: my-group
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 8081}]'
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: guestbook-ui
            port:
              number: 80
        path: /*
        pathType: ImplementationSpecific

You can read more info about IngressGroup here.

  • Related