🎯 Mục Tiêu Lab
Tạo VNet với subnet riêng cho AppGW và subnet backend
Deploy Application Gateway v2 (Standard_v2 SKU) với Public IP Standard
Tạo VM backend chạy Nginx (hoặc dùng App Service)
Cấu hình Backend Pool, HTTP Settings, Listener, Routing Rule
Kiểm tra Health Probe và truy cập web qua AppGW public IP
Chuẩn bị sẵn sàng cho WAF Policy (Lab 27)
📋 Chuẩn Bị
- Azure subscription Contributor role
- Azure CLI đã cài và đăng nhập
- Region: southeastasia
- HTTP/HTTPS load balancing concept
- Backend pool, listener, routing rule
- Health probe cơ bản
🏗️ Kịch Bản
Web app chạy trên VM backend trong subnet private. Không expose VM trực tiếp ra Internet. Application Gateway v2 đứng giữa, nhận HTTP từ Internet và forward vào backend — Layer 7 load balancing với health check tự động.
Internet
│ HTTP :80
▼
[App Gateway v2] Public IP (Standard)
│ snet-appgw: 10.0.1.0/24
│ Listener: port 80
│ Routing Rule → Backend Pool
▼
[Backend VM] Nginx :80
│ snet-backend: 10.0.2.0/24
│ Private IP only
└─ Health Probe: GET / → HTTP 200
🧪 Các Bước Thực Hiện
Tạo VNet, Subnet và Backend VM
Application Gateway cần subnet riêng (không share với VM). Backend VM cài Nginx để test.
- 1.1Tạo RG:
rg-lab26-appgw→ Region: Southeast Asia - 1.2VNet:
vnet-lab26→ 10.0.0.0/16 → Subnetsnet-appgw10.0.1.0/24 (cho AppGW) - 1.3Subnet
snet-backend10.0.2.0/24 (cho backend VMs) - 1.4Tạo VM Ubuntu → snet-backend → Public IP: None → NSG: cho phép port 80 inbound từ VNet
RG="rg-lab26-appgw"
LOCATION="southeastasia"
az group create --name $RG --location $LOCATION
# Tạo VNet với 2 subnet
az network vnet create \
--resource-group $RG --name vnet-lab26 \
--address-prefix 10.0.0.0/16 \
--subnet-name snet-appgw --subnet-prefix 10.0.1.0/24
az network vnet subnet create \
--resource-group $RG --vnet-name vnet-lab26 \
--name snet-backend --address-prefix 10.0.2.0/24
# Tạo backend VM chạy Nginx, không có public IP
az vm create \
--resource-group $RG \
--name vm-backend-lab26 \
--image Ubuntu2204 \
--vnet-name vnet-lab26 \
--subnet snet-backend \
--public-ip-address "" \
--admin-username azureuser \
--generate-ssh-keys \
--size Standard_B1s \
--custom-data '#!/bin/bash
apt-get update -y
apt-get install -y nginx
echo "Backend VM - Lab 26 AZ-500
Served via Application Gateway
" > /var/www/html/index.html
systemctl enable nginx
systemctl start nginx'
# Mở port 80 cho AppGW trong NSG
az vm open-port \
--resource-group $RG \
--name vm-backend-lab26 \
--port 80
# Lấy private IP backend VM
BACKEND_IP=$(az vm show \
--resource-group $RG \
--name vm-backend-lab26 \
--show-details --query privateIps --output tsv)
echo "Backend VM Private IP: $BACKEND_IP"
Tạo Public IP và Application Gateway v2
Application Gateway v2 cần Public IP Standard SKU. Deploy mất ~5–10 phút.
- 2.1Application gateways → + Create → RG: rg-lab26-appgw → Name:
appgw-lab26 - 2.2Tier: Standard V2 → Enable autoscaling: Yes → Min instances: 0, Max: 2 → Zone: None (lab)
- 2.3VNet: vnet-lab26 → Subnet: snet-appgw → Public IP → New → Name: pip-appgw-lab26 → SKU: Standard
- 2.4Tab Backends → + Add a backend pool → Name:
bp-nginx→ Target type: IP → IP: BACKEND_VM_IP - 2.5Tab Configuration → + Add routing rule → Rule name:
rule-http→ Listener: port 80 → Backend: bp-nginx → Review + Create
RG="rg-lab26-appgw"
# Lấy backend IP
BACKEND_IP=$(az vm show \
--resource-group $RG --name vm-backend-lab26 \
--show-details --query privateIps --output tsv)
# Tạo Public IP Standard cho AppGW
az network public-ip create \
--resource-group $RG \
--name pip-appgw-lab26 \
--sku Standard \
--allocation-method Static
# Tạo Application Gateway Standard_v2 với tất cả thành phần
# (mất ~5-10 phút)
az network application-gateway create \
--resource-group $RG \
--name appgw-lab26 \
--location southeastasia \
--sku Standard_v2 \
--capacity 1 \
--vnet-name vnet-lab26 \
--subnet snet-appgw \
--public-ip-address pip-appgw-lab26 \
--frontend-port 80 \
--http-settings-port 80 \
--http-settings-protocol Http \
--routing-rule-type Basic \
--servers "$BACKEND_IP" \
--priority 1
echo "Application Gateway deploy started. Waiting ~10 minutes..."
# Kiểm tra trạng thái
az network application-gateway show \
--resource-group $RG \
--name appgw-lab26 \
--query "provisioningState" --output tsv
Kiểm Tra Health Probe và Kết Nối
Sau khi deploy xong, kiểm tra backend health và truy cập web qua AppGW public IP.
RG="rg-lab26-appgw"
# Lấy Public IP của AppGW
APPGW_IP=$(az network public-ip show \
--resource-group $RG --name pip-appgw-lab26 \
--query ipAddress --output tsv)
echo "AppGW Public IP: $APPGW_IP"
echo "Test: curl http://$APPGW_IP"
# Test kết nối qua curl
curl -s http://$APPGW_IP
# Kiểm tra backend health state
az network application-gateway show-backend-health \
--resource-group $RG \
--name appgw-lab26 \
--query "backendAddressPools[0].backendHttpSettingsCollection[0].servers[0].health" \
--output tsv
# Xem toàn bộ cấu hình AppGW
az network application-gateway show \
--resource-group $RG \
--name appgw-lab26 \
--query "{sku:sku.name,tier:sku.tier,state:provisioningState}" \
--output table
Healthy. curl http://APPGW_IP trả về HTML: "Backend VM - Lab 26 AZ-500". Truy cập browser http://APPGW_IP thấy trang web.
Cấu Hình Custom Health Probe và HTTP Settings
Custom health probe kiểm tra endpoint cụ thể thay vì root path. Connection draining giúp graceful shutdown.
- 4.1appgw-lab26 → Health probes → + Add → Name:
probe-nginx→ Protocol: HTTP → Host:10.0.2.x(backend IP) - 4.2Path:
/→ Interval: 30s → Timeout: 30s → Unhealthy threshold: 3 → Save - 4.3HTTP Settings → appgw-lab26BackendHttpSettings → gắn probe-nginx → Connection draining: Enable, 60s → Save
RG="rg-lab26-appgw"
BACKEND_IP=$(az vm show \
--resource-group $RG --name vm-backend-lab26 \
--show-details --query privateIps --output tsv)
# Thêm custom health probe
az network application-gateway probe create \
--resource-group $RG \
--gateway-name appgw-lab26 \
--name probe-nginx \
--protocol Http \
--host "$BACKEND_IP" \
--path "/" \
--interval 30 \
--timeout 30 \
--threshold 3
# Cập nhật HTTP settings gắn probe
az network application-gateway http-settings update \
--resource-group $RG \
--gateway-name appgw-lab26 \
--name appGatewayBackendHttpSettings \
--probe probe-nginx \
--connection-draining-timeout 60
# Kiểm tra lại backend health sau khi gắn probe
az network application-gateway show-backend-health \
--resource-group $RG \
--name appgw-lab26 \
--output table
📊 Kết Quả Đầu Ra Lab 26
provisioningState: Succeeded, SKU: Standard_v2
Health probe GET / → HTTP 200 từ Nginx backend
curl http://APPGW_IP → "Backend VM - Lab 26 AZ-500"
AppGW Standard_v2 có thể upgrade lên WAF_v2 để gắn WAF Policy
🧹 Dọn Dẹp Tài Nguyên
Lưu ý: Nếu tiếp tục Lab 27, giữ lại rg-lab26-appgw. Chỉ xóa khi hoàn thành Lab 27.
# Chỉ xóa sau khi hoàn thành Lab 27
az group delete --name rg-lab26-appgw --yes --no-wait
❓ Câu Hỏi Ôn Tập
1. Application Gateway v2 khác gì v1? Tại sao v1 bị retire và không được dùng nữa?
Gợi ý: v2 có autoscaling, zone redundancy, static VIP, header rewrite, custom error pages. v1 retire 28/04/2026 — Microsoft yêu cầu migrate sang v2.
2. Subnet của Application Gateway cần có kích thước tối thiểu là bao nhiêu? Tại sao?
Gợi ý: Tối thiểu /24 (256 địa chỉ) cho v2. AppGW dùng nhiều IP private khi autoscale — Microsoft khuyến nghị /24 cho production.
3. Health Probe hoạt động như thế nào? Nếu backend không pass health check thì điều gì xảy ra?
Gợi ý: AppGW định kỳ gửi HTTP request tới backend. Nếu fail (timeout hoặc non-2xx) vượt threshold → backend bị đánh dấu Unhealthy → không nhận traffic mới.
4. Sự khác nhau giữa Standard_v2 và WAF_v2 SKU của Application Gateway?
Gợi ý: WAF_v2 = Standard_v2 + Web Application Firewall. Có thể nâng cấp từ Standard_v2 lên WAF_v2 mà không cần redeploy. Lab 27 sẽ thực hiện điều này.