This README provides a comprehensive guide on deploying a simple Flask application, which serves extremely cheesy and randomly selected programming jokes, on OpenShift, and also outlines how to update the application with new content and versioning.
- Access to an OpenShift cluster
- OpenShift CLI (
oc
) installed - Podman or Docker installed
- Git installed
Authenticate via CLI:
oc login --token=<your-token> --server=<your-cluster-api-url>
Ensure the internal registry is externally accessible:
oc patch configs.imageregistry.operator.openshift.io/cluster --patch '{"spec":{"defaultRoute":true}}' --type=merge
Clone the application's repository:
git clone https://github.com/bstrauss84/programming-jokes-demo.git
cd ./programming-jokes-demo
To simplify the rest of this excercise, let's go ahead and define some environment variables for our OpenShift internal registry route, as well as the namespace, and initial image tag for our sample application:
OPENSHIFT_REGISTRY_ROUTE=$(oc get route default-route -n openshift-image-registry --template='{{ .spec.host }}')
NAMESPACE="demo-$(oc whoami)"
Create your namespace:
oc new-project $NAMESPACE
app.py
: The main Flask application file containing the server logic and joke list.requirements.txt
: Specifies Python package dependencies for the application.templates/index.html
: HTML template for rendering the jokes in a web browser.Containerfile
: Used to build the container image of the application.
Build your image using Podman or Docker:
podman build -t $OPENSHIFT_REGISTRY_ROUTE/$NAMESPACE/programming-jokes:latest .
Authenticate with the OpenShift registry:
podman login -u $(oc whoami) -p $(oc whoami -t) $OPENSHIFT_REGISTRY_ROUTE
Push your image to the internal OpenShift registry:
podman push $OPENSHIFT_REGISTRY_ROUTE/$NAMESPACE/programming-jokes:latest
Deploy your application on OpenShift:
oc new-app $NAMESPACE/programming-jokes:latest --name=programming-jokes-app
oc create route edge programming-jokes-app --service=programming-jokes-app --insecure-policy=Redirect
After deploying your application and exposing it via a route, you can easily find the URL to access it with the following command:
echo "Application URL: https://$(oc get route programming-jokes-app -o jsonpath='{.spec.host}')"
- Navigate to the Application URL: Open a web browser and go to the URL output by the previous command.
- Explore the Application: Click the "Get Another Joke" button a few times to see different programming jokes displayed. This verifies that your application is running successfully and serving content dynamically.
To update your application (e.g., adding new jokes), follow these steps:
-
Edit
app.py
:Add a new joke to the end of the joke list. For example...
"I've been using Vim for a long time now, mainly because I can't figure out how to exit it."
IMPORTANT: Remember, when adding a new joke, ensure you place a comma at the end of the preceding line to maintain valid Python list syntax.
-
(Optional) Update the Containerfile:
Modify the ENV variable
APP_VERSION
in the Containerfile from1.0
to2.0
. -
Rebuild and Push the Updated Image:
Rebuild the image with the new content:
podman build -t $OPENSHIFT_REGISTRY_ROUTE/$NAMESPACE/programming-jokes:latest .
Push the updated image to the registry:
podman push $OPENSHIFT_REGISTRY_ROUTE/$NAMESPACE/programming-jokes:latest
Ensure the deployment has rolled out the new version:
oc rollout status deployment/programming-jokes-app
oc get pods -n $NAMESPACE
After updating your application, pushing the new image, and ensuring your deployment is using this latest version, repeat the process to access your application:
echo "Application URL: https://$(oc get route programming-jokes-app -o jsonpath='{.spec.host}')"
- Visit the Application Again: Use the URL provided by the command to open your application in a web browser.
- Check for New Content: Press the "Get Another Joke" button multiple times until you see the new jokes appear. This confirms that the update was successful and the new version of your application is live. If you did the optional step earlier, where you updated the Containerfile, you should notice that the "Current Version:" listed beneath the button has been updated as well.
This guide walked us through the process of deploying a simple Flask application on OpenShift, making updates to the application, and managing deployment updates. It covered everything from initial setup to deploying and updating the application.