FSND: Deploy Flask App to Kubernetes Using EKS

Python 3.9

  • Follow instructions to install the latest version of python for your platform in the python docs
  • Please also install pip3 with Python

Prerequisites

Initialize and activate a virtualenv using:

  • MacOS/Linux
python -m virtualenv env
source env/bin/activate
  • Windows
python -m virtualenv env
.\env\bin\activate.ps1

How to launch application

  1. Initialize and activate a virtualenv using (according to previous steps explained in README) and install the dependencies:
  • MacOS/Linux
pip install -r requirements.txt
  • Windows
pip install -r requirements.txt
  1. Run the development server:
  • MacOS/Linux
export JWT_SECRET='myjwtsecret'
export LOG_LEVEL=DEBUG
export FLASK_APP=app
export FLASK_ENV=development
python3 main.py
  • Windows with using PowerShell (in case of using 1 installed version of Python use python, otherwise use your version of python3)
$env:JWT_SECRET = 'myjwtsecret'
$env:LOG_LEVEL = 'DEBUG'
$env:FLASK_APP = 'app'
$env:FLASK_ENV = 'development'
python .\main.py

How to test instance

  • MacOS/Linux
export TOKEN=`curl --data '{"email":"abc@xyz.com","password":"mypwd"}' --header "Content-Type: application/json" -X POST localhost:8080/auth  | jq -r '.token'`
curl --request GET 'http://localhost:8080/contents' -H "Authorization: Bearer ${TOKEN}" | jq .
  • Windows
$env:TOKEN=(Invoke-RestMethod -Uri http://localhost:8080/auth -Method POST -Body (@{"email"="abc@xyz.com";"password"="mypwd";}|ConvertTo-Json) -ContentType "application/json").token
Invoke-RestMethod -Uri http://localhost:8080/contents -Method GET -Headers @{"Authorization"="Bearer $($env:TOKEN)"}

How to launch Dockerized application

  • MacOS/Linux
docker build -t simple-flask .
docker run --name simpleflask --env-file=.env_file -p 8080:8080 simple-flask
  • Windows
docker build -t simple-flask .
docker run --name simpleflask --env-file=.env_file -p 8080:8080 simple-flask

How to test Dockerized application

  • MacOS/Linux
export TOKEN=`curl --data '{"email":"abc@xyz.com","password":"mypwd"}' --header "Content-Type: application/json" -X POST localhost:8080/auth  | jq -r '.token'`
curl --request GET 'http://localhost:8080/contents' -H "Authorization: Bearer ${TOKEN}" | jq .
  • Windows
$env:TOKEN=(Invoke-RestMethod -Uri http://localhost:8080/auth -Method POST -Body (@{"email"="abc@xyz.com";"password"="mypwd";}|ConvertTo-Json) -ContentType "application/json").token
Invoke-RestMethod -Uri http://localhost:8080/contents -Method GET -Headers @{"Authorization"="Bearer $($env:TOKEN)"}

References