cant connect to kafka from external machine - spring

I´m starting with Apache Kafka and i´m facing problems when i try to conect from an external machine.
With this configuration bellow, all works fine if the application and the docker are running at the same machine.
but when i put the application in machine A and docker at machine B, the application cant connect.
My spring Kafka #Configuration have this line to #Bean consumerFactory and producerFactory (imagine my machine with docker ip = 10.10.10.10)
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "10.10.10.10:9092");
And my docker file is this:
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper:3.4.6
ports:
- 2181:2181
kafka:
image: wurstmeister/kafka:0.10.1.1
environment:
KAFKA_ADVERTISED_HOST_NAME: 0.0.0.0
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_CREATE_TOPICS: "topic-jhipster:1:1,PROCESS_ORDER:1:1, PROCESS_CHANNEL:1:1"
JMX_PORT: 9999
KAFKA_JMX_OPTS: "-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.rmi.port=9999"
ports:
- 9092:9092
- 9999:9999
kafka-manager:
image: sheepkiller/kafka-manager
ports:
- 9000:9000
links:
- zookeeper
environment:
ZK_HOSTS: zookeeper:2181
i get this error:
org.springframework.kafka.core.KafkaProducerException: Failed to send;
nested exception is org.apache.kafka.common.errors.TimeoutException:
Expiring 1 record(s) for
Edit, add some information..
I think its any configuration about the zookeeper i´m missing .. because if i have only the zookeeper started at my machine A .. and the kafka in machine B.. that works.. i only don´t know how :(

Try setting Listeners,
eg: listeners = PLAINTEXT://your.host.name:9092
Assuming you can telnet between the machines on kafka port.

Set advertised.listeners to the hostname or ip of the docker container's host machine.
environment:
KAFKA_ADVERTISED_LISTENERS: "10.10.10.10:9092"
What is happening is that the client connects to the bootstrap server, does a meta-data request to discover which Kafka broker in the cluster to connect to for a particular topic partition, and gets the advertised host name back as a response (in your case 0.0.0.0) which will only work if everything is on the same machine.
You need to advertise an ip or hostname that will work from the remote machine so not localhost, 127.0.0.1, or 0.0.0.0. Also not the private internal IP or hostname of the docker container. It has to be the external/public IP or hostname.
Also advertised.host.name and advertised.port are deprecated parameters in Kafka 0.10.x so even if you use them (they work for backward compatibility), you also need to set the advertised.host.name to be something that the producer can resolve and connect to. I suggest using a fully qualified host name or IP of the docker host machine (I.e. 10.10.10.10).
From http://kafka.apache.org/0101/documentation.html#brokerconfigs
DEPRECATED: only used when advertised.listeners or listeners are
not set. Use advertised.listeners instead. Hostname to publish to
ZooKeeper for clients to use. In IaaS environments, this may need to
be different from the interface to which the broker binds. If this is
not set, it will use the value for host.name if configured.
Otherwise it will use the value returned from
java.net.InetAddress.getCanonicalHostName().

Some mix of #Krishas and #Hans Jespersen
Here is the code of my docker yml:
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper:3.4.6
ports:
- 2181:2181
kafka:
image: wurstmeister/kafka:0.10.1.1
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://10.10.10.10:9092
KAFKA_ADVERTISED_HOST_NAME: 10.10.10.10
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
That needs the "PLAINTEXT:// prefix !
And config the "host_name" + "port", or "listeners"
The next step is decovery how i will configure another nodes

You need to specify java.rmi.server.hostname=0.0.0.0

Related

Docker: Ports are not available: exposing port TCP 0.0.0.0:61615 -> 0.0.0.0:0: listen tcp 0.0.0.0:61615

I am trying to use ActiveMQ in Docker, but I've started to get this error when starting the container.
Error invoking remote method 'docker-start-container': Error: (HTTP code 500) server error - Ports are not available: exposing port TCP 0.0.0.0:61613 -> 0.0.0.0:0: listen tcp 0.0.0.0:61613: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
I could not find any service that is using these ports (maybe I looking in a wrong way).
I'm seeing that people generally suggest to restart winnat, but I am not sure if it's a good idea and I'd like to know if there are any other solutions to this problem.
Also changing ports ranges won't work in my case since they are already set to a suggested value:
Protocol tcp Dynamic Port Range
---------------------------------
Start Port : 49152
Number of Ports : 16384
Here is a part of my docker-compose file:
version: '3.5'
networks:
test-net:
ipam:
driver: default
config:
- subnet: 172.33.1.0/24
services:
activemq:
image: privat_artifactory
container_name: test-activemq
restart: always
networks:
- test-net
ports:
- "8161:8161"
- "1883:1883"
- "5672:5672"
- "61613-61616:61613-61616"

Error Connecting to server(111): Connection refused when using RabbitMQ in Laravel

I have 2 services written in Laravel. I'm trying to develop my projects with Docker Compose.
one of my services has the below configuration for rabbitMQ in the docker-compose.yml file:
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'product_rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=password
for the second service, I can't use the same port, so I'm changing the port to 5671 or 4000 as the below:
ports:
- 5671:5672
- 15673:15672
But I'm getting the Error Connecting to server(111): Connection refused error.
everything is OK when I shut down the service1 and expose rabbitMQ on port 5672.
also, I have changed my .env file.
RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5671
RABBITMQ_USER=root
RABBITMQ_PASSWORD=password
Should I choose another port?

Hosted Service RabbitMQ connection failure using Docker Compose

Here is the log
MassTransit.RabbitMqTransport.Integration.ConnectionContextFactory.CreateConnection(ISupervisor supervisor)
[02:51:48 DBG] Connect: guest#localhost:5672/
[02:51:48 WRN] Connection Failed: rabbitmq://localhost/
RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
The RabbitMQ control panel is showing the exchanges and queues as created and when I make a publish request I see the queue come through but then get a MassTransit timeout as it tries to respond.
Here is my docker yaml setup. I assume MassTransit pulls its settings to connect from appsettings.json.
version: '3.4'
services:
hostedservice:
environment:
- ASPNETCORE_ENVIRONMENT=development
ports:
- "80"
rabbitmq3:
hostname: "rabbitmq"
image: rabbitmq:3-management
environment:
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest
- RABBITMQ_DEFAULT_VHOST=/
ports:
# AMQP protocol port
- '5672:5672'
# HTTP management UI
- '15672:15672'
I'd suggest using the MassTransit Docker template to get a working setup. Or you can look at the source code and see how when running in a container, the template using rabbitmq as the host name to connect.
You can download the template using NuGet.
Thanks Chris that moved me to the container template usage that cleared up the connection issue!

How to correctly config docker-compose to connect to localhost (non-docker service) [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
I'm new to Docker. I'm build a Spring Boot Application deploying on Docker.
Here is my example docker-compose.yml:
version: "3"
services:
user:
container_name: user
image: user-service
build:
context: user-api/
ports:
- "3001:8000"
restart: always
volumes:
- /home/ubuntu/logs/user_service:/opt/app/logs
networks:
- api_network
cms:
container_name: cms
image:cms-service
build:
context: cms-service/
ports:
- "3003:8000"
restart: always
volumes:
- /home/ubuntu/logs/cms_service:/opt/app/logs
networks:
- api_network
networks:
api_network:
driver: bridge
In the server machine, there's a Redis Server running on Ubuntu. I cannot connect the the Redis Server from Docker container to the host machine.
Here is my redis config inside application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=Password123!##
I also tried to change the localhost to
127.0.0.1
172.17.0.1
0.0.0.0
host
host.docker.internal
That's I've found on the internet. But nothing works. Do I need to specifically config anything to allow my Spring Boot Service inside Docker connect to Redis that's running on localhost.
The issue is probably due to the fact your Redis is bound to the address 127.0.0.1 (which is the default configuration) and your containers are not running on the network host.
To solve this, you should reconfigure Redis to bind to both 127.0.0.1 as well as to the IP address of the host as seen from api_network (sudo ip addr show on the host): the easiest thing to do here, if your scenario allows that, is to just bind Redis to 0.0.0.0 (via redis.conf).
As an alternative, you may also want to run your containers on the host network instead of using the api_network bridge: this appears to be overkill according to your issue, by the way, and may lead to security issues and exposed network ports.

traefik proxy for docker container running in host network

i am running traefik as a proxy in docker container
i am using DockerToolBox in windows 10
the traefik proxy was able to recognize the service app which is running in 127.0.0.1, but the service app is actually running in docker host = 192.168.99.x ip
version: '3'
services:
reverse_proxy:
image: traefik
command: --api --docker
ports:
- "81:80"
- "8081:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- backend
whoami:
image: containous/whoami
labels:
- "traefik.frontend.rule=Host:whoami.default"
- "traefik.enable=true"
- "traefik.port=80"
network_mode: host
networks:
backend:
driver: bridge
in the Traefik dashboard http://192.168.99.100:8081
it shows http://127.0.0.1:80 for whoami service
instead of http://192.168.99.100:80
any help would be appreciated.
i want network_mode: host to pick 192.168.99.100 instead of 127.0.0.1
As traefik official documentation says, when resolving service IP, first it
try a lookup of host.docker.internal
and second
if the lookup was unsuccessful, fall back to 127.0.0.1
This means we can just add a host in the traefik container, using --add-host {docker0_IP}(it's the bridge's IP, you can easily use docker inspect {NAME_OF_TRAEFIK} and find the IP of Gateway(for me, it's 172.18.0.1). If you use docker-compose, you can use add following lines to your definition of traefik:
extra_hosts:
- host.docker.internal:{docker0_IP}
Also, I find that it's ok to use the IP my eth0 IP, which means the IP of your LAN(for me, it's 192.168.0.20).
Then, recreate traefik and everything works like a daisy.

Resources