A Kubernetes Hello World Project for Python Flask. This project uses a simple Flask app that returns correct change as the base project and converts it to Kubernetes.
This recipe is in the book Practical MLOps.
Makefile
: Builds projectDockerfile
: Container configurationapp.py
: Flask appkube-hello-change.yaml
: Kubernetes YAML Configpython3 -m venv ~/.kube-hello && source ~/.kube-hello/bin/activate
make all
to install python libraries, lint project, including Dockerfile
and run testsInstall Docker Desktop
To build the image locally do the following.
docker build -t flask-change:latest .
or run make build
which has the same command.
To verify container run docker image ls
To run do the following: docker run -p 8080:8080 flask-change
or run make run
which has the same command
In a separate terminal invoke the web service via curl, or run make invoke
which has the same command
curl http://127.0.0.1:8080/change/1/34
[
{
"5": "quarters"
},
{
"1": "nickels"
},
{
"4": "pennies"
}
]
control-c
command$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 7h44m v1.23.1
make image-export # saves the image
make copy-image # copies it to minikube, in case this didn't work run the code on terminal
minikube ssh # ssh to minikube
docker load -i flask-change.bz2 # load the docker image
kubectl apply -f kube-hello-change.yaml
or run make run-kube
which has the same command
You can see from the config file that a load-balancer along with three nodes is the configured application.
apiVersion: v1
kind: Service
metadata:
name: hello-flask-change-service
spec:
selector:
app: hello-python
ports:
- protocol: "TCP"
port: 8080
targetPort: 8080
nodePort: 32000 # output port
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-python
spec:
selector:
matchLabels:
app: hello-python
replicas: 3
template:
metadata:
labels:
app: hello-python
spec:
containers:
- name: flask-change
image: flask-change:latest
imagePullPolicy: Never
ports:
- containerPort: 8080
kubectl get pods
Here is the output:
NAME READY STATUS RESTARTS AGE
flask-change-7b7d7f467b-26htf 1/1 Running 0 8s
flask-change-7b7d7f467b-fh6df 1/1 Running 0 7s
flask-change-7b7d7f467b-fpsxr 1/1 Running 0 6s
kubectl describe services hello-flask-change-service
You should see output similar to this:
Name: hello-flask-change-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=hello-python
Type: LoadBalancer
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.99.135.200
IPs: 10.99.135.200
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32000/TCP
Endpoints: 172.17.0.6:8080,172.17.0.7:8080,172.17.0.8:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
To create a visible ip on minikube run the following:
minikube service hello-flask-change-service
Invoke the endpoint to curl it:
make minikube-invoke
curl http://192.168.59.102:32000//change/1/34
[
{
"5": "quarters"
},
{
"1": "nickels"
},
{
"4": "pennies"
}
]
To cleanup the deployment do the following:
kubectl delete deployment hello-python
minikube stop # to stop the minicube
SQLAlchemy Admin for Starlette/FastAPI
work with Tehran stock exchange data in Python
In this repository, I include all my notes and whatever I learn, so that everyone can benefit from them
Python Engineer Roadmap
SelfUp is a free and open source project to helps us become the better version of ourselves.
A simple Aparat Video Downloader Script
Coefficient of Variation (CV) and Coefficient of Quartile Variation (CQV) with Confidence Intervals (CI). Python port of https://github.com/MaaniBeigy/cvcqv)