Azure AKS 负载均衡器在同一 IP 上使用服务为多个 pods 提供服务

Azure AKS Loadbalancer serving multiple pods using service on same IP

我目前每个 pod 都有一个负载均衡器。在我的实例 2 pods 中具有以下 YAML 定义。

apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  ports:
  - name: https-service1
    port: 6379
    targetPort: 6379
  selector:
       app: service1-consoleapp
  type: LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: service2
spec:
  ports:
  - name: https-service2
    port: 443
    targetPort: 443
  selector:
       app: service2-consoleapp
  type: LoadBalancer

当我应用上述 2 个 yaml 文件时,我将获得 2 个外部 ip,然后我用它来配置我的 dns 子域中的 A 记录。

service1.company.com => service1-consoleapp 的外部 ip 1

service2.company.com => service2-consoleapp 的外部 ip 2

有没有办法将 YAML 文件合二为一,让我只能使用一个 IP 地址而不是两个?

此外,在 ingress 中您似乎可以做到,但不确定我如何处理“主机”要求。

谁能解释一下路由是如何工作的,因为我不确定路径中应该有什么值 属性?

我是否仍会获得 2 个外部 ip,用于填充 dns 子域?

 spec:
  rules:
  - host: service1.company.com
    http:
      paths:
      - backend:
          serviceName: service1
          servicePort: 6379
        path: ??
  - host: service2.company.com
    http:
      paths:
      - backend:
          serviceName: service2
          servicePort: 433
        path: ??

如果我输入

,我正在寻找的结果

service1.company.com:6379 在我的浏览器中然后我应该点击 pod 端点 (service1-consoleapp) 如果我输入

service2.company.com:443 在我的浏览器中然后我应该点击 pod 端点 (service2-consoleapp)。

其中 service1.company.com 和 service2.company.com 在同一 IP 地址上。

提前致谢。

你最好使用 Microsoft 文档 - Create an ingress controller in Azure Kubernetes Service (AKS)

来自文档:

An ingress controller is a piece of software that provides reverse proxy, configurable traffic routing, and TLS termination for Kubernetes services. Kubernetes ingress resources are used to configure the ingress rules and routes for individual Kubernetes services. Using an ingress controller and ingress rules, a single IP address can be used to route traffic to multiple services in a Kubernetes cluster.

按照文档设置入口。

稍后,按照评论中提供的@CSharpRocks 关注Kubernetes documentation

来自文档:

If you create an Ingress resource without any hosts defined in the rules, then any web traffic to the IP address of your Ingress controller can be matched without a name based virtual host being required.

For example, the following Ingress resource will route traffic requested for first.bar.com to service1, second.foo.com to service2, and any traffic to the IP address without a hostname defined in request (that is, without a request header being presented) to service3.

Yaml 示例:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: name-virtual-host-ingress
spec:
  rules:
  - host: first.bar.com
    http:
      paths:
      - backend:
          serviceName: service1
          servicePort: 80
  - host: second.foo.com
    http:
      paths:
      - backend:
          serviceName: service2
          servicePort: 80
  - http:
      paths:
      - backend:
          serviceName: service3
          servicePort: 80

更多关于 Path types

您当前拥有的入口资源应该可以使用。完全删除 path 部分。同样在您的 DNS 中,您需要创建子域 service1.company.comservice2.company.comA record 以指向负载均衡器的 IP。

此负载均衡器将流量从外部路由到入口控制器 pods,入口控制器将根据入口资源中定义的规则将流量转发到后端 pods。 host 规则以这种方式工作 - 如果 HTTP 请求有 Host header service1.company.com 入口控制器会将请求发送到 service1 并且如果它有 Host header service2.company.com 入口控制器会将请求发送到 service2

当你部署一个入口控制器比如 Nginx 时你需要创建一个 LoadBalancer 类型 service.So 你将只有一个 loadBalancer 用于暴露入口控制器 pods.