Giter Site home page Giter Site logo

noahgift / kubernetes-hello-world-python-flask Goto Github PK

View Code? Open in Web Editor NEW
33.0 5.0 83.0 27 KB

A Kubernetes Hello World Project

License: Creative Commons Zero v1.0 Universal

Python 64.71% Dockerfile 16.33% Makefile 18.96%
kubernetes flask python-flask docker-container coursera oreilly practicalmlops mlops

kubernetes-hello-world-python-flask's Introduction

Kubernetes Hello World

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. kubernetes-load-balanced-cluster

This recipe is in the book Practical MLOps.

9781098103002

Assets in Repo

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

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"
  }
]
  • Stop the running docker container by using control-c command

Running Kubernetes Locally

  • Verify Kubernetes is working via docker-desktop context
(.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.

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:

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:

Name:                     hello-python-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=hello-python
Type:                     LoadBalancer
IP Families:              <none>
IP:                       10.101.140.123
IPs:                      <none>
LoadBalancer Ingress:     localhost
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  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:                   <none>

Invoke the endpoint to curl it:

make invoke

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

kubernetes-hello-world-python-flask's People

Contributors

noahgift avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kubernetes-hello-world-python-flask's Issues

Outdated flask causing library installation issues

In the requirements.txt, flask 1 is used but it is incompatible with other libraries (the escape library can longer be imported from jinja2). It is recommended to change to flask 2.1.0, the make all command would fine.

Readme file refers to describe a different service name

According to the Kubernetes template "kube-hello-change.yaml", the hello-flask-change-service is created, but the Readme file points to describe "hello-python-service".

Please, update the Readme file: kubectl describe services hello-flask-change-service instead of the kubectl describe services hello-python-service command, also update its output.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.