LAB 05 ~2 giờ Module 4 Virtual Networking

Virtual Networking — VNet, Subnet, NSG & ASG

Tạo Virtual Network, phân chia subnet, triển khai Network Security Group và Application Security Group, kiểm soát traffic giữa VM trong cùng và khác subnet, sử dụng Network Watcher để phân tích luồng dữ liệu.

🎯 Mục Tiêu Lab

Tạo Virtual Network (VNet) với nhiều subnet, phân chia vùng DMZ và Backend

Cấu hình Network Security Group (NSG) — kiểm soát inbound/outbound traffic

Tạo Application Security Group (ASG) — gom VM theo vai trò thay vì IP

Triển khai VM đa NIC kết nối vào nhiều subnet khác nhau

Kiểm tra kết nối giữa VM trong cùng subnet và khác subnet

Sử dụng Network Watcher: IP flow verify, Connection troubleshoot, NSG flow logs

📋 Chuẩn Bị

Yêu cầu:
  • Hoàn thành Lab 01 — Azure subscription active
  • Azure CLI phiên bản 2.50+ đã đăng nhập
  • Quota đủ để tạo 2 VM Standard_B1s tại southeastasia
Tài nguyên sẽ tạo:
  • 1 VNet (10.0.0.0/16) với 3 subnet
  • 2 NSG, 2 ASG, 2 VM Linux
  • Chi phí ước tính: ~$1–2 nếu dọn dẹp ngay sau lab
Lưu ý 2026: Basic Public IP đã bị retired từ 30/09/2025. Mọi Public IP trong lab này dùng --sku Standard. Ngoài ra, default outbound internet access sẽ bị tắt từ 31/03/2026 — nếu VM cần ra internet, cần cấu hình NAT Gateway hoặc Azure Firewall.

🏗️ Kịch Bản & Tài Nguyên

Bạn xây dựng hạ tầng mạng cho một ứng dụng web 2 lớp: Frontend (DMZ subnet) phục vụ web trực tiếp từ internet, và Backend (App subnet) chỉ giao tiếp nội bộ. Một subnet Management dành cho jump host quản trị.

VNet: vnet-lab05
10.0.0.0/16 — southeastasia
subnet-dmz
10.0.1.0/24 → HTTP/HTTPS mở
subnet-app
10.0.2.0/24 → nội bộ only
subnet-mgmt
10.0.3.0/24 → SSH admin
NSG: nsg-dmz
Allow 80, 443 inbound
NSG: nsg-app
Allow từ ASG-WebServers only

🧪 Các Bước Thực Hiện

1

Tạo Resource Group và Virtual Network

Cách 1 — Azure Portal
  1. 1.1Portal → Resource Groups → + Create → Name: rg-lab05-networking, Region: Southeast Asia → Review + Create
  2. 1.2Portal → Virtual Networks → + Create → Basics: Name vnet-lab05, Region: Southeast Asia, RG: rg-lab05-networking
  3. 1.3Tab IP Addresses: Address space 10.0.0.0/16 → xóa subnet default → Add subnet: subnet-dmz / 10.0.1.0/24
  4. 1.4Thêm subnet-app (10.0.2.0/24) và subnet-mgmt (10.0.3.0/24) → Review + Create
Cách 2 — Azure CLI
Azure CLI
# Tạo Resource Group
az group create \
  --name rg-lab05-networking \
  --location southeastasia \
  --tags "lab=05" "module=az104"

# Tạo VNet với 3 subnet cùng lúc
az network vnet create \
  --resource-group rg-lab05-networking \
  --name vnet-lab05 \
  --address-prefix 10.0.0.0/16 \
  --subnet-name subnet-dmz \
  --subnet-prefix 10.0.1.0/24

# Thêm subnet-app
az network vnet subnet create \
  --resource-group rg-lab05-networking \
  --vnet-name vnet-lab05 \
  --name subnet-app \
  --address-prefix 10.0.2.0/24

# Thêm subnet-mgmt
az network vnet subnet create \
  --resource-group rg-lab05-networking \
  --vnet-name vnet-lab05 \
  --name subnet-mgmt \
  --address-prefix 10.0.3.0/24

# Xác nhận cấu trúc VNet
az network vnet show \
  --resource-group rg-lab05-networking \
  --name vnet-lab05 \
  --query "{name:name,addressSpace:addressSpace,subnets:subnets[].name}" \
  --output json
Kết quả (Output)
{
  "name": "vnet-lab05",
  "addressSpace": { "addressPrefixes": ["10.0.0.0/16"] },
  "subnets": ["subnet-dmz", "subnet-app", "subnet-mgmt"]
}
2

Tạo Application Security Group (ASG)

ASG cho phép gom nhóm VM theo vai trò (WebServer, AppServer) rồi dùng nhóm đó làm nguồn/đích trong NSG rules — thay vì phải nhớ từng IP.

