在 AKS 上部署工作负载
创建 AKS 群集,使用 kubectl 和 Helm 图表部署多容器应用程序,并通过 Azure 负载均衡器服务将其公开到外部。
在 AKS 上部署工作负载 是 CoddyKit 上的免费 Cloud & IT Cert Prep 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Cloud & IT Cert Prep 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Cloud & IT Cert Prep 课程共包含 4 节课。
什么是 Azure Kubernetes Service?
Azure Kubernetes Service (AKS) 是一项托管式 Kubernetes 服务,Microsoft 负责运行和维护控制平面(API 服务器、etcd、调度器),且不收取费用。您只需为工作节点(VM)付费。AKS 会处理 Kubernetes 版本升级、节点 OS 修补、控制平面扩展,以及与 Azure 网络、存储和身份的集成。这会大幅降低在生产环境中运行 Kubernetes 的运维负担。
创建 AKS Cluster
使用 az aks create 创建 AKS Cluster,并指定节点数量、VM 大小和网络选项。AKS 会自动创建一个节点资源组,其中包含 VM、托管磁盘、NIC 和负载均衡器。推荐的网络模式是 Azure CNI ——每个容器组都会获得一个真实的 VNet IP 地址,因此无需 NAT 即可与其他 Azure 服务直接连接。
# Create an AKS cluster with 3 nodes
az aks create \
--name myAKSCluster \
--resource-group MyRG \
--location eastus \
--node-count 3 \
--node-vm-size Standard_D2s_v3 \
--enable-managed-identity \
--attach-acr mycontainerregistry \
--network-plugin azure \
--generate-ssh-keys
# Get kubectl credentials
az aks get-credentials --name myAKSCluster --resource-group MyRG节点池
AKS Cluster 可以包含多个节点池,即配置相同的 VM 组。系统节点池运行关键的 Kubernetes 系统组件(kube-system 容器组)。用户节点池运行您的应用程序工作负载。通过分离节点池,您可以混用不同的 VM SKU——例如,为 Web 应用使用通用节点池,为机器学习工作负载使用 GPU 节点池——并分别进行扩展。
# Add a GPU node pool for ML workloads
az aks nodepool add \
--cluster-name myAKSCluster \
--resource-group MyRG \
--name gpupool \
--node-count 2 \
--node-vm-size Standard_NC6s_v3 \
--node-taints sku=gpu:NoSchedule
# List node pools
az aks nodepool list \
--cluster-name myAKSCluster \
--resource-group MyRG \
-o table部署多容器应用程序
要将多层应用程序部署到 AKS,请为每一层分别编写 Kubernetes 清单文件,然后使用 kubectl apply 应用这些文件。典型部署包括:用于 Web 层的部署、用于 API 层的部署、用于连接各层的服务、用于环境配置的 ConfigMap,以及通过单一主机名将应用程序暴露到外部的 Ingress 资源。
# Apply all manifests in a directory
kubectl apply -f k8s/
# Or apply individual files
kubectl apply -f frontend-deployment.yaml
kubectl apply -f frontend-service.yaml
kubectl apply -f api-deployment.yaml
kubectl apply -f api-service.yaml
kubectl apply -f ingress.yaml
# Watch rollout status
kubectl rollout status deployment/frontend
kubectl rollout status deployment/apiIngress 和 Application Gateway
Ingress 资源定义 HTTP 路由规则,将主机名和 URL 路径映射到后端服务。与 LoadBalancer 服务(每个服务对应一个外部 IP)不同,单个 Ingress 控制器会处理所有外部 HTTP 流量,并根据规则进行路由。在 AKS 中,请使用 NGINX Ingress Controller 或 Application Gateway Ingress Controller (AGIC) 来终止 TLS,并将流量路由到多个服务。
# Ingress routing traffic to two services by path
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
tls:
- hosts: [myapp.contoso.com]
secretName: myapp-tls
rules:
- host: myapp.contoso.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service: {name: api-svc, port: {number: 80}}
- path: /
pathType: Prefix
backend:
service: {name: frontend-svc, port: {number: 80}}使用 Helm 图表打包应用程序
Kubernetes 包管理器 Helm 用于管理 Kubernetes。图表会将应用程序的所有 Kubernetes 清单(部署、服务、Ingress、ConfigMaps)打包成一个带版本号且可参数化的软件包。helm install 会使用特定环境的值部署图表。Artifact Hub 上的 Helm 仓库托管了数千个为常见基础设施(NGINX、cert-manager、Prometheus、Redis)预先构建的图表。
# Add the NGINX Ingress Controller chart repo
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
# Install NGINX Ingress Controller
helm install ingress-nginx ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--create-namespace \
--set controller.replicaCount=2
# Install your own app chart with custom values
helm install myapp ./charts/myapp -f values-prod.yaml滚动更新和回滚
通过更改容器映像标签来更新部署时,Kubernetes 会执行滚动更新:创建使用更新后映像的新容器组,并逐步终止旧容器组,从而始终保持应用程序可用。如果新版本存在问题,您可以使用 kubectl rollout undo 立即回滚到上一修订版本。Kubernetes 会为每个部署保留可配置数量的修订历史记录。
# Update the image to a new version
kubectl set image deployment/myapp \
myapp=mycontainerregistry.azurecr.io/myapp:v2.0
# Watch the rollout progress
kubectl rollout status deployment/myapp
# View rollout history
kubectl rollout history deployment/myapp
# Rollback to the previous version
kubectl rollout undo deployment/myapp
# Rollback to a specific revision
kubectl rollout undo deployment/myapp --to-revision=2Cluster Autoscaler
Cluster Autoscaler 会根据待调度的容器组和节点利用率,自动从 AKS 节点池中添加或删除工作节点。当所有节点都已满载、容器组无法调度时,Cluster Autoscaler 会配置新节点。当节点利用率较低且容器组可以合并时,它会排空并删除节点。这与 Horizontal Pod Autoscaler 相辅相成:HPA 扩展容器组,Cluster Autoscaler 扩展节点。
# Enable Cluster Autoscaler on the default node pool
az aks update \
--name myAKSCluster \
--resource-group MyRG \
--enable-cluster-autoscaler \
--min-count 2 \
--max-count 10
# Update autoscaler bounds on a specific node pool
az aks nodepool update \
--cluster-name myAKSCluster \
--resource-group MyRG \
--name nodepool1 \
--enable-cluster-autoscaler \
--min-count 3 \
--max-count 20使用 Azure Monitor 监视 AKS
启用 Azure Monitor Container Insights,即可在不部署第三方监视工具的情况下收集 AKS Cluster 的日志和指标。Container Insights 提供预先构建的仪表板,用于查看 Cluster 运行状况、节点和容器组的 CPU/内存、容器日志以及容器组实时流。它还与 Prometheus 集成以抓取自定义指标,并支持使用 Log Analytics 中的 KQL 查询所有数据。
# Enable Azure Monitor Container Insights on AKS
az aks enable-addons \
--addons monitoring \
--name myAKSCluster \
--resource-group MyRG \
--workspace-resource-id /subscriptions/.../workspaces/MyLogAnalytics
# Stream live logs from a running pod
kubectl logs -f deployment/myapp -c myapp
# Query pod resource usage
kubectl top pods --namespace defaultAKS RBAC 和 Azure Active Directory
将 AKS 与 Microsoft Entra ID 集成,即可使用 Azure AD 用户和组进行 Kubernetes RBAC。您无需管理单独的 Kubernetes 用户帐户,而是将 ClusterRole 或角色绑定分配给 Entra ID 对象 ID。开发人员运行 kubectl 时,AKS 会验证其 Entra ID 令牌。这样可以实现集中式身份管理,并与条件访问和 MFA 策略集成。
# Enable Entra ID RBAC on an AKS cluster
az aks update \
--name myAKSCluster \
--resource-group MyRG \
--enable-azure-rbac
# Assign a built-in AKS role to an Entra group
az role assignment create \
--role 'Azure Kubernetes Service RBAC Reader' \
--assignee '<Entra-Group-Object-ID>' \
--scope /subscriptions/.../resourceGroups/MyRG/providers/Microsoft.ContainerService/managedClusters/myAKSCluster用于容器组级安全的网络策略
网络策略是用于控制容器组之间通信权限的 Kubernetes 资源。默认情况下,Cluster 中的所有容器组都可以访问其他所有容器组;网络策略则在容器组级别提供防火墙功能。在 AKS 上,请启用 Azure 网络策略或 Calico 来强制执行策略。一种常见模式是使用默认拒绝策略阻止所有容器组间流量,然后针对应用程序所需的特定路径显式配置允许策略。
# Default deny all ingress to pods in namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: production
spec:
podSelector: {} # Matches all pods
policyTypes:
- Ingress
# Allow API pods to receive from frontend pods only
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow-frontend
spec:
podSelector: {matchLabels: {app: api}}
ingress:
- from:
- podSelector: {matchLabels: {app: frontend}}快速检查
测试您对本课 Microsoft Azure Fundamentals (AZ-900) 概念的理解。
课程回顾
在本课中,您学习了:AKS 是一项托管式 Kubernetes 服务,Microsoft 负责运行控制平面;Helm 图表用于打包包含多个资源的 Kubernetes 应用程序,以便进行可重复部署;Cluster Autoscaler 会根据容器组的调度需求动态添加和删除节点。接下来,我们将学习 Azure Functions 的触发器和绑定。
常见问题解答
「在 AKS 上部署工作负载」课时是免费的吗?
是的 — 「在 AKS 上部署工作负载」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Cloud & IT Cert Prep 课程的其余内容,请升级到 CoddyKit PRO。 Cloud & IT Cert Prep 课程共包含 4 节课。
「在 AKS 上部署工作负载」这节课中我会学到什么?
创建 AKS 群集,使用 kubectl 和 Helm 图表部署多容器应用程序,并通过 Azure 负载均衡器服务将其公开到外部。 你通过在浏览器中直接运行的动手代码来练习 Cloud & IT Cert Prep,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Cloud & IT Cert Prep 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Cloud & IT Cert Prep 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「在 AKS 上部署工作负载」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Cloud & IT Cert Prep 课中编写并运行代码吗?
能。每节 Cloud & IT Cert Prep 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Azure 容器注册表
- Azure 容器实例
- Azure Kubernetes 概念
- 在 AKS 上部署工作负载