使用 Azure Application Gateway + AGIC 暴露 Kube Service

发布: (2026年3月1日 GMT+8 15:01)
4 分钟阅读
原文: Dev.to

I’m happy to translate the article for you, but I need the actual text of the post. Could you please paste the content you’d like translated (excluding the source link you already provided)? Once I have the text, I’ll translate it into Simplified Chinese while preserving all formatting, markdown, and code blocks.

🔷 前置条件

确保已安装 Azure CLI 和 kubectl

az version
kubectl version --client

登录 Azure

az login

设置变量

RG=rg-aks-agic-demo
LOCATION=eastus2
AKS_NAME=aks-agic-demo
APPGW_NAME=appgw-agic-demo
VNET_NAME=vnet-agic-demo
AKS_SUBNET=aks-subnet
APPGW_SUBNET=appgw-subnet

🟢 第一步 — 创建资源组

az group create \
  --name $RG \
  --location $LOCATION

🟢 第2步 — 创建包含 2 个子网的 VNet

⚠️ 应用网关必须位于专用子网中。

创建 VNet 和 AKS 子网:

az network vnet create \
  --resource-group $RG \
  --name $VNET_NAME \
  --address-prefix 10.0.0.0/8 \
  --subnet-name $AKS_SUBNET \
  --subnet-prefix 10.240.0.0/16

创建应用网关子网:

az network vnet subnet create \
  --resource-group $RG \
  --vnet-name $VNET_NAME \
  --name $APPGW_SUBNET \
  --address-prefix 10.241.0.0/16

🟢 步骤 3 — 为应用网关创建公共 IP

az network public-ip create \
  --resource-group $RG \
  --name appgw-pip \
  --sku Standard \
  --allocation-method Static

🟢 第4步 — 创建应用网关 (WAF v2)

az network application-gateway create \
  --name $APPGW_NAME \
  --resource-group $RG \
  --location $LOCATION \
  --sku Standard_v2 \
  --capacity 2 \
  --vnet-name $VNET_NAME \
  --subnet $APPGW_SUBNET \
  --public-ip-address appgw-pip \
  --priority 100

🟢 第5步 — 获取 AKS 子网 ID

AKS_SUBNET_ID=$(az network vnet subnet show \
  --resource-group $RG \
  --vnet-name $VNET_NAME \
  --name $AKS_SUBNET \
  --query id -o tsv)

Source:

🟢 第6步 — 创建启用 AGIC 的 AKS

首先,获取 Application Gateway 资源 ID:

APPGW_ID=$(az network application-gateway show \
  --name $APPGW_NAME \
  --resource-group $RG \
  --query id -o tsv)

现在创建 AKS 集群并附加现有的 Application Gateway:

az aks create \
  --resource-group $RG \
  --name $AKS_NAME \
  --network-plugin azure \
  --vnet-subnet-id $AKS_SUBNET_ID \
  --enable-addons ingress-appgw \
  --appgw-id $APPGW_ID \
  --node-count 2 \
  --generate-ssh-keys

这会自动在 AKS 中部署 AGIC 插件。

🟢 第7步 — 获取 AKS 凭据

az aks get-credentials \
  --resource-group $RG \
  --name $AKS_NAME

验证 AGIC Pod 正在运行:

kubectl get pods -n kube-system

您应该会看到一个名为 ingress-appgw-... 的 Pod。

🟢 第8步 — 部署示例应用程序

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port 80

验证服务:

kubectl get svc

🟢 步骤 9 — 创建 Ingress 资源

创建一个名为 ingress.yaml 的文件,内容如下:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80

应用该清单:

kubectl apply -f ingress.yaml

🟢 第10步 — 获取公共 IP

az network public-ip show \
  --resource-group $RG \
  --name appgw-pip \
  --query ipAddress \
  --output tsv

等待 2–3 分钟,让 AGIC 同步配置。

在浏览器中打开该 IP 地址:

http://<public-ip>

您应该会看到默认的 NGINX 欢迎页面:

Welcome to nginx!

🔷 刚刚发生了什么(Enterprise Flow)

Internet

Application Gateway

AGIC watches Ingress

Routes to AKS Service

Pod

流量从不直接到达 AKS 节点;应用网关会先对其进行过滤。

🔷 验证 AGIC 正在同步

检查 AGIC 日志:

kubectl logs -n kube-system \
  deploy/ingress-appgw-deployment

您应该会看到有关配置更新的消息。

注意: 如果您遇到类似以下的错误
E0301 06:36:34.657523 1 client.go:191] Code="ErrorApplicationGatewayForbidden"
请参阅故障排除指南。

🧹 清理

az group delete --name $RG --yes --no-wait

🎯 您现在拥有

  • ✅ 集群外的第七层路由
  • ✅ AKS 私有节点
  • ✅ 企业级入口模式
0 浏览
Back to Blog

相关文章

阅读更多 »

当工作成为心理健康风险时

markdown !Ravi Mishrahttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fu...