Table of contents
No headings in the article.
Back again with another ChatGPT assignment, to the newcomers, this is a series where I ask ChatGPT to give me an assignment on a topic which I want to learn. You can check out the previous post here. So this time I wanted to work with a bit of DevOps, so this time it's a Kubernetes-based assignment.
Moving on to the problem statement, as I am pretty comfortable working with flask, I asked it to give me a flask-based Kubernetes assignment.
When working with flask, SQLAlchemy is my favourite database, very simple to initialise as well as work with.
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =\
'sqlite:///' + os.path.join(basedir, 'database.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
description = db.Column(db.Text)
price = db.Column(db.Float)
def __init__(self, title, description, price):
self.title = title
self.description = description
self.price = price
So the above is essentially the database definition that was used for this application. Before we can add the CRUD operations or anything that is required to interact with the database, we first have to initialise it and the way we do that is by opening up a flask shell, importing necessary modules and using create_all()
function.
export FLASK_APP=app
flask shell
python3
from app import db, Item
db.create_all()
Once this was done, finishing the app was pretty simple, defining the required functions, which were adding an item, deleting an item and a landing page, for this, I also needed to create 3 different HTML files too to render them. I added a little bit of CSS too, to make sure the front end is not too plain also. Standard flask app opens up in port 5000 but in MacBooks, but in the past I have faced the issue where if I dockerized an app, it doesn't work in port 5000, StackOverflow says its because airdrop takes up that port, so we will have to use a different port, that is not a big deal as I am not really particular about port numbers, but if I have to term a favourite, it has to be 8000, so I ensured in the Dockerfile that the application would run in port 8000 by adding the CMD ["flask", "run", "--host=0.0.0.0", "--port=8000"]
port number in the command line argument. This is how the dockerising part was done, I did not need a lot of assistance or guidance from the internet as there have been multiple projects where I have worked with flask and docker. There is one more issue people usually face while pushing a docker image to the docker hub, the command line is the best to do and the reason is that we have to tag a docker image before it can be pushed to the hub, which is why clicking on push to the hub in the docker application does not work and tagging has to be done in the terminal itself, so if you are anyway using the terminal for tagging, might as well push it from there only.
This final part is the new thing that this project taught me which was adding the containerised docker into the Kubernetes cluster. To do that I needed a bit of revision of YAML files, I just looked up Kubernetes documentation to write YAML files for deployment and service and all. After a little bit of googling on how to do this for a flask app, the code for deployment and service was completed. The whole process was a bit confusing to me as even a slight mistake means lakalakalaka, but not too much lakalakalaka also which is good. kubectl
is the CLI tool to work with kubernetes and that is exactly what I used for the deployment, service YAML files. The docker flask app was running at port 8000 which becomes the target port and the kubernetes port was defined 80, it can be whatever we want but just for the sake of it I chose it as 80, it can be 8000 also, that also works pretty well.
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
These commands will ensure our service is up and running at whichever port we desire, in this case, 80.
At first, I was sceptical about Kubernetes not being that hard, even though I had worked with it in my second year and even had done lab assignments in a cloud computing course, although I did get confused with writing YAML files and had a few errors along the way which ate my brain, my overconfident would like to say:
Checkout the code here and please tell if there are any issues in it: https://github.com/SammithSB/flask-kubernetes
Ashte, tata bye bye machas. Will come back with another ChatGPT assigned assignment, if you have any ideas, give off, will definitely do.