Kubernetes pod unable to connect to rabbit mq instance running locally

我的未来我决定 提交于 2019-11-29 17:09:59

You are exposing port 80 for both, the pod and the service. Then you curl on port 5672.

Either expose port 5672 of the pod and curl it directly, or expose port 5672 of the service and keep port 80 on the pod, and curl on port 5672 of the service.

This would be a high level "sketch" of how to hit a pod:

you -curl-> service1(80:80) -> pod1(80)
you -curl-> service2(80:5672) -> pod2(5672)

So say you have two pods. One of them is serving on port 80, and the other one on port 5672. You can create two services; each of them targeting one pod. The services can be running on port 80 and map the requests to the ports 80 and 5672 of the pods.

Now you can't make one service to do the both forwarding. Needs to be one service per pod. Can be a deployment, or a group of pods, but these need to be serving on the same port.

If I understood the setup:

  • minikube is running on the local machine.
  • rabbitmq is running on the local machine, too, and is listening on port 5672.
  • the IP where rabbitmq is running is 10.2.10.122 .
  • an application - jks - is running on minikube.

The problem is that it is not possible to connect from the jks application to rabbitmq, correct?

One way to make it work is to first create a Service without selector :

apiVersion: "v1"
kind: "Service"
metadata:
  name: "svc-external-rabbitmq"
spec:
  ports:
  - name: "rabbitmq"
    protocol: "TCP"
    port: 5672
    targetPort: 5672
    nodePort: 0
selector: {}

...next, create Endpoints object for the Service:

apiVersion: "v1"
kind: "Endpoints"
metadata:
  name: "svc-external-rabbitmq"
subsets:
  - addresses:
    - ip: "10.2.10.122"
    ports:
    - name: "rabbitmq"
      port: 5672

...then use the service name - svc-external-rabbitmq - in the jks application to connect to rabbitmq.

For an explanation, see Services without selectors in the Kubernetes documentation. I've used this setup with a Cassandra cluster where the Cassandra nodes' IPs were all listed as addresses.

EDIT: Notice that in some cases a Service of type ExternalName could work, too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!