Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker networking best practices

I have ~4 microservices, a Keycloak and a RabbitMQ Server I want to deploy via Docker. Each microservice has to be able to speak to Keycloak, and some need to speak to RabbitMQ.

To keep it clean I wanted to create a docker-compose file for Keycloak, one for RabbitMQ, and one per microservice. Is this the right way to do it? (Each microservice has its own database.)

Image Microservices Relations etc.

MS A, B, C, D need to be able to call Keycloak; MS C, D needs to be able to talk to RabbitMQ.

What is the best way to connect all the microservices with the Keycloak / RabbitMQ server(s)? Some of the microservices need to be able to talk to each other as well.

like image 403
backup backup Avatar asked Sep 13 '18 18:09

backup backup


People also ask

How does Docker do networking?

Docker includes support for networking containers through the use of network drivers. By default, Docker provides two network drivers for you, the bridge and the overlay drivers. You can also write a network driver plugin so that you can create your own drivers but that is an advanced task.

Is Docker still relevant in 2022?

Can I still use Docker for development? Yes, you absolutely can, now and in the foreseeable future. You see, Docker doesn't run Docker-specific images; it runs OCI-compliant containers.

Are Docker networks secure?

Conclusions. Docker containers are, by default, quite secure; especially if you run your processes as non-privileged users inside the container. You can add an extra layer of safety by enabling AppArmor, SELinux, GRSEC, or another appropriate hardening system.


2 Answers

You can do it via docker, but there are following things you need to consider,

  • Why you want to deploy

If answer is dev or testing then yes you can use docker compose. It help you start all your containers from one place and you can have them configured in one file. You can have keycloak, rmq databases and services all set up on to same host via docker compose and they can talk to each other

If you want to do it for prod or prod like env, then we you can look into docker swarms. It is an incremental update from docker compose and let you deploy containers on multiple host.

If you have just four services, you can do with it, but if you have more, or you need better control at how your services are getting deployed, run and need more metrics and touch points, go for kubernetes.

If you want to keep your databases in containers, that I am not really sure about. Containers are lil volatile but collectively they give you high availability but for databases and rmq I personally will not keep them in containers for prod, but then you can read around and based on your need can decide on that.

Docker swarms and kubernetes can be used for dev and testing env as well. they can always run on single host.

like image 189
Anunay Avatar answered Sep 28 '22 07:09

Anunay


yes you can do it like that you said, by creating a compose file per service, but if you want to keep it clean you can put them in one file like below :

version: '2'
services:  
  rabbitmq:
    image: rabbitmqImg
    networks:
      - rabbitmq
  keycloak:
    image: keycloakImg
    networks:
      - keycloak
  MS-A:
    image: MSImg
    networks:
      - keycloak
  MS-B:
    image: MSImg
    networks:
      - keycloak
  MS-C:
    image: MSImg
    networks:
      - keycloak
      - rabbitmq
  MS-D:
    image: MSImg
    networks:
      - keycloak
      - rabbitmq
networks:
  rabbitmq:
    external:
      name: rabbitmq
  keycloak:
    external:
      name: keycloak

with this yaml microservices can talk to each other through keycloak network, all of microservices can talk to keycloak server also using keycloak network, and MS-C , MS-D can talk to rabbitmq through rabbitmq network. but if there is or will be some other microservices that doesnt need to talk to rabbitmq nor keycloak, you should create another network and put microservices in that and put microservices in other networks as they need.

as Anunay said you can use swarm mode or kubernetes but if you are beginner , stick to docker-compose, after that for scaling and orchestration you can use those frameworks.

EDIT: for databases there are multiple ways that depends on database architecture you have. for example if you have a database container per MS , you should create a network per MS like below:

version: '2'
services:
# General Services      
  rabbitmq:
    image: rabbitmqImg
    networks:
      - rabbitmq
  keycloak:
    image: keycloakImg
    networks:
      - keycloak
  # Micro Services      
  MS-A:
    image: MSImg
    networks:
      - ms-a-net
      - keycloak
  MS-B:
    image: MSImg
    networks:
      - ms-b-net
      - keycloak
  MS-C:
    image: MSImg
    networks:
      - ms-c-net
      - keycloak
      - rabbitmq
  MS-D:
    image: MSImg
    networks:
      - ms-d-net
      - keycloak
      - rabbitmq

  # Database Services      

  DB-A:
    image: DBImg
    networks:
      - ms-a-net
  DB-B:
    image: DBImg
    networks:
      - ms-b-net
  DB-C:
    image: DBImg
    networks:
      - ms-c-net
  DB-D:
    image: DBImg
    networks:
      - ms-d-net

networks:
  rabbitmq:
    external:
      name: rabbitmq
  keycloak:
    external:
      name: keycloak

  # Services Network

  ms-a-net:
    external:
      name: ms-a-net
  ms-b-net:
    external:
      name: ms-b-net          
  ms-c-net:
    external:
      name: ms-c-net
  ms-d-net:
    external:
      name: ms-d-net                    
like image 44
Kayvan Nouredin Avatar answered Sep 28 '22 06:09

Kayvan Nouredin