1) Cloud Run Provision using Cloud Console.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-208.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-209.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-210.png)
Get image from dockerhub public
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-211.png)
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!
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-212.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-213.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-214.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-227.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-222.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-223.png)
Giờ click create thôi
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-224.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-225.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-229.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-230.png)
Giờ bạn có nhu cầu deploy 1 verision mới cho app đó
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-231.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-232.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-233.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-234.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-235.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-236.png)
50% -> version 2
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-237.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-238.png)
Giờ xoá và qua tạo bằng terraform thôi:
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-226.png)
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).
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-240.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-241.png)
Giờ terraform apply tiếp
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-242.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-243.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-244.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-245.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-246.png)
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 onegoogle_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 bygoogle_cloud_run_service_iam_policy
) The policy data generated by agoogle_iam_policy
data source.
2.2.1) Practice in UI
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-247.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-248.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-249.png)
cho phép tất cả các user với role run.invoker
Giờ apply tiếp
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-250.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-251.png)
Giờ thì chắc chắn là truy cập được!
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-252.png)
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", ]
}
}
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-253.png)
Mình thực hiện thay đổi version của image và chạy terraform apply
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-254.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-255.png)
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:
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-256.png)
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", ] } }
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-257.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-258.png)
Cuối cũng xong khi đã học xong thì đừng quên terraform destroy nhé