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 9: Relational DB – Cloud SQL & Spanner with Terraform

Posted on April 16, 2022April 18, 2022 By nim No Comments on [Terraform] Lesson 9: Relational DB – Cloud SQL & Spanner with Terraform

Contents

  • 1) Cloud Spanner with Console
  • 2) Cloud Spanner with Terraform
  • 3) Cloud SQL – Console
  • 4) Cloud SQL with Terraform

1) Cloud Spanner with Console

Các bạn nhờ enable nhé!

Cloud Spanner is a managed, mission-critical, globally consistent and scalable relational database service.

Giờ click create

2) Cloud Spanner with Terraform

Giờ mình cung cấp code terraform
file main.tf

resource "google_spanner_instance" "spanner_tf" {
  name = "spannertf"
  config = "regional-asia-south1"
  display_name = "Spanner from TF"
  num_nodes = 1
  labels = {
    "env" = "learningtf"
  }
  
}


resource "google_spanner_database" "db1"{
    name = "db1"
    instance =  google_spanner_instance.spanner_tf.name
}

giờ bạn terraform init rổi đến apply

Và đương nhiên là chúng ta sẽ gặp lỗi:

Error creating Instance: googleapi: Error 403: Caller is missing IAM permission spanner.instances.create on resource projects/xxxxx

lại vào role kiểm tra và add thêm cho user IAM

giờ thực hiện apply thôi:

Console đã báo thành công
Lên UI kiểm tra.

Chỗ này nếu bạn chạy terraform destroy sẽ bị fail vì mình chưa set deletion_protection=false
Và sẽ bị lỗi bên dưới.
các từ tìm hiểu thêm vào nhé!

Error: cannot destroy instance without setting deletion_protection=false and running `terraform apply`

Còn hông lên UI của GCP xoá thôi.

3) Cloud SQL – Console

Ở đây mình chọn tạo mysql

Giờ chúng ta chọn RAM, CPU, storage, …

Chỗ này bạn chọn style
mỗi style sẽ có định sẵn 1 lượng ram, cpu,… khác nhau
If enabled, whenever you are nearing capacity, storage will be incrementally (and permanently) increased. Learn more
Đây là config network
Chúng ta view 1 chút sem backup có j?

Thôi chúng ta nhấn nut create thôi

Chờ thôi
DB đã tạo xong chúng ta có thể check thử qua IP public
gcloud sql connect mysql-nimtechnology-console --user=root --quiet

ERROR: (gcloud.sql.connect) PERMISSION_DENIED: Cloud SQL Admin API has not been used in project 90059718136 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=90059718136 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

Nếu bạn bị lỗi như ảnh thì click vào đường link trong thông báo.

Hiện thị như dưới là ngon.

GIờ gõ lại command nhé.

4) Cloud SQL with Terraform

resource "google_sql_database_instance" "mysql-from-tf" {
  name = "mysql-from-tf"
  deletion_protection = false
  region = "us-central1"
  
  settings {
    tier = "db-f1-micro"
  }

}

resource "google_sql_user" "myuser" {
  name = "ankit"
  password = "ankit@123"
  instance = google_sql_database_instance.mysql-from-tf.name
}

google_sql_database_instance

Tìm hiểu về tier:
(Required) The machine type to use. See tiers for more details and supported versions. Postgres supports only shared-core machine types, and custom machine types such as db-custom-2-13312. See the Custom Machine Type Documentation to learn about specifying custom machine types.

https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/tiers/list

https://codebeautify.org/jsonviewer

Bạn để ý rằng mỗi tier đã gắn sẵn với một mức RAM, type Database(mysql or sql or …), disk là bao nhiêu!

Ta chỉ cần chọn tier như thế nào là phù hợp
ví dụ mình chọn tier: db-f1-micro

chúng ta cũng đã hiểu tier là gì rồi thì terraform apply thôi

CHúng ta bị lỗi:
Error: Error, failed to create instance mysql-from-tf: googleapi: Error 403: The client is not authorized to make this request., notAuthorized

Giờ apply tiếp.

Đã tạo được mysql 5.6

Còn rất nhiều config chắc các bạn phải tự đọc

GCP - Google Cloud, Terraform

Post navigation

Previous Post: [Terraform] Lesson 8: Cloud PubSub with Terraform
Next Post: [Terraform] Lesson 10: NoSQL Database in GCP with Terraform

More Related Articles

[Terraform] Lesson 4: Google Compute Engine (VM – Virtual Machine) with Terraform GCP - Google Cloud
[Terraform] Error: Error acquiring the state lock AWS - Amazon Web Service
[AWS] Create EKS Cluster and EKS Node Groups in Public and Private Subnets AWS - Amazon Web Service
[Terraform] Lesson 3: GCP Networking with Terraform GCP - Google Cloud
[Terraform] Lesson 1: Terraform with Google Cloud 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

  • [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.