This guide covers the steps to set up a Flask application with Twitter (now X) OAuth, deploy it to a Kubernetes cluster with a zero-downtime upgrade strategy, implement and verify a new version of the application to ensure zero downtime.
- K8s Deployment
- Performing a Rolling Update
- Configure Liveness, Readiness and Startup Probes
- Horizontal Pod Autoscaling
- Zero-Downtime
- Zero-Downtime Application with Kubernetes
- Deployment Full Guide
- Flask
- Twitter(X) OAuth
- Kubernetes
- Docker
- Git
- Gardener-AWS platform
-
Python should already be installed in your machine
-
Docker installed on your local machine.
-
Access to a Docker registry (e.g., Docker Hub) or CE edition configured locally/via a VM
-
Kubernetes cluster (Gardener or any other provider) and kubectl installed.
-
Twitter Developer Account for OAuth credentials.
-
Make sure you have the
app.py
file is created in the parent directory -
Ensure you have the
requirement.txt
file and executed in the terminalFlask==2.0.1 tweepy==4.0.0 python-dotenv==0.19.0
-
The
Dockerfile
should also be in the same directory as well -
Most importantly create
.env
file where your client credentials will be saved in.TWITTER_CONSUMER_KEY=... TWITTER_CONSUMER_SECRET=... TWITTER_ACCESS_TOKEN=... TWITTER_ACCESS_TOKEN_SECRET=...
WARNING: The env
should be kept private and not open to public, one way to ensure that is by ignoring it in a .gitignore
file as used in this workflow or manually setting the OAuth tokens as a enviroment variable via terminal (NOT used!)
-
You can run the application manually if you want, simply by;
python3 app.py
Although here in our case, we're integrating the application to a K8s cluster on Gardener AWS platform
-
Build and the push Docker image
docker build -t your-name/twitter:v1 . docker push your-name/twitter:v1
-
Create the Kubernetes deployment manifest file -
deployment.yaml
;
Creating the file, the important steps to integrate the "zero-downtime upgrades" will be;-
The "strategy rolling-update" field in the specifications ;
spec: replicas: 3 selector: matchLabels: app: app strategy: # field to enable the rolling update for downtime upgrade type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1
-
The constraints topology for the server zone
topologySpreadConstraints: - maxSkew: 1 topologyKey: "topology.kubernetes.io/zone" whenUnsatisfiable: DoNotSchedule labelSelector: matchLabels: app: app
-
-
Ensure you have your
service.yaml
file which contains the LoadBalancer specifications in the parent directory -
Create Kubernetes Secrets for Twitter Credentials
kubectl create secret generic twitter-credentials \ --from-literal=consumer_key=inputkey \ --from-literal=consumer_secret=inputkey \ --from-literal=access_token=inputkey \ --from-literal=access_token_secret=inputkey
NOTE: In this step, if your output is returning an already existing client credentials, you'll have to delete it and re-run the above shell script. To delete the old saved credentials if necessary;
kubectl delete secret twitter-credentials
-
Deploy to the Kubernetes cluster
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
-
Monitor the Deployment
kubectl rollout status deployment/app
-
Running the application with the external IP of the loadbalancer
kubectl get services
Get the external IP url of the running application and paste it on your browser.
-
Modify your application code for a new version i.e your
app.py
and save it! -
Build a new Docker image
docker build -t your-username/twitter:v2 .
-
Push the New Docker Image
docker push your-username/twitter:v2
-
Update the Deployment Manifest
From the
deployment.yaml
file, update the image field in the containers section to the new app version, using the image tag (v2
) e.g.containers: - name: twitter image: your-username/twitter:v2 ...
-
Apply the Updated Deployment
kubectl apply -f deployment.yaml
-
Verify the Rolling Update
Monitor the rollout status to ensure zero downtime:
kubectl rollout status deployment/app
-
Check the Status of Pods
kubectl get pods
-
Access the Updated Application
Use the external IP provided by your LoadBalancer service to access the updated application. You can get that with;
kubectl get services
With this guide, you will be able to setup a Flask application with Twitter OAuth, deploy it to a Kubernetes cluster with a zero-downtime upgrade strategy, and implement and verify a new version of the application to ensure zero downtime