共用方式為


Azure Kubernetes Service (AKS) 上的 HTTP 應用程式路由附加元件 (已淘汰)

警告

Azure Kubernetes Service (AKS) 上的 HTTP 應用程式路由附加元件將於 2025 年 3 月 3 日淘汰。 建議您依該日期移轉至應用程式路由附加元件

HTTP 應用程式路由附加元件可讓您輕鬆存取已部署至 Azure Kubernetes Service (AKS) 叢集的應用程式,方法是:

  • 在 AKS 叢集中設定輸入控制器
  • 為應用程式端點建立可公開存取的 DNS 名稱
  • 在您的訂用帳戶中建立 DNS 區域。 如需 DNS 成本的詳細資訊,請參閱 DNS 定價

開始之前

  • HTTP 應用程式路由附加元件不適用於 AKS 1.22.6+ 版。
  • 如果您要在本機執行命令,請使用 az aks install-cli 命令進行安裝 kubectl

HTTP 應用程式路由附加元件概觀

附加元件會部署兩個元件:Kubernetes 輸入控制器External-DNS 控制器。

  • 輸入控制器:輸入控制器會使用 Kube LoadBalancer 服務對網際網路公開。 輸入控制器會監看與實作 Kube 輸入資源,並建立應用程式端點的路由。
  • External-DNS 控制器:External-DNS 控制器監看 Kubernetes 輸入資源,並在叢集特定 DNS 區域中建立 DNS A 記錄。

啟用 HTTP 應用程式路由

  1. 建立新的 AKS 叢集,並使用 az aks create 命令搭配 --enable-addons 參數來啟用 HTTP 應用程式路由附加元件。

    az aks create \
        --resource-group myResourceGroup \
        --name myAKSCluster \
        --enable-addons http_application_routing \
        --generate-ssh-keys
    

    您也可以使用 az aks enable-addons 命令搭配 --addons 參數,在現有的 AKS 叢集上啟用 HTTP 路由。

    az aks enable-addons --resource-group myResourceGroup --name myAKSCluster --addons http_application_routing
    
  2. 使用 az aks show 命令擷取 DNS 區域名稱。 您需要 DNS 區域名稱,才能將應用程式部署至叢集。

    az aks show --resource-group myResourceGroup --name myAKSCluster --query addonProfiles.httpApplicationRouting.config.HTTPApplicationRoutingZoneName -o table
    

    您的輸出看起來如下列範例輸出所示:

    9f9c1fe7-21a1-416d-99cd-3543bb92e4c3.eastus.aksapp.io
    

連線至您的 AKS 叢集

  • 使用 az aks get-credentials 命令,設定 kubectl 連線到 Kubernetes 叢集。 下列範例會針對 myResourceGroup 中名為 myAKSCluster 的 AKS 叢集取得認證:

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

使用 HTTP 應用程式路由

重要

HTTP 應用程式路由附加元件只能透過下列註釋在輸入資源上觸發:

annotations:
 kubernetes.io/ingress.class: addon-http-application-routing
  1. 建立名為 samples-http-application-routing.yaml 的檔案,然後將下列 YAML 複製進來。 在第 43 行上,使用上一個步驟所收集的 DNS 區域名稱來更新 <CLUSTER_SPECIFIC_DNS_ZONE>

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: aks-helloworld
    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)"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: aks-helloworld
    spec:
      type: ClusterIP
      ports:
     - port: 80
      selector:
        app: aks-helloworld
    ---
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: aks-helloworld
      annotations:
        kubernetes.io/ingress.class: addon-http-application-routing
    spec:
      rules:
     - host: aks-helloworld.<CLUSTER_SPECIFIC_DNS_ZONE>
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: aks-helloworld
                port:
                  number: 80
    
  2. 使用 kubectl apply 命令建立資源。

    kubectl apply -f samples-http-application-routing.yaml
    

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

    deployment.apps/aks-helloworld created
    service/aks-helloworld created
    ingress.networking.k8s.io/aks-helloworld created
    
  3. 開啟網頁瀏覽器至aks-helloworld。<CLUSTER_SPECIFIC_DNS_ZONE >,例如 aks-helloworld.9f9c1fe7-21a1-416d-99cd-3543bb92e4c3.eastus.aksapp.io 並確認您看到示範應用程式。 應用程式可能會需要數分鐘才出現。

