Create Kubernetes clusters on GCP and deploy applications
After having analyzed the different components of Kubernetes, let’s go in a more practical example deploying a simple application on the Google Kubernetes Engine (GKE).
We’ll use the command line tools gcloud, docker and kubectl.
Be sure your gcloud cli is configured with the correct project and region on which you want to work.
The cluster creation is always done with the gcloud commands. In our case we create a cluster with a single node:
gcloud container clusters create kube-test-cluster --num-nodes=1
Now, we need to to configure the kubectl command line to interact with the cluster getting the credentials:
gcloud container clusters get-credentials kube-test-cluster
Create the application
We create a very simple Python application based on Flask that will just send back a response to a HTTP request.
This is the file app.py with the code of the app
from flask import Flaskapp = Flask(__name__)@app.route(‘/’)
def hello_world():
return ‘Hello, GKE!’
and the requirements.txt will look like this:
Flask==2.0.1
Create the Docker image
A container image is required to deploy on Kuberentes. The Dockerfile for our application can be like this:
# syntax=docker/dockerfile:1
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ “python3”, “-m” , “flask”, “run”, “ — host=0.0.0.0”]
and it can be created with the command (the image name will be python-docker):
docker build -t python-docker .
Let’s test the just create image launching it with:
docker run -d -p 5000:5000 python-docker
The site is running and visible visiting http://localhost:5000 in a browser:

To stop the container, we can get its id with
docker ps

and run
docker container stop <container_id>
Create the container’s image
The image needs to be pushed to the container registry on GCP. First, we can tag it:
docker tag python-docker gcr.io/<gcp_project_id>/python-docker
and then push it to GCP:
docker push gcr.io/<gcp_project_id>/python-docker
Going to the GCP console and searching for the Container Registry, we can see our image:


Deploy the application
Once the gcloud CLI to create the cluster, it’s time to deploy the application. For this, we need to use the Kubernetes command line interface kubectl. It can be installed through gcloud:
gcloud components install kubectl
The YAML file to deploy the Kubernetes resources (app.yaml) will look like this:
apiVersion: v1
kind: Service
metadata:
name: python-test-service
spec:
selector:
app: python-test
ports:
- protocol: “TCP”
port: 5000
targetPort: 5000
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-test
spec:
selector:
matchLabels:
app: python-test
replicas: 1
template:
metadata:
labels:
app: python-test
spec:
containers:
- name: python-test
image: gcr.io/<gcp_project_id>/python-docker
ports:
- containerPort: 5000
The file will allow the deployment of our container (1 pod replica) from the image gcr.io/<gcp_project_id>/python-docker (this is the second part of the file after the — — separator) and then, it will create a Load Balancer that will expose our the application on the port 5000 (mapping to the container’s port 5000).
The command
kubectl apply -f app.yaml
will deploy on GKE our application showing this response in the CLI:

Let’s take the IP address that will allow to see our page deployed on GKE. Let’s go to Kubernetes Engine in the GCP console:

and let’s click on ‘Services & Ingress’ on the left column:

In this case the ip address of our cluster app on GKE is http://34.79.16.92:5000/ and visiting it with a browser we see again our simple app page.
To avoid any charges remember to delete your cluster on GCP at the end of the exercise.