Azure 容器服务 #

Azure 容器服务概览 #

Azure 提供多种容器服务,满足不同场景的需求。

text
┌─────────────────────────────────────────────────────────────┐
│                    Azure 容器服务                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  Azure Container Instances (ACI)                             │
│  ├── 最简单的容器运行方式                                   │
│  ├── 按秒计费                                               │
│  ├── 适合:简单任务、测试                                   │
│  └── 无编排                                                 │
│                                                             │
│  Azure Kubernetes Service (AKS)                              │
│  ├── 托管 Kubernetes                                        │
│  ├── 企业级编排                                             │
│  ├── 适合:生产环境、微服务                                 │
│  └── 自动扩展和管理                                         │
│                                                             │
│  Azure Container Apps                                        │
│  ├── 无服务器容器                                           │
│  ├── 简化的编排                                             │
│  ├── 适合:事件驱动应用                                     │
│  └── 基于 AKS                                               │
│                                                             │
│  Azure Container Registry (ACR)                              │
│  ├── 容器镜像仓库                                           │
│  ├── 与 Azure 服务集成                                      │
│  └── 安全扫描                                               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Azure Container Instances #

创建容器实例 #

bash
# 创建资源组
az group create --name myResourceGroup --location eastus

# 创建容器实例
az container create \
  --resource-group myResourceGroup \
  --name mycontainer \
  --image nginx:latest \
  --dns-name-label mycontainer-demo \
  --ports 80

# 查看容器状态
az container show \
  --resource-group myResourceGroup \
  --name mycontainer \
  --query "{FQDN:ipAddress.fqdn,ProvisioningState:provisioningState}" \
  --out table

# 查看容器日志
az container logs \
  --resource-group myResourceGroup \
  --name mycontainer

容器组 #

bash
# 创建多容器组 (YAML)
cat <<EOF > container-group.yaml
apiVersion: 2019-12-01
location: eastus
name: myContainerGroup
properties:
  containers:
  - name: myapp
    properties:
      image: nginx:latest
      ports:
      - port: 80
      resources:
        requests:
          cpu: 1
          memoryInGB: 1.5
  - name: mysidecar
    properties:
      image: redis:latest
      resources:
        requests:
          cpu: 0.5
          memoryInGB: 0.5
  osType: Linux
  ipAddress:
    type: Public
    ports:
    - protocol: TCP
      port: 80
type: Microsoft.ContainerInstance/containerGroups
EOF

# 部署容器组
az container create \
  --resource-group myResourceGroup \
  --file container-group.yaml

Azure Container Registry #

创建容器注册表 #

bash
# 创建容器注册表
az acr create \
  --resource-group myResourceGroup \
  --name myRegistry \
  --sku Basic \
  --admin-enabled true

# 登录注册表
az acr login --name myRegistry

# 查看注册表信息
az acr show --name myRegistry --query loginServer --output tsv

推送镜像 #

bash
# 标记镜像
docker tag myapp:v1 myregistry.azurecr.io/myapp:v1

# 推送镜像
docker push myregistry.azurecr.io/myapp:v1

# 列出镜像
az acr repository list --name myRegistry --output table

# 查看镜像标签
az acr repository show-tags --name myRegistry --repository myapp --output table

构建镜像 #

bash
# 使用 ACR Tasks 构建
az acr build \
  --registry myRegistry \
  --image myapp:v1 \
  .

# 从 Git 构建
az acr build \
  --registry myRegistry \
  --image myapp:v1 \
  https://github.com/user/repo.git

Azure Kubernetes Service (AKS) #

创建 AKS 集群 #

bash
# 创建资源组
az group create --name myResourceGroup --location eastus

# 创建 AKS 集群
az aks create \
  --resource-group myResourceGroup \
  --name myAKSCluster \
  --node-count 3 \
  --node-vm-size Standard_D2s_v3 \
  --generate-ssh-keys \
  --enable-managed-identity

# 获取集群凭据
az aks get-credentials \
  --resource-group myResourceGroup \
  --name myAKSCluster

# 验证连接
kubectl get nodes

AKS 集群配置 #

text
┌─────────────────────────────────────────────────────────────┐
│                    AKS 集群配置                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  节点池                                                      │
│  ├── 系统节点池:运行系统 Pod                                │
│  ├── 用户节点池:运行工作负载                                │
│  ├── 支持多种 VM 大小                                       │
│  └── 可配置自动扩展                                         │
│                                                             │
│  网络模式                                                    │
│  ├── Kubenet:简单,IP 节省                                 │
│  ├── Azure CNI:完整 Pod IP                                 │
│  └── CNI Overlay:高效 IP 使用                              │
│                                                             │
│  认证和授权                                                  │
│  ├── 托管身份                                               │
│  ├── Azure AD 集成                                          │
│  └── RBAC                                                   │
│                                                             │
│  安全                                                        │
│  ├── 私有集群                                               │
│  ├── 网络策略                                               │
│  └── Azure Policy                                           │
│                                                             │
└─────────────────────────────────────────────────────────────┘

