Azure AKS Outbound 流量最佳化 - 為什麼選擇 NAT Gateway 而不是 Load Balancer
前言
當部署 Azure Kubernetes Service (AKS) 叢集時,如何高效管理出站流量 (Outbound Traffic) 是影響系統穩定性、可擴展性和成本優化的關鍵因素。傳統上,AKS 會透過 Azure Load Balancer 來處理出站流量,但這種方式有諸多限制。因此,使用 Azure NAT Gateway 是更優秀的選擇。
為什麼 NAT Gateway 比 Load Balancer 更適合 AKS 出站流量?
1. 避免 SNAT Port 枯竭
當使用 Azure Load Balancer 作為 AKS 的出站流量處理方式時,會透過 Source Network Address Translation (SNAT) 來分配臨時埠 (Ephemeral Ports),但這些埠數量有限,對於高流量環境來說很容易 耗盡 (SNAT Exhaustion)。
2. 提供固定的 Outbound IP,增強安全性
透過 NAT Gateway,AKS 叢集的所有出站流量都會透過 單一的靜態 Public IP,使安全控管更加容易,可確保外部服務僅允許來自特定 IP 的請求。
3. 更佳的擴展性與效能
- 更高吞吐量: 支援高達 50 Gbps 的流量處理能力。
- 自動擴展: 無需手動調整,適用於動態擴展的 AKS 叢集。
4. 降低成本與簡化設定
使用 Azure Load Balancer 處理出站流量時,會產生額外的 SNAT 規則管理成本,且 Load Balancer 本身的收費也會隨著規模增加。相比之下,NAT Gateway 的計價方式更簡單。
Terraform 設定範例 - 如何在 AKS 上配置 NAT Gateway
1. 建立 NAT Gateway 的靜態 Public IP
resource "azurerm_public_ip" "natgw_ip" {
name = "natgw-pip"
location = var.location
resource_group_name = var.resource_group_name
allocation_method = "Static"
sku = "Standard"
}
2. 創建 NAT Gateway 並設置超時時間
resource "azurerm_nat_gateway" "natgw" {
name = "natgw"
location = var.location
resource_group_name = var.resource_group_name
sku_name = "Standard"
idle_timeout_in_minutes = 10
}
3. 連結 NAT Gateway 到 AKS Subnet
resource "azurerm_subnet_nat_gateway_association" "aks_subnet_natgw" {
subnet_id = var.aks_subnet_id
nat_gateway_id = azurerm_nat_gateway.natgw.id
}
4. 設定 AKS Network Profile
network_profile {
network_plugin = var.aks_network_plugin
outbound_type = "userAssignedNATGateway"
# NAT Gateway will handle outbound traffic, no need for outbound Load Balancer
}
結論
- 避免 SNAT Port 枯竭,提高穩定性。
- 提供固定的 Outbound Public IP,簡化安全控管。
- 提升擴展性與效能,支援高達 50 Gbps Outbound 流量。
- 降低維運成本,移除 Load Balancer Outbound 規則。
- 確保 AKS 設定 network_profile 正確使用 NAT Gateway。
如果您的 AKS 叢集在 高流量場景 下面臨 SNAT 枯竭 或 IP 變更問題,使用 NAT Gateway 是最佳解決方案。
留言
張貼留言