使用應用程式路由附加元件設定 NGINX 輸入控制器以支援 Azure 私人 DNS 區域

本文示範如何設定 NGINX 輸入控制器以使用 Azure 內部負載平衡器,並設定私人 Azure DNS 區域,以啟用私人端點的 DNS 解析,以解析特定網域。

開始之前

連線至您的 AKS 叢集

如果要從本機電腦連線至 Kubernetes 叢集,應使用 Kubernetes 命令列用戶端 kubectl。 您可以使用 az aks install-cli 命令將其安裝於本機。 如果您使用 Azure Cloud Shell,則 kubectl 已安裝。

下列範例會使用 az aks get-credentials 命令,設定連線到 myResourceGroup 中名為 myAKSCluster叢集。

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

建立虛擬網路

若要將私人 DNS 區域發佈至您的虛擬網路,您必須指定允許解析區域內記錄的虛擬網路清單。 這些稱為 虛擬網路連結

下列範例會在 myResourceGroup 資源群組中建立名為 myAzureVNet 的虛擬網路,以及一個名為 mySubnet 的子網,以在特定地址前綴的 VNet 內建立。

az network vnet create \
  --name myAzureVNet \
  --resource-group myResourceGroup \
  --location eastus \
  --address-prefix 10.2.0.0/16 \
  --subnet-name mysubnet \
  --subnet-prefixes 10.2.0.0/24

建立 Azure 私人 DNS 區域

注意

您可以設定應用程式路由附加元件,針對輸入資源上定義的主機,在一或多個 Azure 全域和私人 DNS 區域上自動建立記錄。 所有全域 Azure DNS 區域和所有私人 Azure DNS 區域都必須位於相同的資源群組中。

您可以使用 az network private-dns zone create 命令來建立 DNS 區域,並指定要在其中建立的區域名稱和資源群組。 下列範例會在 myResourceGroup 資源群組中建立名為 private.contoso.comDNS 區域。

az network private-dns zone create --resource-group myResourceGoup -n private.contoso.com

您可以使用 az network private-dns link vnet create 命令,建立先前所建立 DNS 區域的虛擬網路連結。 下列範例會建立名為 myDNSLink 的連結至虛擬網路 myAzureVNet 的區域 private.contoso.com --registration-enabled包含參數來指定連結未啟用註冊。

az network private-dns link vnet create --resource-group myResourceGroup \
  --name myDNSLink \
  --zone-name private.contoso.com \
  --virtual-network myAzureVNet \
  --registration-enabled false

Azure DNS 私人區域自動註冊功能會管理虛擬網路中部署之虛擬機的 DNS 記錄。 當您鏈接虛擬網路與已啟用此設定的私人 DNS 區域時,會針對部署在虛擬網路中之 AKS 節點的每個 Azure 虛擬機建立 DNS 記錄。

將 Azure 私人 DNS 區域附加至應用程式路由附加元件

注意

此命令 az aks approuting zone add 會使用執行 命令的用戶許可權來建立 Azure DNS 區域 角色指派。 私用 DNS 區域參與者角色是用來管理私人 DNS 資源的內建角色,並指派給附加元件受控識別。 如需 AKS 受控識別的詳細資訊,請參閱受控識別的摘要

  1. 使用 az network dns zone show 命令擷取 DNS 區域的資源識別碼,並將輸出設定為名為的 ZONEID變數。 下列範例會查詢資源群組 myResourceGroup 中的區域 private.contoso.com

    ZONEID=$(az network private-dns zone show --resource-group myResourceGroup --name private.contoso.com --query "id" --output tsv)
    
  2. 更新附加元件,以使用 az aks approuting zone 命令啟用與 Azure DNS 的整合。 您可以傳遞以逗號分隔的 DNS 區域資源識別碼清單。 下列範例會更新資源群組 myResourceGroup 中的 AKS 叢集 myAKSCluster

    az aks approuting zone add --resource-group myResourceGroup --name myAKSCluster --ids=${ZONEID} --attach-zones
    

使用私人IP位址和內部負載平衡器建立NGINX輸入控制器

應用程式路由附加元件會使用名為 的 Kubernetes 自定義資源定義 (CRD) 來設定 NGINX 輸入控制器。NginxIngressController 您可以建立更多輸入控制器或修改現有的組態。

NginxIngressController CRD 具有欄位 loadBalancerAnnotations ,可藉由設定 負載平衡器註釋來控制 NGINX 輸入控制器服務的行為。

