user2058904
user2058904

Reputation:

Microservice Discovery With Docker And Consul

I'm interested in building microservices, but I'm getting a bit stuck on how service discovery should work when I've got multiple instances of a single microservice.

Suppose I've got an "OCR" app that reads text from an image. Deploying that as 1 instance is easy, however, what if I want 50 instances of those?

I can run docker swarm to spin up get those 50 instances, but how do I send a request to any one of them, i.e. I don't want to have to know the exact container name of a specific instance, I don't care which one I get, as long as it's healthy, just send my request to any of the "OCR" containers.

How do I achieve this?

I've been looking into Consul and it seems very promising. I especially like the HTTP api, (Although I'm a little unsure of how I would retrieve the url for the service I'm interested in. Would I need to do it before every request to make sure I'm pointing to a healthy instance?).

If I wanted to use consul, what would be the steps be in relation to docker swarm? Do I just need to register the service in consul when the container starts up, and it will automatically get de-registered if it fails right?).

After that, all of my containers just need to be aware of where consul is (And I guess I could stick a load balancer infront of it, incase I ever want to scale out consul itself to a bunch of instances?)

Please let me know if I'm going completely in the wrong direction.

If anyone could also suggest any articles or books on this topic I'd appreciate it.

Thanks.

Upvotes: 1

Views: 536

Answers (1)

Markus
Markus

Reputation: 3148

When you're using Docker Swarm Mode, you get service discovery with load balancing for free.
DNSRR is in the key concept: https://docs.docker.com/engine/swarm/key-concepts/#load-balancing

Say you deploy OCR-app.

docker service create --network dev --name ORC-app --replicas 5 OCR-app:latest

The docker manager will deploy OCR-app in this case five times on nodes of your swarm network. Every other service which is part of the same docker network dev can request the OCR-app by it's name. E.g. GET http://OCR-app:4000/do/something.
Internally docker swarm uses round robin for forward the request automatically to one of the five services.

Upvotes: 1

Related Questions