Share via


建立 Windows Server 容器

適用於:Azure Stack HCI 版本 23H2

在本文中,您會使用 Azure CLI 將 nodepool 部署到執行 Windows Server 容器的現有 AKS 叢集。 您也可以將 Windows Server 容器中的 ASP.NET 範例應用程式部署到叢集。

必要條件

遵循 如何建立 AKS 叢集中的指示建立 AKS 叢集

新增 nodepool

根據預設,會使用可執行Linux容器的 nodepool來建立 Kubernetes 叢集。 您必須新增另一個節點集區,以在Linux nodepool旁邊執行 Windows Server 容器。

使用 az aksarc nodepool add 命令搭配 參數 --os-type Windows,搭配 Windows 容器主機新增 nodepool。 如果未指定操作系統 SKU,nodepool 會根據叢集的 Kubernetes 版本設定為預設 OS。 Windows Server 2022 是 Kubernetes 1.25.0 版和更新版本的預設操作系統。 Windows Server 2019 是舊版的預設 OS。

  • 若要使用 Windows Server 2019,請指定下列參數:
    • 請將 os-type 設定為 Windows
    • 請將 os-sku 設定為 Windows2019
  • 若要使用 Windows Server 2022,請指定下列參數:
    • 請將 os-type 設定為 Windows
    • os-sku 設定為 Windows2022 (選擇性) 。

下列命令會建立名為 $mynodepool 的新 nodepool,並使用一個 Windows 2019 節點將其新增至 $myAKSCluster

az aksarc nodepool add --resource-group $myResourceGroup --cluster-name $myAKSCluster --name $mynodepool --node-count 1 --os-type Windows --os-sku Windows2019

連線到 AKS 叢集

現在您可以從本機電腦執行 az connectedk8s proxy 命令來連線到 Kubernetes 叢集。 執行此命令之前,請務必先登入 Azure。 如果您有多個 Azure 訂用帳戶,請使用 az account set 命令選取適當的訂用帳戶標識碼。

此命令會將 Kubernetes 叢集的 kubeconfig 下載到本機計算機,並開啟 Proxy 連線通道至內部部署 Kubernetes 叢集。 只要此命令執行,通道就會開啟。 只要您想要存取叢集,即可執行此命令。 如果命令逾時,請關閉 CLI 視窗,開啟新的命令,然後再次執行命令。

您必須擁有載入 AKS 叢集之資源群組的參與者許可權,才能成功執行下列命令:

az connectedk8s proxy --name $aksclustername --resource-group $resource_group --file .\aks-arc-kube-config

預期輸出:

Proxy is listening on port 47011
Merged "aks-workload" as current context in .\aks-arc-kube-config
Start sending kubectl requests on 'aks-workload' context using kubeconfig at .\aks-arc-kube-config
Press Ctrl+C to close proxy.

讓此工作階段保持執行狀態,並從不同的終端機/命令提示字元連線到 Kubernetes 叢集。 執行 kubectl get 命令,確認您可以連線到 Kubernetes 叢集。 這個指令會傳回叢集節點的清單:

kubectl get node -A --kubeconfig .\aks-arc-kube-config

下列輸出範例顯示先前步驟中建立的節點。 確定節點的狀態為就緒

NAME              STATUS   ROLES           AGE     VERSION
moc-lesdc78871d   Ready    control-plane   6d8h    v1.26.3
moc-lupeeyd0f8c   Ready    <none>          6d8h    v1.26.3
moc-ww2c8d5ranw   Ready    <none>          7m18s   v1.26.3

部署應用程式

Kubernetes 資訊清單檔會定義叢集所需的狀態,例如要執行哪些容器映像。

您可以使用 YAML 指令清單來建立在 Windows Server 容器中執行 ASP.NET 範例應用程式所需的所有物件。 此指令清單包含 ASP.NET 範例應用程式的 Kubernetes 部署 ,以及可從因特網存取應用程式的 Kubernetes 服務。

ASP.NET 範例應用程式會作為 .NET Framework 範例的一部分提供,並在 Windows Server 容器中執行。 AKS 需要 Windows Server 容器根據 Windows Server 2019 或更新版本的映像。 Kubernetes 指令清單檔案也必須定義 節點選取器 ,以確保您 ASP.NET 範例應用程式的 Pod 排程在可執行 Windows Server 容器的節點上。

  1. 建立名為 sample.yaml 的檔案,並在下列YAML定義中複製:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: sample
      labels:
        app: sample
    spec:
      replicas: 1
      template:
        metadata:
          name: sample
          labels:
            app: sample
        spec:
          nodeSelector:
            "kubernetes.io/os": windows
          containers:
          - name: sample
            image: mcr.microsoft.com/dotnet/framework/samples:aspnetapp
            resources:
              limits:
                cpu: 1
                memory: 800M
            ports:
              - containerPort: 80
      selector:
        matchLabels:
          app: sample
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: sample
    spec:
      type: LoadBalancer
      ports:
      - protocol: TCP
        port: 80
      selector:
        app: sample
    

    如需 YAML 資訊清單檔案的明細,請參閱部署與 YAML 資訊清單

  2. 使用 kubectl apply 命令來部署應用程式並指定 YAML 資訊清單的名稱:

    kubectl apply -f sample.yaml --kubeconfig .\\aks-arc-kube-config
    

下列範例輸出會顯示已成功建立的部署和服務:

deployment.apps/sample created
service/sample created

測試應用程式

執行應用程式時,Kubernetes 服務會向網際網路公開前端應用程式。 此程序需要數分鐘的時間完成。 服務有時可能需要超過幾分鐘的時間來佈建。 布建最多允許 10 分鐘。

  1. 使用 kubectl get service 命令搭配 --watch 引數來監視進度。

    kubectl get service sample --watch --kubeconfig .\aks-arc-kube-config
    

    一開始,輸出會將範例服務的 EXTERNAL-IP 顯示為 擱置中:

    NAME   TYPE         CLUSTER-IP EXTERNAL-IP PORT(S)      AGE
    sample LoadBalancer 10.0.37.27 <pending>   80:30572/TCP 6s
    

    EXTERNAL-IP 位址從擱置變更為 IP 位址時,請使用 CTRL-C 停止 kubectl watch 程式。 下列範例輸出顯示指派給服務的有效公用 IP 位址:

    sample LoadBalancer 10.0.37.27 52.179.23.131 80:30572/TCP 2m
    
  2. 藉由開啟網頁瀏覽器至 範例 服務的外部IP位址和埠,查看範例應用程式的運作情形。

    顯示範例應用程式的螢幕快照 ASP.NET。

    如果您在嘗試載入頁面時收到連線逾時,您應該確認範例應用程式已準備好使用 kubectl get pods --watch 命令。 有時候,Windows 容器不會在外部IP位址可用時啟動。

刪除 nodepool

使用 az akshybrid nodepool delete 命令刪除 nodepool。

az aksarc nodepool delete -g $myResourceGroup --cluster-name $myAKSCluster --name $mynodepool --no-wait

下一步

在本文中,您已將 Windows nodepool 部署到現有的 AKS 叢集,並在 Windows Server 容器中部署 ASP.NET 範例應用程式。