Search code examples
dockerdocker-swarm

Docker - can you create a container on a node in a cluster without being a service?


I have a use case that seems half and half like docker swarm would fit, and kind of would not.

I'm trying to setup a situation like this;

enter image description here

I only have two boxes at my disposal. I want traefik to load balance requests to either of the services. I only want the services to communicate with the hot instance of the database. In the instance that a node dies, the other instance of treafik should take over seamlessly. In the instance where the hot database dies, I'd like an immediate switch to the slave to make it the hot db. All of these are docker containers.

Is this a good fit for swarm? At first, I was thinking that I could just make a swarm of two manager nodes with all global services, but the database configurations will be unique. If I ever added a third node or N number of nodes, I wouldn't want traefik to automatically scale to a third one. Also, the databases would not ever scale, I'd only want the two and they'd each have unique configs. Really, all I want is the overlay network provided by the swarm and scaling on the 1 service.

So the question is, can I have boxes participate as a node in a cluster, but host single containers outside the swarm with unique configs while everything shares the same overlay network?


Solution

  • Docker swarm doesn't limit you. You can define the number of service instances, their location and network settings. Sample scenario:

    version: '3.8'
    
    services:
      traefik:
        hostname: 'traefik'
        image: traefik
        deploy:
          mode: replicated
          replicas: 2
          placement:
            max_replicas_per_node: 1
        networks:
          backend:
            aliases:
             - traefik      
      service1:
        hostname: 'service1'
        image: service1
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints: [node.hostname == host01]
        networks:
          backend:
            aliases:
             - service1 
      service2:
        hostname: 'service2'
        image: service2
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints: [node.hostname == host02]          
        networks:
          backend:
            aliases:
             - service2
      db_master:
        hostname: 'db_master'
        image: image_db
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints: [node.hostname == host01]    
        networks:
          backend:
            aliases:
             - db_master
      db_slave:
        hostname: 'db_slave'
        image: image_db
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints: [node.hostname == host02]          
        networks:
          backend:
            aliases:
             - db_slave
             
    networks:
      backend:
        name: backend
        driver: overlay     
        attachable: true
    

    Using the parameter attachable: true you can attach containers to overlay network

    docker run -itd --network=backend busybox