为生产者-消费者模型设置带内集成

Network Security Integration 使用生产者-消费者模型来检查和监控数据。它使用带内集成与数据包拦截技术,将第三方网络设备插入网络流量中,并执行深度数据包检测。

本教程介绍了如何创建和配置生产者和消费者资源,以设置带内集成。

目标

本教程介绍了如何在项目中完成以下任务:

  • 在同一项目中为提供方和使用方创建包含子网的自定义 VPC 网络。
  • 对于提供方,请创建并配置提供方资源,例如内部直通式网络负载平衡器、拦截服务和拦截部署资源。
  • 对于消费者,请创建并配置客户端和服务器虚拟机 (VM) 实例、消费者端点资源和防火墙政策,以拦截客户端-服务器流量。
  • 测试连接,并验证网络流量数据包是否被使用方的端点资源拦截,以及是否被提供方的拦截资源检查。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. 为您的项目启用 Compute Engine API
  7. 在要用于结算的 Google Cloud 项目中启用 Network Security API
  8. 确保您在项目级层拥有 Compute Network Admin 角色 (roles/compute.networkAdmin),并在组织级层拥有 Security Profile Admin 角色 (roles/networksecurity.securityProfileAdmin)。
  9. 如果您希望通过命令行进行操作,请安装 Google Cloud CLI。如需了解该工具的概念性信息和安装信息,请参阅 gcloud CLI 概览

    注意:如果您之前未运行过 Google Cloud CLI,请先运行 gcloud init 以初始化 gcloud CLI 目录。

  10. 获取项目 ID。您需要使用该值来创建拦截资源和端点资源。

    如果您使用的是 gcloud CLI,则可以在此处或稍后在相应命令中替换 PROJECT_ID

  11. 获取组织的 ID。您需要此权限才能创建组织级层资源,例如安全配置文件和安全配置文件组。

    如果您使用的是 gcloud CLI,则可以在此处或稍后在相应命令中替换 ORG_ID

创建生产者资源

在本部分中,您将为生产者创建以下资源:

  • 包含子网的自定义 VPC 网络。
  • 一个非托管式实例组,其中包含一个运行通用网络虚拟化封装 (GENEVE) 回显服务器的虚拟机实例。
  • 具有后端服务和转发规则的内部直通式网络负载平衡器。
  • 防火墙规则,用于允许 Google Cloud 健康检查。
  • 拦截部署群组和拦截部署。

创建自定义 VPC 网络

在本部分中,您将创建一个包含子网的 VPC 网络。

控制台

  1. 在 Google Cloud 控制台中,前往 VPC 网络页面。

    进入 VPC 网络页面

  2. 点击创建 VPC 网络

  3. 对于名称,输入 producer-network

  4. 对于说明,输入 Producer VPC network

  5. 子网部分中,执行以下操作:

    1. 对于子网创建模式,选择自定义
    2. 新子网部分中,输入以下信息:
      • 名称producer-subnet
      • 区域us-west1
      • IP 栈类型IPv4(单栈)
      • IPv4 范围10.10.0.0/16
    3. 点击完成
  6. 点击创建

  7. 打开 Google Cloud CLI 并运行以下命令,以增加 VPC 网络的最大传输单元 (MTU)。

    gcloud compute networks update producer-network \
        --mtu=1768
    

    在该命令中,您指定了 1768 字节的 MTU,这是 VPC 网络的默认 MTU(1460 字节)与 Network Security Integration GENEVE 封装开销(308 字节)的总和。

gcloud

  1. 创建 VPC 网络。

    gcloud compute networks create producer-network \
        --subnet-mode=custom \
        --mtu=1768 \
        --description="Producer VPC network"
    

    在该命令中,您指定的最大传输单元 (MTU) 为 1768 字节,这是 VPC 网络的默认 MTU(1460 字节)与Network Security Integration GENEVE 封装开销(308 字节)之和。

  2. 在 VPC 网络中,创建一个子网。

    gcloud compute networks subnets create producer-subnet \
        --network=producer-network \
        --region=us-west1 \
        --range=10.10.0.0/16
    

创建非托管实例组

在本部分中,您将创建一个非托管式实例组。

控制台

  1. 在 Google Cloud 控制台中,前往实例群组页面。

    进入“实例组”

  2. 点击创建实例组

  3. 点击新建非代管式实例组

  4. 对于名称,输入 producer-instance-group

  5. 位置部分,为区域选择 us-west1,为可用区选择 us-west1-b

  6. 对于网络,请选择 producer-network

  7. 对于子网,选择 producer-subnet

  8. 点击创建

