4

Consider there is a docker service with 5 replicas. I want to make a rest call to all 5 replicas. If any replica fails the whole request should fail. I want to do this because sometimes the code inside the container stops running and does not respond to rest calls. Is is possible to make a single rest call to a service and if any container fails to return a response the whole request fails

Luke101
  • 63,072
  • 85
  • 231
  • 359
  • If you mention the replicas as 5, docker swarm will make sure that there are 5 services available. If something goes down, it restarts them. So do you really need to bother about the service failure? – Vamsi Nov 24 '17 at 10:29

2 Answers2

6

For future reference, to new viewers, starting from Compose file version 3.3 onwards, a service's deploy now supports 'endpoint_mode' options. Instead of the default 'vip' (virtual IP/proxy load balancer), option 'dnsrr' is now also available:

DNS round-robin (DNSRR) service discovery does not use a single virtual IP. Docker sets up DNS entries for the service such that a DNS query for the service name returns a list of IP addresses, and the client connects directly to one of these. DNS round-robin is useful in cases where you want to use your own load balancer, or for Hybrid Windows and Linux applications.

Meaning a command like 'nslookup'/'dig +short'/etc on your service (within the network) will now resolve to a list of container IPs, instead of the proxy load balancer in front of them.

nslookup <yourservice> | awk '/^Address: / { print $2 }' | xargs | sed -e 's/ /,/g'

can be used for a comma-seperated IP address string. A Java alternative:

Arrays.asList(java.net.InetAddress.getAllByName(<yourservice>));

You can adjust your application's code accordingly.

Based on this, you can implement your own behavior on awaiting each container's response and how to handle situations like not all containers replying within a specified time.

For compose file version 3.3+ compatibility, the docker engine version should be 17.06.0+.

Another approach is to resolve tasks.<service-name>. More information can be found in this service discovery question.

Forvaine
  • 71
  • 1
  • 6
1

Docker's load balancer proxies your request to one of the five tasks. That's what it's intended to to. If you want to send a message to the service and collect the results of all tasks you will have to implement that by yourself. You may put a proxy in front or implement some cluster message distribution in your application.

In your case look at docker healthchecks. You define a command that is periodically run inside your container and if that fails docker assumes your container unhealthy and kills it. You need to write a short script that send your REST call and returns a non zero exit code if it fails.

mikgne
  • 41
  • 2