RabbitMQ, .NET Core and Kubernetes (configuration)

4/4/2019

I'm trying to set up a few micro services in Kubernetes. Everything is working as expected, except the connection from one micro service to RabbitMQ.

Problem flow:

  • .NET Core app --> rabbitmq-kubernetes-service.yml --> RabbitMQ

In the .NET Core app the rabbit connection factory config looks like this:

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

The kubernetes rabbit service looks like this:

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

As well as the rabbit deployment:

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

So this setup is currently not working in k8s. Locally it works like a charm with a basic docker-compose.

However, what I can do in k8s is to go from a LoadBalancer --> to the running rabbit pod and access the management GUI with these config settings.

apiVersion: v1 kind: Service metadata: name: rabbitmqmanagement-loadbalancer spec: type: LoadBalancer selector: app: rabbitmq ports:

  • port: 80 targetPort: 15672

Where am I going wrong?

-- Henkolicious
.net-core
docker
kubernetes
rabbitmq

1 Answer

5/28/2019

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.

-- Crou
Source: StackOverflow