gcloud

gcloud compute instance-groups unmanaged create producer-instance-group \
    --zone=us-west1-b

配置负载均衡器组件

在本部分中,您将为内部直通式网络负载平衡器创建组件,包括后端服务和转发规则。

控制台

开始配置

  1. 在 Google Cloud 控制台中,前往负载均衡页面。

    转到“负载均衡”

  2. 点击创建负载均衡器
  3. 对于负载均衡器的类型,选择网络负载均衡器 (TCP/UDP/SSL),然后点击下一步
  4. 对于代理或直通,选择直通式负载均衡器,然后点击下一步
  5. 公共或内部字段中,选择内部,然后点击下一步
  6. 点击配置

基本配置

创建内部直通网络负载均衡器页面上,输入以下信息:

  • 负载均衡器名称producer-ilb
  • 区域us-west1
  • 网络producer-network

配置后端

  1. 点击后端配置
  2. 对于协议,请选择 UDP
  3. 健康检查列表中,选择创建健康检查,输入以下信息,然后点击创建
    • 名称producer-health-check
    • 范围区域
    • 端口80
    • 代理协议NONE
  4. 后端新建后端部分,选择 IP 栈类型作为 IPv4(单栈)
  5. 实例组中,选择 producer-instance-group 实例组,然后点击完成
  6. 请先确认后端配置旁边是否有蓝色对勾标记,然后再继续操作。

配置前端

新建前端 IP 和端口部分,执行以下操作:

  1. 对于名称,输入 producer-ilb-fr
  2. 对于子网,选择 producer-subnet
  3. 对于端口,选择单个,然后在端口号中输入 6081
  4. 请先确认前端配置旁边是否有蓝色对勾标记,然后再继续操作。

检查配置

  1. 点击检查并最终确定
  2. 查看负载均衡器配置设置。
  3. 点击创建

gcloud

  1. 创建区域级健康检查。

    gcloud compute health-checks create tcp producer-health-check \
        --region=us-west1 \
        --port=80
    
  2. 创建后端服务。

    gcloud compute backend-services create producer-backend-service \
        --protocol=UDP \
        --region=us-west1 \
        --health-checks=producer-health-check \
        --health-checks-region=us-west1 \
        --load-balancing-scheme=INTERNAL
    
  3. 为后端服务创建转发规则。

    gcloud compute forwarding-rules create producer-ilb-fr \
        --backend-service=producer-backend-service \
        --region=us-west1 \
        --network=producer-network \
        --subnet=producer-subnet \
        --ip-protocol=UDP \
        --load-balancing-scheme=INTERNAL \
        --ports=6081
    

创建虚拟机实例并将其添加到实例组

在本部分中,您将创建一个包含启动脚本的虚拟机实例,该脚本用于设置 GENEVE 封装的数据包的回显服务器。

在创建虚拟机实例之前,请获取内部直通式网络负载平衡器的转发规则和子网网关的 IP 地址。您需要启动脚本的 IP 地址。

控制台

获取内部直通式网络负载平衡器的 IP 地址

  1. 在 Google Cloud 控制台中,前往负载均衡页面。

    转到“负载均衡”

  2. 点击 producer-ilb 负载均衡器。

  3. 前端部分,记下IP:端口列中转发规则的 IP 地址。

获取子网网关的 IP 地址

  1. 在 Google Cloud 控制台中,前往 VPC 网络页面。

    进入 VPC 网络页面

  2. 点击 producer-network VPC 网络。

  3. 点击子网标签页。

  4. 子网部分,记下网关列中的网关 IP 地址。

