简体   繁体   中英

How to fetch Ips of a service in docker swarm cluster ?

I am running a docker swarm mode cluster with 2 nodes, and deploy 5 services : [ mysql , mongo , app ] and wish to filldb with an ansible script from my manager node. But I can not get the Ip from nodes to access db services in container ?
eg: mysql -h {{ mysql_service_host }} ....

how to get the container Ip or the service ip from node ? is it possible to use mode host in docker swarm ?

For services (containers) that are part of the same network you can simply use the service name. Docker includes a DNS resolver that handles ip resolution. You will need to make your services part of an overlay network. An overlay network can span more than one node.

Eg:

services:
  myapp:
    image: myimage:1.0
    deploy:
      replicas: 1
    networks:
      - privnet

  maindb:
    image: mysql
    deploy:
      replicas: 1
    networks:
      - privnet

networks:
  privnet:
    driver: overlay

This creates an overlay network with two services. The corresponding containers could be created on any node. It doesn't matter where. They will all be able to communicate to each other since they're part of the same overlay network. Within myapp, you can use maindb as a DNS for the mysql service. It will be resolved by Docker to the proper ip within the privnet network.

btw, a swarm cluster with 2 nodes doesn't make much sense. Swarm requires a minimum of 3 nodes for the Raft consensus protocol to work. https://raft.github.io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM