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
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Jquery & JavaScript
    • Git
    • Selenium
  • Log & Monitor
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
  • 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

  • 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

[Terraform] Lesson 7: Cloud BigQuery with Terraform GCP - Google Cloud
[Terraform] Lesson 9: Relational DB – Cloud SQL & Spanner with Terraform GCP - Google Cloud
[Terraform] Error: Error acquiring the state lock AWS - Amazon Web Service
[Terraform] Lesson 4: Google Compute Engine (VM – Virtual Machine) with Terraform GCP - Google Cloud
[Terraform] Lesson 3: GCP Networking with Terraform GCP - Google Cloud
[Terraform] – Terraform Beginner – Lesson 2: Working with Terraform. AWS - Amazon Web Service

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

  • [Argocd/Vault] Integrate Vault inside Argocd by the plugin July 1, 2022
  • [Vault] Using Service Acount of Kubernetes to login Vault system. June 28, 2022
  • Protected: My Assignment  June 24, 2022
  • [Spinnaker] Spinnaker writes too many logs – Reduce spinnaker log level June 22, 2022
  • [Jenkins] Jobs will be created automatically by Jenkins Job Builder June 20, 2022

Archives

  • 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
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Jenkins
    • Spinnaker
  • Coding
    • Git
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2022 NimTechnology.