创建虚拟机实例

  1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

    转到虚拟机实例

    1. 点击创建实例
    2. 对于名称,输入 producer-instance
    3. 对于区域,请选择 us-west1
    4. 对于可用区,请选择 us-west1-b
    5. 点击网络并配置以下字段:

      网络接口部分中,点击 default 并修改网络接口。

      • 网络producer-network
      • 子网producer-subnet
      • 外部 IPv4 地址
    6. 点击高级,然后在启动脚本中输入以下脚本:

      #!/bin/bash
      # Enable IP forwarding.
      echo 1 > /proc/sys/net/ipv4/ip_forward
      
      # Log incoming packets from the gateway IP and the GENEVE 6081
      # port.
      iptables -t nat -A PREROUTING -p udp -s GATEWAY_IP/32 --dport 6081 -j LOG --log-prefix="[NSI INTERCEPT] "
      
      # Apply DNAT (Destination NAT) on incoming packets from the gateway IP
      # and the GENEVE 6081 port.
      # Set the destination IP to the gateway IP so the packet is rerouted
      # back to the sender.
      iptables -t nat -A PREROUTING -p udp -s GATEWAY_IP/32 --dport 6081 -j DNAT --to-destination GATEWAY_IP
      
      # Apply SNAT (Source NAT) on outgoing packets that have the GENEVE 6081
      # port and source IP that equals the gateway IP.
      # Set the source IP to the internal load balancer IP which
      # effectively swaps the IPs of the original packet.
      iptables -t nat -A POSTROUTING -p udp -s GATEWAY_IP/32 --dport 6081 -j SNAT --to-source LOAD_BALANCER_IP
      
      # Spin up a simple server for health checks on port 80.
      nohup python3 -u -m http.server 80 &
      

      替换以下内容:

      • GATEWAY_IP:子网网关的 IP 地址。
      • LOAD_BALANCER_IP:负载平衡器转发规则的 IP 地址。
    7. 点击创建

将虚拟机实例添加到实例组

  1. 在 Google Cloud 控制台中,前往实例群组页面。

    进入“实例组”

  2. 点击 producer-instance-group

  3. 点击修改

  4. 虚拟机实例部分中,从选择虚拟机列表中选择 producer-instance 虚拟机。

  5. 点击保存

gcloud

  1. 获取负载均衡器转发规则的 IP 地址。

    ILB_IP=$(gcloud compute forwarding-rules describe producer-ilb-fr \
        --region=us-west1 \
        --format="get(IPAddress)")
    
  2. 获取子网网关的 IP 地址。

    GW_IP=$(gcloud compute networks subnets describe producer-subnet \
        --region=us-west1 \
        --format="get(gatewayAddress)")
    
  3. 创建虚拟机实例。

    gcloud compute instances create producer-instance \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=producer-subnet,no-address" \
        --metadata=startup-script='#!/bin/bash
        # Enable IP forwarding.
        echo 1 > /proc/sys/net/ipv4/ip_forward
    
        # Log incoming packets from the gateway IP and the GENEVE 6081 port.
        iptables -t nat -A PREROUTING -p udp -s '"$GW_IP"'/32 --dport 6081 -j LOG --log-prefix="[NSI INTERCEPT] "
    
        # Apply DNAT (Destination NAT) on incoming packets from the gateway
        # IP and the GENEVE 6081 port.
        # Set the destination IP to the gateway IP so the packet is rerouted
        # back to the sender.
        iptables -t nat -A PREROUTING -p udp -s '"$GW_IP"'/32 --dport 6081 -j DNAT --to-destination '"$GW_IP"'
    
        # Apply SNAT (Source NAT) on outgoing packets that have the GENEVE
        # 6081 port and source IP that equals the gateway IP.
        # Set the source IP to the internal load balancer IP which
        # effectively swaps the IPs of the original packet.
        iptables -t nat -A POSTROUTING -p udp -s '"$GW_IP"'/32 --dport 6081 -j SNAT --to-source '"$ILB_IP"'
    
        # Spin up a simple server for health checks on port 80.
        nohup python3 -u -m http.server 80 &'
    
  4. 将虚拟机实例添加到实例组。

    gcloud compute instance-groups unmanaged add-instances producer-instance-group \
        --instances=producer-instance \
        --zone=us-west1-b
    
  5. 将实例组添加到后端服务。

    gcloud compute backend-services add-backend producer-backend-service \
        --region=us-west1 \
        --instance-group=producer-instance-group \
        --instance-group-zone=us-west1-b
    

创建防火墙政策并添加防火墙规则

在本部分中,您将创建防火墙政策并添加防火墙规则,以允许通过 Identity-Aware Proxy (IAP) 建立 UDP 连接、 Google Cloud 健康检查和 SSH 连接到生产者虚拟机实例。

