如何仅将特定服务映射到 ALB(k8s 入口)中的特定侦听器端口

How to map only specific services to a specific listener port in ALB (k8s ingress)

比如k8s中安装了guestbook-ui服务和bbs-ui服务

并且我只想将guestbook-ui映射到8080监听端口,bbs-ui服务映射到8081监听端口到预生成的k8s ALB入口。

但是,如果你在spec中写入并存储以下内容,那么所有的guestbook-ui和bbs-ui服务都会被部署到8080、8081的所有端口,并且路由被扭曲。

# 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

如何将服务部署到我想要的监听端口?

有一个功能可以自动合并同一个 ingress 组 中所有入口的多个入口规则。 AWS ALB 入口控制器通过单个 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.

因此,您可以像这样创建入口:

---
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

您可以阅读有关 IngressGroup here 的更多信息。