教程:准备用于 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 投票应用的屏幕截图,该应用在本地运行并在本地 Web 浏览器中打开

在后面的教程中,此容器映像会上传到 Azure 容器注册表,然后部署到 AKS 群集中。In later 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 本地 Shell 不包含完成这些教程的每个步骤所需的 Docker 组件。Azure local 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

本教程中使用的示例应用程序是一个包含前端 Web 组件和后端 Redis 实例的基本投票应用。The sample application used in this tutorial is a basic voting app consisting 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. 目录的内容和结构如下所示:The contents and structure of the directory are as follows:

azure-voting-app-redis
│   azure-vote-all-in-one-redis.yaml
│   docker-compose.yaml
│   LICENSE
│   README.md
│
├───azure-vote
│   │   app_init.supervisord.conf
│   │   Dockerfile
│   │   Dockerfile-for-app-service
│   │   sshd_config
│   │
│   └───azure-vote
│       │   config_file.cfg
│       │   main.py
│       │
│       ├───static
│       │       default.css
│       │
│       └───templates
│               index.html
│
└───jenkins-tutorial
        config-jenkins.sh
        deploy-jenkins-vm.sh

创建容器映像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
mcr.microsoft.com/azuredocs/azure-vote-front   v1                  84b41c268ad9        9 seconds ago       944MB
mcr.microsoft.com/oss/bitnami/redis            6.0.8               3a54a920bb6c        2 days ago          103MB
tiangolo/uwsgi-nginx-flask                     python3.6           a16ce562e863        6 weeks ago         944MB

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

$ docker ps

CONTAINER ID        IMAGE                                             COMMAND                  CREATED             STATUS              PORTS                           NAMES
d10e5244f237        mcr.microsoft.com/azuredocs/azure-vote-front:v1   "/entrypoint.sh /sta…"   3 minutes ago       Up 3 minutes        443/tcp, 0.0.0.0:8080->80/tcp   azure-vote-front
21574cb38c1f        mcr.microsoft.com/oss/bitnami/redis:6.0.8         "/opt/bitnami/script…"   3 minutes ago       Up 3 minutes        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 投票应用的屏幕截图,该应用在本地运行并在本地 Web 浏览器中打开

清理资源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.