Running Flask API application on Openshift with Nginx as Reverse Proxy | Medium

Utilizing Podman

Picture by 愚木混株 cdd20 on Unsplash

In my earlier article on running a Flask application with Nginx proxy on Podman, I defined how a Flask utility served by gunicorn could possibly be configured behind a Nginx reverse proxy server on Podman. We need to obtain the identical outcomes on Pink Hat Openshift containers platform. Few modifications should be made for this to work.

Containers on Openshift talk by way of service. The Nginx reverse proxy should ship visitors to the Flask utility by way of its service identify and port.

There are a number of methods to launch an utility to Openshift. For this text, I’ve chosen to make use of a prebuilt container picture the place I’ll construct the picture domestically, push it to quay.io repository, and level my new Openshift utility to launch a container utilizing that picture.

To start out with, right here’s the applying’s file construction:

First, let’s take a look at the Flask utility Dockerfile. I’ve added an instruction to reveal the applying on port 8000.

For Nginx Dockerfile, we’re utilizing Redhat ubi9/nginx-120 base picture, which is well reconfigurable to run as a reverse proxy. There are parameters within the base image environment that we are going to use so as to add our customized Nginx configurations. The bottom picture setting has the next entries:

A very powerful Nginx configuration is the server block which can outline the place the visitors will likely be forwarded.

So our Nginx reverse proxy utility picture could have the next Dockerfile directions:

Construct each photographs and push them to quay.io.

Log into the Openshift cluster utilizing oc cli.

$ oc login -u <username> <openshift_api_url>
Password:

Create a venture for use for our utility on Openshift.

$ oc new-project govtstructure

As a result of we’re pulling photographs from quay.io, now we have to authenticate Openshift to have the ability to pull photographs from the picture registry. We’ll create a secret that accommodates the marina.io API entry token and hyperlink the default service account to make use of the key for authentication when pulling photographs.

$ oc create secret generic quayio --from-file    .dockerconfigjon=$XDG_RUNTIME_DIR/containers/auth.json 
--type kubernetes.io/dockerconfigjson
#hyperlink secret to default service account for pull
$ oc secrets and techniques hyperlink default quayio --for pull

We’re able to launch the purposes. We begin with the Flask utility.

$ oc new-app --name govtapplication 
--image quay.io/wainaina3/govtapplication
#verify all of the providers which have been launched
$ oc get all -l app=govtapplication
#Confirm the applying is working
$ oc get pods
NAME READY STATUS RESTARTS AGE
govtapplication-858ffb9777-nfgbg 1/1 Working 0 4m53s
#confirm service is up and listening on port 8000
$ oc get svc
NAME TYPE CLUSTER-IP PORT(S) AGE
govtapplication ClusterIP 172.30.220.218 8000/TCP,8080/TCP 5m57s

The govtapplication is up and working. Subsequent, we are able to create the Nginx reverse proxy utility to serve our visitors.

#create nginx utility with picture from quay.io
$ oc new-app --name govtapplication
--image quay.io/wainaina3/govtnginxproxy

Confirm the Nginx container is working and listening on port 8181 as anticipated.

#Confirm utility is working as anticipated
$ oc get pods

All providers are OK, and the pods run as anticipated. We’ll create a route by exposing the Nginx service and explicitly select port 8181 so it will possibly serve our Flask utility visitors.

$ oc expose svc govtnginxproxy --port 8181

Voila! Our utility can now be accessed from the Nginx route by simply including the next utility routes:

#Get the route
$ oc get routes
#Entry the applying from the route. use the /sayhello or /getAllCounties(would require database connection) to check$ curl http://<route_url>/sayhello
"greetings":"Whats up there"

Yow will discover the applying code on my GitHub repository.

More Posts