Cách 1 — Azure Portal
  1. 2.1Portal → tìm Application Security Groups → + Create
  2. 2.2Tạo asg-webservers — Region: Southeast Asia, RG: rg-lab05-networking
  3. 2.3Tương tự tạo asg-appservers cùng RG và region
Cách 2 — Azure CLI
Azure CLI
# Tạo ASG cho Web Servers
az network asg create \
  --resource-group rg-lab05-networking \
  --name asg-webservers \
  --location southeastasia

# Tạo ASG cho App Servers
az network asg create \
  --resource-group rg-lab05-networking \
  --name asg-appservers \
  --location southeastasia

# Liệt kê ASGs
az network asg list \
  --resource-group rg-lab05-networking \
  --output table
3

Tạo và Cấu Hình Network Security Group (NSG)

NSG hoạt động như firewall Layer 4 — filter traffic theo IP, port, protocol. Gắn NSG vào subnet hoặc NIC của VM.

Cách 1 — Azure Portal
  1. 3.1Portal → Network Security Groups → + Create → Name: nsg-dmz, RG: rg-lab05-networking
  2. 3.2Vào nsg-dmz → Inbound security rules → + Add: Port 80, TCP, Allow, Priority 100, name: Allow-HTTP
  3. 3.3Thêm rule Port 443, TCP, Allow, Priority 110, name: Allow-HTTPS
  4. 3.4Tạo nsg-app → thêm rule: Source ASG = asg-webservers, Port 8080, Allow, Priority 100
  5. 3.5Gắn nsg-dmz vào subnet-dmz: vnet-lab05 → Subnets → subnet-dmz → NSG: nsg-dmz
Cách 2 — Azure CLI
Azure CLI
RG="rg-lab05-networking"

# Tạo NSG cho DMZ subnet
az network nsg create \
  --resource-group $RG \
  --name nsg-dmz \
  --location southeastasia

# Cho phép HTTP từ internet
az network nsg rule create \
  --resource-group $RG \
  --nsg-name nsg-dmz \
  --name Allow-HTTP \
  --priority 100 \
  --protocol Tcp \
  --destination-port-ranges 80 \
  --access Allow --direction Inbound

# Cho phép HTTPS từ internet
az network nsg rule create \
  --resource-group $RG \
  --nsg-name nsg-dmz \
  --name Allow-HTTPS \
  --priority 110 \
  --protocol Tcp \
  --destination-port-ranges 443 \
  --access Allow --direction Inbound

# Tạo NSG cho App subnet
az network nsg create \
  --resource-group $RG \
  --name nsg-app \
  --location southeastasia

# Cho phép traffic từ ASG web → app port 8080
az network nsg rule create \
  --resource-group $RG \
  --nsg-name nsg-app \
  --name Allow-WebToApp \
  --priority 100 \
  --source-asgs asg-webservers \
  --destination-asgs asg-appservers \
  --destination-port-ranges 8080 \
  --protocol Tcp \
  --access Allow --direction Inbound

# Gắn NSG vào các subnet
az network vnet subnet update \
  --resource-group $RG \
  --vnet-name vnet-lab05 \
  --name subnet-dmz \
  --network-security-group nsg-dmz

az network vnet subnet update \
  --resource-group $RG \
  --vnet-name vnet-lab05 \
  --name subnet-app \
  --network-security-group nsg-app
4

Triển Khai VM — Web Server (DMZ) và App Server (Backend)

Cách 1 — Azure Portal
  1. 4.1Portal → Virtual Machines → + Create → Name: vm-web01, Ubuntu 22.04 LTS, Size: Standard_B1s
  2. 4.2Tab Networking: VNet = vnet-lab05, Subnet = subnet-dmz, Public IP: mới (Standard SKU), NIC NSG: None (dùng subnet NSG)
  3. 4.3Sau tạo xong vm-web01: NIC → Application Security Groups → Associate → asg-webservers
  4. 4.4Tạo vm-app01 tương tự, nhưng Subnet = subnet-app, không có Public IP, sau đó gắn vào asg-appservers
Cách 2 — Azure CLI
Azure CLI
RG="rg-lab05-networking"

# Tạo Public IP Standard cho vm-web01
az network public-ip create \
  --resource-group $RG \
  --name pip-web01 \
  --sku Standard \
  --allocation-method Static \
  --location southeastasia

# Tạo NIC cho vm-web01 (DMZ subnet) gắn ASG
az network nic create \
  --resource-group $RG \
  --name nic-web01 \
  --vnet-name vnet-lab05 \
  --subnet subnet-dmz \
  --public-ip-address pip-web01 \
  --application-security-groups asg-webservers

# Triển khai vm-web01
az vm create \
  --resource-group $RG \
  --name vm-web01 \
  --nics nic-web01 \
  --image Ubuntu2204 \
  --size Standard_B1s \
  --admin-username azureuser \
  --generate-ssh-keys \
  --no-wait

# Tạo NIC cho vm-app01 (App subnet) — không có Public IP
az network nic create \
  --resource-group $RG \
  --name nic-app01 \
  --vnet-name vnet-lab05 \
  --subnet subnet-app \
  --application-security-groups asg-appservers