控制台

  1. 在 Google Cloud 控制台中,前往防火墙政策页面。

    转到“防火墙政策”

  2. 点击创建防火墙政策

  3. 名称字段中,输入 producer-firewall-policy

  4. 部署范围列表中,选择全局,然后点击继续

  5. 为您的政策创建以下规则。

    允许从 VPC 网关 IP 地址建立与 GENEVE 端口的 UDP 连接

    1. 点击创建防火墙规则,然后配置以下字段:
      • 优先级100
      • 流量方向入站
      • 匹配时执行的操作允许
      • 来源过滤条件 > IP 范围GATEWAY_IP
      • 协议和端口:选择指定的协议和端口,选中 UDP 复选框,然后在端口中指定 6081
    2. 点击创建

    GATEWAY_IP 替换为子网网关的 IP 地址。

    允许 Google Cloud 健康检查

    1. 点击创建防火墙规则,然后配置以下字段:
      • 优先级101
      • 流量方向入站
      • 匹配时执行的操作允许
      • IP 范围35.191.0.0/16130.211.0.0/22
      • 协议和端口:选择指定的协议和端口,选中 TCP 复选框,然后在端口中指定 80
    2. 点击创建

    允许通过 Identity-Aware Proxy 建立与生产者虚拟机实例的 SSH 连接

    1. 点击创建防火墙规则,然后配置以下字段:
      • 优先级102
      • 流量方向入站
      • 匹配时执行的操作允许
      • IP 范围35.235.240.0/20
      • 协议和端口:选择指定的协议和端口,选中 TCP 复选框,然后在端口中指定 22
    2. 点击创建
  6. 如需将政策与网络关联,请点击继续,然后点击关联

  7. 选中 producer-network 复选框。

  8. 点击继续

  9. 点击创建

gcloud

  1. 创建全球网络防火墙政策。

    gcloud compute network-firewall-policies create producer-firewall-policy \
        --global
    
  2. 将防火墙政策与提供方网络相关联。

    gcloud compute network-firewall-policies associations create \
        --name=producer-firewall-policy-assoc \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --network=producer-network
    
  3. 创建防火墙规则,以允许从 VPC 网关 IP 地址建立与 GENEVE 端口的 UDP 连接。

    gcloud compute network-firewall-policies rules create 100 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=udp:6081 \
        --src-ip-ranges=$GW_IP/32
    
  4. 创建防火墙规则以允许 Google Cloud 健康检查。

    gcloud compute network-firewall-policies rules create 101 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:80 \
        --src-ip-ranges=35.191.0.0/16,130.211.0.0/22
    
  5. 创建防火墙规则,以允许通过 Identity-Aware Proxy 建立与生产者虚拟机实例的 SSH 连接。

    gcloud compute network-firewall-policies rules create 102 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20
    

创建生产者拦截资源

在本部分中,您将创建拦截部署组和拦截部署。

控制台

  1. 在 Google Cloud 控制台中,前往部署组页面。

    前往“部署组”页面

  2. 点击创建部署组

  3. 对于名称,输入 producer-deployment-group

  4. 对于网络,请选择 producer-network

  5. 对于用途,请选择 NSI 带内

  6. 拦截部署部分,点击创建拦截部署,指定以下字段,然后点击创建

    • 名称producer-deployment
    • 区域us-west1
    • 可用区us-west1-b
    • 内部负载均衡器producer-ilb
  7. 点击创建

gcloud

  1. 创建拦截部署组。

    gcloud network-security intercept-deployment-groups create producer-deployment-group \
        --location=global \
        --network=projects/PROJECT_ID/global/networks/producer-network \
        --no-async
    

    PROJECT_ID 替换为您的项目 ID。

  2. 创建拦截部署。

    gcloud network-security intercept-deployments create producer-deployment \
        --location=us-west1-b \
        --forwarding-rule=producer-ilb-fr \
        --forwarding-rule-location=us-west1 \
        --intercept-deployment-group=projects/PROJECT_ID/locations/global/interceptDeploymentGroups/producer-deployment-group \
        --no-async
    

    PROJECT_ID 替换为您的项目 ID。

创建使用方资源

在本部分中,您将为消费者创建以下资源:

  • 包含子网的自定义 VPC 网络。
  • 一个服务器和一个客户端虚拟机。
  • 用于拦截流量的防火墙政策和规则。
  • 拦截端点组和拦截端点组关联。
  • 安全配置文件和安全配置文件组。

创建自定义 VPC 网络

在本部分中,您将创建一个包含子网的 VPC 网络。

控制台

  1. 在 Google Cloud 控制台中,前往 VPC 网络页面。

    进入 VPC 网络页面

  2. 点击创建 VPC 网络

  3. 对于名称,输入 consumer-network

  4. 对于说明,输入 Consumer VPC network

  5. 子网部分中,执行以下操作:

    1. 子网创建模式设置为自定义
    2. 新子网部分中,输入以下信息:
      • 名称consumer-subnet
      • 区域us-west1
      • IP 栈类型IPv4(单栈)
      • IPv4 范围10.10.0.0/16
    3. 点击完成
  6. 点击创建

