練習 - 在 Azure Kubernetes Service 叢集上部署應用程式

已完成

在此練習中,將公司的網站作為測試應用程式部署至 Azure Kubernetes Service (AKS)。 此網站是具有 HTML、CSS 和 JavaScript 基礎技術堆疊的靜態網站。 它不會收到像其他服務一樣多的要求,且會提供安全的方式供我們測試部署選項。

注意

若想要進一步探索原始程式碼,您可在這個 GitHub 存放庫取得 Web 應用程式的程式碼。 此外,此範例應用程式只會部署在 Linux 節點集區上。

重要

您必須有自己的 Azure 訂用帳戶才能執行本練習,且可能會產生費用。 如果您還沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

建立部署資訊清單

您可建立部署資訊清單檔來部署應用程式。 資訊清單檔可供定義所要部署的資源類型,以及與工作負載有關的所有詳細資料。

Kubernetes 會將容器分組成稱為 Pod 的邏輯結構,其中不包含任何情報。 部署會新增遺漏的情報以建立應用程式。 讓我們來建立部署檔案。

  1. 登入 Azure Cloud Shell。

  2. 在 Cloud Shell 中,使用整合式編輯器來為 Kubernetes 部署建立稱為 deployment.yaml 的資訊清單檔。

    touch deployment.yaml
    
  3. 輸入 code .,在 Cloud Shell 中開啟整合式編輯器

  4. 開啟 deployment.yaml 檔案並新增下列 YAML 的程式碼區段。

    # deployment.yaml
    apiVersion: apps/v1 # The API resource where this workload resides
    kind: Deployment # The kind of workload we're creating
    metadata:
      name: contoso-website # This will be the name of the deployment
    

    在此程式碼中,您所新增前兩個機碼會告知 Kubernetes 您正在建立之資訊清單的 apiVersionkindname 是部署的名稱。 在使用 kubectl 時,使用此名稱來識別和查詢部署資訊。

    提示

    如需 apiVersion 的詳細資訊,以及應該在此機碼中輸入什麼值,請參閱官方 Kubernetes 文件。 請在此課程模組的結尾尋找連結。

  5. 部署會包裝 Pod。 您可使用範本定義以在資訊清單檔中定義 Pod 資訊。 範本會放置在資訊清單檔中部署規格區段的下方。

    更新 deployment.yaml 檔案以使其符合下列 YAML。

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-website
    spec:
      template: # This is the template of the pod inside the deployment
        metadata: # Metadata for the pod
          labels:
            app: contoso-website
    

    Pod 不會使用與部署相同的名稱。 Pod 名稱是由部署名稱以及加在結尾的隨機識別碼組合而成。

    請留意 labels 索引鍵的使用。 您可新增 labels 索引鍵以供部署尋找和分組 Pod。

  6. Pod 會包裝一或多個容器。 所有 Pod 都具備規格區段,可供定義該 Pod 內的容器。

    更新 deployment.yaml 檔案以使其符合下列 YAML。

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-website
    spec:
      template: # This is the template of the pod inside the deployment
        metadata:
          labels:
            app: contoso-website
        spec:
          containers: # Here we define all containers
            - name: contoso-website
    

    containers 機碼是容器規格的陣列,因為 Pod 可以有一或多個容器。 規格會定義 imagenameresourcesports,以及容器的其他重要資訊。

    所有正在執行的 Pod 都遵循 contoso-website-<UUID> 名稱,其中 UUID 是用來唯一識別所有資源的產生識別碼。

  7. 定義允許應用程式在叢集中使用的資源數下限和上限是良好做法。 您會使用 resources 機碼來指定此資訊。

    更新 deployment.yaml 檔案以使其符合下列 YAML。

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-website
    spec:
      template: # This is the template of the pod inside the deployment
        metadata:
          labels:
            app: contoso-website
        spec:
          containers:
            - image: mcr.microsoft.com/mslearn/samples/contoso-website
              name: contoso-website
              resources:
                requests: # Minimum amount of resources requested
                  cpu: 100m
                  memory: 128Mi
                limits: # Maximum amount of resources requested
                  cpu: 250m
                  memory: 256Mi
    

    請注意,資源區段如何允許將資源數下限作為要求指定,以及將資源數上限作為限制指定。

  8. 最後一個步驟是定義此容器將透過 ports 索引鍵向外部公開的連接埠。 ports 索引鍵是物件的陣列,其表示 Pod 中的容器可使用多個名稱來公開多個連接埠。

    更新 deployment.yaml 檔案以使其符合下列 YAML。

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-website
    spec:
      template: # This is the template of the pod inside the deployment
        metadata:
          labels:
            app: contoso-website
        spec:
          nodeSelector:
            kubernetes.io/os: linux
          containers:
            - image: mcr.microsoft.com/mslearn/samples/contoso-website
              name: contoso-website
              resources:
                requests:
                  cpu: 100m
                  memory: 128Mi
                limits:
                  cpu: 250m
                  memory: 256Mi
              ports:
                - containerPort: 80 # This container exposes port 80
                  name: http # We named that port "http" so we can refer to it later
    

    請注意使用 name 機碼來命名連接埠的方式。 命名連接埠可供變更公開的連接埠,而無須變更參考該連接埠的檔案。

  9. 最後,新增選取器區段來定義部署管理的工作負載。 selector 索引鍵會放置在資訊清單檔的部署規格區段下方。 使用 matchLabels 索引鍵來列出部署所管理所有 Pod 的標籤。

    更新 deployment.yaml 檔案以使其符合下列 YAML。

    # deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: contoso-website
    spec:
      selector: # Define the wrapping strategy
        matchLabels: # Match all pods with the defined labels
          app: contoso-website # Labels follow the `name: value` template
      template: # This is the template of the pod inside the deployment
        metadata:
          labels:
            app: contoso-website
        spec:
          nodeSelector:
            kubernetes.io/os: linux
          containers:
            - image: mcr.microsoft.com/mslearn/samples/contoso-website
              name: contoso-website
              resources:
                requests:
                  cpu: 100m
                  memory: 128Mi
                limits:
                  cpu: 250m
                  memory: 256Mi
              ports:
                - containerPort: 80
                  name: http
    

    注意

    在具有多個節點集區 (Linux 和 Windows) 的 AKS 叢集中,先前列示的部署資訊清單檔案也會定義 nodeSelector,以告知 AKS 叢集在可執行 Linux 容器的節點上執行範例應用程式的 Pod。

    Linux 節點無法執行 Windows 容器,反之亦然。

  10. 儲存資訊清單檔並關閉編輯器。

套用資訊清單

  1. 在 Cloud Shell 中,執行 kubectl apply 命令以將部署資訊清單提交到叢集。

    kubectl apply -f ./deployment.yaml
    

    此命令應輸出與下列範例相似的結果。

    deployment.apps/contoso-website created
    
  2. 執行 kubectl get deploy 命令來檢查部署是否成功。

    kubectl get deploy contoso-website
    

    命令將會輸出與下列範例相似的表格。

    NAME              READY   UP-TO-DATE   AVAILABLE   AGE
    contoso-website   0/1     1            0           16s
    
  3. 執行 kubectl get pods 命令,以檢查 Pod 是否正在執行。

    kubectl get pods
    

    命令將會輸出與下列範例相似的表格。

    NAME                               READY   STATUS    RESTARTS   AGE
    contoso-website-7c58c5f699-r79mv   1/1     Running   0          63s