In this hands-on you will deploy a simple container application to Google Kubernetes Engine
(GKE
) using GitHub Actions.
-
Create a cloud project in console.cloud.google.com and note the project-id (not the name!).
-
Create a fork of this repository (if you haven't done so) by clicking the
Fork
button at the top right corner. -
Open setup-gke.sh and replace the value of
GKE_PROJECT
with your project-id:export GKE_PROJECT=valid-octagon-330106
Also replace
xyz
with for example your username in the names of the cluster and the service:export GKE_CLUSTER=xyz-demo-cluster export GKE_APP_NAME=xyz-demo-shop export GKE_SERVICE=xyz-service export GKE_SERVICE_ACCOUNT=xyz-serviceaccount export GKE_DEPLOYMENT_NAME=xyz-demo-shop-deployment
Adjust region and zone with the values of your choice. You can get a list of available regions with the command
$ gcloud compute regions list
and a list of the zones with$ gcloud compute zones list
. You can execute tis commands in the cloud shell in step 3:export GKE_REGION=europe-west3 export GKE_ZONE=europe-west3-a
Commit and push the changes to your forked repository.
-
Open Google Cloud Shell and authenticate.
-
Clone your forked repository to the cloud shell. Change directory and mark the setup script as executable:
$ git clone https://github.com/<github-handle>/AccelerateDevOps.git $ cd AccelerateDevOps/ch9_release/ $ chmod +x setup-gke.sh
-
Execute the script setup-gke.sh:
$ ./setup-gke.sh
The script creates a cluster, artifact repository, and service account. It than builds the docker container, uploads it to the artifact registry, and deploys it to the cluster.
The script executes
kubectl get service
- but probably your IP takes some time to get provisioned. Wait some time and execute the command again until you see an IP address:Open the IP in browser and verify that the container is running successfully:
The script also reads the
key.json
. You could run the following command again if you missed it:$ cat key.json | base64
Copy the base64 encoded content of the file to your clipboard.
-
Add a new repository secret with the name
GKE_SA_KEY
and the value from key.json (past it from your clipboard or go back to the console to copy it). Add another secretGKE_PROJECT
with the project-id from step 1. -
Edit Deploy-GKE.yml in
.github/workflows/
and replace the values underenv:
with the same values in step 3:
env:
GKE_PROJECT: ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER: xyz-demo-cluster
GKE_APP_NAME: xyz-demo-shop
GKE_SERVICE: xyz-service
GKE_SERVICE_ACCOUNT: xyz-serviceaccount
GKE_DEPLOYMENT_NAME: xyz-demo-shop-deployment
GKE_REGION: europe-west3
GKE_ZONE: europe-west3-a
-
Have a look at Service.yml and Deployment.yml. You don't have to modify the files - just take a look at them as they will perform the deployment. The environment variables will be replaced during the deployment.
-
Run the workflow Deploy-GKE.yml under
Actions
|Deploy-GKE
|Run workflow
:The workflow will build the docker container and deploy it to your cluster.
-
Clean up your resources by executing the destroy-gke.sh script:
$ chmod +x destroy-gke.sh $ ./destroy-gke.sh