NoSQL 客户端服务器

+

架构

NoSQL 客户端服务器将配置并创建两个 Compute Engine 实例:

  • 服务器 - 将运行 MongoDB
  • 客户端 - 将运行与 MongoDB 通信的自定义 Go 应用,然后公开一个 API,以便您从 MongoDB 使用数据

开始使用

点击以下链接,在 Cloud Shell 中查看源代码的副本。进入该环境后,只需一个命令即可在项目中启动应用的工作副本。

在 Cloud Shell 中打开

在 GitHub 上查看源代码


NoSQL 客户端服务器组件

NoSQL 客户端-服务器架构使用一款关键产品。 以下内容重点介绍了该产品,并提供了更多信息,包括指向相关视频、产品文档和互动式演示文稿的链接。
视频 文档 演示
Compute Engine Compute Engine 是 Google Cloud 的虚拟化技术。借助它,您可以启动许多不同配置的虚拟机,以满足您各种计算需求。

脚本

安装脚本使用使用 go 和 Terraform CLI 工具编写的可执行文件,获取一个空项目并在其中安装应用。输出应为一个正常运行的应用和负载均衡 IP 地址的网址。

./main.tf

启用服务

默认情况下,Google Cloud 服务在项目中处于停用状态。如需使用本文中的任何解决方案,我们必须启用以下功能:

  • Compute Engine - 虚拟机和网络
variable "gcp_service_list" {
    description = "The list of apis necessary for the project"
    type        = list(string)
    default = [
        "compute.googleapis.com",
    ]
}

resource "google_project_service" "all" {
  for_each                   = toset(var.gcp_service_list)
  project                    = var.project_number
  service                    = each.key
  disable_dependent_services = false
  disable_on_destroy         = false
}

创建防火墙规则

创建一条规则,用于在防火墙上打开端口 80,然后将其应用于标记为 http-server 的任何计算机

resource "google_compute_firewall" "default-allow-http" {
  name    = "deploystack-allow-http"
  project = var.project_number
  network = "projects/${var.project_id}/global/networks/default"

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }

  source_ranges = ["0.0.0.0/0"]

  target_tags = ["http-server"]
}

创建服务器实例

启动一个将用作服务器的虚拟机,并提供 MongoDB

# Create Instances
resource "google_compute_instance" "server" {
  name         = "server"
  zone         = var.zone
  project      = var.project_id
  machine_type = "e2-standard-2"
  tags         = ["http-server"]


  boot_disk {
    auto_delete = true
    device_name = "server"
    initialize_params {
      image = "family/ubuntu-1804-lts"
      size  = 10
      type  = "pd-standard"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Ephemeral public IP
    }
  }

  metadata_startup_script = <<SCRIPT
    apt-get update
    apt-get install -y mongodb
    service mongodb stop
    sed -i 's/bind_ip = 127.0.0.1/bind_ip = 0.0.0.0/' /etc/mongodb.conf
    iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 27017
    service mongodb start
  SCRIPT
  depends_on              = [google_project_service.all]
}

创建客户端实例

启动一个将用作客户端的虚拟机,并使用 MongoDB 中的数据

resource "google_compute_instance" "client" {
  name         = "client"
  zone         = var.zone
  project      = var.project_id
  machine_type = "e2-standard-2"
  tags         = ["http-server", "https-server"]

  boot_disk {
    auto_delete = true
    device_name = "client"
    initialize_params {
      image = "family/ubuntu-1804-lts"
      size  = 10
      type  = "pd-standard"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral public IP
    }
  }

  metadata_startup_script = <<SCRIPT
    add-apt-repository ppa:longsleep/golang-backports -y && \
    apt update -y && \
    apt install golang-go -y
    mkdir /modcache
    mkdir /go
    mkdir /app && cd /app
    curl https://raw.githubusercontent.com/GoogleCloudPlatform/golang-samples/main/compute/quickstart/compute_quickstart_sample.go --output main.go
    go mod init exec
    GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache go mod tidy
    GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache go get -u 
    sed -i 's/mongoport = "80"/mongoport = "27017"/' /app/main.go
    echo "GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache HOST=${google_compute_instance.server.network_interface.0.network_ip} go run main.go"
    GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache HOST=${google_compute_instance.server.network_interface.0.network_ip} go run main.go &
  SCRIPT

  depends_on = [google_project_service.all]
}

总结

运行后,您现在应该有一个使用 MongoDB 的客户端-服务器 API 在两个 Compute Engine 实例上运行。此外,您应该拥有修改或扩展此解决方案以适应您的环境所需的所有代码。