Skip to content

Latest commit

 

History

History
102 lines (68 loc) · 4.7 KB

Deploy_to_GKE.md

File metadata and controls

102 lines (68 loc) · 4.7 KB

Hands-on: Deploy to Google Kubernetes Engine (GKE)

In this hands-on you will deploy a simple container application to Google Kubernetes Engine (GKE) using GitHub Actions.

  1. Create a cloud project in console.cloud.google.com and note the project-id (not the name!).

  2. Create a fork of this repository (if you haven't done so) by clicking the Fork button at the top right corner.

  3. 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.

  4. Open Google Cloud Shell and authenticate.

  5. 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
  6. 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:

    01_get-service

    Open the IP in browser and verify that the container is running successfully:

    02_app-in-cluster

    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.

    03_key-json
  7. 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 secret GKE_PROJECT with the project-id from step 1.

  8. Edit Deploy-GKE.yml in .github/workflows/ and replace the values under env: 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
  1. 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.

  2. Run the workflow Deploy-GKE.yml under Actions | Deploy-GKE | Run workflow:

    04_run-workflow

    The workflow will build the docker container and deploy it to your cluster.

    05_success
  3. Clean up your resources by executing the destroy-gke.sh script:

    $ chmod +x destroy-gke.sh
    $ ./destroy-gke.sh