教程:在 Azure Stack HCI 上的 Azure Kubernetes 服务中部署 Linux 应用程序Tutorial: Deploy Linux applications in Azure Kubernetes Service on Azure Stack HCI

适用于 Azure Stack HCI 上的 AKS、Windows Server 2019 Datacenter 上的 AKS 运行时Applies to: AKS on Azure Stack HCI, AKS runtime on Windows Server 2019 Datacenter

在本教程中,你会在 Azure Stack HCI 上的 Azure Kubernetes 服务群集中部署包含 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 服务的 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 服务群集,其中至少有一个启动并运行的 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 上的 Azure Kubernetes 服务 PowerShell 模块。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 Vote 应用程序所需的所有对象。In this quickstart, a manifest is used to create all objects needed to run the Azure vote application. 此清单包括两个 Kubernetes 部署 - 一个用于 Azure Vote 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 实例,一个外部服务用于从 Internet 访问 Azure Vote 应用程序。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 服务将向 Internet 公开应用程序前端。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 Vote 应用的实际效果,请打开 Web 浏览器并转到服务的外部 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-vote-front 部署中的 Pod 数,请使用 kubectl scale 命令。To change the number of pods in the azure-vote-front deployment, use the kubectl scale command. 下面的示例将前端箱数增加到 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