教學課程:在 Azure Stack HCI 的 Azure Kubernetes Service 中部署 Linux 應用程式Tutorial: Deploy Linux applications in Azure Kubernetes Service on Azure Stack HCI

適用于: AKS on Azure Stack HCI、Windows Server 2019 Datacenter 上的 AKS runtimeApplies to: AKS on Azure Stack HCI, AKS runtime on Windows Server 2019 Datacenter

在本教學課程中,您會在 Azure Stack HCI 叢集上的 Azure Kubernetes Service 中,部署包含 web 前端和 Redis 資料庫實例的多容器應用程式。In this tutorial, you deploy a multi-container application that includes a web front end and a Redis database instance in your Azure Kubernetes Service on Azure Stack HCI cluster. 然後,您會看到如何測試及調整您的應用程式。You then see how to test and scale your application.

本教學課程假設您對 Kubernetes 概念有基本的瞭解。This tutorial assumes a basic understanding of Kubernetes concepts. 如需詳細資訊,請參閱 Azure Stack HCI 上 Azure Kubernetes Service 的 Kubernetes 核心概念For more information, see Kubernetes core concepts for Azure Kubernetes Service on Azure Stack HCI.

開始之前Before you begin

確認您已備妥下列需求:Verify you have the following requirements ready:

  • Azure Stack HCI 叢集上的 Azure Kubernetes Service,其中至少有一個已啟動且正在執行的 Linux 背景工作節點。An Azure Kubernetes Service on Azure Stack HCI cluster with at least one Linux worker node that is up and running.
  • 用來存取叢集的 kubeconfig 檔。A kubeconfig file to access the cluster.
  • 已安裝 Azure Stack HCI PowerShell 模組上的 Azure Kubernetes Service。Have the Azure Kubernetes Service on Azure Stack HCI PowerShell module installed.
  • 在 PowerShell 系統管理視窗中執行這份檔中的命令。Run the commands in this document in a PowerShell administrative window.
  • 確定作業系統特定工作負載落在適當的容器主機上。Ensure that OS-specific workloads land on the appropriate container host. 如果您有混合的 Linux 和 Windows 背景工作節點 Kubernetes 叢集,您可以使用節點選取器或污點和容差。If you have a mixed Linux and Windows worker nodes Kubernetes cluster, you can either use node selectors or taints and tolerations. 如需詳細資訊,請參閱 使用節點選取器和污點和容差For more information, see using node selectors and taints and tolerations.

部署應用程式Deploy the application

Kubernetes 資訊清單檔會定義所需的叢集狀態,例如要執行哪些容器映像。A Kubernetes manifest file defines a desired state for the cluster, such as what container images to run. 在本快速入門中,資訊清單是用來建立執行 Azure 投票應用程式所需的所有物件。In this quickstart, a manifest is used to create all objects needed to run the Azure vote application. 此資訊清單包含兩個 Kubernetes 部署:一個適用於範例 Azure 投票 Python 應用程式,而另一個適用於 Redis 執行個體。This manifest includes two Kubernetes deployments - one for the sample Azure Vote Python applications, and the other for a Redis instance. 此外也會建立兩個 Kubernetes 服務-Redis 實例的內部服務,以及用來從網際網路存取 Azure 投票應用程式的外部服務。Two Kubernetes services are also created - an internal service for the Redis instance, and an external service to access the Azure Vote application from the internet.

建立名為 azure-vote.yaml 的檔案,然後將下列 YAML 定義複製進來。Create a file named azure-vote.yaml and copy in the following YAML definition.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-back
spec:
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-back
  template:
    metadata:
      labels:
        app: azure-vote-back
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-back
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-back
spec:
  ports:
  - port: 6379
  selector:
    app: azure-vote-back
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-front
spec:
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-front
  template:
    metadata:
      labels:
        app: azure-vote-front
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-front
        image: mcr.microsoft.com/azuredocs/azure-vote-front:v1
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 80
        env:
        - name: REDIS
          value: "azure-vote-back"
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-front
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: azure-vote-front

