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

教程:准备用于 Azure Kubernetes 服务 (AKS) 的应用程序Tutorial: Prepare an application for Azure Kubernetes Service (AKS)

在本教程的第 1 部分(共 7 部分),将准备一个要在 Kubernetes 中使用的多容器应用程序。In this tutorial, part one of seven, a multi-container application is prepared for use in Kubernetes. 现有的开发工具(例如 Docker Compose)用于在本地生成和测试应用程序。Existing development tools such as Docker Compose are used to locally build and test an application. 学习如何:You learn how to:

  • 克隆 GitHub 中的示例应用程序源Clone a sample application source from GitHub
  • 根据示例应用程序源创建容器映像Create a container image from the sample application source
  • 在本地 Docker 环境中测试多容器应用程序Test the multi-container application in a local Docker environment

完成后,以下应用程序会在本地开发环境中运行:Once completed, the following application runs in your local development environment:

Azure 上的 Kubernetes 群集映像

在其他教程中,此容器映像会上传到 Azure 容器注册表,然后部署到 AKS 群集中。In additional tutorials, the container image is uploaded to an Azure Container Registry, and then deployed into an AKS cluster.

开始之前Before you begin

本教程假定你基本了解核心 Docker 概念,如容器、容器映像和 docker 命令。This tutorial assumes a basic understanding of core Docker concepts such as containers, container images, and docker commands. 有关容器的入门基础知识,请参阅 Docker 入门For a primer on container basics, see Get started with Docker.

若要完成本教程,需要运行 Linux 容器的本地 Docker 开发环境。To complete this tutorial, you need a local Docker development environment running Linux containers. Docker 提供的包可在 MacWindowsLinux 系统上配置 Docker。Docker provides packages that configure Docker on a Mac, Windows, or Linux system.

Azure Cloud Shell 不包含完成这些教程的每个步骤所需的 Docker 组件。Azure Cloud Shell does not include the Docker components required to complete every step in these tutorials. 因此,我们建议使用完整的 Docker 开发环境。Therefore, we recommend using a full Docker development environment.

获取应用程序代码Get application code

本教程使用的示例应用程序是一个基本的投票应用。The sample application used in this tutorial is a basic voting app. 该应用程序由前端 Web 组件和后端 Redis 实例组成。The application consists of a front-end web component and a back-end Redis instance. Web 组件打包到自定义容器映像中。The web component is packaged into a custom container image. Redis 实例使用 Docker 中心提供的未修改的映像。The Redis instance uses an unmodified image from Docker Hub.

使用 git 可将示例应用程序克隆到开发环境:Use git to clone the sample application to your development environment:

git clone https://github.com/Azure-Samples/azure-voting-app-redis.git

切换到克隆目录。Change into the cloned directory.

cd azure-voting-app-redis

目录内包含应用程序源代码、预创建的 Docker Compose 文件和 Kubernetes 清单文件。Inside the directory is the application source code, a pre-created Docker compose file, and a Kubernetes manifest file. 整套教程都会使用这些文件。These files are used throughout the tutorial set.

创建容器映像Create container images

使用 Docker Compose,可自动生成容器映像和部署多容器应用程序。Docker Compose can be used to automate building container images and the deployment of multi-container applications.

使用示例 docker-compose.yaml 文件创建容器映像、下载 Redis 映像和启动应用程序:Use the sample docker-compose.yaml file to create the container image, download the Redis image, and start the application:

docker-compose up -d

完成后,使用 docker images 命令查看创建的映像。When completed, use the docker images command to see the created images. 已下载或创建三个映像。Three images have been downloaded or created. azure-vote-front 映像包含前端应用程序,并以 nginx-flask 映像为依据。The azure-vote-front image contains the front-end application and uses the nginx-flask image as a base. redis 映像用于启动 Redis 实例。The redis image is used to start a Redis instance.

$ docker images

REPOSITORY                   TAG        IMAGE ID            CREATED             SIZE
azure-vote-front             latest     9cc914e25834        40 seconds ago      694MB
redis                        latest     a1b99da73d05        7 days ago          106MB
tiangolo/uwsgi-nginx-flask   flask      788ca94b2313        9 months ago        694MB

运行 docker ps 命令,查看正在运行的容器:Run the docker ps command to see the running containers:

$ docker ps

CONTAINER ID        IMAGE             COMMAND                  CREATED             STATUS              PORTS                           NAMES
82411933e8f9        azure-vote-front  "/usr/bin/supervisord"   57 seconds ago      Up 30 seconds       443/tcp, 0.0.0.0:8080->80/tcp   azure-vote-front
b68fed4b66b6        redis             "docker-entrypoint..."   57 seconds ago      Up 30 seconds       0.0.0.0:6379->6379/tcp          azure-vote-back

在本地测试应用程序Test application locally

若要查看正在运行的应用程序,请在本地 Web 浏览器中输入 http://localhost:8080To see the running application, enter http://localhost:8080 in a local web browser. 示例应用程序会加载,如以下示例所示:The sample application loads, as shown in the following example:

Azure 上的 Kubernetes 群集映像

清理资源Clean up resources

现已验证应用程序的功能,可停止并删除正在运行的容器。Now that the application's functionality has been validated, the running containers can be stopped and removed. 请勿删除容器映像 - 在下一教程中,会将 azure-vote-front 映像上传到 Azure 容器注册表实例。Do not delete the container images - in the next tutorial, the azure-vote-front image is uploaded to an Azure Container Registry instance.

使用 docker-compose down 命令停止并删除容器实例和资源:Stop and remove the container instances and resources with the docker-compose down command:

docker-compose down

删除本地应用程序以后,你就有了一个包含 Azure 投票应用程序的 Docker 映像 azure-vote-front ,可以在下一教程中使用。When the local application has been removed, you have a Docker image that contains the Azure Vote application, azure-vote-front, for use with the next tutorial.

后续步骤Next steps

本教程测试了应用程序并针对应用程序创建了容器映像。In this tutorial, an application was tested and container images created for the application. 你已了解如何:You learned how to:

  • 克隆 GitHub 中的示例应用程序源Clone a sample application source from GitHub
  • 根据示例应用程序源创建容器映像Create a container image from the sample application source
  • 在本地 Docker 环境中测试多容器应用程序Test the multi-container application in a local Docker environment

请转到下一教程,了解如何在 Azure 容器注册表中存储容器映像。Advance to the next tutorial to learn how to store container images in Azure Container Registry.