Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noahgift/kubernetes-hello-world-python-flask
A Kubernetes Hello World Project
https://github.com/noahgift/kubernetes-hello-world-python-flask
coursera docker-container flask kubernetes mlops oreilly practicalmlops python-flask
Last synced: 2 days ago
JSON representation
A Kubernetes Hello World Project
- Host: GitHub
- URL: https://github.com/noahgift/kubernetes-hello-world-python-flask
- Owner: noahgift
- License: cc0-1.0
- Created: 2021-03-16T13:52:31.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-10T23:01:00.000Z (20 days ago)
- Last Synced: 2025-01-21T03:07:05.554Z (10 days ago)
- Topics: coursera, docker-container, flask, kubernetes, mlops, oreilly, practicalmlops, python-flask
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 34
- Watchers: 5
- Forks: 84
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 🎓 Pragmatic AI Labs | Join 1M+ ML Engineers
### 🔥 Hot Course Offers:
* 🤖 [Master GenAI Engineering](https://ds500.paiml.com/learn/course/0bbb5/) - Build Production AI Systems
* 🦀 [Learn Professional Rust](https://ds500.paiml.com/learn/course/g6u1k/) - Industry-Grade Development
* 📊 [AWS AI & Analytics](https://ds500.paiml.com/learn/course/31si1/) - Scale Your ML in Cloud
* ⚡ [Production GenAI on AWS](https://ds500.paiml.com/learn/course/ehks1/) - Deploy at Enterprise Scale
* 🛠️ [Rust DevOps Mastery](https://ds500.paiml.com/learn/course/ex8eu/) - Automate Everything### 🚀 Level Up Your Career:
* 💼 [Production ML Program](https://paiml.com) - Complete MLOps & Cloud Mastery
* 🎯 [Start Learning Now](https://ds500.paiml.com) - Fast-Track Your ML Career
* 🏢 Trusted by Fortune 500 TeamsLearn end-to-end ML engineering from industry veterans at [PAIML.COM](https://paiml.com)
# Kubernetes Hello World
A Kubernetes Hello World Project for Python Flask. This project uses [a simple Flask app that returns correct change](https://github.com/noahgift/flask-change-microservice) as the base project and converts it to Kubernetes.
![kubernetes-load-balanced-cluster](https://user-images.githubusercontent.com/58792/111511557-3f45a280-8725-11eb-8e4a-5f5ef787796d.png)This recipe is in the book Practical MLOps.
![9781098103002](https://user-images.githubusercontent.com/58792/111000927-eb1b7680-8350-11eb-8e24-d41064590fc1.jpeg)
## Assets in Repo
* `Makefile`: [Builds project](https://github.com/noahgift/kubernetes-hello-world-python-flask/blob/main/Makefile)
* `Dockerfile`: [Container configuration](https://github.com/noahgift/kubernetes-hello-world-python-flask/blob/main/Dockerfile)
* `app.py`: [Flask app](https://github.com/noahgift/kubernetes-hello-world-python-flask/blob/main/app.py)
* `kube-hello-change.yaml`: [Kubernetes YAML Config](https://github.com/noahgift/kubernetes-hello-world-python-flask/blob/main/kube-hello-change.yaml)## Get Started
* Create Python virtual environment `python3 -m venv ~/.kube-hello && source ~/.kube-hello/bin/activate`
* Run `make all` to install python libraries, lint project, including `Dockerfile` and run tests## Build and Run Docker Container
* Install [Docker Desktop](https://www.docker.com/products/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`
```bash
[
{
"5": "quarters"
},
{
"1": "nickels"
},
{
"4": "pennies"
}
]
```* Stop the running docker container by using `control-c` command
## Running Kubernetes Locally
* Verify Kubernetes is working via docker-desktop context
```bash
(.kube-hello) ➜ kubernetes-hello-world-python-flask git:(main) kubectl get nodes
NAME STATUS ROLES AGE VERSION
docker-desktop Ready master 30d v1.19.3
```* Run the application in Kubernetes using the following command which tells Kubernetes to setup the load balanced service and run it:
`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.
```yaml
apiVersion: v1
kind: Service
metadata:
name: hello-flask-change-service
spec:
selector:
app: hello-python
ports:
- protocol: "TCP"
port: 8080
targetPort: 8080
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
```* Verify the container is running
`kubectl get pods`
Here is the output:
```bash
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
```* Describe the load balanced service:
`kubectl describe services hello-python-service`
You should see output similar to this:
```bash
Name: hello-python-service
Namespace: default
Labels:
Annotations:
Selector: app=hello-python
Type: LoadBalancer
IP Families:
IP: 10.101.140.123
IPs:
LoadBalancer Ingress: localhost
Port: 8080/TCP
TargetPort: 8080/TCP
NodePort: 30301/TCP
Endpoints: 10.1.0.27:8080,10.1.0.28:8080,10.1.0.29:8080
Session Affinity: None
External Traffic Policy: Cluster
Events:
```Invoke the endpoint to curl it:
`make invoke`
```bash
curl http://127.0.0.1:8080/change/1/34
[
{
"5": "quarters"
},
{
"1": "nickels"
},
{
"4": "pennies"
}
]
```To cleanup the deployment do the following: `kubectl delete deployment hello-python`
## References
* Azure [Kubernetes deployment strategy](https://azure.microsoft.com/en-us/overview/kubernetes-deployment-strategy/)
* Service [Cluster Config](https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/) YAML file
* [Kubernetes.io Hello World](https://kubernetes.io/blog/2019/07/23/get-started-with-kubernetes-using-python/)