Flask
For the flask app waiting for deployment, the only thing we need to focus on is the host and the port this app exposed to. For example:
app.run(host='127.0.0.1', port=8080)
This command let the application run on localhost and listen on pport 8080.
Docker
After finishing the python code writing, next step we need to do is to pack it into a docker image.
First, create a Dockerfile
to set up for docker.
FROM python:3.9
RUN apt update
WORKDIR /Khipu_Flask
ADD . /Khipu_Flask
RUN python -m pip install --upgrade pip
RUN pip install -r /Khipu_Flask/requirements.txt
EXPOSE 8080
CMD ["gunicorn", "main:app", "--config=config.py"]
In this, WORKDIR is the root directory we set in the docker container. Then We ADD all the file under this directory into /Khipu_Flask
, which is set by us as the root dir. Then We run python command to install the requirement packages. Also, we use the gunicore to run app in main.py.
Also create a .dockerignore
there is any file you do not want to pack.
Dockerfile
README.md
*.pyc
*.pyo
*.pyd
__pycache__
Second, build
the docker file under the directory where the Dockerfile
is in.
docker build -t <name:tag> <path>
docker build -t myapp:1.0 /path/to/Dockerfile
-t will build a new image which name and tag specified and will take the files under into build.
Next, tag
the built file with the GCR storage.
docker tag <source_image> <target_image>
Where <source_image> is the name of the image you want to tag, and <target_image> is the name and tag you want to give to the new tag.
docker tag khipu_flask:latest gcr.io/flaskkhipu/khipu_flask:latest
This command creates a new tag under GCR. You should following the convention: gcr.io/<project name>
After that, push
the docker image to GCR.
docker push gcr.io/flaskkhipu/khipu_flask:latest
Just add the tag of docker image you behind the push command.
Tips
docker image ls
is used to show the docker image under the current docker application.
docker rmi
is used to remove tags. You should add <REPOSITORY:TAG>
kubectl
1. Set Google Cloud Context
# Get login
gcloud auth login
# set cluster name, zone, and project name
gcloud container clusters get-credentials khipu-cluster --zone us-east1 --project flaskkhipu
# Pass to kubectl
kubectl config current-context
2. Edit .yaml File
Template file.
apiVersion: [API_VERSION]
kind: [KIND]
metadata:
name: [NAME]
labels:
[LABEL_KEY]: [LABEL_VALUE]
spec:
[RESOURCE_SPEC]
[API_VERSION]: The API version of the Kubernetes resource you want to create, such as v1 or apps/v1.
[KIND]: The kind of Kubernetes resource you want to create, such as Deployment, Service, or ConfigMap.
[NAME]: The name you want to give to your Kubernetes resource.
[LABEL_KEY] and [LABEL_VALUE]: A key-value pair that you can use to label your Kubernetes resource.
[RESOURCE_SPEC]: The specification of the Kubernetes resource you want to create. This will vary depending on the kind of resource you want to create.
apiVersion: apps/v1
kind: Deployment
metadata:
name: khipu-deploy
spec:
replicas: 3
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: khipu-flask
image: gcr.io/flaskkhipu/khipu_flask:latest
ports:
- containerPort: 8080
3. Create Deployment
kubectl create -f deploy.yaml
4. Edit Expose
kubectl expose deployment khipu-deploy \
--type=LoadBalancer --port 80 --target-port 8080
5. Get Services and Visit
➜ Khipu_Flask kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
khipu-deploy LoadBalancer 10.43.1.207 34.139.224.175 8080:30978/TCP 62m
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 81m