移除 HTTP 應用程式路由

  1. 使用 [az aks disable-addons][az-aks-disable-addons] command with the addons' 參數移除 HTTP 應用程式路由附加元件。

    az aks disable-addons --addons http_application_routing --name myAKSCluster --resource-group myResourceGroup --no-wait
    
  2. 停用 HTTP 應用程式路由附加元件時,某些 Kubernetes 資源可能會留在叢集中。 這些資源包括 configMapssecrets,且會建立在 kube-system 命名空間中。 為了讓叢集保持乾淨,建議您移除這些資源。 使用下列 kubectl get 命令尋找 addon-http-application-routing 資源:

    kubectl get deployments --namespace kube-system
    kubectl get services --namespace kube-system
    kubectl get configmaps --namespace kube-system
    kubectl get secrets --namespace kube-system
    

    下列範例輸出會顯示應該刪除的 configmaps

    NAMESPACE     NAME                                                       DATA   AGE
    kube-system   addon-http-application-routing-nginx-configuration         0      9m7s
    kube-system   addon-http-application-routing-tcp-services                0      9m7s
    kube-system   addon-http-application-routing-udp-services                0      9m7s
    
  3. 使用 kubectl delete 命令刪除剩餘的資源。 請務必指定資源類型、資源名稱和命名空間。 下列範例會刪除先前的其中一個 configmaps:

    kubectl delete configmaps addon-http-application-routing-nginx-configuration --namespace kube-system
    
  4. 針對叢集中保留的所有 addon-http-application-routing 資源重複先前的 kubectl delete 步驟。

