Docker Community Forums

Share and learn in the Docker community.

Doesn't work load balancer in GetStarted Part3 Tutorial


(Mzmey37) #1

Hello, I was trying to repeat example from getstarted part3 tutorial and faced a problem.
I have one difference with tutorial: I try to use tornado instead of flask. I attach a code for app.py. and requirements.txt, Dockerfile is just like in example of tutorial

from tornado import web, ioloop, httpserver
from redis import Redis, RedisError
import socket


class RequestHandler(web.RequestHandler):

    def initialize(self):
        self.redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

    def get(self, query):
        x_real_ip = self.request.headers.get("X-Real-IP")
        try:
            count = self.redis.incr(self.request.remote_ip)
        except RedisError:
            count = "redis error"

        self.write(f'<p>My hostname: {socket.gethostname()};</p>'\
                   f'<p>remote_ip = {self.request.remote_ip};</p>'\
                   f'<p>remote_ip count: {count};</p>'\
                   f'<p>x_real_ip = {x_real_ip};</p>'\
                   f'<p>headers = {repr(self.request.headers)}</p>')

if __name__ == "__main__":
    app = web.Application([
        (r'/(.*)', RequestHandler)
    ])
    server = httpserver.HTTPServer(app)

    server.listen(80)
    server.start()
    ioloop.IOLoop.current().start()

Tornado
Redis

Than I build image and push to docker hub:

docker build -t tornado-img .
docker login
docker tag tornado-img mzmey37/tornado-img
docker push mzmey37/tornado-img

Than I make docker-compose.yml file:

version: "3"
services:
  web:
    image: mzmey37/tornado-img
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
  redis:
    image: redis
    volumes:
      - "/Users/mzmeev/adblog/image/redis_data:/data"
    ports:
      - "6379:6379"
    command: "redis-server --appendonly yes"
    networks:
      - webnet
networks:
  webnet:

Than I run deployment

docker swarm init
docker stack deploy -c docker-compose.yml tornado-app

As result I can access localhost:4000 and see
37

The problem is that my hostname field never changes after I reload the page, but has to change and I should see 5 different values. Why is it happening? Also it sometimes increments counter by 2 but not 1.
Help please, thank you in advance.