【问题标题】:How to connect to rabbitmq service using load balancer hostname如何使用负载均衡器主机名连接到 rabbitmq 服务
【发布时间】:2019-07-31 03:11:23
【问题描述】:

kubectl describe service the-load-balancer 命令返回:

Name:                     the-load-balancer
Namespace:                default
Labels:                   app=the-app
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"the-app"},"name":"the-load-balancer","namespac...
Selector:                 app=the-app
Type:                     LoadBalancer
IP:                       10.100.129.251
LoadBalancer Ingress:     1234567-1234567890.us-west-2.elb.amazonaws.com
Port:                     the-load-balancer  15672/TCP
TargetPort:               15672/TCP
NodePort:                 the-load-balancer  30080/TCP
Endpoints:                172.31.77.44:15672
Session Affinity:         None
External Traffic Policy:  Cluster

在负载均衡器后面的另一个容器上运行的 RabbitMQ 服务器可以通过负载均衡器的Endpoints 172.31.77.44:15672 从另一个容器访问。

但它无法使用the-load-balancer 主机名或通过其本地10.100.129.251 IP 地址进行连接。

需要做什么才能通过负载均衡器的the-load-balancer 主机名访问RabbitMQ 服务?

稍后编辑:

从另一个容器运行一个简单的 Python 测试:

import socket
print(socket.gethostbyname('the-load-balancer'))

返回负载均衡器本地 IP 10.100.129.251

使用 '172.31.18.32' 连接到 RabbitMQ 效果很好:

import pika
credentials = pika.PlainCredentials('guest', 'guest')
parameters = pika.ConnectionParameters(host='172.31.18.32', port=5672, credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
print('...channel: %s' % channel)

但是将host='172.31.18.32'替换为host='the-load-balancer'host='10.100.129.251'后,客户端连接失败。

【问题讨论】:

    标签: amazon-web-services kubernetes google-cloud-platform rabbitmq amazon-eks


    【解决方案1】:

    从负载均衡器后面为 RabbitMQ 提供服务时,您需要打开端口 567215672。正确配置后,kubectl describe service the-load-balancer 命令应返回映射到本地 IP 地址的两个端口:

    Name:                     the-load-balancer
    Namespace:                default
    Labels:                   app=the-app
    Selector:                 app=the-app
    Type:                     LoadBalancer
    IP:                       10.100.129.251
    LoadBalancer Ingress:     123456789-987654321.us-west-2.elb.amazonaws.com
    
    Port:                     the-load-balancer-port-15672  15672/TCP
    TargetPort:               15672/TCP
    NodePort:                 the-load-balancer-port-15672  30080/TCP
    Endpoints:                172.31.18.32:15672
    
    Port:                     the-load-balancer-port-5672  5672/TCP
    TargetPort:               5672/TCP
    NodePort:                 the-load-balancer-port-5672  30081/TCP
    Endpoints:                172.31.18.32:5672
    

    下面是用于创建 RabbitMQ 服务的the-load-balancer.yaml 文件:

    apiVersion: v1
    kind: Service
    metadata:
      name: the-load-balancer
      labels:
        app: the-app
    spec:
      type: LoadBalancer
      ports:
      - port: 15672
        nodePort: 30080
        protocol: TCP
        name: the-load-balancer-port-15672 
      - port: 5672
        nodePort: 30081
        protocol: TCP
        name: the-load-balancer-port-5672   
      selector:
        app: the-app
    

    【讨论】:

      【解决方案2】:

      我注意到在您的代码中,您使用端口 5672 直接与端点通信,而服务定义中的 15672 是 Web 控制台的端口?

      【讨论】:

        【解决方案3】:

        确保负载均衡器服务和rabbitmq 在你的应用程序的同一个命名空间中。

        如果没有,你必须使用完整的dns记录service-x.namespace-b.svc.cluster.local,根据DNS for Services and Pods documentation

        【讨论】:

          猜你喜欢
          • 2021-09-24
          • 2018-09-13
          • 2013-08-26
          • 2017-07-15
          • 2020-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多