Stephen kanyi
Google Cloud - Community
4 min readJul 23, 2017

--

Deploy Python Application to Google Cloud with Docker and Kubernetes.

In this post, I will be trying to deploy one of my python application to Google Cloud. I decided to use an existing application to avoid wasting much time showing how to create the application. So let’s get to it.

Prerequisites:

Create a project on google cloud.

Log in to your google cloud account and create a new project if you don’t have one already. I created a project called bucketlist.

Follow the links to see how to install each of them

  • docker
  • gcloud
  • kubernetes — install with gcloud components install kubectl

Packaging the app in Container and then pushing the google cloud registry.

Since we already have the application, https://github.com/andela-skanyi/bucketlist_api

Now we create the Dockerfile in the root directory.

FROM python:3.5.2-slim

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app

RUN apt-get update && \

apt-get install -y postgresql python-psycopg2 libpq-dev cython && \

pip install — upgrade pip && pip install -r requirements.txt

COPY . /usr/src/app

EXPOSE 50050

ENTRYPOINT [“python3”]

CMD [“manage.py” ]

Then build the image that we will push to google cloud. We will use docker for this. Docker has a build command and since we will push the image to google cloud, we will create the image according to google cloud guidelines.

They have a naming conventions, gcr.io/[YOUR_PROJECT_ID]/[YOUR_APPLICATION_NAME]

docker build -t [DOCKER_IMAGE_NAME]  -f [DOCKERFILE_LOCATION] [APPLICATION_FILE_PATH] 

In our case this translate to something like ```docker build -t gcr.io/[YOUR_PROJECT_ID]/[YOUR_APPLICATION_NAME]:v1 .```

Now that we have built the image, we are now going to push it to google registry. To list all the images that you have.

docker images

To push the image,

gcloud docker -- push gcr.io/your-project-id/image-name

If you get this error,

denied: Please enable Google Container Registry API in Cloud Console at https://console.cloud.google.com/apis/api/containerregistry.googleapis.com/overview?project=your-project-id before performing this operation.

Go to the link and click on enable and retry to push again.

Create a cluster for the full guide on how to create the cluster, have a look at https://cloud.google.com/container-engine/docs/clusters/operations

gcloud container clusters create staging --zone ZONE

The above command will create a cluster called staging in the zone you have specified.

If you have more than one project, you might want to set the project that you want to create the cluster in with,

gcloud config set project <project-id>

Configuring kubectl

We are going to configure kubectl so that it can know which cluster to target.

gcloud config set project <project-id>
gcloud config set container/cluster staging
gcloud config set compute/zone us-east1-c
gcloud container clusters get-credentials staging

You can combine all these commands to one,

gcloud container clusters get-credentials staging --zone us-east1-c --project <project-name>

Deploying the app to the cluster.

We have two options on how we can achieve this.

  • On the command line
  • Deploy with configuration file

Option A:

kubectl run {deployment_name} --image=gcr.io/$PROJECT_ID/{image-name}:{tag} --port={port}

Which translate to in our case,

kubectl run bucketlist-api --image=gcr.io/bucketlist-174407/bucketlistapi:latest --port=50050

A successful deployment should return deployment “bucketlist-api” created. You can add different variables to the deployment like — env and so on.

To get the deployment, kubectl get deployment bucketlist-api.

To observe if the application deployment is up and running, Check the status should be running.

kubectl get po 
or
watch kubectl get po

Option B:

We are going to create a deployment file called deployment.yml. Here we can manipulate several things like the environment variables and so on.

Notice that I get the Secret key from secretKeyRef, I created a secret deployment file.

Now the app is up and running.

Expose the app to the Internet

We can also do this in the terminal, or create a service file,

kubectl expose deployment bucketlist-api --type="LoadBalancer"

With a service file,

To create the service, run

kubectl create -f service.yml

To list available services, use

kubectl get services

Use the external IPaddress together with the port to access the app.

Update or Delete a deployment

kubectl edit deployment <name-of-your-deployment>kubectl delete deployment <name-of-your-deployment>

Conclusion,

We have now deployed our application and exposed it to the outside world.

Steps:

  • Create a dockerfile
  • Build an image
  • Push it to google registry
  • Create a deployment file
  • Create secrets deployment file to store the environment variables
  • Deploy the application with kubernetes
  • Create a service deployment file to expose the app
  • Can edit the deployment
  • Can also delete the deployment

There are many things that you can do including scaling the app or even set auto scaling. Feel free to explore beyond.

What Next?

I will be automating the deployment pipeline, when the developer pushes the code from their local machine up to the deployed application without breaking the app due to the changes introduced.

--

--

Stephen kanyi
Google Cloud - Community

A Chemist by Training, A Software Developer(DevOps) by Profession and A Penetration Tester as a Hobby.