部署应用 #

yaml
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 500m
            memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
bash
# 部署应用
kubectl apply -f deployment.yaml

# 查看部署状态
kubectl get deployments
kubectl get pods
kubectl get services

# 扩展副本
kubectl scale deployment nginx-deployment --replicas=5

# 更新镜像
kubectl set image deployment/nginx-deployment nginx=nginx:1.22

# 查看滚动更新状态
kubectl rollout status deployment/nginx-deployment

自动扩展 #

bash
# 安装 Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# 创建 HPA
kubectl autoscale deployment nginx-deployment --cpu-percent=50 --min=3 --max=10

# 查看 HPA 状态
kubectl get hpa

使用 ACR 镜像 #

bash
# 授予 AKS 访问 ACR 的权限
az aks update \
  --name myAKSCluster \
  --resource-group myResourceGroup \
  --attach-acr myRegistry

# 在部署中使用 ACR 镜像
kubectl create deployment myapp --image=myregistry.azurecr.io/myapp:v1

Azure Container Apps #

创建 Container Apps 环境 #

bash
# 安装 Container Apps 扩展
az extension add --name containerapp

# 创建环境
az containerapp env create \
  --name myContainerAppsEnv \
  --resource-group myResourceGroup \
  --location eastus

部署容器应用 #

bash
# 创建容器应用
az containerapp create \
  --name myapp \
  --resource-group myResourceGroup \
  --environment myContainerAppsEnv \
  --image nginx:latest \
  --target-port 80 \
  --ingress 'external' \
  --query properties.configuration.ingress.fqdn

# 更新容器应用
az containerapp update \
  --name myapp \
  --resource-group myResourceGroup \
  --image nginx:1.22

# 配置自动扩展
az containerapp update \
  --name myapp \
  --resource-group myResourceGroup \
  --scale-rule-name http-scale \
  --scale-rule-type http \
  --scale-rule-metadata concurrentRequests=100

监控和日志 #

启用监控 #

bash
# 为 AKS 启用监控
az aks enable-addons \
  --resource-group myResourceGroup \
  --name myAKSCluster \
  --addons monitoring \
  --workspace-resource-id <workspace-id>

# 查看 Pod 日志
kubectl logs <pod-name>

# 查看事件
kubectl get events --sort-by='.lastTimestamp'

Container Insights #

text
┌─────────────────────────────────────────────────────────────┐
│                    Container Insights                        │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  监控内容                                                    │
│  ├── 容器 CPU/内存使用                                      │
│  ├── Pod 状态                                               │
│  ├── 节点性能                                               │
│  ├── 网络流量                                               │
│  └── 日志收集                                               │
│                                                             │
│  查看方式                                                    │
│  ├── Azure Monitor                                          │
│  ├── Log Analytics                                          │
│  └── Grafana 仪表板                                         │
│                                                             │
└─────────────────────────────────────────────────────────────┘

最佳实践 #

安全建议 #

text
┌─────────────────────────────────────────────────────────────┐
│                    安全最佳实践                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 镜像安全                                                 │
│     ├── 使用可信基础镜像                                    │
│     ├── 启用 ACR 安全扫描                                   │
│     └── 定期更新镜像                                        │
│                                                             │
│  2. 网络安全                                                 │
│     ├── 使用网络策略                                        │
│     ├── 私有集群                                            │
│     └── 限制入站/出站流量                                   │
│                                                             │
│  3. 访问控制                                                 │
│     ├── 使用 Azure AD 认证                                  │
│     ├── 最小权限 RBAC                                       │
│     └── 使用托管身份                                        │
│                                                             │
│  4. 密钥管理                                                 │
│     ├── 使用 Azure Key Vault                                │
│     ├── 不在镜像中存储机密                                  │
│     └── 使用 Kubernetes Secrets                             │
│                                                             │
└─────────────────────────────────────────────────────────────┘

成本优化 #

text
┌─────────────────────────────────────────────────────────────┐
│                    成本优化建议                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 选择正确的 VM 大小                                       │
│     └── 根据工作负载选择                                    │
│                                                             │
│  2. 使用自动扩展                                             │
│     └── 按需调整节点数量                                    │
│                                                             │
│  3. 使用 Spot 实例                                           │
│     └── 适合可中断工作负载                                  │
│                                                             │
│  4. 清理未使用资源                                           │
│     └── 定期审查和清理                                      │
│                                                             │
│  5. 使用预留实例                                             │
│     └── 稳定工作负载                                        │
│                                                             │
└─────────────────────────────────────────────────────────────┘

下一步 #

现在你已经了解了 Azure 容器服务,接下来学习 存储服务 掌握 Azure 存储解决方案!

最后更新:2026-03-29