| Loading...
Azure Blob Storage
, AWS
, or MongoDB
. The aim is to deploy a data object server (Blob
) and a database service in production.MongoDB cluster
and a PostgreSQL
server.Aspect | PostgreSQL | MongoDB |
---|---|---|
Type | Relational (SQL) | Documentary (NoSQL) |
Schema | Rigid (strict model) | Flexible (free schema) |
Use cases | Applications requiring a structured schema (banking, ERP) | Applications with dynamic data (web, IoT) |
MongoDB Compass
and pgAdmin
tools to interact with the services. However, we're also going to integrate a small web application into the stack for this purpose; so we can interact with our databases via any device (on my smart while I'm at the puppy).On my www.roo7690.me site, for example, user data is stored via postgresql and blog information (blog status, blog comments) is managed via mongodb.
Docker
technology.app_db/docker-compose.ymversion: 1.0.0 networks: # setting up a network in our stack datanet: # specifies the network name driver: bridge # the network uses the bridge driver. This makes it a virtual network that links stack services. volumes: # additional storage space, for data persistence in the event of service restarts. postgres-data: driver: local mongo1-data: driver: local mongo2-data: driver: local mongo3-data: driver: local mongo4-data: driver: local
mongo
and postgres
for the databases; mongo-express
and adminer
for the web access platforms. And nginx
to link everything together under a single domain name.app_db/docker-compose.ymversion: 1.0.0 networks: datanet: driver: bridge services: # let's start defining our services. pg: # the name assigned to the service in the network container_name: postgres.db image: postgres restart: always # tells docker to always restart the container, should it stop due to any error, except in the case of an explicit shutdown. ports: - '5423:5432' # we map container port 5432 (the port on which the postresql server will start) to port 5423 on the host machine. networks: - datanet # we connect it to the datanet virtual network volumes: - postgres-data:/var/lib/postgresql/data # the directory /var/lib/postgresql/data will point to the postgres-data volume, defined locally as follows volumes: postgres-data: driver: local mongo1-data: driver: local mongo2-data: driver: local mongo3-data: driver: local mongo4-data: driver: local
postgres
needs environment variables such as your username or password. We could set them by assigning an env
property to pg
, but we'll opt instead to import a file on which we've mentioned its information.app_db/.env.locaPOSTGRES_USER=username POSTGRES_PASSWORD=password POSTGRES_URL=postgres://username:password@pg:5432/
app_db/docker-compose.ymversion: 1.0.0 networks: datanet: driver: bridge services: pg: container_name: postgres.db image: postgres restart: always ports: - '5423:5432' networks: - datanet volumes: - postgres-data:/var/lib/postgresql/data env_file: # add environment variables - .env.local adminer: # web platform for interaction with pg added container_name: adminer.db image: adminer restart: always networks: - datanet volumes: postgres-data: driver: local mongo1-data: driver: local mongo2-data: driver: local mongo3-data: driver: local mongo4-data: driver: local
app_db/docker-compose.ymversion: 1.0.0 networks: datanet: driver: bridge x-mongo: &mongo # anchor the property block with the name mongo image: mongo restart: always env_file: - .env.local # (TODO: environment variable to be defined) networks: - datanet command: --replSet rset --bind_ip_all --keyFile /data/db/keyfile # command to be run at container startup with mongosh client. # start the server in replica set mode under the name rset (--replSet rset) # --bind_ip_all, listens on all networks # --keyFile, define the location of the file containing the replica set identification key (TODO: file to be imported) services: mongo1: <<: *mongo # by reference to mongo1, we give all the properties defined by mongo container_name: mongo1.db volumes: - mongo1-data:/data/db - ./key/keyfile:/data/db/keyfile # copy ./key/keyfile to /data/db/keyfile (TODO: ./key/keyfile to be created) ports: - '27017:27017' mongo2: <<: *mongo container_name: mongo2.db volumes: - mongo2-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27018:27017' mongo3: <<: *mongo container_name: mongo3.db volumes: - mongo3-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27019:27017' mongo4: <<: *mongo container_name: mongo4.db volumes: - mongo4-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27020:27017' mongo-express: # added web platform for interaction with the cluster container_name: mongo-express.db image: mongo-express restart: always env_file: - .env.local networks: - database pg: container_name: postgres.db image: postgres restart: always ports: - '5423:5432' networks: - datanet volumes: - postgres-data:/var/lib/postgresql/data env_file: - .env.local adminer: container_name: adminer.db image: adminer restart: always networks: - datanet volumes: postgres-data: driver: local mongo1-data: driver: local mongo2-data: driver: local mongo3-data: driver: local mongo4-data: driver: local
app_db/.env.loca#mongo MONGO_INITDB_ROOT_USERNAME=username MONGO_INITDB_ROOT_PASSWORD=password #mongo-express ME_CONFIG_MONGODB_ADMINUSERNAME=username ME_CONFIG_MONGODB_ADMINPASSWORD=password ME_CONFIG_MONGODB_URL="mongodb://username:password@mongo1:27017,mongo2:27017,mongo3:27017,mongo4:27017/?replicaSet=rs" ME_CONFIG_BASICAUTH=true ME_CONFIG_BASICAUTH_USERNAME=username ME_CONFIG_BASICAUTH_PASSWORD=password POSTGRES_USER=username POSTGRES_PASSWORD=password POSTGRES_URL=postgres://username:password@pg:5432/
app_db/nginx.conuser www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; } http { # http protocol configuration include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/conf.d/*.conf; }
app_db/webapp.con# define a server to be included in our protocol server { listen 8082; server_name nom_de_domaine; # we define the headers proxy_set_header Host $host; proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-NginX-Proxy true; real_ip_header X-Real-IP; proxy_connect_timeout 300; proxy_set_header Upgrade $http_upgrade; proxy_cache_bypass $http_upgrade; proxy_set_header Connection "upgrade"; chunked_transfer_encoding off; add_header X-Frame-Options "DENY"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block"; # we define the proxies location /mongo/ { rewrite ^/mongo/(.*) /$1 break; # you'll have to rewrite the html code received in the mongo-express response sub_filter 'href="/' 'href="/mongo/'; sub_filter 'src="/' 'src="/mongo/'; sub_filter 'action="/' 'action="/mongo/'; sub_filter 'url(/' 'url(/mongo/'; sub_filter_once off; proxy_redirect <http://mongo-express:8082/> https://nom_de_domaine/mongo/; proxy_pass <http://mongo-express:8081/>; } location /checkValid { # you'll have to redirect mongo-express's /checkValid route proxy_pass <http://mongo-express:8081/checkValid>; } location /db { # rediriger la route /db de mongo-express return 301 https://nom_de_domaine/mongo$request_uri; } location /postgres/ { rewrite ^/postgres/(.*) /$1 break; # rewriting the html code received in adminer's reply sub_filter 'href="/' 'href="/postgres/'; sub_filter 'src="/' 'src="/postgres/'; sub_filter 'action="/' 'action="/postgres/'; sub_filter 'url(/' 'url(/postgres/'; sub_filter_once off; proxy_redirect / /postgres/; proxy_pass <http://adminer:8080/>; } }
nginx
.app_db/docker-compose.ymversion: 1.0.0 networks: datanet: driver: bridge x-mongo: &mongo image: mongo restart: always env_file: - .env.local networks: - datanet command: --replSet rset --bind_ip_all --keyFile /data/db/keyfile services: db: container_name: nginx.db image: nginx volumes: # copy configuration files - ./webapp.conf:/etc/nginx/conf.d/webapp.conf - ./nginx.conf:/etc/nginx/nginx.conf - ./.env.local:/etc/nginx/.env.local ports: - '8082:8082' # Instead, we set up a server which, once started, will listen on port 8082. # For my part, I map it to port 8082 on the host machine. My port 443 (the default https port is occupied by a nginx server that handles proxying to 8082 and all other servers). networks: - datanet mongo1: <<: *mongo container_name: mongo1.db volumes: - mongo1-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27017:27017' mongo2: <<: *mongo container_name: mongo2.db volumes: - mongo2-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27018:27017' mongo3: <<: *mongo container_name: mongo3.db volumes: - mongo3-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27019:27017' mongo4: <<: *mongo container_name: mongo4.db volumes: - mongo4-data:/data/db - ./key/keyfile:/data/db/keyfile ports: - '27020:27017' mongo-express: container_name: mongo-express.db image: mongo-express restart: always env_file: - .env.local networks: - database pg: container_name: postgres.db image: postgres restart: always ports: - '5423:5432' networks: - datanet volumes: - postgres-data:/var/lib/postgresql/data env_file: - .env.local adminer: container_name: adminer.db image: adminer restart: always networks: - datanet volumes: postgres-data: driver: local mongo1-data: driver: local mongo2-data: driver: local mongo3-data: driver: local mongo4-data: driver: local
replica set
of our MongoDB cluster, and initiate communication between them via a replica set
.app_db/keopenssl rand -base64 756 > keyfile chmod 400 keyfile chown mongodb:mongodb keyfile
app_ddocker-compose up -d
replica set
via one of them. To do this, we'll launch the mongo1
mongo shell.app_ddocker exec -it mongo1 mongosh --username username --password password --authenticationDatabase admin
rs.status()
, to check the current state of the replica set.mongocf=rs.conf(); cf._id='rset'; cf.members=[ { _id:0,host:'mongo1',priority:1}, { _id:1,host:'mongo2',priority:0.5}, { _id:2,host:'mongo3',priority:0.5}, { _id:3,host:'mongo4',priority:0.5} ]; rs.reconfig(cf,{force:true});
db.roo7690.me
.