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
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • 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

  • 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 11: Create GKE (kubernetes) on Google with Terraform GCP - Google Cloud
[Terraform] Error: Error acquiring the state lock AWS - Amazon Web Service
[Terraform] Lesson 6: Google Cloud Function with Terraform GCP - Google Cloud
[Terraform] Lesson 8: Cloud PubSub with Terraform GCP - Google Cloud
[Terraform] How to create or public a module terraform on the public registry Terraform
[Terraform/Init] Starting to learn Terraform easily. 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

  • [Prometheus/Grafana] Install Prometheus and Grafana on ubuntu. March 27, 2023
  • [Kong Gateway] WebSocket connection failed March 26, 2023
  • [Nextcloud] Can’t download files to have a size bigger than 2Gi on NextCloud – RaspBerry March 24, 2023
  • [Datadog] Using DataDog to monitor all services on kubernetes March 19, 2023
  • [Metrics Server] Failed to make webhook authorizer request: the server could not find the requested resource March 17, 2023

Archives

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

Copyright © 2023 NimTechnology.