Azure 网络安全组 #

什么是网络安全组? #

网络安全组(NSG)是 Azure 提供的网络流量过滤工具,用于控制入站和出站网络流量。

text
┌─────────────────────────────────────────────────────────────┐
│                    NSG 概览                                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  功能                                                        │
│  ├── 入站流量过滤                                           │
│  ├── 出站流量过滤                                           │
│  ├── 端口控制                                               │
│  ├── IP 地址过滤                                            │
│  └── 协议控制                                               │
│                                                             │
│  应用范围                                                    │
│  ├── 子网级别                                               │
│  ├── 网络接口级别                                           │
│  └── 可同时应用                                             │
│                                                             │
│  规则特点                                                    │
│  ├── 有状态:自动允许返回流量                               │
│  ├── 优先级:按优先级处理                                   │
│  └── 方向:入站和出站分别配置                               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

创建 NSG #

使用 Azure CLI #

bash
# 创建网络安全组
az network nsg create \
  --resource-group myResourceGroup \
  --name myNSG

# 查看 NSG
az network nsg show \
  --resource-group myResourceGroup \
  --name myNSG

关联 NSG #

bash
# 关联到子网
az network vnet subnet update \
  --resource-group myResourceGroup \
  --vnet-name myVNet \
  --name WebSubnet \
  --network-security-group myNSG

# 关联到网络接口
az network nic update \
  --resource-group myResourceGroup \
  --name myNic \
  --network-security-group myNSG

安全规则 #

规则结构 #

text
┌─────────────────────────────────────────────────────────────┐
│                    安全规则结构                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  规则属性                                                    │
│  ├── 优先级: 100-4096 (数字越小优先级越高)                  │
│  ├── 名称: 规则名称                                         │
│  ├── 方向: 入站/出站                                        │
│  ├── 操作: 允许/拒绝                                        │
│  ├── 协议: TCP/UDP/ICMP/Any                                 │
│  ├── 源: IP 地址/服务标签/应用安全组                        │
│  ├── 源端口范围: 端口范围                                   │
│  ├── 目标: IP 地址/服务标签/应用安全组                      │
│  └── 目标端口范围: 端口范围                                 │
│                                                             │
│  规则处理顺序                                                │
│  ├── 1. 按优先级排序                                        │
│  ├── 2. 匹配第一个规则                                      │
│  └── 3. 停止处理                                            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

创建安全规则 #

bash
# 允许 SSH (22)
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name AllowSSH \
  --priority 100 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes '*' \
  --source-port-ranges '*' \
  --destination-address-prefixes '*' \
  --destination-port-ranges 22

# 允许 HTTP (80)
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name AllowHTTP \
  --priority 110 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes '*' \
  --source-port-ranges '*' \
  --destination-address-prefixes '*' \
  --destination-port-ranges 80

# 允许 HTTPS (443)
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name AllowHTTPS \
  --priority 120 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes '*' \
  --source-port-ranges '*' \
  --destination-address-prefixes '*' \
  --destination-port-ranges 443

默认规则 #

text
┌─────────────────────────────────────────────────────────────┐
│                    默认安全规则                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  入站规则                                                    │
│  ├── AllowVNetInBound: 允许 VNet 内通信                     │
│  ├── AllowAzureLoadBalancerInBound: 允许负载均衡器探测      │
│  └── DenyAllInBound: 拒绝所有其他入站流量                   │
│                                                             │
│  出站规则                                                    │
│  ├── AllowVnetOutBound: 允许 VNet 内通信                    │
│  ├── AllowInternetOutBound: 允许访问互联网                  │
│  └── DenyAllOutBound: 拒绝所有其他出站流量                  │
│                                                             │
│  优先级                                                      │
│  └── 默认规则优先级为 65000                                 │
│                                                             │
└─────────────────────────────────────────────────────────────┘

服务标签 #

常用服务标签 #

text
┌─────────────────────────────────────────────────────────────┐
│                    服务标签                                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  标签名称            描述                                   │
│  ─────────────────────────────────────────────────────────  │
│  VirtualNetwork      虚拟网络地址空间                       │
│  AzureLoadBalancer   Azure 基础结构负载均衡器               │
│  Internet            虚拟网络外部的 IP 地址                 │
│  AzureCloud          Azure 数据中心 IP 地址                 │
│  Storage             Azure 存储服务 IP 地址                 │
│  Sql                 Azure SQL 服务 IP 地址                 │
│  AzureActiveDirectory Azure AD 服务 IP 地址                │
│  AzureKeyVault       Azure Key Vault IP 地址               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

使用服务标签 #

bash
# 允许访问 Azure Storage
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name AllowStorage \
  --priority 200 \
  --direction Outbound \
  --access Allow \
  --protocol '*' \
  --source-address-prefixes '*' \
  --source-port-ranges '*' \
  --destination-address-prefixes 'Storage' \
  --destination-port-ranges '*'

# 允许访问 Azure SQL
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name AllowSQL \
  --priority 210 \
  --direction Outbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes '*' \
  --source-port-ranges '*' \
  --destination-address-prefixes 'Sql' \
  --destination-port-ranges '1433'

应用安全组 #

ASG 概念 #