gcloud

  1. 创建使用方 VPC 网络。

    gcloud compute networks create consumer-network \
        --subnet-mode=custom \
        --description="Consumer VPC network"
    
  2. 在 VPC 网络中,创建一个子网。

    gcloud compute networks subnets create consumer-subnet \
        --network=consumer-network \
        --region=us-west1 \
        --range=10.10.0.0/16
    

创建服务器和客户端虚拟机

在本部分中,您将创建一个服务器虚拟机和一个客户端虚拟机。

控制台

创建服务器虚拟机

  1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

    转到虚拟机实例

  2. 点击创建实例

  3. 名称设置为 consumer-server-vm

  4. 区域设置为 us-west1

  5. 地区设置为 us-west1-b

  6. 机器类型设置为 e2-micro

  7. 点击网络并配置以下字段:

  8. 网络接口部分中,点击 default 并修改网络接口:

    • 网络consumer-network
    • 子网consumer-subnet
    • 外部 IPv4 地址
  9. 点击高级,然后在启动脚本中输入以下脚本:

    echo success > /tmp/connection_test && nohup python3 -u -m http.server --directory /tmp 8000
    
  10. 点击创建

创建客户端虚拟机

  1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

    转到虚拟机实例

  2. 点击创建实例

  3. 名称设置为 consumer-client-vm

  4. 区域设置为 us-west1

  5. 地区设置为 us-west1-b

  6. 机器类型设置为 e2-micro

  7. 点击网络并配置以下字段:

  8. 网络接口部分中,点击 default 并修改网络接口:

    • 网络consumer-network
    • 子网consumer-subnet
    • 外部 IPv4 地址
  9. 点击创建

gcloud

  1. 创建服务器虚拟机。

    gcloud compute instances create consumer-server-vm \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=consumer-subnet,no-address" \
        --metadata=startup-script="echo success > /tmp/connection_test && nohup python3 -u -m http.server --directory /tmp 8000"
    
  2. 创建客户端虚拟机。

    gcloud compute instances create consumer-client-vm \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=consumer-subnet,no-address"
    

创建防火墙政策以允许通过 SSH 进行访问

在本部分中,您将创建防火墙政策并添加防火墙规则,以允许通过 SSH 访问使用方虚拟机。

控制台

  1. 在 Google Cloud 控制台中,前往防火墙政策页面。

    转到“防火墙政策”

  2. 点击创建防火墙政策

  3. 名称字段中,输入 consumer-firewall-policy

  4. 部署范围列表中,选择全局,然后点击继续

  5. 点击创建防火墙规则,配置以下字段,然后点击创建

    • 优先级100
    • 流量方向入站
    • 匹配时执行的操作允许
    • 来源过滤条件 > IP 范围35.235.240.0/20
    • 协议和端口:选择指定的协议和端口,选中 TCP 复选框,然后在端口中指定 22

    IPv4 范围 35.235.240.0/20 包含 Identity-Aware Proxy 用于 TCP 转发的所有 IP 地址。如需了解详情,请参阅准备您的项目以进行 TCP 转发

  6. 如需将政策与网络关联,请点击继续,然后点击关联

  7. 选中 consumer-network 复选框。

  8. 点击继续

  9. 点击创建

gcloud

  1. 创建全球网络防火墙政策。

    gcloud compute network-firewall-policies create consumer-firewall-policy \
        --global
    
  2. 将防火墙政策与使用方网络相关联。

    gcloud compute network-firewall-policies associations create \
        --name=consumer-firewall-policy-assoc \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --network=consumer-network
    
  3. 创建 SSH 允许规则,以允许通过 Identity-Aware Proxy 建立与客户端虚拟机实例的 SSH 连接。

    gcloud compute network-firewall-policies rules create 101 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20
    

    IPv4 范围 35.235.240.0/20 包含 Identity-Aware Proxy (IAP) 用于 TCP 转发的所有 IP 地址。如需了解详情,请参阅准备您的项目以进行 TCP 转发

创建使用方端点组

在本部分中,您将创建拦截端点组和拦截端点组关联。

控制台

  1. 在 Google Cloud 控制台中,前往端点组页面。

    前往“端点组”页面

  2. 点击创建端点组

  3. 对于名称,输入 consumer-endpoint-group

  4. 对于部署群组,从选择项目菜单中选择选择当前项目

  5. 对于拦截部署组,选择 producer-deployment-group

  6. 点击继续

  7. 点击添加端点组关联

  8. 对于项目,选择当前项目。

  9. 对于网络,请选择 consumer-network

  10. 点击创建

