Deploy a docker image on GCP Kubernetes Engine
Posted on February 20, 2020
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
orgcloud 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.
- Enable the Container Registry API
gcloud services enable containerregistry.googleapis.com
- Configure the Docker command-line tool to authenticate to Container Registry:
gcloud auth configure-docker
- 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.
- On UI, click actions->Expose.
- Set target port to application port.
- Service type set to load-balancer.
- 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
- Delete Service
- Delete cluster
- Delete images