How does load balancing works in Docker Swarm and whats the difference with Kubernetes?

12/17/2018

I am learning Docker and I did a simple exercise:

  • prepared 2 machines
  • set up Leader and Worker on them
  • deployed 2 instances of simple web-app with stack command

when I send HTTP request to BOTH machines I get correct reply.

That is strange for me. I thought only Leader node should handle requests because there is some load balancing in Swarm. I thought if some node failes Swarm should automatically redirect requests to another one and Leader node is where it happens. But looks like Swarm works in a different way.

What is the idea behind Swarm clustering? Is Kubernetes different?

-- MiamiBeach
docker
docker-swarm
kubernetes

1 Answer

12/18/2018

In Docker swarm the leader handles decisions on how to schedule the containers in your nodes and how to have them setup so that traffic is forwarded to them. However, the traffic doesn't go to the leader per se, but rather to the Docker swarm ingress/network mesh

You do this through the command line with:

$ docker service create \
  --name <SERVICE-NAME> \
  --publish published=8080,target=80 \
  <IMAGE>

Then all your nodes will receive traffic on the published port and will get forwarded to the containers.

swarm

In the case above, from an external load balancer, you would could forward traffic to either port 80 (container exposed port) or port 8080 (published)

Kubernetes is very similar but not quite the same. Services are exposed externally through a LoadBalancer or NodePort. However, you can't get directly to the pod IP addresses from the outside because those are not seen on the outside, as opposed to 192.168.99.100:80 in the Docker swarm example above. Also, the traffic in Kubernetes doesn't go through the master (except when you are making a call to the kube-apiserver), but directly to the nodes.

-- Rico
Source: StackOverflow