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

快速入门:使用 Azure CLI 部署 Azure Kubernetes 服务 (AKS) 群集Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Azure CLI

在本快速入门中,将使用 Azure CLI 部署 Azure Kubernetes 服务 (AKS) 群集。In this quickstart, you deploy an Azure Kubernetes Service (AKS) cluster using the Azure CLI. AKS 是可用于快速部署和管理群集的托管式 Kubernetes 服务。AKS is a managed Kubernetes service that lets you quickly deploy and manage clusters. 该群集中将运行一个包含 Web 前端和 Redis 实例的多容器应用程序。A multi-container application that includes a web front end and a Redis instance is run in the cluster. 然后,你将了解如何监视群集的运行状况,以及监视运行该应用程序的 Pod。You then see how to monitor the health of the cluster and pods that run your application.

如果希望使用 Windows Server 容器(目前在 AKS 中以预览版提供),请参阅创建支持 Windows Server 容器的 AKS 群集If you want to use Windows Server containers (currently in preview in AKS), see Create an AKS cluster that supports Windows Server containers.

浏览到 Azure Vote 的图像

本快速入门假设读者基本了解 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).

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 通过 Cloud Shell 可以将 bashPowerShell 与 Azure 服务配合使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To launch Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 选择“试用” 不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell” 按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上方菜单栏中的“Cloud Shell” 按钮。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Launch Cloud Shell.

  2. 选择代码块上的“复制”按钮 以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上使用 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上使用 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.

  4. Enter 运行此代码。Press Enter to run the code.

如果选择在本地安装并使用 CLI,本快速入门要求运行 Azure CLI 2.0.64 版或更高版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.64 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

备注

如果在本地运行此快速入门中的命令(而不是 Azure Cloud Shell),请确保以管理员身份运行命令。If running the commands in this quickstart locally (instead of Azure Cloud Shell), ensure you run the commands as administrator.

创建资源组Create a resource group

Azure 资源组是在其中部署和管理 Azure 资源的逻辑组。An Azure resource group is a logical group in which Azure resources are deployed and managed. 创建资源组时,系统会要求你指定一个位置,When you create a resource group, you are asked to specify a location. 此位置是资源组元数据的存储位置,如果你在创建资源期间未指定另一个区域,则它还是你的资源在 Azure 中的运行位置。This location is where resource group metadata is stored, it is also where your resources run in Azure if you don't specify another region during resource creation. 使用 az group create 命令创建资源组。Create a resource group using the az group create command.

以下示例在“eastus” 位置创建名为“myResourceGroup” 的资源组。The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

以下示例输出显示已成功创建资源组:The following example output shows the resource group created successfully:

{
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

创建 AKS 群集Create AKS cluster

使用 az aks create 命令创建 AKS 群集。Use the az aks create command to create an AKS cluster. 以下示例创建一个具有一个节点的名为 myAKSCluster 的群集。The following example creates a cluster named myAKSCluster with one node. 也可通过 --enable-addons monitoring 参数启用用于容器的 Azure Monitor。Azure Monitor for containers is also enabled using the --enable-addons monitoring parameter. 此操作将需要几分钟才能完成。This will take several minutes to complete.

az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys

片刻之后,该命令将会完成,并返回有关群集的 JSON 格式信息。After a few minutes, the command completes and returns JSON-formatted information about the cluster.

连接至群集Connect to the cluster

若要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectlTo manage a Kubernetes cluster, you use kubectl, the Kubernetes command-line client. 如果使用的是 Azure Cloud Shell,则 kubectl 已安装。If you use Azure Cloud Shell, kubectl is already installed. 若要在本地安装 kubectl,请使用 az aks install-cli 命令:To install kubectl locally, use the az aks install-cli command:

az aks install-cli

若要将 kubectl 配置为连接到 Kubernetes 群集,请使用 az aks get-credentials 命令。To configure kubectl to connect to your Kubernetes cluster, use the az aks get-credentials command. 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。This command downloads credentials and configures the Kubernetes CLI to use them.

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

若要验证到群集的连接,请使用 kubectl get 命令返回群集节点的列表。To verify the connection to your cluster, use the kubectl get command to return a list of the cluster nodes.

kubectl get nodes

以下示例输出显示在上一步创建的单个节点。The following example output shows the single node created in the previous steps. 请确保节点的状态为 ReadyMake sure that the status of the node is Ready:

NAME                       STATUS   ROLES   AGE     VERSION
aks-nodepool1-31718369-0   Ready    agent   6m44s   v1.12.8

运行应用程序Run 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.

提示

在本快速入门中,请手动创建应用程序清单并将其部署到 AKS 群集。In this quickstart, you manually create and deploy your application manifests to the AKS cluster. 在更实际的方案中,可以使用 Azure Dev Spaces 直接在 AKS 群集中快速地循环访问代码并对其进行调试。In more real-world scenarios, you can use Azure Dev Spaces to rapidly iterate and debug your code directly in the AKS cluster. 可以跨 OS 平台和开发环境使用 Dev Spaces,并可与团队中的他人进行协作。You can use Dev Spaces across OS platforms and development environments, and work together with others on your team.

创建名为 azure-vote.yaml 的文件,并将其复制到以下 YAML 定义中。Create a file named azure-vote.yaml and copy in the following YAML definition. 如果使用 Azure Cloud Shell,则可以使用 vinano 来创建此文件,就像在虚拟或物理系统中操作一样:If you use the Azure Cloud Shell, this file can be created using vi or nano as if working on a virtual or physical system:

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: microsoft/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   6s

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.37.27   52.179.23.131   80:30572/TCP   2m

若要查看 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 Vote 的图像

创建 AKS 群集时,即已启用了用于容器的 Azure Monitor 来捕获群集节点和 Pod 的运行状况指标。When the AKS cluster was created, Azure Monitor for containers was enabled to capture health metrics for both the cluster nodes and pods. Azure 门户提供这些运行状况指标。These health metrics are available in the Azure portal.

删除群集Delete the cluster

若要避免 Azure 费用,应清除不需要的资源。To avoid Azure charges, you should clean up unneeded resources. 如果不再需要群集,可以使用 az group delete 命令删除资源组、容器服务及所有相关资源。When the cluster is no longer needed, use the az group delete command to remove the resource group, container service, and all related resources.

az group delete --name myResourceGroup --yes --no-wait

备注

删除群集时,AKS 群集使用的 Azure Active Directory 服务主体不会被删除。When you delete the cluster, the Azure Active Directory service principal used by the AKS cluster is not removed. 有关如何删除服务主体的步骤,请参阅 AKS 服务主体的注意事项和删除For steps on how to remove the service principal, see AKS service principal considerations and deletion.

获取代码Get the code

本快速入门使用预先创建的容器映像创建了 Kubernetes 部署。In this quickstart, pre-created container images were used to create a Kubernetes deployment. GitHub 上提供了相关的应用程序代码、Dockerfile 和 Kubernetes 清单文件。The related application code, Dockerfile, and Kubernetes manifest file are available on GitHub.

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

后续步骤Next steps

在本快速入门中,部署了 Kubernetes 群集,并向该群集部署了多容器应用程序。In this quickstart, you deployed a Kubernetes cluster and deployed a multi-container application to it. 还可以访问 AKS 群集的 Kubernetes Web 仪表板You can also access the Kubernetes web dashboard for your AKS cluster.

若要详细了解 AKS 并演练部署示例的完整代码,请继续阅读“Kubernetes 群集”教程。To learn more about AKS, and walk through a complete code to deployment example, continue to the Kubernetes cluster tutorial.