Liveness/Readiness Probes Failing when using Talisman for Flask/Python - python

I am using a Flask app deployed into Google Kubernetes Engine. The deployed application uses Waitress to serve the Flask application. I would like to ensure that all connections are over https. My deployed application currently allows http and/or https, but I would like to force https. To do so, I am trying to use Talisman.
from flask import Flask
from flask_talisman import Talisman
app = Flask(__name__)
Talisman(app)
However, when I deploy the version using Talisman, my probes fail and I cannot access the web app.
livenessProbe:
httpGet:
port: 8080
path: /health
initialDelaySeconds: 60
timeoutSeconds: 1
readinessProbe:
httpGet:
port: 8080
path: /health
initialDelaySeconds: 60
timeoutSeconds: 1
I tried adding scheme: HTTPS to httpGet, but the probes still fail.
Where am I going wrong?

Related

Facing an error with django channels when deploying to elastic beanstalk

I am facing an issue with websockets. When I run the program locally it works fine but when I deploy it to aws elastic beanstalk I face the following issue. I have a simple code as mentioned below.
django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: playzone.wsgi:application
aws:elasticbeanstalk:environment:process:http:
Port: '80'
Protocol: HTTP
aws:elasticbeanstalk:environment:process:websocket:
Port: '5000'
Protocol: HTTP
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
Procfile
web: gunicorn my_app.wsgi
websocket: daphne -b :: -p 5000 my_app.asgi:application
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter,URLRouter
from . import routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
application = get_asgi_application()
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter(
routing.websocket_urlpatterns
)
})
And there are 2 more simple files routing.py and consumers.py. I've even configured the load balancers on the environment's settings (80 - HTTP ; 5000 - HTTP). So once deployed, when I try routing to the webpage which has a websocket connection, I get an error saying WebSocket connection to 'ws://yyy.com/ws/' failed:. Please help me out on how I can fix it.
Also am not using redis or any channel layer. Please help me out on how I can fix it.

How do I use a specific host in an h2o-wave app

Currently trying to run an h2o-wave app on a remote server. I'm restricted to using 0.0.0.0 as the host on the server (the specific port is not as restrictive).
I've looked at the h2o configuration documentation and tried several variations of what they suggest:
H2O_WAVE_INTERNAL_ADDRESS=ws://0.0.0.0:8000
H2O_WAVE_EXTERNAL_ADDRESS=ws://0.0.0.0:8000
H2O_WAVE_APP_ADDRESS=ws://0.0.0.0:8000
But, the app is still running on the default localhost: http://127.0.0.1:8000
Just setting H2O_WAVE_APP_ADDRESS is enough.
$ H2O_WAVE_APP_ADDRESS=http://0.0.0.0:8000 uvicorn my_app:main

Deploying Flask Socket.io application with eventlet on heroku

I am using flask socket.io configured with eventlet on a free heroku instance. According to the flask socket.io documentation: https://flask-socketio.readthedocs.io/en/latest/ running socketio.run(app) will start a webserver if eventlet is installed.
I have a local react web app attempting to establish a WebSocket connection with this app engine instance. The web app uses socket.io and initially defaults to polling HTTP requests. These requests all timeout-- I'm unable to establish a web socket connection. I'm not sure what might be causing my issue, whether it is my app engine configuration or my flask socket.io setup.
Here is my initialization code for flask socket.io:
app = Flask(__name__)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
..
..
if __name__ == '__main__':
socketio.run(app, debug=True)
Here is my Procfile:
web: python3 server.py
Here is the web app code attempting to set up the connection:
import io from 'socket.io-client'
const socketURL = "<app-engine-instance-url>:5000"
const socket = io.connect(socketURL)
You mentioned Heroku, then switched to App Engine. I assume you still meant Heroku?
The application running on Heroku must honor the $PORT environment variable and put the website on that port. Try this:
socketio.run(app, port=int(os.environ.get('PORT', '5000')))
Then from your client application connect to the Heroku URL for your app.

Deploying Flask Socket.io application with eventlet on Google AppEngine

I am using flask socket.io configured with eventlet on a flexible google appengine instance. According to the flask socket.io documentation: https://flask-socketio.readthedocs.io/en/latest/ running socketio.run(app) will start a webserver if eventlet is installed.
I have a local react web app attempting to establish a WebSocket connection with this app engine instance. The web app uses socket.io and initially defaults to polling HTTP requests. These requests all timeout-- I'm unable to establish a web socket connection. I'm not sure what might be causing my issue, whether it is my app engine configuration or my flask socket.io setup.
Here is my initialization code for flask socket.io:
app = Flask(__name__)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
..
..
if __name__ == '__main__':
socketio.run(app, debug=True)
Here is my app.yaml:
runtime: python
env: flex
entrypoint: python3 server.py
runtime_config:
python_version: 3
manual_scaling:
instances: 1
network:
session_affinity: true
Here is the web app code attempting to set up the connection:
import io from 'socket.io-client'
const socketURL = "<app-engine-instance-url>:5000"
const socket = io.connect(socketURL)

How to enforce HTTPS traffic to Flexible Google App Engine with custom domain?

I have a site on Google Domains (http://example.com).
But I want it should redirect to https://example.com on flexible app engine environment.
what changes required in app.yaml for flexible app engine environment.
Mu current app.yaml as below:
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
In order to redirect HTTP to HTTPS using a Flask application in App Engine Flexible Environment you only need to use the small Flask extension called Talisman.
In your requirements.txt you have to add a line containing flask-talisman.
In your main.py you just have to import Talisman and wrap your Flask app with it:
from flask import Flask
from flask_talisman import Talisman
app = Flask(__name__)
Talisman(app)
By default, doing this redirects to HTTPS.
Take into account that Talisman is not an official Google product, experimental or otherwise.

Categories