gcloud

  1. 创建拦截端点组。

    gcloud network-security intercept-endpoint-groups create consumer-endpoint-group \
        --location=global \
        --intercept-deployment-group=projects/PROJECT_ID/locations/global/interceptDeploymentGroups/producer-deployment-group \
        --no-async
    

    PROJECT_ID 替换为您的项目 ID。

  2. 创建拦截端点组关联。

    gcloud network-security intercept-endpoint-group-associations create consumer-endpoint-group-association \
        --location=global \
        --intercept-endpoint-group=projects/PROJECT_ID/locations/global/interceptEndpointGroups/consumer-endpoint-group \
        --network=consumer-network \
        --no-async
    

    PROJECT_ID 替换为您的项目 ID。

创建安全配置文件和安全配置文件组

创建安全配置文件组和自定义安全配置文件以拦截流量。

控制台

创建安全配置文件

  1. 在 Google Cloud 控制台中,前往安全配置文件页面。

    进入“安全配置文件”

  2. 从项目选择器中选择您的组织。

  3. 安全配置文件标签页中,点击创建配置文件

  4. 对于名称,输入 consumer-security-profile

  5. 对于安全配置文件用途,请选择 NSI 带内

  6. 对于项目,选择当前项目。

  7. 对于拦截端点组,选择 consumer-endpoint-group

  8. 点击创建

创建安全配置文件组

  1. 在 Google Cloud 控制台中,前往安全配置文件组页面。

    前往“安全配置文件组”

  2. 从项目选择器中选择您的组织。

  3. 安全配置文件组标签页中,点击创建配置文件组

  4. 对于名称,输入 consumer-security-profile-group

  5. 对于安全配置文件组用途,选择 NSI 带内

  6. 对于自定义拦截配置文件,请选择 consumer-security-profile

  7. 点击创建

gcloud

  1. 创建拦截安全配置文件。

    gcloud network-security security-profiles custom-intercept create consumer-security-profile \
        --location=global \
        --organization=ORG_ID \
        --intercept-endpoint-group=projects/PROJECT_ID/locations/global/interceptEndpointGroups/consumer-endpoint-group \
        --no-async
    

    替换以下内容:

    • ORG_ID:您的组织的 ID。 安全配置文件是组织级资源。如需创建安全配置文件组,您需要在组织级层具有 Security Profile Admin 角色 (networksecurity.securityProfileAdmin)。
    • PROJECT_ID:您的项目的 ID。
  2. 创建拦截安全配置文件组。

    gcloud network-security security-profile-groups create consumer-security-profile-group \
        --location=global \
        --organization=ORG_ID \
        --custom-intercept-profile=consumer-security-profile \
        --billing-project=PROJECT_ID \
        --no-async
    

    替换以下内容:

    • ORG_ID:您的组织的 ID。 安全配置文件组是组织级资源。如需创建安全配置文件,您需要在组织级层具有 Security Profile Admin 角色 (networksecurity.securityProfileAdmin)。
    • PROJECT_ID:您的项目的 ID。

创建防火墙政策以拦截流量

在本部分中,您将创建防火墙政策和防火墙规则来拦截流量。

控制台

  1. 在 Google Cloud 控制台中,前往防火墙政策页面。

    转到“防火墙政策”

  2. 点击 consumer-firewall-policy

  3. 点击创建防火墙规则,然后配置以下字段:

    • 优先级100
    • 流量方向入站
    • 匹配时执行的操作转到 L7 检查
    • 目的NSI 带内
    • 安全配置文件组consumer-security-profile-group
    • IP 范围10.10.0.0/16
    • 协议和端口:选择指定的协议和端口,选中 TCP 复选框,然后在端口中指定 8000
  4. 点击创建

gcloud

  1. 添加防火墙规则,以拦截服务器虚拟机上 TCP 端口 8000 上的流量。

    gcloud compute network-firewall-policies rules create 100 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=apply_security_profile_group \
        --security-profile-group=organizations/ORG_ID/locations/global/securityProfileGroups/consumer-security-profile-group \
        --direction=INGRESS \
        --layer4-configs=tcp:8000 \
        --src-ip-ranges=10.10.0.0/16
    

    ORG_ID 替换为您的组织的 ID。

测试连接