使用命令部署應用程式 kubectl apply ,並指定 YAML 資訊清單的名稱:Deploy the application using the kubectl apply command and specify the name of your YAML manifest:

kubectl apply -f azure-vote.yaml

下列範例輸出會顯示已成功建立的部署和服務:The following example output shows the Deployments and Services created successfully:

deployment "azure-vote-back" created
service "azure-vote-back" created
deployment "azure-vote-front" created
service "azure-vote-front" created

測試應用程式Test the application

執行應用程式時,Kubernetes 服務會向網際網路公開前端應用程式。When the application runs, a Kubernetes service exposes the application front end to the internet. 此程序需要數分鐘的時間完成。This process can take a few minutes to complete.

若要監視進度,請使用 kubectl get service 命令搭配 --watch 引數。To monitor progress, use the kubectl get service command with the --watch argument.

kubectl get service azure-vote-front --watch

一開始,azure-vote-front 服務的 EXTERNAL-IP 會顯示為 pendingInitially the EXTERNAL-IP for the azure-vote-front service is shown as pending.

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
azure-vote-front   LoadBalancer   10.0.37.27      <pending>     80:30572/TCP   22m

EXTERNAL-IP 位址從 pending 變成實際的公用 IP 位址時,請使用 CTRL-C 停止 kubectl 監看式流程。When the EXTERNAL-IP address changes from pending to an actual public IP address, use CTRL-C to stop the kubectl watch process. 下列範例輸出會顯示已指派給服務的有效公用 IP 位址:The following example output shows a valid public IP address assigned to the service:

NAME               TYPE           CLUSTER-IP   EXTERNAL-IP     PORT(S)        AGE
azure-vote-front   LoadBalancer   10.0.37.27   52.179.23.131   80:30572/TCP   24m

若要查看 Azure 投票應用程式的實際運作情況,請開啟網頁瀏覽器並瀏覽至服務的外部 IP 位址。To see the Azure Vote app in action, open a web browser to the external IP address of your service.

Azure 上 Kubernetes 叢集的影像

調整應用程式 podScale application pods

我們已建立 Azure 投票前端和 Redis 實例的單一複本。We have created a single replica of the Azure Vote front end and Redis instance. 若要查看叢集中的 pod 數目和狀態,請使用如下所示的 kubectl get 命令:To see the number and state of pods in your cluster, use the kubectl get command as follows:

kubectl get pods -n default

下列範例輸出顯示一個前端 pod 和一個後端 pod:The following example output shows one front end pod and one back-end pod:

NAME                                READY     STATUS    RESTARTS   AGE
azure-vote-back-6bdcb87f89-g2pqg    1/1       Running   0          25m
azure-vote-front-84c8bf64fc-cdq86   1/1       Running   0          25m

若要變更 azure 投票之前 部署中的 pod 數目,請使用 kubectl scale 命令。To change the number of pods in the azure-vote-front deployment, use the kubectl scale command. 下列範例會將前端 pod 的數目增加至 5The following example increases the number of front end pods to 5:

kubectl scale --replicas=5 deployment/azure-vote-front

kubectl get pods再次執行以確認已建立其他 pod。Run kubectl get pods again to verify that additional pods have been created. 大約一分鐘後,其他 Pod 就會成為叢集中的可用項目:After a minute or so, the additional pods are available in your cluster:

kubectl get pods -n default

Name                                READY   STATUS    RESTARTS   AGE
azure-vote-back-6bdcb87f89-g2pqg    1/1     Running   0          31m
azure-vote-front-84c8bf64fc-cdq86   1/1     Running   0          31m
azure-vote-front-84c8bf64fc-56h64   1/1     Running   0          80s
azure-vote-front-84c8bf64fc-djkp8   1/1     Running   0          80s
azure-vote-front-84c8bf64fc-jmmvs   1/1     Running   0          80s
azure-vote-front-84c8bf64fc-znc6z   1/1     Running   0          80s

後續步驟Next steps