Skip to content

Latest commit

 

History

History
159 lines (149 loc) · 4.18 KB

File metadata and controls

159 lines (149 loc) · 4.18 KB

GSP663 - Deploy, Scale, and Update Your Website on Google Kubernetes Engine

Task 1. Create a GKE cluster

  1. enable the Kubernetes Engine API
    gcloud services enable container.googleapis.com
  2. buat cluster GKE dengan nama fancy-cluster dengan 3 node
    gcloud container clusters create fancy-cluster --num-nodes 3
  3. cek cluster yang sudah dibuat
    gcloud compute instances list
  4. cek cluster yang sudah dibuat juga bisa dengan menuju ke console GCP > Kubernetes Engine > Clusters

Task 2. Clone source repository

  1. clone repository
    cd ~
    git clone https://github.com/googlecodelabs/monolith-to-microservices.git
  2. install dependencies
    cd ~/monolith-to-microservices
    ./setup.sh
  3. pastikan menggunakan versi npm terbaru
    nvm install --lts
  4. jalankan aplikasi
    cd ~/monolith-to-microservices/monolith
    npm start
  5. lihat hasilnya di browser dengan membuka web preview > preview on port 8080

Task 3. Create Docker container with Cloud Build

  1. pastikan cloud build API sudah di enable
    gcloud services enable cloudbuild.googleapis.com
  2. build image dengan cloud build
    cd ~/monolith-to-microservices/monolith
    gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0 .

Task 4. Deploy container to GKE

  • deploy container ke GKE
    kubectl create deployment monolith --image=gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0

Verify deployment

  • cek deployment

    kubectl get all
    # Show pods
    kubectl get pods
    # Show deployments
    kubectl get deployments
    # Show replica sets
    kubectl get rs
    #You can also combine them
    kubectl get pods,deployments
    
  • bisa juga Navigation menu > Kubernetes Engine > Workloads.

  • delete pods

    kubectl delete pod/<POD_NAME>

Task 5. Expose GKE deployment

  • expose deployment
    kubectl expose deployment monolith --type=LoadBalancer --port 80 --target-port 8080

Accessing the service

  • cek service
    kubectl get service

Task 6. Scale GKE deployment

  • scale deployment dengan 3 replica
    kubectl scale deployment monolith --replicas=3
  • cek deployment
    kubectl get all

Task 7. Make changes to the website

  1. edit file
    cd ~/monolith-to-microservices/react-app/src/pages/Home
    mv index.js.new index.js
  2. rebuild app
    cd ~/monolith-to-microservices/react-app
    npm run build:monolith
  3. build image
    cd ~/monolith-to-microservices/monolith
    gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0 .

Task 8. Update website with zero downtime

  • update deployment
    kubectl set image deployment/monolith monolith=gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0
  • cek deployment
    kubectl get pods
  • jalankan app
    npm start

Task 9. Cleanup

  1. delete git repository
    rm -rf ~/monolith-to-microservices
  2. delete google container registry images
    # Delete the container image for version 1.0.0 of the monolith
    gcloud container images delete gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:1.0.0 --quiet
    # Delete the container image for version 2.0.0 of the monolith
    gcloud container images delete gcr.io/${GOOGLE_CLOUD_PROJECT}/monolith:2.0.0 --quiet
  3. Delete Google Cloud Build artifacts from Google Cloud Storage
    # The following command will take all source archives from all builds and delete them from cloud storage
    # Run this command to print all sources:
    # gcloud builds list | awk 'NR > 1 {print $4}'
    gcloud builds list | grep 'SOURCE' | cut -d ' ' -f2 | while read line; do gsutil rm $line; done
  4. Delete GKE Service:
    kubectl delete service monolith
    kubectl delete deployment monolith
  5. Delete GKE Cluster:
    gcloud container clusters delete fancy-cluster