在本部分中,您将从使用方客户端虚拟机实例向使用方服务器虚拟机实例发送一些网络流量,然后检查提供方虚拟机实例的日志以验证拦截情况。

  1. 运行以下命令,通过 SSH 连接到消费者客户端虚拟机实例,并向消费者服务器虚拟机实例发送请求。

    gcloud compute ssh consumer-client-vm \
        --tunnel-through-iap \
        --zone=us-west1-b \
        --command="curl -m 3 -s http://consumer-server-vm:8000/connection_test || echo fail"
    

    运行上一个命令时,您会看到 success 消息。 表示流量从客户端发送到服务器。

  2. 运行以下命令以检查提供方虚拟机实例的日志。

    gcloud compute ssh producer-instance \
        --tunnel-through-iap \
        --zone=us-west1-b \
        --command="cat /var/log/syslog | grep 'NSI INTERCEPT'"
    

    您会看到类似 [NSI INTERCEPT] IN=ens4 OUT= MAC=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx SRC=10.10.0.1 DST=10.10.0.2 LEN=136 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=UDP SPT=45554 DPT=6081 LEN=116 的消息。这表示客户端-服务器流量被提供方虚拟机实例拦截。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除使用方资源

  1. 将防火墙政策与使用方网络解除关联,并删除防火墙政策。

    控制台

    1. 在 Google Cloud 控制台中,前往防火墙政策页面。

      转到“防火墙政策”

    2. 点击 consumer-firewall-policy 政策。

    3. 点击关联标签页。

    4. 选中 consumer-network 关联复选框。

    5. 点击移除关联

    6. 点击移除

    7. 点击页面顶部的删除

    8. 再次点击删除进行确认。

    gcloud

    1. 移除关联。

      gcloud compute network-firewall-policies associations delete \
          --name=consumer-firewall-policy-assoc \
          --firewall-policy=consumer-firewall-policy \
          --global-firewall-policy
      
    2. 删除防火墙政策。

      gcloud compute network-firewall-policies delete consumer-firewall-policy \
          --global
      
  2. 删除安全配置文件组。

    控制台

    1. 在 Google Cloud 控制台中,前往安全配置文件页面。

      进入“安全配置文件”

    2. 从项目选择器中选择您的组织。

    3. 安全配置文件组标签页中,选择 consumer-security-profile-group 安全配置文件组,然后点击删除

    4. 再次点击删除进行确认。

    gcloud

    gcloud network-security security-profile-groups delete consumer-security-profile-group \
        --location=global \
        --organization=ORG_ID \
        --billing-project=PROJECT_ID \
        --quiet
    

    替换以下内容:

    • ORG_ID:您的组织的 ID
    • PROJECT_ID:您的项目的 ID
  3. 删除安全配置文件。

    控制台

    1. 在 Google Cloud 控制台中,前往安全配置文件页面。

      进入“安全配置文件”

    2. 从项目选择器中选择您的组织。

    3. 安全配置文件标签页中,选择 consumer-security-profile 安全配置文件,然后点击删除

    4. 再次点击删除进行确认。

    gcloud

    gcloud network-security security-profiles custom-intercept delete consumer-security-profile \
        --location=global \
        --organization=ORG_ID \
        --billing-project=PROJECT_ID \
        --quiet
    

    替换以下内容:

    • ORG_ID:您的组织的 ID
    • PROJECT_ID:您的项目的 ID
  4. 删除拦截端点组关联。

    控制台

    1. 在 Google Cloud 控制台中,前往端点组页面。

      前往“端点组”页面

    2. 点击 consumer-endpoint-group

    3. 选择 consumer-network 关联,然后点击删除

    4. 再次点击删除进行确认。

    gcloud

    gcloud network-security intercept-endpoint-group-associations delete consumer-endpoint-group-association \
        --location=global \
        --no-async
    
  5. 删除拦截端点组。

    控制台

    1. 在 Google Cloud 控制台中,前往端点组页面。

      前往“端点组”页面

    2. 选择 consumer-endpoint-group,然后点击删除

    3. 再次点击删除进行确认。

    gcloud

    gcloud network-security intercept-endpoint-groups delete consumer-endpoint-group \
        --location=global \
        --no-async
    
  6. 删除服务器和客户端虚拟机。

    控制台

    1. 在 Google Cloud 控制台中,前往虚拟机实例页面。

      转到虚拟机实例

    2. 选中 consumer-client-vmconsumer-server-vm 对应的复选框,然后点击删除

    3. 再次点击删除进行确认。

    gcloud

    1. 删除客户端虚拟机。

      gcloud compute instances delete consumer-client-vm \
          --zone=us-west1-b \
          --quiet
      
    2. 删除服务器虚拟机。

      gcloud compute instances delete consumer-server-vm \
          --zone=us-west1-b \
          --quiet
      
  7. 删除使用方网络和子网。

    控制台

    1. 在 Google Cloud 控制台中,前往 VPC 网络页面。

      进入 VPC 网络页面

    2. 点击 consumer-network

    3. 子网标签页中,选中 consumer-subnet 的复选框,然后点击删除

    4. 再次点击删除进行确认。

    5. 点击删除 VPC 网络

    6. 在文本字段中输入 consumer-network,然后点击删除

    gcloud

    1. 删除子网。

      gcloud compute networks subnets delete consumer-subnet \
          --region=us-west1 \
          --quiet
      
    2. 删除 VPC 网络。

      gcloud compute networks delete consumer-network \
          --quiet
      