text
┌─────────────────────────────────────────────────────────────┐
│                    应用安全组 (ASG)                          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  特点                                                        │
│  ├── 按应用逻辑分组网络接口                                 │
│  ├── 简化规则管理                                           │
│  └── 动态成员关系                                           │
│                                                             │
│  示例架构:                                                  │
│  ┌─────────────┐                                           │
│  │ WebServers  │ ← ASG: 包含所有 Web 服务器 NIC            │
│  └─────────────┘                                           │
│  ┌─────────────┐                                           │
│  │ AppServers  │ ← ASG: 包含所有应用服务器 NIC             │
│  └─────────────┘                                           │
│  ┌─────────────┐                                           │
│  │ DbServers   │ ← ASG: 包含所有数据库服务器 NIC           │
│  └─────────────┘                                           │
│                                                             │
│  规则示例:                                                  │
│  WebServers → AppServers: 允许 8080                        │
│  AppServers → DbServers: 允许 1433                         │
│                                                             │
└─────────────────────────────────────────────────────────────┘

创建和使用 ASG #

bash
# 创建应用安全组
az network asg create \
  --resource-group myResourceGroup \
  --name WebServers

az network asg create \
  --resource-group myResourceGroup \
  --name AppServers

# 将 NIC 添加到 ASG
az network nic update \
  --resource-group myResourceGroup \
  --name webNic1 \
  --application-security-groups WebServers

# 使用 ASG 创建规则
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name AllowWebToApp \
  --priority 100 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-asgs WebServers \
  --source-port-ranges '*' \
  --destination-asgs AppServers \
  --destination-port-ranges 8080

流量流程 #

规则评估顺序 #

text
┌─────────────────────────────────────────────────────────────┐
│                    流量评估流程                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  入站流量:                                                  │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                    入站流量                          │   │
│  └──────────────────────┬──────────────────────────────┘   │
│                         │                                   │
│                         ▼                                   │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              子网 NSG 规则检查                       │   │
│  └──────────────────────┬──────────────────────────────┘   │
│                         │                                   │
│                         ▼                                   │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              NIC NSG 规则检查                        │   │
│  └──────────────────────┬──────────────────────────────┘   │
│                         │                                   │
│                         ▼                                   │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                    到达目标                          │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
│  出站流量:相反顺序                                          │
│  NIC NSG → 子网 NSG → 出站                                  │
│                                                             │
└─────────────────────────────────────────────────────────────┘

常见配置场景 #

Web 服务器 NSG #

bash
# 创建 Web 服务器 NSG
az network nsg create \
  --resource-group myResourceGroup \
  --name WebNSG

# 允许 HTTP
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name WebNSG \
  --name AllowHTTP \
  --priority 100 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes '*' \
  --destination-port-ranges 80

# 允许 HTTPS
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name WebNSG \
  --name AllowHTTPS \
  --priority 110 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes '*' \
  --destination-port-ranges 443

数据库服务器 NSG #

bash
# 创建数据库 NSG
az network nsg create \
  --resource-group myResourceGroup \
  --name DbNSG

# 仅允许来自应用子网的访问
az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name DbNSG \
  --name AllowAppAccess \
  --priority 100 \
  --direction Inbound \
  --access Allow \
  --protocol Tcp \
  --source-address-prefixes 10.0.2.0/24 \
  --destination-port-ranges 1433

流量日志 #

启用 NSG 流日志 #

bash
# 创建存储账户
az storage account create \
  --name nsgflowlogs \
  --resource-group myResourceGroup \
  --location eastus \
  --sku Standard_LRS

# 启用流日志
az network watcher flow-log configure \
  --resource-group myResourceGroup \
  --nsg myNSG \
  --storage-account nsgflowlogs \
  --enabled true \
  --retention 7

最佳实践 #

规则设计 #

text
┌─────────────────────────────────────────────────────────────┐
│                    规则设计最佳实践                          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 最小权限原则                                             │
│     └── 只开放必要的端口和 IP                               │
│                                                             │
│  2. 使用服务标签                                             │
│     └── 简化 Azure 服务访问规则                             │
│                                                             │
│  3. 使用 ASG                                                 │
│     └── 按应用逻辑分组                                      │
│                                                             │
│  4. 规则优先级规划                                           │
│     ├── 预留空间给未来规则                                  │
│     └── 重要规则使用较低优先级                              │
│                                                             │
│  5. 定期审计规则                                             │
│     └── 清理不需要的规则                                    │
│                                                             │
│  6. 启用流日志                                               │
│     └── 监控和分析流量                                      │
│                                                             │
└─────────────────────────────────────────────────────────────┘

安全建议 #

text
┌─────────────────────────────────────────────────────────────┐
│                    安全最佳实践                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 限制管理端口访问                                         │
│     ├── SSH (22): 仅允许特定 IP                             │
│     └── RDP (3389): 仅允许特定 IP                           │
│                                                             │
│  2. 使用 Just-In-Time 访问                                   │
│     └── 临时开放管理端口                                    │
│                                                             │
│  3. 分层安全                                                 │
│     ├── Web 层: 开放 HTTP/HTTPS                             │
│     ├── 应用层: 仅接受 Web 层                               │
│     └── 数据层: 仅接受应用层                                │
│                                                             │
│  4. 限制出站流量                                             │
│     └── 仅允许必要的外部访问                                │
│                                                             │
└─────────────────────────────────────────────────────────────┘

下一步 #

现在你已经掌握了网络安全组的使用,接下来学习 负载均衡器 了解流量分发!

最后更新:2026-03-29