🌐
Cloud DNSの使い方
Google Cloud DNSの基本的な使い方について説明します。
1. Cloud DNSの基本概念
2. パブリックゾーンの作成と管理
# パブリックゾーンの作成
resource "google_dns_managed_zone" "public_zone" {
name = "my-public-zone"
dns_name = "example.com." # 末尾のドットを忘れずに
description = "Public DNS zone for example.com"
# パブリックゾーンの場合はvisibilityの指定は不要
}
# record.tf
resource "google_dns_record_set" "website" {
name = "www.example.com."
managed_zone = google_dns_managed_zone.public_zone.name
type = "A"
ttl = 300
rrdatas = ["203.0.113.10"] # IPアドレス
}
# cname.tf
resource "google_dns_record_set" "cname" {
name = "blog.example.com."
managed_zone = google_dns_managed_zone.public_zone.name
type = "CNAME"
ttl = 300
rrdatas = ["www.example.com."]
}
3. プライベートゾーンの作成と管理
# VPCネットワークの作成
resource "google_compute_network" "private_network" {
name = "private-network"
auto_create_subnetworks = false
}
# dns_zone.tf
resource "google_dns_managed_zone" "private_zone" {
name = "private-zone"
dns_name = "internal.example."
description = "Private DNS zone"
visibility = "private"
private_visibility_config {
networks {
network_url = google_compute_network.private_network.id
}
}
}
# 内部サービス用のAレコード
resource "google_dns_record_set" "internal_service" {
name = "service.internal.example."
managed_zone = google_dns_managed_zone.private_zone.name
type = "A"
ttl = 300
rrdatas = ["10.0.0.10"] # 内部IPアドレス
}
4. よくある使用例
- Webサイトのドメイン設定
# Webサイトのドメイン設定
resource "google_dns_record_set" "website" {
name = "www.example.com."
managed_zone = google_dns_managed_zone.public_zone.name
type = "A"
ttl = 300
# Cloud Load BalancerのグローバルIPを指定
rrdatas = [google_compute_global_address.lb_ip.address]
}
# Apexドメイン(ルートドメイン)の設定
resource "google_dns_record_set" "apex" {
name = "example.com."
managed_zone = google_dns_managed_zone.public_zone.name
type = "A"
ttl = 300
rrdatas = [google_compute_global_address.lb_ip.address]
}
- メールサーバーの設定
# MXレコードの設定
resource "google_dns_record_set" "mx" {
name = "example.com."
managed_zone = google_dns_managed_zone.public_zone.name
type = "MX"
ttl = 3600
rrdatas = [
"1 aspmx.l.google.com.",
"5 alt1.aspmx.l.google.com.",
"5 alt2.aspmx.l.google.com."
]
}
# SPFレコードの設定
resource "google_dns_record_set" "spf" {
name = "example.com."
managed_zone = google_dns_managed_zone.public_zone.name
type = "TXT"
ttl = 3600
rrdatas = ["\"v=spf1 include:_spf.google.com ~all\""]
}
- 内部サービスディスカバリー
# マイクロサービス用の内部DNS
resource "google_dns_record_set" "services" {
for_each = {
api = "10.0.1.10"
cache = "10.0.1.11"
db = "10.0.1.12"
}
name = "${each.key}.service.internal."
managed_zone = google_dns_managed_zone.private_zone.name
type = "A"
ttl = 300
rrdatas = [each.value]
}
5. DNS設定の検証
# ゾーンの一覧表示
gcloud dns managed-zones list
# レコードセットの表示
gcloud dns record-sets list \
--zone=my-public-zone
# DNSの名前解決テスト
gcloud dns record-sets list \
--zone=my-public-zone \
--name=www.example.com.
# プライベートDNSの確認
# GCEインスタンス内から
nslookup service.internal.example
6. セキュリティベストプラクティス
# DNSSEC の設定
resource "google_dns_managed_zone" "secure_zone" {
name = "secure-zone"
dns_name = "secure.example."
dnssec_config {
state = "on"
default_key_specs {
algorithm = "rsasha256"
key_length = 2048
key_type = "keySigning"
kind = "dns#dnsKeySpec"
}
}
}
# Cloud DNSポリシーの設定
resource "google_dns_policy" "default" {
name = "dns-policy"
enable_inbound_forwarding = true
networks {
network_url = google_compute_network.private_network.id
}
}
7. 監視とログ記録
監視項目:
- クエリ数
- レイテンシー
- エラーレート
- DNSSEC検証失敗
ログ設定:
- Cloud Monitoring
- Cloud Logging
- アラート設定
これらの設定を適切に組み合わせることで、効果的なDNS管理が可能になります。
Discussion