Running a Kubernetes cluster in Google Cloud

Running a Kubernetes cluster in Google Cloud

This is a brief and practical article on how to run Python code with a public endpoint using Flask in a Kubernetes cluster running on Google Cloud Platform. Let's see some basic concepts:

No alt text provided for this image
No alt text provided for this image

In order to simplify the authentication process, we will use the terminal of a pre built instance on Google Compute Engine.

First, we install kubectl:

sudo apt-get install kubectl

mkdir helloworld-gke
cd helloworld-gke

gcloud beta auth configure-docker us-central1-docker.pkg.dev

Then we login into Docker so that our python script can access other APIs:

cat key_123_bla_bla.json | docker login -u _json_key --password-stdin https://us-central1-docker.pkg.dev

Then we create an app.py running our Python code and the Flask application. After that we create a Dockerfile with the following content:

FROM python:3.7-slim

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

RUN pip install Flask gunicorn        # Plus other libraries



CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

Also, a .dockerignore file so that folder content does not interfere with container creation:

Dockerfile
README.md
*.pyc
*.pyo
*.pyd
__pycache__

Then we will create container image:

gcloud builds submit --tag gcr.io/<project-id>/helloworld-gke . --timeout=85000

This will install the libraries and will make a Docker build and push:

No alt text provided for this image
No alt text provided for this image

Now we can create a Kubernetes Engine cluster with three instances by running:

gcloud container clusters create helloworld-gke --num-nodes 1 --enable-basic-auth --issue-client-certificate --enable-autoscaling --min-nodes 1 --max-nodes 3 --zone us-central1 --machine-type n1-standard-4 --local-ssd-count 1 --scopes=cloud-platform,storage-rw,compute-rw,service-control --async --preemptible --enable-autorepair
No alt text provided for this image

This creates the cluster and a health check of the cluster is performed. You can go to Google Cloud Console / Kubernetes Engine and see what is going on:

No alt text provided for this image

Now run:

kubectl get nodes
No alt text provided for this image

Now we are ready to implement Deployment and Service on the new cluster.

We must have two files: deployment.yaml and service.yaml. Content of deployment.yaml:

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

name: helloworld-gke

spec:

replicas: 1

selector:

matchLabels:

app: hello

template:

metadata:

labels:

app: hello

spec:

containers:

- name: hello-app

image: gcr.io/$GCLOUD_PROJECT/helloworld-gke:latest

ports:

- containerPort: 8080

env:

- name: PORT

value: "8080"

Then we install in the cluster and wait a few seconds to get it to work:

kubectl apply -f deployment.yaml

Let's check:

kubectl get deployments
No alt text provided for this image

Yesss, we have a pod running:

kubectl get pods
No alt text provided for this image

If you have problems like CrashLoopBackOff in the pod, like the image below, run $ kubectl logs <pod-name> to see what is going on, if it is a library problem or maybe authentication of the instance to other GCP APIs:

No alt text provided for this image

Now we do the same with service.yaml:

kubectl apply -f service.yaml

By running the following line of code, we will have an endpoint for the Flask application (EXTERNAL-IP):

kubectl get services
No alt text provided for this image

Now we can easily CURL the endpoint:

No alt text provided for this image

To clean your project:

gcloud container clusters delete helloworld-gke

gcloud container images delete gcr.io/project-id/helloworld-gke

要查看或添加评论,请登录

Rubens Zimbres, Ph.D.的更多文章

社区洞察

其他会员也浏览了