教學課程:使用 Cloud DNS 設定網域


本教學課程將逐步說明註冊網域、設定網路伺服器範例,以及使用 Cloud DNS 將網域網址指向伺服器的程序。

如果您是第一次使用 Terraform 為 Google Cloud建立資源,請參閱「開始使用 Terraform」。

目標

本教學課程將示範如何完成下列工作:

  • 使用 Cloud Domains 註冊網域名稱
  • 建立 Compute Engine 虛擬機器 (VM) 執行個體
  • 執行基本 Apache 網路伺服器
  • 使用 Cloud DNS 設定網域
  • 更新名稱伺服器
  • 驗證設定

費用

註冊網域名稱需要付費。 如要瞭解 Cloud Domains 的定價,請參閱「Cloud Domains 定價」。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. 啟用 Compute Engine API。

    啟用 API

註冊網域名稱

如果您已註冊網域,請略過本節。

您可以使用 Cloud Domains 註冊網域。在「開始前」一節中,您已為網域建立了帳單帳戶,Cloud Domains 可讓您使用該帳戶。如需使用 Cloud Domains 註冊網域的操作說明,請參閱「註冊網域」。

建立虛擬機器執行個體

主控台

如要在 Compute Engine 中建立 Linux 虛擬機器 (VM) 執行個體,請按照下列操作說明操作:

  1. In the Google Cloud console, go to the Create an instance page.

    Go to Create an instance

  2. In the Boot disk section, click Change to begin configuring your boot disk.
  3. 在「Public images」分頁上,選擇「Debian GNU/Linux」
  4. 按一下 [Select] (選取)
  5. 在「防火牆」專區中,選取 「允許 HTTP 流量」
  6. 如要建立 VM,請按一下「建立」

啟動執行個體會花費一些時間。當執行個體準備就緒時,就會列在「VM instances」(VM 執行個體) 頁面中,並顯示綠色的狀態圖示。

連線至執行個體

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.

    SSH button next to instance name.

您現在擁有一個終端機視窗,可讓您與 Linux 執行個體互動。

詳情請參閱「使用 Linux VM 的快速入門導覽課程」。

執行基本 Apache 網路伺服器

在本節中,您將執行及測試 Apache 網路伺服器。

  1. 在「SSH」視窗中使用 Debian 套件管理工具安裝 apache2 套件。

     sudo apt-get update && sudo apt-get install apache2 -y
     

    安裝 Apache 之後,作業系統會自動啟動 Apache 伺服器。

  2. 使用下列指令覆寫 Apache 網路伺服器的預設網頁:

     echo "<!doctype html><html><body><h1>Hello World!</h1></body></html>" 
    | sudo tee /var/www/html/index.html

測試伺服器

測試執行個體是否是以外部 IP 供應流量。

主控台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 在「External IP」(外部 IP) 欄中,複製執行個體的外部 IP 位址。
  3. 在瀏覽器中,前往 http://[EXTERNAL_IP]。請勿使用 https 進行連線,因為伺服器會傳回 Connection Refused 錯誤。

您現在應該會看到「Hello World!」頁面。

詳情請參閱「執行基本 Apache 網路伺服器」一節。

使用 Cloud DNS 設定網域

如果您要從現有供應商遷移,可以將現有區域匯入 Cloud DNS。如需操作說明,請參閱「從現有的供應商匯出 DNS 設定」。否則,請按照下列步驟建立新的公開區。

主控台

  1. 前往 Google Cloud 控制台的「Create a DNS zone」頁面。

    前往「建立 DNS 區域」

  2. 在「可用區類型」中,選取「公開」

  3. 在「Zone name」(可用區名稱) 輸入 my-new-zone

  4. 針對「DNS name」(DNS 名稱),請使用您註冊的網域名稱 (例如 example.com) 輸入區域的 DNS 名稱後置字串。

  5. 如為 DNSSEC,請確認已選取 Off 設定。

  6. 按一下「建立」,即可建立填入 NS 和 SOA 記錄的可用區。

  7. 如要將已註冊的網域名稱指向託管伺服器的 IP 位址,您必須在可用區中新增 A 記錄:

    1. 在「Zone details」(可用區詳細資料) 頁面中,按一下「Add Standard」(新增標準)
    2. 從「Resource Record Type」(資源記錄類型) 選單中選取「A」
    3. 在「IPv4 位址」中,輸入執行個體的外部 IP 位址。
    4. 點選「Create」(建立),即可為可用區建立 A 記錄。
  8. 選用:新增 CNAME 記錄,以便在網域名稱中加入前置字串 (例如 www.):

    1. 按一下「新增標準」
    2. 在「DNS 名稱」欄位中,為網域新增前置字串 www
    3. 在「Resource Record Type」中,選擇「CNAME」
    4. 在「Canonical name」(正式名稱) 中輸入網域名稱,後面加上半形句號 (例如 example.com.)。
    5. 按一下 [建立]。

