你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure Kubernetes 服务 (AKS) 是可用于快速部署和管理群集的托管式 Kubernetes 服务。 在本快速入门中,请执行以下操作:
- 使用 Bicep 部署 AKS 群集。
- 运行一个示例多容器应用程序,其中的一组微服务和 Web 前端模拟零售场景。
注意
为了开始快速预配 AKS 群集,本文介绍了仅针对评估目的部署具有默认设置的群集的步骤。 在部署生产就绪群集之前,建议熟悉我们的基线参考体系结构,考虑它如何与你的业务需求保持一致。
开始之前
- 本快速入门假设读者基本了解 Kubernetes 的概念。 有关详细信息,请参阅 Azure Kubernetes 服务 (AKS) 的 Kubernetes 核心概念。
- 需要一个具有活动订阅的 Azure 帐户。 如果你没有帐户,请免费创建一个。
- 若要详细了解如何创建 Windows Server 节点池,请参阅创建支持 Windows Server 容器的 AKS 群集。
- Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。
在 Azure Cloud Shell 中使用 Bash 环境。 有关详细信息,请参阅 Azure Cloud Shell 入门。
如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。
如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅 使用 Azure CLI 向 Azure 进行身份验证。
出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 使用和管理 Azure CLI 中的扩展。
运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade。
- 本文需要 Azure CLI 2.0.64 或更高版本。 如果你使用的是 Azure Cloud Shell,则表示已安装最新版本。
- 本文需要现成的 Azure 资源组。 如果需要创建一个,可以使用 az group create 命令。
- 若要使用 Bicep 文件创建 AKS 群集,请提供 SSH 公钥。 如果需要此资源,请参阅以下部分。 否则,请跳到查看 Bicep 文件。
- 确保用于创建群集的标识具有合适的的最低权限。 有关 AKS 访问和标识的详细信息,请参阅 Azure Kubernetes Service (AKS) 的访问和标识选项。
- 若要部署 Bicep 文件,则需要对创建的资源具有写入访问权限,并有权访问
Microsoft.Resources/deployments
资源类型上的所有操作。 例如,若要创建虚拟机,需要Microsoft.Compute/virtualMachines/write
和Microsoft.Resources/deployments/*
权限。 有关角色和权限的列表,请参阅 Azure 内置角色。
创建 SSH 密钥对
在浏览器中访问 https://shell.azure.com 以打开 Cloud Shell。
使用 az-sshkey-create Azure CLI 命令或
ssh-keygen
命令创建 SSH 密钥对。# Create an SSH key pair using Azure CLI az sshkey create --name "mySSHKey" --resource-group "myResourceGroup" # Create an SSH key pair using ssh-keygen ssh-keygen -t rsa -b 4096
有关创建 SSH 密钥的详细信息,请参阅在 Azure 中创建和管理用于身份验证的 SSH 密钥。
查阅 Bicep 文件
本快速入门中使用的 Bicep 文件来自 Azure 快速入门模板。
@description('The name of the Managed Cluster resource.')
param clusterName string = 'aks101cluster'
@description('The location of the Managed Cluster resource.')
param location string = resourceGroup().location
@description('Optional DNS prefix to use with hosted Kubernetes API server FQDN.')
param dnsPrefix string
@description('Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize.')
@minValue(0)
@maxValue(1023)
param osDiskSizeGB int = 0
@description('The number of nodes for the cluster.')
@minValue(1)
@maxValue(50)
param agentCount int = 3
@description('The size of the Virtual Machine.')
param agentVMSize string = 'standard_d2s_v3'
@description('User name for the Linux Virtual Machines.')
param linuxAdminUsername string
@description('Configure all linux machines with the SSH RSA public key string. Your key should include three parts, for example \'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm\'')
param sshRSAPublicKey string
resource aks 'Microsoft.ContainerService/managedClusters@2024-02-01' = {
name: clusterName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
dnsPrefix: dnsPrefix
agentPoolProfiles: [
{
name: 'agentpool'
osDiskSizeGB: osDiskSizeGB
count: agentCount
vmSize: agentVMSize
osType: 'Linux'
mode: 'System'
}
]
linuxProfile: {
adminUsername: linuxAdminUsername
ssh: {
publicKeys: [
{
keyData: sshRSAPublicKey
}
]
}
}
}
}
output controlPlaneFQDN string = aks.properties.fqdn
Bicep 文件中定义了以下资源:
有关更多 AKS 示例,请参阅 AKS 快速入门模板站点。
部署 Bicep 文件
- 将该 Bicep 文件另存为本地计算机上的 main.bicep。
重要
Bicep 文件将 clusterName
参数设置为字符串 aks101cluster。 如果要使用其他群集名称,请确保先将字符串更新为首选群集名称,然后再将文件保存到计算机。
使用 Azure CLI 或 Azure PowerShell 来部署该 Bicep 文件。
az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters dnsPrefix=<dns-prefix> linuxAdminUsername=<linux-admin-username> sshRSAPublicKey='<ssh-key>'
在命令中提供以下值:
- DNS 前缀:输入群集的唯一 DNS 前缀,例如 myakscluster。
- Linux 管理员用户名:输入一个用户名用于通过 SSH 进行连接,例如 azureuser。
- SSH RSA 公钥:复制并粘贴 SSH 密钥对的 public 部分(默认为 ~/.ssh/id_rsa.pub 的内容)。
创建 AKS 群集需要几分钟时间。 等待群集成功部署,然后转到下一步骤。
验证 Bicep 部署
连接到群集
若要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectl。 如果使用的是 Azure Cloud Shell,则 kubectl
已安装。
-
az aks install-cli
使用
kubectl
命令将 配置为连接到你的 Kubernetes 群集。 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
使用 kubectl get 命令验证与群集之间的连接。 此命令将返回群集节点的列表。
kubectl get nodes
以下示例输出显示在上一步创建的单个节点。 确保节点状态为“就绪”。
NAME STATUS ROLES AGE VERSION aks-agentpool-41324942-0 Ready agent 6m44s v1.12.6 aks-agentpool-41324942-1 Ready agent 6m46s v1.12.6 aks-agentpool-41324942-2 Ready agent 6m45s v1.12.6
部署应用程序
若要部署应用程序,请使用清单文件创建运行 AKS 应用商店应用程序所需的所有对象。 Kubernetes 清单文件定义群集的所需状态,例如,要运行哪些容器映像。 该清单包含以下 Kubernetes 部署和服务:
- 门店:Web 应用程序,供客户查看产品和下单。
- 产品服务:显示产品信息。
- 订单服务:下单。
- Rabbit MQ:订单队列的消息队列。
注意
不建议在没有持久性存储用于生产的情况下,运行有状态容器(例如 Rabbit MQ)。 为简单起见,建议使用托管服务,例如 Azure CosmosDB 或 Azure 服务总线。
创建名为
aks-store-quickstart.yaml
的文件,并将以下清单复制到其中:apiVersion: apps/v1 kind: Deployment metadata: name: rabbitmq spec: replicas: 1 selector: matchLabels: app: rabbitmq template: metadata: labels: app: rabbitmq spec: nodeSelector: "kubernetes.io/os": linux containers: - name: rabbitmq image: mcr.microsoft.com/mirror/docker/library/rabbitmq:3.10-management-alpine ports: - containerPort: 5672 name: rabbitmq-amqp - containerPort: 15672 name: rabbitmq-http env: - name: RABBITMQ_DEFAULT_USER value: "username" - name: RABBITMQ_DEFAULT_PASS value: "password" resources: requests: cpu: 10m memory: 128Mi limits: cpu: 250m memory: 256Mi volumeMounts: - name: rabbitmq-enabled-plugins mountPath: /etc/rabbitmq/enabled_plugins subPath: enabled_plugins volumes: - name: rabbitmq-enabled-plugins configMap: name: rabbitmq-enabled-plugins items: - key: rabbitmq_enabled_plugins path: enabled_plugins --- apiVersion: v1 data: rabbitmq_enabled_plugins: | [rabbitmq_management,rabbitmq_prometheus,rabbitmq_amqp1_0]. kind: ConfigMap metadata: name: rabbitmq-enabled-plugins --- apiVersion: v1 kind: Service metadata: name: rabbitmq spec: selector: app: rabbitmq ports: - name: rabbitmq-amqp port: 5672 targetPort: 5672 - name: rabbitmq-http port: 15672 targetPort: 15672 type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: order-service spec: replicas: 1 selector: matchLabels: app: order-service template: metadata: labels: app: order-service spec: nodeSelector: "kubernetes.io/os": linux containers: - name: order-service image: ghcr.io/azure-samples/aks-store-demo/order-service:latest ports: - containerPort: 3000 env: - name: ORDER_QUEUE_HOSTNAME value: "rabbitmq" - name: ORDER_QUEUE_PORT value: "5672" - name: ORDER_QUEUE_USERNAME value: "username" - name: ORDER_QUEUE_PASSWORD value: "password" - name: ORDER_QUEUE_NAME value: "orders" - name: FASTIFY_ADDRESS value: "0.0.0.0" resources: requests: cpu: 1m memory: 50Mi limits: cpu: 75m memory: 128Mi initContainers: - name: wait-for-rabbitmq image: busybox command: ['sh', '-c', 'until nc -zv rabbitmq 5672; do echo waiting for rabbitmq; sleep 2; done;'] resources: requests: cpu: 1m memory: 50Mi limits: cpu: 75m memory: 128Mi --- apiVersion: v1 kind: Service metadata: name: order-service spec: type: ClusterIP ports: - name: http port: 3000 targetPort: 3000 selector: app: order-service --- apiVersion: apps/v1 kind: Deployment metadata: name: product-service spec: replicas: 1 selector: matchLabels: app: product-service template: metadata: labels: app: product-service spec: nodeSelector: "kubernetes.io/os": linux containers: - name: product-service image: ghcr.io/azure-samples/aks-store-demo/product-service:latest ports: - containerPort: 3002 resources: requests: cpu: 1m memory: 1Mi limits: cpu: 1m memory: 7Mi --- apiVersion: v1 kind: Service metadata: name: product-service spec: type: ClusterIP ports: - name: http port: 3002 targetPort: 3002 selector: app: product-service --- apiVersion: apps/v1 kind: Deployment metadata: name: store-front spec: replicas: 1 selector: matchLabels: app: store-front template: metadata: labels: app: store-front spec: nodeSelector: "kubernetes.io/os": linux containers: - name: store-front image: ghcr.io/azure-samples/aks-store-demo/store-front:latest ports: - containerPort: 8080 name: store-front env: - name: VUE_APP_ORDER_SERVICE_URL value: "http://order-service:3000/" - name: VUE_APP_PRODUCT_SERVICE_URL value: "http://product-service:3002/" resources: requests: cpu: 1m memory: 200Mi limits: cpu: 1000m memory: 512Mi --- apiVersion: v1 kind: Service metadata: name: store-front spec: ports: - port: 80 targetPort: 8080 selector: app: store-front type: LoadBalancer
有关 YAML 清单文件的明细,请参阅部署和 YAML 清单。
如果在本地创建并保存 YAML 文件,则可以通过选择“上传/下载文件”按钮并选择本地文件系统中的文件,将清单文件上传到 CloudShell 中的默认目录。
使用 kubectl apply 命令部署应用程序,并指定 YAML 清单的名称。
kubectl apply -f aks-store-quickstart.yaml
以下示例输出显示部署和服务:
deployment.apps/rabbitmq created service/rabbitmq created deployment.apps/order-service created service/order-service created deployment.apps/product-service created service/product-service created deployment.apps/store-front created service/store-front created
测试应用程序
应用程序运行时,Kubernetes 服务将向 Internet 公开应用程序前端。 此过程可能需要几分钟才能完成。
使用 kubectl get pods 命令查看已部署的 Pod 的状态。 在继续操作之前,将所有 Pod 都设置为
Running
。kubectl get pods
检查应用商店前端应用程序的公共 IP 地址。 使用带有 参数的
--watch
命令来监视进度。kubectl get service store-front --watch
服务的 EXTERNAL-IP
store-front
输出最初显示为“pending”:NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE store-front LoadBalancer 10.0.100.10 <pending> 80:30025/TCP 4h4m
在 EXTERNAL-IP 地址从 pending 更改为实际公共 IP 地址后,请使用
CTRL-C
来停止kubectl
监视进程。以下示例输出显示向服务分配了有效的公共 IP 地址:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE store-front LoadBalancer 10.0.100.10 20.62.159.19 80:30025/TCP 4h5m
打开 Web 浏览器并转到服务的外部 IP 地址,以查看 Azure 应用商店应用的实际效果。
删除群集
如果不打算完成 AKS 教程,请清理不必要的资源以避免产生 Azure 费用。
使用 az group delete 命令移除资源组、容器服务和所有相关资源。
az group delete --name myResourceGroup --yes --no-wait
注意
AKS 群集是使用系统分配的托管标识创建的,这是本快速入门中使用的默认标识选项。 平台将负责管理此标识,因此你无需手动删除它。
后续步骤
在本快速入门中,你部署了一个 Kubernetes 群集,然后在其中部署了示例多容器应用程序。 此示例应用程序仅用于演示目的,并未展示出 Kubernetes 应用程序的所有最佳做法。 有关使用生产版 AKS 创建完整解决方案的指南,请参阅 AKS 解决方案指南。
若要详细了解 AKS 并演练完整的代码到部署示例,请继续阅读 Kubernetes 群集教程。