Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] Lesson 5: Google Cloud Run with Terraform

Posted on April 11, 2022May 2, 2022 By nim No Comments on [Terraform] Lesson 5: Google Cloud Run with Terraform

Contents

Toggle
  • 1) Cloud Run Provision using Cloud Console.
  • 2) Hands-on Cloud Run with Terraform
  • 2.1) Cloud Run with Terraform
    • 2.2) allow Public access
      • 2.2.1) Practice in UI
      • 2.2.2)Config terraform script
    • 2.3) Deploy new revision

1) Cloud Run Provision using Cloud Console.

Chỗ này sẽ cần 1 chút lưu ý cho các bạn mới

Get image from dockerhub public

Như bạn thấy thường dockerhub theo kiểu official thì chúng ta chỉ cần copy tên image là ok!

Bạn làm như mình thì thấy kết quả ở hình bên dưới
Lúc này UI sẽ tự động thêm cho bạn một image để chạy thử!

Get image from different hub that isn’t Dockerhub Official(hub.docker.com)

Mình ví dụ như mình sử dụng harbor

docker pull docker.nimtechnology.com/dockerhub/kennethreitz/httpbin:latest
Sẽ không được

Chúng ta cần push image lên gcr

Mình sẽ cho gửi bạn 1 link
https://console.cloud.google.com/gcr/images/google-samples/global/hello-app

Nếu bạn muốn chỉnh nhiều thứ hơn

Giờ click create thôi

Giờ chờ thôi
Giờ container cũng đã running

Nếu bạn có container của bạn có expose port http thì lúc này bạn cấu hình port là gì
tý chúng ta có thể truy cập bằng link như ở trên ảnh và có ssl luôn –> ngon

Đã hiển thị web ngon lành

Giờ bạn có nhu cầu deploy 1 verision mới cho app đó

thay đổi tag của image
Bạn nên bỏ chọn Serve this revision immediately
vì nếu bạn chọn khi app:2.0 running thì traffic chuyển toàn bổ lên new version.
App mới mà lỗi thì toang
Bạn thấy traffic ko vào app mới
Giờ mình chỉnh cân bằng tải.
Giờ mình chỉnh traffic 50% -> version1
50% -> version 2
Bạn nhấn liên tục và để ý nó sẽ chuyển verion 1 và version 2

Giờ xoá và qua tạo bằng terraform thôi:

2) Hands-on Cloud Run with Terraform

2.1) Cloud Run with Terraform

resource "google_cloud_run_service" "run-app-from-tf" {
  name = "run-app-from-tf"
  location = "asia-southeast1"
  
  template {
    spec {
      containers {
        image = "gcr.io/google-samples/hello-app:1.0"
      }
    }
  }
}

Giờ bạn gõ terraform init và terraform apply

và đương nhiên là chúng ta sẽ gặp lỗi
Error: Error creating Service: googleapi: Error 403: Permission ‘run.services.create‘ denied on resource ‘namespaces/terraform-gcp-346216/services/run-app-from-tf’ (or resource may not exist).

Giờ terraform apply tiếp

Chúng ta đã thấy tạo 1 Container
403 Forbidden

2.2) allow Public access

Chúng ta sẽ tìm hiểu link dưới:
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service_iam

  • role – (Required) The role that should be applied. Only one google_cloud_run_service_iam_binding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.
  • policy_data – (Required only by google_cloud_run_service_iam_policy) The policy data generated by a google_iam_policy data source.

2.2.1) Practice in UI

Bạn sẽ add như hình

2.2.2)Config terraform script

resource "google_cloud_run_service" "run-app-from-tf" {
  name = "run-app-from-tf"
  location = "asia-southeast1"
  
  template {
    spec {
      containers {
        image = "gcr.io/google-samples/hello-app:1.0"
        # image = "gcr.io/google-samples/hello-app:2.0"
      }
    }
  }
}


resource "google_cloud_run_service_iam_policy" "pub_access" {
  service = google_cloud_run_service.run-app-from-tf.name
  location = google_cloud_run_service.run-app-from-tf.location
  policy_data = data.google_iam_policy.pub-1.policy_data
}

data "google_iam_policy" "pub-1" {
  binding {
    role = "roles/run.invoker"
    members = [ "allUsers", ]
  }
}


Bạn cần đọc thêm về google_iam_policy

Ở trên mình tạo policy
cho phép tất cả các user với role run.invoker

Giờ apply tiếp

run đã ok!
Link truy cập đã sáng!

Giờ thì chắc chắn là truy cập được!

Ngon nghẻ

2.3) Deploy new revision

resource "google_cloud_run_service" "run-app-from-tf" {
  name = "run-app-from-tf"
  location = "asia-southeast1"
  
  template {
    spec {
      containers {
        # image = "gcr.io/google-samples/hello-app:1.0"
        image = "gcr.io/google-samples/hello-app:2.0"
      }
    }
  }
}


resource "google_cloud_run_service_iam_policy" "pub_access" {
  service = google_cloud_run_service.run-app-from-tf.name
  location = google_cloud_run_service.run-app-from-tf.location
  policy_data = data.google_iam_policy.pub-1.policy_data
}

data "google_iam_policy" "pub-1" {
  binding {
    role = "roles/run.invoker"
    members = [ "allUsers", ]
  }
}

Mình thực hiện thay đổi version của image và chạy terraform apply

terraform chạy ok.
Bạn thấy là traffic chuyển hoàn toàn sang new version

Vậy giờ chúng ta muốn cần bằng tải.

Bạn cần nhớ tên của các container để cấu hình cần bằng tải:

resource "google_cloud_run_service" "run-app-from-tf" {
  name = "run-app-from-tf"
  location = "asia-southeast1"
  
  template {
    spec {
      containers {
        # image = "gcr.io/google-samples/hello-app:1.0"
        image = "gcr.io/google-samples/hello-app:2.0"
      }
    }
  }

  traffic {
    revision_name = "run-app-from-tf-ldhlx"
    percent = 50
  }
  traffic {
    revision_name = "run-app-from-tf-qdkh6"
    percent = 50
  }
}


resource "google_cloud_run_service_iam_policy" "pub_access" {
  service = google_cloud_run_service.run-app-from-tf.name
  location = google_cloud_run_service.run-app-from-tf.location
  policy_data = data.google_iam_policy.pub-1.policy_data
}

data "google_iam_policy" "pub-1" {
  binding {
    role = "roles/run.invoker"
    members = [ "allUsers", ]
  }
}

Cuối cũng xong khi đã học xong thì đừng quên terraform destroy nhé

GCP - Google Cloud, Terraform

Post navigation

Previous Post: [Terraform] Lesson 4: Google Compute Engine (VM – Virtual Machine) with Terraform
Next Post: [Terraform] Lesson 6: Google Cloud Function with Terraform

More Related Articles

[DevSecOps] Scan Terraform DevSecOps
[GCP] How to delete payments, the visa card, the master cards come out of the Google Cloud flatform so easily GCP - Google Cloud
[Terraform] Lesson 9: Relational DB – Cloud SQL & Spanner with Terraform GCP - Google Cloud
[Terraform] How to create or public a module terraform on the public registry Terraform
[Terraform] Your query returned no results. Please change your search criteria and try again Terraform
[Terraform ] Using Terragrunt to provision AWS base on Terraform Module Terraform

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025
  • [Windows] Remove the process that consumes too much CPU. June 3, 2025
  • Deploying Web-Based File Managers: File Browser and KubeFileBrowser with Docker and Kubernetes June 3, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.