教學課程:準備 Azure Kubernetes Service (AKS) 的應用程式Tutorial: Prepare an application for Azure Kubernetes Service (AKS)

在本教學課程 (七個章節的第一部分) 中,多容器應用程式已準備好用於 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 Container Registry,然後部署到 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. 如需容器基本概念的入門參考資料,請參閱開始使用 DockerFor 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 Hub 使用未修改的映像。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

若要查看執行中的應用程式,請在本機網頁瀏覽器中輸入 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 Container Registry 執行個體。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-front-front,可供下一個教學課程使用。When the local application has been removed, you have a Docker image that contains the Azure Vote application, azure-front-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 Container Registry 中儲存容器映像。Advance to the next tutorial to learn how to store container images in Azure Container Registry.