GCP: Deploying a Containerised Web Application

Below is a video that explains how to deploy a kubernetes containerised web application on the Google Cloud Platform (GCP).

The following diagram illustrates what we will deploy in this article.

Below are the commands executed in this article.

Prerequisites: Set defaults for the gcloud command-line tool

Set the default project ID and compute zone

gcloud config set project k8s-tutorial-192815
gcloud config set compute/zone europe-west2-a


1) Build the container image

Clone the hello-app image from github and navigate to the application directory

git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
cd kubernetes-engine-samples/hello-app

View the Dockerfile

$ cat Dockerfile
FROM golang:1.8-alpine
ADD . /go/src/hello-app
RUN go install hello-app
FROM alpine:latest
COPY --from=0 /go/bin/hello-app .
CMD ["./hello-app"]


Create and verify an environment variable to hold the Project ID.

$ export PROJECT_ID="$(gcloud config get-value project -q)"
$ env | grep PROJECT_ID


Build a Docker image

docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
docker images


2) Upload the container image

Push Docker image to Google Container Registry (GCR)

gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1


3) Run your container locally (optional)

Run the Docker container on the local machine

$ docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1
$ curl http://localhost:8080
Hello, World!
Version: 1.0.0
Hostname: 40e30cb3f577


4) Create a container cluster

gcloud container clusters create hello-cluster --num-nodes=3 --machine-type=f1-micro
gcloud compute instances list


5) Deploy your application

kubectl run hello-web --image=gcr.io/${PROJECT_ID}/hello-app:v1 --port 8080
kubectl get pods -o wide


Here we can see we have only one pod running on one of the GCE instances.

We can even SSH into the node gke-hello-cluster-default-pool-5446a576-p2k6 to view the container running inside the node.

gcloud compute ssh gke-hello-cluster-default-pool-5446a576-p2k6

And now we can search for the container running the hello-app application in the node.

sudo docker ps | grep hello-app

We can see that the node is running a container for the hello-app. The Container ID is 6c48090040ff.


6) Expose your application to the Internet

kubectl expose deployment hello-web --type=LoadBalancer --port 80 --target-port 8080
kubectl get service

We will now view the hello-web application via a web browser.

Note that the host name displayed on the web browser hello-web-4238197917-n4jn1 is different from the host name displayed in step 5hello-web-3682218174-60fbr. This is because I terminated the pod and the Kubernetes system recreated a new pod. This is a good point to note – that pods are mortal. Once they get deleted they cannot be brought back. Instead, a replacement pod will be created.


7) Scale up your application

kubectl scale deployment hello-web --replicas=4
kubectl get deployment hello-web


kubectl get pods -o wide

Note again, that the pod names are different from what we observed earlier. This is because I tried the replica scaling command a few times. And with each time it increases or decreases the number of pods, pods get created or deleted. And you could end up with pods with different names but performing the exact same function.

We can observe the following:

gke-hello-cluster-default-pool-5446a576-p2k6 has two pods (containers)
gke-hello-cluster-default-pool-5446a576-c81c has one pod
gke-hello-cluster-default-pool-5446a576-cn2c has one pod

And if we SSH into node gke-hello-cluster-default-pool-5446a576-p2k6, we can see the two containers inside the node.


8) Deploy a new version of your app

We will now build a new image. We can start by editing the main.go file in the hello-app directory and change the string “Hello, World!” to “Hi, Planet earth!”.

And then we build the image, push it to the registry and instruct Kubernetes to deploy the new image across the cluster.

docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .
gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v2
kubectl set image deployment/hello-web hello-web=gcr.io/${PROJECT_ID}/hello-app:v2


And with this change you should be able to view the new web page text “Hi, Planet earth!” on your web browser.

Leave a Reply

Your email address will not be published. Required fields are marked *