Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบเครือข่าย >> ความปลอดภัยเครือข่าย

Network Security Groups (NSG) ใน Azure คืออะไร? คู่มือครบวงจร

Network Security Group (NSG) คืออะไร?

Network Security Group (NSG) เป็นบริการของ Microsoft Azure ที่ทำหน้าที่เป็น Virtual Firewall แบบ Layer 3/4 (Network/Transport Layer) ใช้ควบคุมทราฟฟิกเครือข่ายเข้า-ออก (Inbound/Outbound) ไปยัง Azure Resources เช่น Virtual Machines (VM), Subnets, Network Interfaces (NIC) ผ่านกฎ (Rules) ที่กำหนดเอง

คุณสมบัติหลักของ NSG

  • Stateful: ติดตามสถานะการเชื่อมต่อ (Allow return traffic อัตโนมัติ)
  • Priority-based: ประมวลผลกฎตาม Priority (เลขน้อย = สำคัญกว่า) 100-4096
  • 5-Tuple Matching: Source IP/Port, Destination IP/Port, Protocol (TCP/UDP/ICMP/*)
  • Default Rules: มีกฎเริ่มต้น (Allow VNet Inbound, Allow Internet Outbound, Deny All Inbound, Deny All Outbound) ไม่สามารถลบได้ แต่ override ได้
  • Association: ผูกกับ Subnet (ผลกระทบทุก Resource ใน Subnet) หรือ NIC (เฉพาะ VM นั้น)

โครงสร้างกฎ NSG (NSG Rule Structure)

Service Tags (Service Tags) - ทำให้ง่ายขึ้น

แทนที่จะระบุ IP Range เอง ใช้ Tag ที่ Azure จัดการให้:

  • VirtualNetwork: ทั้ง VNet (รวม Peered VNet)
  • Internet: ทุก Public IP
  • AzureLoadBalancer: Health Probe ของ Azure LB
  • AzureCloud, AzureStorage, AzureSql, AzureKeyVault, AzureMonitor,...
  • Region-specific: AzureCloud.EastUS, Storage.WestEurope

Application Security Groups (ASG)

ASG คือการจัดกลุ่ม VMs ตามหน้าที่/แอปพลิเคชัน (เช่น Web, App, DB) แทนที่จะระบุ IP ใช้ ASG เป็น Source/Destination ใน NSG Rule

ประโยชน์:

  • จัดการกฎง่ายขึ้น ไม่ต้องแก้ IP ทุกครั้งที่ Scale VM
  • Logical Grouping ตรงกับ Architecture (Microservices)
  • ASG เป็น Object ภายใน NSG ไม่ใช่ Resource แยกต่างหาก

NSG vs Azure Firewall

ฟิลด์คำอธิบายตัวอย่าง
Nameชื่อกฎ (Unique ใน NSG)Allow-HTTP-Inbound
Priorityลำดับความสำคัญ (100-4096)100
Source / DestinationIP Address, CIDR, Service Tag, ASG10.0.0.0/16, Internet, VirtualNetwork
ProtocolTCP, UDP, ICMP, ESP, AH, * (All)TCP
DirectionInbound / OutboundInbound
Port RangePort หรือ Range (1-65535, *)80, 443, 80-8080
ActionAllow / DenyAllow
คุณสมบัติNSGAzure Firewall
OSI LayerLayer 3/4 (L3/L4)Layer 3/4/7 (L3/L4/L7)
Statefulใช่ใช่
Application Inspection (L7)ไม่ใช่ (FQDN, SQL, HTTP Headers)
Threat Intelligenceไม่ใช่ (Built-in)
SNAT/DNATไม่ใช่
Costฟรี (คิดค่า Data Transfer ปกติ)มีค่าใช้จ่ายต่อชั่วโมง + Data
Use CaseBasic Segmentation, Micro-segmentationCentralized Egress/Ingress, L7 Inspection, Hybrid

ลำดับการประมวลผลกฎ (Rule Processing Order)

Inbound Traffic:

  1. NSG ผูกกับ NIC (ถ้ามี)
  2. NSG ผูกกับ Subnet (ถ้ามี)
  3. กฎ Default

Outbound Traffic:

  1. NSG ผูกกับ Subnet (ถ้ามี)
  2. NSG ผูกกับ NIC (ถ้ามี)
  3. กฎ Default
กฎแรกที่ Match (Priority ต่ำสุด) จะถูก Execute แล้วหยุด (First Match Wins)

สร้าง NSG (Azure Portal / CLI / PowerShell / Bicep / Terraform)

Azure CLI

# สร้าง NSG
az network nsg create -g MyResourceGroup -n MyNSG -l eastus

# สร้างกฎ Allow HTTP
az network nsg rule create -g MyResourceGroup --nsg-name MyNSG \
  -n Allow-HTTP --priority 100 --direction Inbound \
  --protocol Tcp --destination-port-range 80 \
  --source-address-prefix Internet --destination-address-prefix '*' \
  --access Allow

# ผูกกับ Subnet
az network vnet subnet update -g MyResourceGroup --vnet-name MyVNet \
  -n MySubnet --network-security-group MyNSG

Terraform

resource "azurerm_network_security_group" "example" {
  name                = "example-nsg"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  security_rule {
    name                       = "AllowHTTP"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefix      = "Internet"
    destination_address_prefix = "*"
  }
}

Best Practices

  1. Least Privilege: เปิด Port/Source น้อยที่สุดเท่าที่จำเป็น
  2. ใช้ Service Tags / ASG: ลดความซับซ้อนของ IP Management
  3. แยก NSG ต่อ Environment/Tier: Prod/Dev, Web/App/DB
  4. Log Analytics: เปิด NSG Flow Logs ส่งไป Log Analytics Workspace เพื่อ Audit/Alert
  5. Test ก่อน Deploy: ใช้ az network nsg rule list และ Effective Security Rules ใน Portal
  6. Deny by Default: ปิดทั้งหมด เปิดเฉพาะที่ต้องการ (Default Deny All Inbound ช่วยอยู่แล้ว)
  7. Naming Convention: Allow-HTTP-Inbound, Deny-SSH-From-Internet, Allow-DB-From-ASG-App

Troubleshooting

  • Effective Security Rules: ใน Portal VM > Networking > Effective security rules (ดูกฎที่รวม NIC+Subnet+Default)
  • NSG Flow Logs: บันทึกทราฟฟิก Allow/Deny ไป Storage/Log Analytics
  • Connection Troubleshoot: Network Watcher > Connection Troubleshoot / IP Flow Verify
  • Priority Conflict: ตรวจสอบ Priority ซ้ำหรือทับซ้อน

สรุป

NSG เป็น Virtual Firewall พื้นฐานของ Azure ควบคุมทราฟฟิก L3/L4 ด้วยกฎ Priority-based ใช้ร่วมกับ Service Tags และ ASG เพื่อจัดการ Scale ได้ ควรใช้ร่วมกับ Azure Firewall สำหรับ L7/Threat Intel และ Centralized Egress NSG ฟรี (คิดค่า Data Transfer) เหมาะสำหรับ Micro-segmentation และ Basic Network Security