# Triển khai vm-app01
az vm create \
  --resource-group $RG \
  --name vm-app01 \
  --nics nic-app01 \
  --image Ubuntu2204 \
  --size Standard_B1s \
  --admin-username azureuser \
  --generate-ssh-keys
5

Kiểm Tra Kết Nối & Network Watcher

Cách 1 — Azure Portal
  1. 5.1Portal → Network WatcherIP Flow Verify → chọn vm-web01, Protocol TCP, Direction Inbound, Local port 80, Remote IP (IP của bạn) → Kiểm tra → Allowed
  2. 5.2IP Flow Verify cho vm-app01 với port 80 từ internet → phải trả về Denied by nsg-app
  3. 5.3Network Watcher → Connection Troubleshoot → Source: vm-web01, Dest IP: private IP của vm-app01, Port: 8080 → Check → Reachable
  4. 5.4Network Watcher → NSG Flow Logs → Enable cho nsg-dmz → chọn Storage Account → Save
Cách 2 — Azure CLI
Azure CLI
RG="rg-lab05-networking"

# Lấy ID của vm-web01
VM_WEB_ID=$(az vm show \
  --resource-group $RG \
  --name vm-web01 \
  --query id -o tsv)

# Lấy private IP của vm-app01
APP_PRIVATE_IP=$(az vm show \
  --resource-group $RG \
  --name vm-app01 \
  -d --query privateIps -o tsv)

# IP Flow Verify: internet → vm-web01 port 80 (phải Allow)
az network watcher test-ip-flow \
  --resource-group NetworkWatcherRG \
  --watcher-name NetworkWatcher_southeastasia \
  --vm $VM_WEB_ID \
  --direction Inbound \
  --protocol TCP \
  --local-ip 10.0.1.4 \
  --local-port 80 \
  --remote-ip 8.8.8.8 \
  --remote-port 60000

# Connection Troubleshoot: vm-web01 → vm-app01 port 8080
az network watcher test-connectivity \
  --resource-group $RG \
  --source-resource vm-web01 \
  --dest-address $APP_PRIVATE_IP \
  --dest-port 8080
Kết quả (Output) — IP Flow Verify port 80 vào vm-web01
{
  "access": "Allow",
  "ruleName": "Allow-HTTP"
}

📊 Kết Quả Đầu Ra Lab 05

VNet với 3 subnet

vnet-lab05 / 10.0.0.0/16 có subnet-dmz, subnet-app, subnet-mgmt

NSG hoạt động đúng

Port 80/443 Allow vào DMZ, App subnet chỉ nhận từ ASG-WebServers

ASG group hoạt động

vm-web01 trong asg-webservers, vm-app01 trong asg-appservers

Network Watcher xác nhận

IP Flow Verify trả về Allow/Deny đúng theo rules đã cấu hình

🧹 Dọn Dẹp Tài Nguyên

Azure CLI
# Xóa toàn bộ resource group (bao gồm VM, VNet, NSG, ASG, Public IP)
az group delete \
  --name rg-lab05-networking \
  --yes \
  --no-wait

# Xác nhận đã xóa
az group exists --name rg-lab05-networking

❓ Câu Hỏi Ôn Tập

1. NSG có thể gắn vào những đối tượng nào trong Azure? Khi cả subnet NSG lẫn NIC NSG đều có rule, thứ tự áp dụng là gì?

Gợi ý: Subnet và NIC. Inbound: Subnet NSG trước → NIC NSG sau. Outbound: NIC NSG trước → Subnet NSG sau.

2. Application Security Group (ASG) giải quyết vấn đề gì so với cách dùng IP trong NSG rules?

Gợi ý: Khi IP thay đổi hoặc scale, không cần sửa rules — chỉ cần gắn VM vào ASG đúng vai trò.

3. Tại sao từ 31/03/2026 VM trong Azure cần cấu hình NAT Gateway nếu muốn truy cập internet?

Gợi ý: Microsoft tắt default outbound access. VM không có Public IP hoặc NAT GW sẽ mất kết nối internet outbound.

4. Network Watcher có các công cụ nào để debug kết nối? Phân biệt IP Flow Verify và Connection Troubleshoot.

Gợi ý: IP Flow Verify kiểm tra NSG rules theo chiều Inbound/Outbound. Connection Troubleshoot test end-to-end TCP từ VM đến IP/port đích.

5. Sự khác nhau giữa gắn NSG ở cấp subnet vs cấp NIC? Khi nào nên dùng mỗi loại?

Gợi ý: Subnet NSG áp dụng cho tất cả VM trong subnet (quản lý tập trung). NIC NSG chỉ cho 1 VM cụ thể (granular control). Thực tế thường dùng Subnet NSG, NIC NSG khi cần exception.

Lab 04: Storage Thư viện Labs Lab 06: Intersite Connectivity
Zalo