您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:在 Azure Kubernetes 服务 (AKS) 中运行应用程序Tutorial: Run applications in Azure Kubernetes Service (AKS)

Kubernetes 为容器化应用程序提供一个分布式平台。Kubernetes provides a distributed platform for containerized applications. 你生成自己的应用程序和服务并将其部署到 Kubernetes 群集中,让群集管理可用性和连接性。You build and deploy your own applications and services into a Kubernetes cluster, and let the cluster manage the availability and connectivity. 在本教程中(第 4 部分,共 7 部分),示例应用程序会部署到 Kubernetes 群集中。In this tutorial, part four of seven, a sample application is deployed into a Kubernetes cluster. 学习如何:You learn how to:

  • 更新 Kubernetes 清单文件Update a Kubernetes manifest file
  • 在 Kubernetes 中运行应用程序Run an application in Kubernetes
  • 测试应用程序Test the application

在另外的教程中,此应用程序将进行横向扩展和更新。In additional tutorials, this application is scaled out and updated.

本快速入门假设读者基本了解 Kubernetes 的概念。This quickstart assumes a basic understanding of Kubernetes concepts. 有关详细信息,请参阅 Azure Kubernetes 服务 (AKS) 的 Kubernetes 核心概念For more information, see Kubernetes core concepts for Azure Kubernetes Service (AKS).

开始之前Before you begin

在前面的教程中,我们已将应用程度打包到容器映像中,将此映像上传到 Azure 容器注册表,并创建了 Kubernetes 群集。In previous tutorials, an application was packaged into a container image, this image was uploaded to Azure Container Registry, and a Kubernetes cluster was created.

必须先预创建 azure-vote-all-in-one-redis.yaml Kubernetes 清单文件,然后才能完成本教程。To complete this tutorial, you need the pre-created azure-vote-all-in-one-redis.yaml Kubernetes manifest file. 此文件是在上一教程中与应用程序源代码一同下载。This file was downloaded with the application source code in a previous tutorial. 验证是否已克隆存储库,并且是否已将目录更改为克隆的存储库。Verify that you've cloned the repo, and that you have changed directories into the cloned repo. 如果尚未完成这些步骤,并且想要逐一完成,请先阅读教程 1 – 创建容器映像If you haven't done these steps, and would like to follow along, start with Tutorial 1 – Create container images.

此教程需要运行 Azure CLI 2.0.53 或更高版本。This tutorial requires that you're running the Azure CLI version 2.0.53 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

更新清单文件Update the manifest file

在这些教程中,请使用 Azure 容器注册表 (ACR) 实例来存储示例应用程序的容器映像。In these tutorials, an Azure Container Registry (ACR) instance stores the container image for the sample application. 若要部署此应用程序,必须更新 Kubernetes 清单文件中的映像名称,使之包括 ACR 登录服务器名称。To deploy the application, you must update the image name in the Kubernetes manifest file to include the ACR login server name.

使用 az acr list 命令获取 ACR 登录服务器名称,如下所示:Get the ACR login server name using the az acr list command as follows:

az acr list --resource-group myResourceGroup --query "[].{acrLoginServer:loginServer}" --output table

在第一个教程中克隆的 git 存储库中的示例清单文件使用登录服务器名称 microsoftThe sample manifest file from the git repo cloned in the first tutorial uses the login server name of microsoft. 确保位于所克隆的 azure-voting-app-redis 目录中,然后使用某个文本编辑器(例如 vi)打开清单文件:Make sure that you're in the cloned azure-voting-app-redis directory, then open the manifest file with a text editor, such as vi:

vi azure-vote-all-in-one-redis.yaml

microsoft 替换为 ACR 登录服务器名称。Replace microsoft with your ACR login server name. 映像名称位于清单文件的第 51 行。The image name is found on line 51 of the manifest file. 以下示例展示了默认映像名称:The following example shows the default image name:

containers:
- name: azure-vote-front
  image: microsoft/azure-vote-front:v1

提供自己的 ACR 登录服务器名称,使清单文件如以下示例所示:Provide your own ACR login server name so that your manifest file looks like the following example:

containers:
- name: azure-vote-front
  image: <acrName>.azurecr.io/azure-vote-front:v1

保存并关闭该文件。Save and close the file. vi 中,使用 :wqIn vi, use :wq.

部署应用程序Deploy the application

若要部署应用程序,请使用 kubectl apply 命令。To deploy your application, use the kubectl apply command. 此命令分析清单文件并创建定义的 Kubernetes 对象。This command parses the manifest file and creates the defined Kubernetes objects. 指定示例清单文件,如以下示例所示:Specify the sample manifest file, as shown in the following example:

kubectl apply -f azure-vote-all-in-one-redis.yaml

下面的示例输出表明已成功在 AKS 群集中创建了资源:The following example output shows the resources successfully created in the AKS cluster:

$ kubectl apply -f azure-vote-all-in-one-redis.yaml

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:

azure-vote-front   LoadBalancer   10.0.34.242   <pending>     80:30676/TCP   5s

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:

azure-vote-front   LoadBalancer   10.0.34.242   52.179.23.131   80:30676/TCP   67s

若要查看应用程序的实际效果,请打开 Web 浏览器,以转到服务的外部 IP 地址:To see the application in action, open a web browser to the external IP address of your service:

Azure 上的 Kubernetes 群集映像

如果应用程序未加载,可能是因为映像注册表存在授权问题。If the application didn't load, it might be due to an authorization problem with your image registry. 若要查看容器的状态,请使用 kubectl get pods 命令。To view the status of your containers, use the kubectl get pods command. 如果无法拉取容器映像,请参阅允许使用 Kubernetes 机密访问容器注册表If the container images can't be pulled, see allow access to Container Registry with a Kubernetes secret.

后续步骤Next steps

在本教程中,已将示例 Azure 投票应用程序部署到 AKS 中的 Kubernetes 群集。In this tutorial, a sample Azure vote application was deployed to a Kubernetes cluster in AKS. 你已了解如何:You learned how to:

  • 更新 Kubernetes 清单文件Update a Kubernetes manifest files
  • 在 Kubernetes 中运行应用程序Run an application in Kubernetes
  • 测试应用程序Test the application

转到下一教程,了解如何缩放 Kubernetes 应用程序和底层 Kubernetes 基础结构。Advance to the next tutorial to learn how to scale a Kubernetes application and the underlying Kubernetes infrastructure.