Azure 网络安全组(NSG) 和 GCP 防火墙规则 的详细配置指南

以下是 Azure 网络安全组(NSG) 和 GCP 防火墙规则 的详细配置指南,包含 CLI 操作示例和实战场景!



一、Azure 网络安全组(NSG)配置

1. 创建网络安全组

 创建 NSG(关联到 VNet)
az network nsg create \
  --resource-group my-resource-group \
  --name my-nsg \
  --vnet-name my-vnet \
  --location eastus

2. 添加入站/出站规则

允许 SSH(端口 22)入站

az network nsg rule create \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --name allow-ssh \
  --protocol tcp \
  --direction inbound \
  --priority 100 \
  --source-address-prefix 0.0.0.0/0 \
  --source-port-range '*' \
  --destination-port-range 22/22 \
  --access allow

允许 HTTP/HTTPS(端口 80/443)入站

az network nsg rule create \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --name allow-http-https \
  --protocol tcp \
  --direction inbound \
  --priority 110 \
  --source-address-prefix 0.0.0.0/0 \
  --destination-port-range 80/80,443/443 \
  --access allow

拒绝其他所有入站流量(默认策略)

az network nsg rule create \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --name deny-all-inbound \
  --protocol all \
  --direction inbound \
  --priority 1000 \
  --source-address-prefix 0.0.0.0/0 \
  --access deny

3. 关联 NSG 到子网或 VM

关联到子网

az network vnet subnet update \
  --resource-group my-resource-group \
  --subnet-name my-subnet \
  --vnet-name my-vnet \
  --network-security-groups my-nsg

关联到 VM

az vm update \
  --resource-group my-resource-group \
  --name my-vm \
  --network-interface-names my-ni \
  --set networkInterfaces[0].networkSecurityGroups=my-nsg

4. 查看 NSG 规则

az network nsg rule list \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --output table

5. 高级配置

5.1 IP 白名单(限制 SSH 访问)

az network nsg rule create \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --name allow-ssh-whitelist \
  --protocol tcp \
  --direction inbound \
  --priority 100 \
  --source-address-prefix 192.168.1.0/24 \
  --destination-port-range 22/22 \
  --access allow

5.2 添加富规则(基于多条件)

az network nsg rule create \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --name rich-rule \
  --protocol udp \
  --direction inbound \
  --priority 200 \
  --source-address-prefix 10.0.0.0/8 \
  --source-port-range 53/53 \
  --destination-port-range 53/53 \
  --access allow

5.3 启用日志记录

az network nsg set-log-validation \
  --resource-group my-resource-group \
  --nsg-name my-nsg \
  --enable true
 配置存储账户记录日志
az storage account create \
  --resource-group my-resource-group \
  --name my-log-storage \
  --location eastus \
  --sku {name:Standard_LRS, tier:Standard}

二、GCP 防火墙规则配置

1. 创建 VPC 防火墙规则

 创建防火墙规则(允许 HTTP/HTTPS)
gcloud compute firewall-rules create \
  --project my-project \
  --network my-vpc \
  --name allow-http-https \
  --priority 1000 \
  --direction inbound \
  --protocol tcp \
  --target-tags http-server \
  --ports 80,443 \
  --action allow \
  --source-ranges 0.0.0.0/0

2. 添加 SSH 访问规则

gcloud compute firewall-rules create \
  --project my-project \
  --network my-vpc \
  --name allow-ssh \
  --priority 2000 \
  --direction inbound \
  --protocol tcp \
  --target-tags ssh-server \
  --ports 22 \
  --action allow \
  --source-ranges 192.168.1.0/24

3. 关联防火墙规则到实例

• 在创建 VM 时指定 --tags http-server--tags ssh-server,防火墙规则会自动匹配标签。

4. 查看防火墙规则

gcloud compute firewall-rules list \
  --project my-project \
  --network my-vpc \
  --format table

5. 高级配置

5.1 限制 IP 白名单

gcloud compute firewall-rules create \
  --project my-project \
  --network my-vpc \
  --name allow-ssh-whitelist \
  --priority 3000 \
  --direction inbound \
  --protocol tcp \
  --target-tags ssh-server \
  --ports 22 \
  --action allow \
  --source-ranges 10.0.0.1/32   单个 IP

5.2 设置优先级

 优先级数值越小,规则优先级越高
gcloud compute firewall-rules update \
  --project my-project \
  --network my-vpc \
  --name allow-http-https \
  --priority 500

5.3 启用日志记录

gcloud compute firewall-rules set-logging \
  --project my-project \
  --network my-vpc \
  --name allow-http-https \
  --enable true \
  --log-config destination=cloud-storage, bucket=my-log-bucket

三、实战场景

场景 1:Web 服务器(Azure + GCP)

Azure 配置

 允许 HTTP/HTTPS 入站(全局访问)
az network nsg rule create \
  --resource-group my-rg \
  --nsg-name web-sg \
  --name allow-web \
  --protocol tcp \
  --direction inbound \
  --priority 100 \
  --source-address-prefix 0.0.0.0/0 \
  --destination-port-range 80,443 \
  --access allow

 关联到 VM
az vm update \
  --resource-group my-rg \
  --name web-vm \
  --network-interface-names web-ni \
  --set networkInterfaces[0].networkSecurityGroups=web-sg

GCP 配置

gcloud compute firewall-rules create \
  --project my-project \
  --network my-vpc \
  --name allow-web \
  --priority 1000 \
  --direction inbound \
  --protocol tcp \
  --target-tags web \
  --ports 80,443 \
  --action allow \
  --source-ranges 0.0.0.0/0

场景 2:数据库服务器(仅 Azure)

 仅允许应用服务器 IP 访问 MySQL(3306)
az network nsg rule create \
  --resource-group my-rg \
  --nsg-name db-sg \
  --name allow-db \
  --protocol tcp \
  --direction inbound \
  --priority 200 \
  --source-address-prefix 10.0.1.0/24 \
  --destination-port-range 3306/3306 \
  --access allow

 关联到子网
az network vnet subnet update \
  --resource-group my-rg \
  --subnet-name db-subnet \
  --vnet-name my-vpc \
  --network-security-groups db-sg

四、常见问题排查

1. 规则未生效

• Azure:检查规则优先级是否被更高优先级的规则覆盖。
• GCP:确认防火墙规则的目标标签(--target-tags)是否与 VM 标签匹配。

2. SSH 无法连接

• 确认安全组允许来源 IP 和端口。
• 检查本地 SSH 配置(如密钥对权限)。

3. 跨子网流量问题

• 在 Azure 中,确保子网关联了正确 NSG。
• 在 GCP 中,检查网络接口的路由表是否正确。


五、工具推荐

• Azure CLI:az network nsg 命令集。
• GCP CLI:gcloud compute firewall-rules
• CloudShell:直接在云平台控制台运行 CLI。

通过以上配置,你可以灵活控制 Azure 和 GCP 的网络安全策略,确保资源既安全又高效!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独隅

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值