疑難排解

  1. 使用 kubectl logs 命令來檢視 External-DNS 應用程式的應用程式記錄。

    kubectl logs -f deploy/addon-http-application-routing-external-dns -n kube-system
    

    記錄應該確認 已成功建立 ATXT DNS 記錄,如下列範例輸出所示:

    time="2018-04-26T20:36:19Z" level=info msg="Updating A record named 'aks-helloworld' to '52.242.28.189' for Azure DNS zone '471756a6-e744-4aa0-aa01-89c4d162a7a7.canadaeast.aksapp.io'."
    time="2018-04-26T20:36:21Z" level=info msg="Updating TXT record named 'aks-helloworld' to '"heritage=external-dns,external-dns/owner=default"' for Azure DNS zone '471756a6-e744-4aa0-aa01-89c4d162a7a7.canadaeast.aksapp.io'."
    
  2. 使用 kubectl logs 命令檢視 NGINX 輸入控制器的應用程式記錄。

    kubectl logs -f deploy/addon-http-application-routing-nginx-ingress-controller -n kube-system
    

    記錄應該確認輸入資源的 CREATE 以及是否已重新載入控制器,如下列範例輸出所示:

    -------------------------------------------------------------------------------
    NGINX Ingress controller
      Release:    0.13.0
      Build:      git-4bc943a
      Repository: https://github.com/kubernetes/ingress-nginx
    -------------------------------------------------------------------------------
    
    I0426 20:30:12.212936       9 flags.go:162] Watching for ingress class: addon-http-application-routing
    W0426 20:30:12.213041       9 flags.go:165] only Ingress with class "addon-http-application-routing" will be processed by this ingress controller
    W0426 20:30:12.213505       9 client_config.go:533] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
    I0426 20:30:12.213752       9 main.go:181] Creating API client for https://10.0.0.1:443
    I0426 20:30:12.287928       9 main.go:225] Running in Kubernetes Cluster version v1.8 (v1.8.11) - git (clean) commit 1df6a8381669a6c753f79cb31ca2e3d57ee7c8a3 - platform linux/amd64
    I0426 20:30:12.290988       9 main.go:84] validated kube-system/addon-http-application-routing-default-http-backend as the default backend
    I0426 20:30:12.294314       9 main.go:105] service kube-system/addon-http-application-routing-nginx-ingress validated as source of Ingress status
    I0426 20:30:12.426443       9 stat_collector.go:77] starting new nginx stats collector for Ingress controller running in namespace  (class addon-http-application-routing)
    I0426 20:30:12.426509       9 stat_collector.go:78] collector extracting information from port 18080
    I0426 20:30:12.448779       9 nginx.go:281] starting Ingress controller
    I0426 20:30:12.463585       9 event.go:218] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"kube-system", Name:"addon-http-application-routing-nginx-configuration", UID:"2588536c-4990-11e8-a5e1-0a58ac1f0ef2", APIVersion:"v1", ResourceVersion:"559", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap kube-system/addon-http-application-routing-nginx-configuration
    I0426 20:30:12.466945       9 event.go:218] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"kube-system", Name:"addon-http-application-routing-tcp-services", UID:"258ca065-4990-11e8-a5e1-0a58ac1f0ef2", APIVersion:"v1", ResourceVersion:"561", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap kube-system/addon-http-application-routing-tcp-services
    I0426 20:30:12.467053       9 event.go:218] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"kube-system", Name:"addon-http-application-routing-udp-services", UID:"259023bc-4990-11e8-a5e1-0a58ac1f0ef2", APIVersion:"v1", ResourceVersion:"562", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap kube-system/addon-http-application-routing-udp-services
    I0426 20:30:13.649195       9 nginx.go:302] starting NGINX process...
    I0426 20:30:13.649347       9 leaderelection.go:175] attempting to acquire leader lease  kube-system/ingress-controller-leader-addon-http-application-routing...
    I0426 20:30:13.649776       9 controller.go:170] backend reload required
    I0426 20:30:13.649800       9 stat_collector.go:34] changing prometheus collector from  to default
    I0426 20:30:13.662191       9 leaderelection.go:184] successfully acquired lease kube-system/ingress-controller-leader-addon-http-application-routing
    I0426 20:30:13.662292       9 status.go:196] new leader elected: addon-http-application-routing-nginx-ingress-controller-5cxntd6
    I0426 20:30:13.763362       9 controller.go:179] ingress backend successfully reloaded...
    I0426 21:51:55.249327       9 event.go:218] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"aks-helloworld", UID:"092c9599-499c-11e8-a5e1-0a58ac1f0ef2", APIVersion:"extensions", ResourceVersion:"7346", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/aks-helloworld
    W0426 21:51:57.908771       9 controller.go:775] service default/aks-helloworld does not have any active endpoints
    I0426 21:51:57.908951       9 controller.go:170] backend reload required
    I0426 21:51:58.042932       9 controller.go:179] ingress backend successfully reloaded...
    167.220.24.46 - [167.220.24.46] - - [26/Apr/2018:21:53:20 +0000] "GET / HTTP/1.1" 200 234 "" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" 197 0.001 [default-aks-helloworld-80] 10.244.0.13:8080 234 0.004 200
    

清除資源

  • 使用 kubectl delete 命令,移除您在本文中建立的相關聯 Kube 物件。

    kubectl delete -f samples-http-application-routing.yaml
    

    下列範例輸出顯示,已經移除 Kube 物件:

    deployment "aks-helloworld" deleted
    service "aks-helloworld" deleted
    ingress "aks-helloworld" deleted
    

下一步

如需在 AKS 中安裝 HTTPS 所保護的輸入控制器有關的資訊,請參閱 Azure Kubernetes Service (AKS) 上的 HTTPS 輸入