{"id":15491338,"url":"https://github.com/theodesp/flask-envoy-docker-microservice","last_synced_at":"2025-04-22T19:12:41.689Z","repository":{"id":97066486,"uuid":"113912788","full_name":"theodesp/flask-envoy-docker-microservice","owner":"theodesp","description":"A simple tutorial how to setup a Flask microservice using https://www.envoyproxy.io Service mesh and Docker.","archived":false,"fork":false,"pushed_at":"2018-10-09T09:09:28.000Z","size":12,"stargazers_count":10,"open_issues_count":0,"forks_count":8,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T19:12:29.280Z","etag":null,"topics":["docker","docker-compose","docker-machine","envoy","flask-application","microservice"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theodesp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-11T21:56:17.000Z","updated_at":"2022-09-16T17:49:49.000Z","dependencies_parsed_at":"2023-04-22T18:57:43.691Z","dependency_job_id":null,"html_url":"https://github.com/theodesp/flask-envoy-docker-microservice","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fflask-envoy-docker-microservice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fflask-envoy-docker-microservice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fflask-envoy-docker-microservice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theodesp%2Fflask-envoy-docker-microservice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theodesp","download_url":"https://codeload.github.com/theodesp/flask-envoy-docker-microservice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250306640,"owners_count":21408926,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","docker-machine","envoy","flask-application","microservice"],"created_at":"2024-10-02T07:45:05.856Z","updated_at":"2025-04-22T19:12:41.676Z","avatar_url":"https://github.com/theodesp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Flask on Envoy Cluster microservice example\n---\nA simple tutorial how to setup a Flask microservice using [Envoy](https://www.envoyproxy.io/) Service mesh and Docker.\n\n## Running the service\n**Step 1: Install Docker**\n\nEnsure that you have a recent versions of \n`docker`, `docker-compose` and `docker-machine` \ninstalled.\n\n**Step 2: Setup Docker Machine**\n\nCreate a new machine which will hold the containers:\n\n```bash\n$ docker-machine create --driver virtualbox default\n$ eval $(docker-machine env default)\n```\n_**Tip**_\nRun the bootstrap.sh program to perform the list of steps in one go.\n\n\n**Step 3: Create configuration for Front End Envoy Gateway**\nFirst create an Envoy config that will act as a Front End proxy server:\n\n```bash\n$ mkdir gateway\n$ touch front-proxy-envoy.json\n```\n\nThe configuration is a simple proxy redirect to a backend url:\n\n```json\n{\n  \"listeners\": [\n    {\n      \"address\": \"tcp://0.0.0.0:80\",\n      \"filters\": [\n        {\n          \"name\": \"http_connection_manager\",\n          \"config\": {\n            \"codec_type\": \"auto\",\n            \"stat_prefix\": \"ingress_http\",\n            \"route_config\": {\n              \"virtual_hosts\": [\n                {\n                  \"name\": \"app_backend\",\n                  \"domains\": [\n                    \"*\"\n                  ],\n                  \"routes\": [\n                    {\n                      \"timeout_ms\": 0,\n                      \"prefix\": \"/\",\n                      \"cluster\": \"app\"\n                    }\n                  ]\n                }\n              ]\n            },\n            \"filters\": [\n              {\n                \"name\": \"router\",\n                \"config\": {}\n              }\n            ]\n          }\n        }\n      ]\n    }\n  ],\n  \"admin\": {\n    \"access_log_path\": \"/dev/null\",\n    \"address\": \"tcp://0.0.0.0:8001\"\n  },\n  \"cluster_manager\": {\n    \"clusters\": [\n      {\n        \"name\": \"app\",\n        \"connect_timeout_ms\": 250,\n        \"type\": \"strict_dns\",\n        \"lb_type\": \"round_robin\",\n        \"features\": \"http2\",\n        \"hosts\": [\n          {\n            \"url\": \"tcp://app:80\"\n          }\n        ]\n      }\n    ]\n  }\n}\n\n\n```\n\n**Step 4: Add Dockerfile for Front End Envoy Gateway**\n\n```bash\n$ touch DockerFile\n$ cat \u003c\u003cEOF \u003e DockerFile\n    FROM envoyproxy/envoy:latest\n\n    RUN apt-get update \u0026\u0026 apt-get -q install -y \\\n        curl\n\n    CMD /usr/local/bin/envoy -c /etc/front-proxy-envoy.json --service-cluster front-proxy\nEOF\n\n```\n\n**Step 5: Create configuration for Back End Envoy Proxy Server**\nFirst create an Envoy config that will act as a Back End proxy server. Make sure you match the listener address to the \nhosts address of the front-end proxy.\n\n\n```json\n{\n  \"listeners\": [\n    {\n      \"address\": \"tcp://0.0.0.0:80\",\n      \"filters\": [\n        {\n          \"name\": \"http_connection_manager\",\n          \"config\": {\n            \"codec_type\": \"auto\",\n            \"stat_prefix\": \"ingress_http\",\n            \"route_config\": {\n              \"virtual_hosts\": [\n                {\n                  \"name\": \"app\",\n                  \"domains\": [\n                    \"*\"\n                  ],\n                  \"routes\": [\n                    {\n                      \"timeout_ms\": 0,\n                      \"prefix\": \"/\",\n                      \"cluster\": \"local_service\"\n                    }\n                  ]\n                }\n              ]\n            },\n            \"filters\": [\n              {\n                \"name\": \"router\",\n                \"config\": {}\n              }\n            ]\n          }\n        }\n      ]\n    }\n  ],\n  \"admin\": {\n    \"access_log_path\": \"/dev/null\",\n    \"address\": \"tcp://0.0.0.0:8001\"\n  },\n  \"cluster_manager\": {\n    \"clusters\": [\n      {\n        \"name\": \"local_service\",\n        \"connect_timeout_ms\": 250,\n        \"type\": \"strict_dns\",\n        \"lb_type\": \"round_robin\",\n        \"hosts\": [\n          {\n            \"url\": \"tcp://127.0.0.1:8080\"\n          }\n        ]\n      }\n    ]\n  }\n}\n\n\n```\n\n**Step 6: Add Dockerfile for Backend Envoy Gateway**\n\n```bash\n$ touch DockerFile\n$ cat \u003c\u003cEOF \u003e DockerFile\n    FROM envoyproxy/envoy:latest\n\n\n    RUN apt-get update \u0026\u0026 apt-get -q install -y \\\n        curl \\\n        software-properties-common \\\n        python-software-properties\n    RUN add-apt-repository ppa:deadsnakes/ppa\n    RUN apt-get update \u0026\u0026 apt-get -q install -y \\\n        python3 \\\n        python3-pip\n    RUN python3 --version \u0026\u0026 pip3 --version\n    \n    RUN mkdir /code\n    COPY . /code\n    WORKDIR /code\n    \n    RUN pip3 install -r ./requirements.txt\n    ADD ./start_service.sh /usr/local/bin/start_service.sh\n    RUN chmod u+x /usr/local/bin/start_service.sh\n    \n    ENTRYPOINT /usr/local/bin/start_service.sh\nEOF\n```\n\nadd also a start up shell for your app\n\n```bash\n$ touch start_service.sh\n$ cat \u003c\u003cEOF \u003e start_service.sh\n    #!/bin/bash\n    set -xe\n    python3 ./app.py \u0026 envoy -c /etc/app-service-envoy.json --service-cluster app\nEOF\n\n```\n\n**Step 7: Add docker-compose script for setting up all containers**\n\n```bash\n$ touch docker-compose.yml\ncat\u003c\u003cEOF \u003e docker-compose.yml\nversion: '2'\nservices:\n\n  front-envoy:\n    build:\n      context: .\n      dockerfile: gateway/Dockerfile\n    volumes:\n      - ./gateway/front-proxy-envoy.json:/etc/front-proxy-envoy.json\n    networks:\n      - envoymesh\n    expose:\n      - \"80\"\n      - \"8001\"\n    ports:\n      - \"8000:80\"\n      - \"8001:8001\"\n\n  app:\n    build:\n      context: ./app\n      dockerfile: Dockerfile\n    volumes:\n      - ./app/app-service-envoy.json:/etc/app-service-envoy.json\n    networks:\n      envoymesh:\n        aliases:\n          - app\n    expose:\n      - \"80\"\n\nnetworks:\n  envoymesh: {}\n\nEOF\n```\n\n**Step 8: Add requirements.txt and flask app**\nCreate the basic flask micro-service and add the following code.\n\n```bash\n$ touch requirements.txt\n$ cat\u003c\u003cEOF \u003e requirements.txt\nFlask==0.12.2\ngunicorn==18.0\npython-dotenv==0.7.1\nEOF\n\n$ touch app.py\n\n```\n\n*app.py*\n```python\nfrom flask import Flask\nimport settings\n\n\ndef init():\n    \"\"\" Create a Flask app. \"\"\"\n    server = Flask(__name__)\n\n    return server\n\napp = init()\n\n\n@app.route('/')\ndef index():\n    return 'My awesome micro-service'\n\nif __name__ == \"__main__\":\n    app.run(\n        host=settings.API_BIND_HOST,\n        port=settings.API_BIND_PORT,\n        debug=settings.DEBUG)\n```\n\n*settings.py*\n```python\n\"\"\"\nSettings file, which is populated from the environment while enforcing common\nuse-case defaults.\n\"\"\"\nimport os\nfrom os.path import join, dirname\nfrom dotenv import load_dotenv\n\ndotenv_path = join(dirname(__file__), '.env')\nload_dotenv(dotenv_path)\n\n# OR, the same with increased verbosity:\nload_dotenv(dotenv_path, verbose=True)\n\n\nDEBUG = True\nif os.getenv('DEBUG', '').lower() in ['0', 'no', 'false']:\n    DEBUG = False\n\nAPI_BIND_HOST = os.getenv('SERVICE_API_HOST', '127.0.0.1')\nAPI_BIND_PORT = int(os.getenv('SERVICE_API_PORT', 8080))\nSERVICE_NAME = os.getenv('SERVICE_NAME', 'app')\n\n```\n\n**Step 9: Test the service locally**\n```bash\n$ python app.py                                       \n * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)\n * Restarting with stat\n * Debugger is active!\n * Debugger PIN: 185-774-130\n\n```\n\nand in another terminal\n\n```bash\n$ curl http://127.0.0.1:8080/\nMy awesome micro-service%                 \n```\n\n\n**Step 10: Start all containers**\n\n```bash\n$ docker-compose up --build -d\n$ docker-compose ps\n        Name                       Command               State      Ports\n-------------------------------------------------------------------------------------------------------------\napp_service_1      /bin/sh -c /usr/local/bin/ ...    Up       80/tcp\nfront-proxy-envoy_1   /bin/sh -c /usr/local/bin/ ...    Up       0.0.0.0:8000-\u003e80/tcp, 0.0.0.0:8001-\u003e8001/tcp\n```\n\n**Step 11: Test routing capabilities**\n\nYou can now send a request to services \nvia the front-envoy.\n\n```bash\ncurl -X GET -v $(docker-machine ip default):8000\n* Rebuilt URL to: 0.0.0.0:8000/\n*   Trying 0.0.0.0...\n* TCP_NODELAY set\n* Connected to 0.0.0.0 (0.0.0.0) port 8000 (#0)\n\u003e GET / HTTP/1.1\n\u003e Host: 0.0.0.0:8000\n\u003e User-Agent: curl/7.54.0\n\u003e Accept: */*\n\u003e \n\u003c HTTP/1.1 200 OK\n\u003c content-type: text/html; charset=utf-8\n\u003c content-length: 24\n\u003c server: envoy\n\u003c date: Thu, 14 Dec 2017 08:51:39 GMT\n\u003c x-envoy-upstream-service-time: 4\n\u003c \n* Connection #0 to host 0.0.0.0 left intact\nMy awesome micro-service%                        \n```\n\n**Step 12: Enjoy your micro-service and have some Beer 🍺🍺🍺**\n\n\nLICENCE\n---\nMIT @ Theo Despoudis","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheodesp%2Fflask-envoy-docker-microservice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheodesp%2Fflask-envoy-docker-microservice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheodesp%2Fflask-envoy-docker-microservice/lists"}