I'm using docker-compose for mongodb replica set. Everything work fine on my host where docker is running. I had problems when I tried to connect from another client which is in the same network.I have exposed ports in my docker.
version: '3.2'
networks:
netBackEnd:
ipam:
driver: default
config:
- subnet: 192.168.0.0/24
services:
mongo1:
hostname: mongo1
container_name: mongo1
image: mongo:4.2-bionic
expose:
- 27017
ports:
- 27017:27017
restart: always
volumes:
- "/etc/mongodb/ssl/mongo1.pem:/data/mongo1.pem:ro"
- "/etc/mongodb/ssl/testing.ca.crt:/data/testing.ca.crt:ro"
- "/usr/local/mongo-volume1:/data/db"
command: --replSet rs0 --sslMode requireSSL --clusterAuthMode x509 --sslClusterFile /data/mongo1.pem --sslPEMKeyFile /data/mongo1.pem --sslCAFile /data/testing.ca.crt --bind_ip 0.0.0.0 --port 27017
networks:
netBackEnd:
ipv4_address: 192.168.0.2
mongo2:
hostname: mongo2
container_name: mongo2
image: mongo:4.2-bionic
expose:
- 27018
ports:
- 27018:27018
restart: always
volumes:
- "/etc/mongodb/ssl/mongo2.pem:/data/mongo2.pem:ro"
- "/etc/mongodb/ssl/testing.ca.crt:/data/testing.ca.crt:ro"
- "/usr/local/mongo-volume2:/data/db"
command: --replSet rs0 --sslMode requireSSL --clusterAuthMode x509 --sslClusterFile /data/mongo2.pem --sslPEMKeyFile /data/mongo2.pem --sslCAFile /data/testing.ca.crt --bind_ip 0.0.0.0 --port 27018
networks:
netBackEnd:
ipv4_address: 192.168.0.3
mongo3:
hostname: mongo3
container_name: mongo3
image: mongo:4.2-bionic
expose:
- 27019
ports:
- 27019:27019
restart: always
volumes:
- "/etc/mongodb/ssl/mongo3.pem:/data/mongo3.pem:ro"
- "/etc/mongodb/ssl/testing.ca.crt:/data/testing.ca.crt:ro"
- "/usr/local/mongo-volume3:/data/db"
command: --replSet rs0 --sslMode requireSSL --clusterAuthMode x509 --sslClusterFile /data/mongo3.pem --sslPEMKeyFile /data/mongo3.pem --sslCAFile /data/testing.ca.crt --bind_ip 0.0.0.0 --port 27019
networks:
netBackEnd:
ipv4_address: 192.168.0.4
/etc/hosts file where the docker is running
127.0.0.1 localhost
127.0.1.1 xxxx-ThinkPad-X270
192.168.0.1 mongo1
192.168.0.2 mongo2
192.168.0.3 mongo3
I can connect to individual host using ip_address of the host where docker is running but not to complete replica set as replica set only works when uses the same host-names as in the replica set initialization.
mongo --ssl --sslCAFile /etc/mongodb/ssl/testing.ca.crt --host host_ip_address:27017 --sslPEMKeyFile /etc/mongodb/ssl/remote_client.pem --authenticationDatabase '$external' --authenticationMechanism 'MONGODB-X509'
But not uisng below connection string
mongo --ssl --sslCAFile /etc/mongodb/ssl/testing.ca.crt --host rs0/host_ip_address:27017,host_ip_address:27018,host_ip_address:27019 --sslPEMKeyFile /etc/mongodb/ssl/client.pem --authenticationDatabase '$external' --authenticationMechanism 'MONGODB-X509'
So I thought of using nginx server as reverse proxy to my replica set. but again the same problem I can only connect to individual host.
nginx config file
stream {
server {
listen 27020 so_keepalive=on;
proxy_connect_timeout 20s;
proxy_pass stream_mongo_backend;
proxy_timeout 10m;
}
upstream stream_mongo_backend {
server mongo1:27017;
server mongo1:27018;
server mongo1:27019;
}
}
Connection string
mongo --ssl --sslCAFile /etc/mongodb/ssl/testing.ca.crt --host rs0/host_ip_address:27020 --sslPEMKeyFile /etc/mongodb/ssl/remote_client.pem --authenticationDatabase '$external' --authenticationMechanism 'MONGODB-X509
I have found the below post regarding replicaset config behind proxy. I haven't completely understood the solution and I'm looking something for docker and using nginx.
Any idea where it's going wrong.