docker - RabbitMQ、.NET Core 和 Kubernetes(配置)
问题描述
我正在尝试在 Kubernetes 中设置一些微服务。一切都按预期工作,除了从一个微服务到 RabbitMQ 的连接。
问题流程:
- .NET Core 应用程序 --> rabbitmq-kubernetes-service.yml --> 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
我哪里错了?
解决方案
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
.
推荐阅读
- python - 如何在 Tkinter python 中将字符串转换为整数
- reactjs - 尝试在 React 组件中接受名称为 css 的 Props 无效。@情感/反应
- python - 如何使用 timedelta 日期计算管理缺失值
- php - 如何在 php 中使用 Google 控制台服务帐户通过 Google Drive API 获取文件夹/文件列表
- python - PySpark:在 UDF 中使用列名并根据逻辑连接列名
- python - 使用python根据类百分比从数据集中获取样本
- python - NumPy 数组计算时间问题
- audio - 在实体中使用声音控制器时出现“aframe-audio-analyser”组件的问题
- reactjs - 使用 react-redux 实现 CRUD
- c# - IsCancellationRequested = true 并转到异常。如何从 API 获取所有响应?