删除提供方资源

  1. 将防火墙政策与提供方网络解除关联,然后删除防火墙政策。

    控制台

    1. 在 Google Cloud 控制台中,前往防火墙政策页面。

      转到“防火墙政策”

    2. 点击 producer-firewall-policy 政策。

    3. 点击关联标签页。

    4. 选中 producer-network 关联复选框。

    5. 点击移除关联

    6. 点击移除

    7. 点击页面顶部的删除

    8. 再次点击删除进行确认。

    gcloud

    1. 移除关联。

      gcloud compute network-firewall-policies associations delete \
          --name=producer-firewall-policy-assoc \
          --firewall-policy=producer-firewall-policy \
          --global-firewall-policy
      
    2. 删除防火墙政策。

      gcloud compute network-firewall-policies delete producer-firewall-policy \
          --global
      
  2. 删除拦截部署和拦截部署群组。

    控制台

    1. 在 Google Cloud 控制台中,前往部署组页面。

      前往“部署组”页面

    2. 点击 producer-deployment-group

    3. 删除 producer-deployment

      1. 选中 producer-deployment 复选框,然后点击删除
      2. 再次点击删除进行确认。
    4. 点击页面顶部的删除按钮。

    5. 再次点击删除进行确认。

    gcloud

    1. 删除拦截部署。

      gcloud network-security intercept-deployments delete producer-deployment \
          --location=us-west1-b \
          --no-async
      
    2. 删除拦截部署群组。

      gcloud network-security intercept-deployment-groups delete producer-deployment-group \
          --location=global \
          --no-async
      
  3. 删除负载均衡器资源。

    控制台

    1. 在 Google Cloud 控制台中,前往负载均衡页面。

      转到“负载均衡”

    2. 选中 producer-ilb 复选框。

    3. 点击页面顶部的删除按钮。

    4. 选中 producer-health-check 复选框,然后点击删除负载均衡器和所选的资源

    gcloud

    1. 删除转发规则。

      gcloud compute forwarding-rules delete producer-ilb-fr \
          --region=us-west1 \
          --quiet
      
    2. 移除后端服务。

      gcloud compute backend-services delete producer-backend-service \
          --region=us-west1 \
          --quiet
      
    3. 删除健康检查。

      gcloud compute health-checks delete producer-health-check \
          --region=us-west1 \
          --quiet
      
  4. 删除生产者虚拟机实例。

    控制台

    1. 在 Google Cloud 控制台中,前往虚拟机实例页面。

      转到虚拟机实例

    2. 选中 producer-instance 复选框,然后点击删除

    3. 再次点击删除进行确认。

    gcloud

    gcloud compute instances delete producer-instance \
        --zone=us-west1-b \
        --quiet
    
  5. 删除实例组。

    控制台

    1. 在 Google Cloud 控制台中,前往实例群组页面。

      进入“实例组”

    2. 选中 producer-instance-group 复选框。

    3. 点击删除

    4. 在确认窗口中,点击删除

    gcloud

    gcloud compute instance-groups unmanaged delete producer-instance-group \
        --zone=us-west1-b \
        --quiet
    
  6. 删除提供方网络和子网。

    控制台

    1. 在 Google Cloud 控制台中,前往 VPC 网络页面。

      进入 VPC 网络页面

    2. 点击 producer-network

    3. 子网标签页中,选中 producer-subnet 复选框,然后点击删除

    4. 再次点击删除进行确认。

    5. 点击删除 VPC 网络

    6. 在文本字段中输入 producer-network,然后点击删除

    gcloud

    1. 删除子网。

      gcloud compute networks subnets delete producer-subnet \
          --region=us-west1 \
          --quiet
      
    2. 删除网络。

      gcloud compute networks delete producer-network \
          --quiet
      

后续步骤