如何使用负载均衡主机名连接到rabbitmq服务

ctzwtxfj  于 7个月前  发布在  RabbitMQ
关注(0)|答案(4)|浏览(91)

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地址连接。
为了使RabbitMQ服务可以通过负载均衡器的the-load-balancer主机名访问,需要做什么?

后期编辑:

从另一个容器运行简单的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'之后,客户端无法连接。

iqjalb3h

iqjalb3h1#

当从负载均衡器后面为RabbitMQ服务时,需要打开端口567215672。如果配置正确,kubectl describe service the-load-balancer命令应该返回Map到本地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

uqdfh47h

uqdfh47h2#

我注意到,在你的代码中,你使用端口5672直接与端点对话,而在服务定义中它是15672,这是Web控制台的端口?

vfh0ocws

vfh0ocws3#

确保负载均衡器服务和rabbitmq位于应用程序的同一命名空间中。
如果没有,你必须使用完整的dns记录service-x.namespace-b.svc.cluster.local,根据DNS for Services and Pods documentation

n9vozmp4

n9vozmp44#

如果你想从web访问RabbitMQ UI,根据LB定义,它在port#15672上公开。
尝试curl -u$username:$password $LB_IP_OR_HOSTNAME:15672/api/overview。每当您使用像pika这样的客户端时,它都会使用5672上的amqp协议连接到RabbitMQ。
我希望这能澄清

  • 从应用程序客户端,它通常连接到5672
  • Curl/Web不使用amqp,因此与15672通信

相关问题