Deploy an application to Google Kubernetes Engine


Welcome back, this is the continuation post of the previous article, where we have discussed the Kubernetes and its concepts in detail. In this tutorial, we are going to see how to deploy the python application to the GKE. 
Let us do the Kubernetes Dracarys :-)

kubernetes datacloudies

I have followed this reference throughout this tutorial. Please do check out below video tutorial for this deployment as well. 



1.download the GitHub project module from GCP.
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
2. Create a PostgreSQL database using Cloud SQL.
kubernetes datacloudies
Provide the instance ID and select the appropriate region. Make other settings as default. 
kubernetes datacloudies

3. create a service account in IAM, which is going to help the Kubernetes to authenticate the cloud SQL instance. 
kubernetes datacloudies
Assign the role of cloud SQL admin and generate the key in JSON format. 
kubernetes datacloudies
4. create a database and database user in the cloud SQL PostgreSQL instance.
kubernetes datacloudies
kubernetes datacloudies
5. download the cloud SQL proxy from the below URL and assign the connection name which has taken from the created instance.
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
./cloud_sql_proxy -instances=”connection-name”=tcp:5432
kubernetes datacloudies
6. Set the environment variable for DATABASE_USER and DATABASE_PASSWORD and create the python models for this project.
python manage.py runserver
which will produce the output locally. 
7. Now, create a bucket and transfer the static content files to the Storage engine which will be referred when we deploy the complete app to the Kubernetes engine for static pages.
kubernetes datacloudies
gsutil mb gs://[bucketname]

gsutil defacl set public-read gs://[bucketname]

gsutil –m rsync –r static gs://[bucketname]
8. change the static URL from the setttings.py file as per the bucket name.
kubernetes datacloudies
9. Create a Kubernetes cluster. Provide the cluster name and select the region, and then leave other options as default for this tutorial.
kubernetes datacloudies

10. Change the image and cloud proxy name in the polls.yaml file, the image should be the same as the docker image which is going to be built. 
kubernetes datacloudies
11. get the credential from the Kubernetes cluster to establish the connection. 
gcloud container clusters get-credentials polls –zone us-central1-a 
12.  create two secret components in the Kubernetes 
kubectl create secret generic cloudsql-oauth-credentials --from file=credentials.json=[PATH_TO_CREDENTIAL_FILE]
kubectl create secret generic cloudsql --from-literal=username [PROXY_USERNAME] --from-literal=password=[PASSWORD]
kubernetes datacloudies
13. download the cloud SQL image from the GCR repository.
kubernetes datacloudies
14. build the image as per the docker file,
docker build -t gcr.io/project-id/polls .
kubernetes datacloudies
15. Now, push the local image to the container registry,
docker push gcr.io/project-id/polls
kubernetes datacloudies
16. deploy the application to the Kubernetes engine.
kubectl create –f polls.yaml
kubernetes datacloudies
17. check the running status of pods and then collect the external IP from the load balancer service,
kubernetes datacloudies
18. check the application and verify the deployment.
kubernetes datacloudies

Excellent, here it concludes, we have successfully deployed the application to Kubernetes engine. Please do check out the other tutorials in datacloudies. 

Recent Posts