skip to Main Content

so I have multiple services runing in the same docker network with flask
they are set like this:

version: '3'
services:
  login:
    build:
      context: ./repo_login
      dockerfile: Development.Dockerfile
    volumes:
      - ./repo_login:/home/app
    ports:
      - "5007:5001"
    restart: unless-stopped
    environment:
      ENV: 'development'
    networks:
      - qa_network
  jobs:
    build:
      context: ./repo_jobs
      dockerfile: Development.Dockerfile
    volumes:
      - ./repo_jobs:/home/app
    ports:
      - "5017:5001"
    restart: unless-stopped
    environment:
      PYTHONUNBUFFERED: 1
      ENV: 'development'
    networks:
      - qa_network
  sla:
    build:
      context: ./repo_sla
      dockerfile: Development.Dockerfile
    volumes:
      - ./repo_sla:/home/app
    ports:
      - "5008:5001"
    restart: unless-stopped
    environment:
      ENV: 'development'
    networks:
      - qa_network

I know I can petitions from on service to the another using only the service name like this:
requests.post('http://jobs:5001/some/api', data=json.dumps(data), headers=headers)
but is there a way to know from which service a petition came from?

I already try with request.environ, but I only get An IP and port from whitch I don’t know its service. I used the next code

@app.route('/some/api', methods=['POST'])
def execute_task():
    try:
        origin = request.environ['REMOTE_ADDR']
        origin_port = request.environ['REMOTE_PORT']
        logging.warning(f'the origin of the petition is: {origin} : {origin_port}')
   except Exception as ex:
        data = None
        message = getattr(ex, 'message', str(ex))
        logging.exception(ex)

2

Answers


  1. Chosen as BEST ANSWER

    I found a way you can resolve de dns for the services that you have and compare it with the remote_addr in the request Something like this:

    @app.route('/some/api', methods=['POST'])
    def execute_task():
        try:
            payload = request.json
            origin = request.environ['REMOTE_ADDR']
            val = '172.18.0.0'
            result = dns.resolver.resolve('login', 'A')
            if len(result) >= 1:
                val = result[0].to_text()
            if val == origin:
                logging.warning(f'The origin of the petition is login)
            else:
                logging.warning(f'The origin of the petition is not login')
        except Exception as ex:
            data = None
            message = getattr(ex, 'message', str(ex))
            logging.exception(ex)
    

    This can be expanded to check all the services you have


  2. you can use more advanced tools like service discovery systems that do the job. But there is a simple solution for it if you don’t want to use those tools.
    You can add the headers in your requester application, like:

    import requests
    headers = {'service': 'foo'}
    requests.post('http://jobs:5001/some/api', data=json.dumps(data), headers=headers)
    

    and then in your receiver application, you can check the headers:

    from flask import request
    serviceName = request.headers.get('service')
    

    Then you will have the service name to play with.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search