更新名稱伺服器

如要在 Cloud Domains 中更新名稱伺服器,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Cloud Domains」頁面。

    前往「Cloud Domains」頁面

  2. 按一下要編輯的網域名稱。您也可以按一下網域名稱旁的 「更多」,查看編輯選單。

  3. 如要編輯 DNS 詳細資料,請按一下「編輯 DNS 詳細資料」

  4. 選取「使用 Cloud DNS (建議選項)」

  5. 在「Cloud DNS 可用區」清單中選取「my-new-zone」。

  6. 按一下 [儲存]

Terraform

您可以使用 Terraform 資源建立虛擬機器 (VM) 執行個體、執行 Apache 網路伺服器、使用 Cloud DNS 設定網域,以及更新名稱伺服器。

您也可以使用 Terraform 模組,透過公開、私人、對等或轉送區域設定 Cloud DNS。如要進一步瞭解區域,請參閱「 DNS 區域總覽」。

# to setup a web-server

resource "random_id" "rnd" {
  byte_length = 4
}

resource "google_compute_instance" "default" {
  name         = "dns-compute-instance"
  machine_type = "g1-small"
  zone         = "us-central1-b"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Ephemeral public IP
    }
  }
  metadata_startup_script = <<-EOF
  sudo apt-get update && \
  sudo apt-get install apache2 -y && \
  echo "<!doctype html><html><body><h1>Hello World!</h1></body></html>" > /var/www/html/index.html
  EOF
}

# to allow http traffic
resource "google_compute_firewall" "default" {
  name    = "allow-http-traffic"
  network = "default"
  allow {
    ports    = ["80"]
    protocol = "tcp"
  }
  source_ranges = ["0.0.0.0/0"]
}

# to create a DNS zone
resource "google_dns_managed_zone" "default" {
  name          = "example-zone-googlecloudexample"
  dns_name      = "example-${random_id.rnd.hex}.com."
  description   = "Example DNS zone"
  force_destroy = "true"
}

# to register web-server's ip address in DNS
resource "google_dns_record_set" "default" {
  name         = google_dns_managed_zone.default.dns_name
  managed_zone = google_dns_managed_zone.default.name
  type         = "A"
  ttl          = 300
  rrdatas = [
    google_compute_instance.default.network_interface[0].access_config[0].nat_ip
  ]
}

驗證設定

如要確認設定是否正常運作,請更新名稱伺服器後,前往您的網域名稱 (例如 example.com)。網域應會解析為您的 IP 位址,並指向您在執行基本 Apache 網路伺服器中建立的「Hello World!」頁面。

如要確認設定是否正確,您也可以在終端機視窗中執行 dig +trace example.com 指令。將 example.com 替換成註冊的網域名稱。

dig +trace example.com

輸出內容結尾應包含下列內容:IP_ADDRESS 是網路伺服器的 IP 位址。

example.com.    300 IN  A   IP_ADDRESS
;; Received 62 bytes from 216.239.34.109#53(ns-cloud-d2.googledomains.com) in 62 ms

如要確認變更是否成功,請執行下列指令:

example.com IN NS <your Cloud DNS name servers>

等待 DNS 傳播完成後,您也可以執行 nslookup 指令來驗證設定:

nslookup example.com

輸出內容應包含下列內容:IP_ADDRESS 是網路伺服器的 IP 位址。

Server:     127.0.0.1
Address:    127.0.0.1#53

Non-authoritative answer:
Name:   example.com
Address: IP_ADDRESS

清除所用資源

主控台

  1. 前往 Google Cloud 控制台的「Cloud DNS 區域」頁面。

    前往 Cloud DNS 區域

  2. 按一下區域名稱 (例如 my-new-zone) 即可前往「Zone details」(區域詳細資料) 頁面。

  3. 選取您建立的 A 和 CNAME 記錄。

  4. 按一下「Delete record sets」

  5. 如要刪除儲存區,請按一下儲存區名稱 my-new-zone 旁的「刪除儲存區」

  6. 前往「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  7. 選取要刪除的執行個體。

  8. 在執行個體所在資料列的「更多選單中,按一下「刪除」

後續步驟