首页 > 解决方案 > RabbitMQ、.NET Core 和 Kubernetes(配置)

问题描述

我正在尝试在 Kubernetes 中设置一些微服务。一切都按预期工作,除了从一个微服务到 RabbitMQ 的连接。

问题流程:


在 .NET Core 应用程序中,rabbit 连接工厂配置如下所示:

"RabbitMQ": {
    "Host": "rabbitmq-service",
    "Port": 7000,
    "UserName": "guest",
    "Password": "guest"
}

kubernetes rabbit 服务如下所示:

apiVersion: v1
kind: Service
metadata:
  name: rabbitmq-service
spec:
  selector:
    app: rabbitmq
  ports:
    - port: 7000
      targetPort: 5672

以及rabbit部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rabbitmq
  labels:
    app: rabbitmq
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rabbitmq
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      containers:
        - name: rabbitmq
          image: <private ACR with vanilla cfg - the image is: rabbitmq:3.7.9-management-alpine>
          imagePullPolicy: Always        
          resources:
            limits:
              cpu: "1"
              memory: 512Mi
            requests:
              cpu: "0.5"
          ports:
            - containerPort: 5672

所以这个设置目前在 k8s 中不起作用在本地,它就像一个带有基本 docker-compose 的魅力。

但是,我可以在 k8s 中做的是从 LoadBalancer --> 到正在运行的 rabbit pod 并使用这些配置设置访问管理 GUI。

api版本:v1
种类:服务
元数据:
  名称:rabbitmqmanagement-loadbalancer
规格:
  类型:负载均衡器
  选择器:
    应用程序:rabbitmq
  端口:
  - 端口:80
    目标端口:15672

我哪里错了?

标签: dockerkubernetes.net-corerabbitmq

解决方案


I'm assuming you are running the .NET Core app outside the Kubernetes cluster. If this is indeed the case then you need to use type: LoadBalancer.

LoadBalancer is used to expose a service to the internet.

ClusterIP exposes the service inside cluster-internal IP. So Service will be only accessible from within the cluster, also this is a default ServiceType.

NodePort exposes the service on each Node's IP at a static port.

For more details regarding Services please check the Kubernetes docs.

You can if the connection is working using a python script:

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='RABBITMQ_SERVER_IP'))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

This script will try to connect RABBITMQ_SERVER_IP using port 5672.

Script requires a library pika which can be installed using pip install pika.


推荐阅读