Deploy a docker image on GCP Kubernetes Engine

1. Command line tool setup:

  • Install gcloud command line tool
  • Run gcloud init to login account

View current account: gcloud info

List accounts that’s been added: gcloud auth list or gcloud config configurations list

Switch to another account: gcloud config set account <name>@gmail.com

  • Install kubectl component gcloud components install kubectl

2. Package a sample web application into a Docker image.

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

View images: docker images

Run image: docker run —rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1

<host port>:<container’s port>

3. Upload the Docker image to Container Registry.

  1. Enable the Container Registry API
gcloud services enable containerregistry.googleapis.com
  1. Configure the Docker command-line tool to authenticate to Container Registry:
gcloud auth configure-docker
  1. Push the Docker image that you just built to Container Registry:
docker push gcr.io/${PROJECT_ID}/hello-app:v1

## Deploy the sample app to the cluster

In **cloud shell** Setup local shell’s kubectl:

gcloud container clusters get-credentials ${CLUSTER_NAME} --region=${region}

Check setup:

Log pods: kubectl get pods

kubectl create deployment hello-app —image=gcr.io/${PROJECT_ID}/hello-app:v1

kubectl scale deployment hello-app —replicas=3

kubectl autoscale deployment hello-app —cpu-percent=80 —min=1 —max=5

Or using UI: Click Deploy Button, and follow the instructions.

Expose the sample app to the internet.

  1. On UI, click actions->Expose.
  2. Set target port to application port.
  3. Service type set to load-balancer.
  4. Done

In service tab, External endpoints can be accessed via internet.

Deploy a new version of the sample app.

docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .
docker push gcr.io/${PROJECT_ID}/hello-app:v2

Rolling update:
In console UI:
Actions->Rolling update
Set new image url

Clean up

  1. Delete Service
  2. Delete cluster
  3. Delete images

References:

🏷 note
🏷 backend
🏷 gcp