執行下列步驟,建立具有私人IP位址的內部 Azure Load Balancer 的 NGINX 輸入控制器。

  1. 將下列 YAML 指令清單複製到名為 nginx-internal-controller.yaml 的新檔案中,並將檔案儲存至本機計算機。

    apiVersion: approuting.kubernetes.azure.com/v1alpha1
    kind: NginxIngressController
    metadata:
      name: nginx-internal
    spec:
      ingressClassName: nginx-internal
      controllerNamePrefix: nginx-internal
      loadBalancerAnnotations: 
        service.beta.kubernetes.io/azure-load-balancer-internal: "true"
    
  2. 使用 kubectl apply 命令建立 NGINX 輸入控制器資源。

    kubectl apply -f nginx-internal-controller.yaml
    

    下列範例輸出顯示已建立的資源:

    nginxingresscontroller.approuting.kubernetes.azure.com/nginx-internal created
    
  3. 確認已建立輸入控制器

    您可以使用 命令來驗證 NGINX 輸入控制器 kubectl get nginxingresscontroller 的狀態。

    kubectl get nginxingresscontroller
    

    下列範例輸出顯示已建立的資源。 控制器可能需要幾分鐘的時間才能使用:

    NAME             INGRESSCLASS                         CONTROLLERNAMEPREFIX   AVAILABLE
    default          webapprouting.kubernetes.azure.com   nginx                  True
    nginx-internal   nginx-internal                       nginx-internal         True
    

部署應用程式

應用程式路由附加元件會使用 Kubernetes 輸入物件上的註釋來建立適當的資源。

  1. 建立名為 hello-web-app-routing 的應用程式命名空間,以使用 kubectl create namespace 命令執行範例 Pod。

    kubectl create namespace hello-web-app-routing
    
  2. 將下列 YAML 指令清單複製到名為 deployment.yaml 的新檔案,並將檔案儲存到本機計算機,以建立部署。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: aks-helloworld  
      namespace: hello-web-app-routing
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: aks-helloworld
      template:
        metadata:
          labels:
            app: aks-helloworld
        spec:
          containers:
          - name: aks-helloworld
            image: mcr.microsoft.com/azuredocs/aks-helloworld:v1
            ports:
            - containerPort: 80
            env:
            - name: TITLE
              value: "Welcome to Azure Kubernetes Service (AKS)"
    
  3. 將下列 YAML 指令清單複製到名為 service.yaml 的新檔案,並將檔案儲存到本機計算機,以建立服務。

    apiVersion: v1
    kind: Service
    metadata:
      name: aks-helloworld
      namespace: hello-web-app-routing
    spec:
      type: ClusterIP
      ports:
      - port: 80
      selector:
        app: aks-helloworld
    
  4. 使用 kubectl apply 命令建立叢集資源。

    kubectl apply -f deployment.yaml -n hello-web-app-routing
    

    下列範例輸出顯示已建立的資源:

    deployment.apps/aks-helloworld created created
    
    kubectl apply -f service.yaml -n hello-web-app-routing
    

    下列範例輸出顯示已建立的資源:

    service/aks-helloworld created created
    

在 Azure 私人 DNS 區域和私人 IP 位址上建立使用主機名的輸入資源

  1. 將下列 YAML 資訊清單複製到名為 ingress.yaml 的新檔案中,並儲存於本機電腦。

    使用 DNS 主機的名稱進行更新 <Hostname> ,例如 helloworld.private.contoso.com。 確認您正在指定ingressClassName指定 nginx-internal

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: aks-helloworld
      namespace: hello-web-app-routing
    spec:
      ingressClassName: nginx-internal
      rules:
      - host: <Hostname>
        http:
          paths:
          - backend:
              service:
                name: aks-helloworld
                port:
                  number: 80
            path: /
            pathType: Prefix
    
  2. 使用 kubectl apply 命令建立叢集資源。

    kubectl apply -f ingress.yaml -n hello-web-app-routing
    

    下列範例輸出顯示已建立的資源:

    ingress.networking.k8s.io/aks-helloworld created
    

確認已建立受控輸入

您可以使用 kubectl get ingress 命令來確認已建立受控輸入。

kubectl get ingress -n hello-web-app-routing

下列範例輸出顯示已建立的受控輸入:

NAME             CLASS            HOSTS                            ADDRESS      PORTS   AGE
aks-helloworld   nginx-internal   helloworld.private.contoso.com   10.224.0.7   80      98s

確認 Azure 私人 DNS 區域已更新

在幾分鐘內,執行 az network private-dns record-set a list 命令,以檢視 Azure 私人 DNS 區域的 A 記錄。 指定資源組名和 DNS 區域的名稱。 在此範例中,資源群組是 myResourceGroup,DNS 區域 private.contoso.com

az network private-dns record-set a list --resource-group myResourceGroup --zone-name private.contoso.com

下列範例輸出顯示已建立的記錄:

[
  {
    "aRecords": [
      {
        "ipv4Address": "10.224.0.7"
      }
    ],
    "etag": "188f0ce5-90e3-49e6-a479-9e4053f21965",
    "fqdn": "helloworld.private.contoso.com.",
    "id": "/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx/resourceGroups/foo/providers/Microsoft.Network/privateDnsZones/private.contoso.com/A/helloworld",
    "isAutoRegistered": false,
    "name": "helloworld",
    "resourceGroup": "foo",
    "ttl": 300,
    "type": "Microsoft.Network/privateDnsZones/A"
  }
]

下一步

如需其他與 SSL 加密相關的其他進階 NGINX 輸入控制器和輸入資源設定的元件資訊,請檢閱 DNS 和 SSL 組態和 應用程式路由附加元件設定