diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..5d0c39a --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.6 +WORKDIR /app +COPY ./requirements.txt /app/requirements.txt +RUN pip install --no-cache-dir -r requirements.txt +COPY . /app +EXPOSE 5000 +ENTRYPOINT ["python"] +CMD ["app.py"] diff --git a/backend/app.py b/backend/app.py new file mode 100644 index 0000000..d858009 --- /dev/null +++ b/backend/app.py @@ -0,0 +1,31 @@ +from flask import Flask, render_template +import socket +import requests +import boto3 +app = Flask(__name__) +servicediscovery = boto3.client("servicediscovery") + + +@app.route("/ping") +def ping(): + return "", 200 + + +@app.route("/") +def hello(): + resp = servicediscovery.list_services( + Filters=[{'Name': 'NAMESPACE_ID', 'Values': ['SUPER_COOL_VALUE_HERE']}] + ) + service_obj = [service for service in resp.get('Services', []) if service['Name'] != "backend"] + services = [] + for service in service_obj: + services.append({ + 'name': service['Name'], + 'addr': socket.gethostbyname(service['Name']+".corp"), + 'num_hosts': len(servicediscovery.list_instances(ServiceId=service['Id'])['Instances']), + 'text': requests.get("http://"+service['Name']+".corp").content.decode("utf-8") + }) + return render_template("index.html", services=services) + +if __name__ == "__main__": + app.run(debug=True, host='0.0.0.0') diff --git a/backend/requirements.txt b/backend/requirements.txt new file mode 100644 index 0000000..7244507 --- /dev/null +++ b/backend/requirements.txt @@ -0,0 +1,3 @@ +flask +requests +boto3 diff --git a/backend/static/dust_scratches.png b/backend/static/dust_scratches.png new file mode 100644 index 0000000..a0ae0e1 Binary files /dev/null and b/backend/static/dust_scratches.png differ diff --git a/backend/static/favicon.ico b/backend/static/favicon.ico new file mode 100644 index 0000000..8f2cd64 Binary files /dev/null and b/backend/static/favicon.ico differ diff --git a/backend/static/sd.png b/backend/static/sd.png new file mode 100644 index 0000000..a915615 Binary files /dev/null and b/backend/static/sd.png differ diff --git a/backend/templates/index.html b/backend/templates/index.html new file mode 100644 index 0000000..4c8e4df --- /dev/null +++ b/backend/templates/index.html @@ -0,0 +1,46 @@ + + + + + + + + + + Service Discovery + + +
+
+

Amazon ECS Service Discovery

+
+
+ architecture diagram +
+
+
+

This is a short demo of service discovery in Amazon ECS.

+ +

You can find more details in the blog post.

+
+
+
+
+
+

These are the services I found, if you refresh the page the IPs of the workers should change:

+
    + {% for service in services %} +
  • {{service.name}} - {{service.addr}}

    Worker Response:{{service.text|safe}} +

    Chose one out of {{service.num_hosts}} posible hosts

    +
  • + {% endfor %} +
+
+
+
+ + diff --git a/workers/Dockerfile b/workers/Dockerfile new file mode 100644 index 0000000..c41f5d6 --- /dev/null +++ b/workers/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.6 +WORKDIR /app +COPY ./requirements.txt /app/requirements.txt +RUN pip install --no-cache-dir -r requirements.txt +COPY . /app +EXPOSE 80 +ENTRYPOINT ["python"] +CMD ["app.py"] diff --git a/workers/app.py b/workers/app.py new file mode 100644 index 0000000..865e3bc --- /dev/null +++ b/workers/app.py @@ -0,0 +1,10 @@ +from flask import Flask +import socket +app = Flask(__name__) + +@app.route("/") +def do_work(): + return "I'm a worker and I'm doing some work!" + +if __name__ == "__main__": + app.run(debug=True, host='0.0.0.0', port=80) diff --git a/workers/requirements.txt b/workers/requirements.txt new file mode 100644 index 0000000..7e10602 --- /dev/null +++ b/workers/requirements.txt @@ -0,0 +1 @@ +flask