您在 Azure Kubernetes Service (AKS) 叢集中建立負載平衡器資源時,指派給這個資源的公用 IP 位址僅適用於該資源的生命週期。 如果您刪除 Kubernetes 服務,相關聯的負載平衡器和 IP 位址也會一併刪除。 如果您想要針對已重新部署的 Kubernetes 服務指派特定的 IP 位址或保留 IP 位址,您可以建立並使用靜態公用 IP 位址。
此文章示範如何建立靜態公用 IP 位址,並將它指派給您的 Kubernetes 服務。
開始之前
- 您必須安裝並設定 Azure CLI 2.0.59 版或更新版本。 執行
az --version
以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure CLI。 - 本文說明如何搭配使用標準 SKU IP 與標準 SKU 負載平衡器。 如需詳細資訊,請參閱 Azure 中的 IP 位址類型及配置方法。
建立 AKS 叢集
使用
az group create
命令建立 Azure 資源群組。az group create --name myNetworkResourceGroup --location eastus
使用
az aks create
命令建立 AKS 叢集。az aks create --name myAKSCluster --resource-group myNetworkResourceGroup --generate-ssh-keys
建立靜態 IP 位址
使用
az aks show
命令取得節點資源群組的名稱,並查詢nodeResourceGroup
屬性。az aks show --name myAKSCluster --resource-group myNetworkResourceGroup --query nodeResourceGroup -o tsv
使用
az network public ip create
命令在節點資源群組中建立靜態公用 IP 位址。az network public-ip create \ --resource-group <node resource group name> \ --name myAKSPublicIP \ --sku Standard \ --allocation-method static
注意
如果您在 AKS 叢集中使用基本 SKU 負載平衡器,請在定義公用 IP 時針對
--sku
參數使用 Basic。 只有基本 SKU IP 可用於基本 SKU 負載平衡器,且只有標準 SKU IP 可用於標準 SKU 負載平衡器。使用
az network public-ip list
命令取得靜態公用 IP 位址。 指定節點資源群組的名稱以及您建立的公用 IP 位址,並查詢ipAddress
。az network public-ip show --resource-group <node resource group name> --name myAKSPublicIP --query ipAddress --output tsv
使用靜態 IP 位址建立服務
首先,判斷 AKS 叢集所使用的受控識別類型、系統指派或使用者指派。 如果您不確定,請呼叫 az aks show 命令,並查詢身分識別的 type 屬性。
az aks show \ --name myAKSCluster \ --resource-group myResourceGroup \ --query identity.type \ --output tsv
如果叢集使用受控識別,則 type 屬性的值會是 SystemAssigned 或 UserAssigned。
如果叢集使用服務主體,則 type 屬性的值會是 Null。 請考慮升級叢集以使用受控識別。
如果您的 AKS 叢集使用系統指派的受控識別,請查詢受控識別的主體識別碼,如下所示:
# Get the principal ID for a system-assigned managed identity. CLIENT_ID=$(az aks show \ --name myAKSCluster \ --resource-group myNetworkResourceGroup \ --query identity.principalId \ --output tsv)
如果您的 AKS 叢集使用使用者指派的受控識別,則主體識別碼會是 Null。 請改為查詢使用者指派受控識別的用戶端識別碼:
# Get the client ID for a user-assigned managed identity. CLIENT_ID=$(az aks show \ --name myAKSCluster \ --resource-group myNetworkResourceGroup \ --query identity.userAssignedIdentities.*.clientId \ --output tsv
呼叫
az role assignment create
命令,為公用 IP 資源群組的 AKS 叢集所使用的受控識別指派委派權限。# Get the resource ID for the node resource group. RG_SCOPE=$(az group show \ --name <node resource group> \ --query id \ --output tsv) # Assign the Network Contributor role to the managed identity, # scoped to the node resource group. az role assignment create \ --assignee ${CLIENT_ID} \ --role "Network Contributor" \ --scope ${RG_SCOPE}
重要
如果自訂輸出 IP,請確定您的叢集身分識別具有輸出公用 IP 和此輸入公用 IP 的存取權限。
建立名為
load-balancer-service.yaml
的檔案,並在下列 YAML 檔案的內容中複製,提供您在上一個步驟中建立的專屬公用 IP 位址和節點資源群組名稱。重要
將
loadBalancerIP
屬性新增到負載平衡器 YAML 資訊清單的功能,已在上游 Kube 推出後淘汰。 雖然目前的使用量將維持不變,且現有服務繼續運作,不需修改,但我們仍強烈建議設定服務註釋。 若要設定服務註釋,您可以對於公用 IP 名稱使用service.beta.kubernetes.io/azure-pip-name
,也可以對於 IPv4 位址使用service.beta.kubernetes.io/azure-load-balancer-ipv4
並對於 IPv6 位址使用service.beta.kubernetes.io/azure-load-balancer-ipv6
,如範例 YAML 所示。apiVersion: v1 kind: Service metadata: annotations: service.beta.kubernetes.io/azure-load-balancer-resource-group: <node resource group name> service.beta.kubernetes.io/azure-pip-name: myAKSPublicIP name: azure-load-balancer spec: type: LoadBalancer ports: - port: 80 selector: app: azure-load-balancer
注意
新增
service.beta.kubernetes.io/azure-pip-name
註釋可確保最有效率的 LoadBalancer 建立,而且強烈建議您避免潛在的節流。使用
service.beta.kubernetes.io/azure-dns-label-name
服務註釋,將公開的 DNS 標籤設定為服務。 這會使用 Azure 的公用 DNS 伺服器和頂層網域,為您的服務發佈完整網域名稱 (FQDN)。 註釋值在 Azure 位置中必須是唯一值,因此建議使用足夠合格的標籤。 Azure 會自動將所選位置中的預設尾碼 (例如<location>.cloudapp.azure.com
) 附加至您提供的名稱,以建立 FQDN。注意
如果要在您自己的網域上發佈服務,請參閱 Azure DNS 和 external-dns 專案。
apiVersion: v1 kind: Service metadata: annotations: service.beta.kubernetes.io/azure-load-balancer-resource-group: <node resource group name> service.beta.kubernetes.io/azure-pip-name: myAKSPublicIP service.beta.kubernetes.io/azure-dns-label-name: <unique-service-label> name: azure-load-balancer spec: type: LoadBalancer ports: - port: 80 selector: app: azure-load-balancer
使用
kubectl apply
命令建立服務和部署。kubectl apply -f load-balancer-service.yaml
若要查看負載平衡器的 DNS 標籤,請使用
kubectl describe service
命令。kubectl describe service azure-load-balancer
DNS 標籤會列在
Annotations
下方,如下列緊縮範例輸出所示:Name: azure-load-balancer Namespace: default Labels: <none> Annotations: service.beta.kuberenetes.io/azure-dns-label-name: <unique-service-label>
疑難排解
如果在 Kube 服務資訊清單的 loadBalancerIP
屬性中定義的 IP 位址不存在,或尚未建立在節點資源群組中,且未設定其他任何委派,則負載平衡器服務的建立將會失敗。 若要進行疑難排解,請使用 kubectl describe
命令檢閱服務建立事件。 提供 YAML 資訊清單中所指定的服務名稱,如下列範例所示:
kubectl describe service azure-load-balancer
輸出會顯示 Kube 服務資源的相關資訊。 下列範例輸出顯示 Events
: "user supplied IP address was not found
" 中的 Warning
。在此案例中,請確認您已在節點資源群組中建立靜態公用 IP 位址,以及 Kube 服務資訊清單中所指定的 IP 位址正確無誤。
Name: azure-load-balancer
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=azure-load-balancer
Type: LoadBalancer
IP: 10.0.18.125
IP: 40.121.183.52
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 32582/TCP
Endpoints: <none>
Session Affinity: None
External Traffic Policy: Cluster
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CreatingLoadBalancer 7s (x2 over 22s) service-controller Creating load balancer
Warning CreatingLoadBalancerFailed 6s (x2 over 12s) service-controller Error creating load balancer (will retry): Failed to create load balancer for service default/azure-load-balancer: user supplied IP Address 40.121.183.52 was not found
下一步
若要進一步控制應用程式的網路流量,請使用 AKS 的應用程式路由附加元件。 如需應用程式路由附加元件的詳細資訊,請參閱使用應用程式路由附加元件的受控 NGINX 輸入 (部分機器翻譯)。