跳转到内容
搜索文档

虚拟网络

最后更新 查看 MarkdownAgent 设置

功能可用性

客户端模式 Zero Trust 计划
  • 流量和 DNS 模式
  • 仅流量模式
所有计划
系统 可用性
Windows
macOS
Linux
iOS
Android
ChromeOS

虚拟网络在您的 Cloudflare 账户内提供路由隔离。每个虚拟网络都维护自己的路由表,使您能够隔离不同环境、合作伙伴或应用程序之间的流量。

例如,某个组织可能拥有独立的“生产”(production)和“测试”(staging)VPC 网络,它们都使用相同的私有 IP 范围(例如 10.128.0.0/24)。如果没有虚拟网络,Cloudflare 就无法区分生产环境中的 10.128.0.1 与测试环境中的 10.128.0.1。通过创建两个虚拟网络,您可以确定性地将流量路由到正确的环境。用户可以在 Cloudflare One Client 中选择他们想要连接的虚拟网络。

有关虚拟网络的概念性概述(包括它们在各类 Cloudflare 产品中的工作方式),请参阅虚拟网络(Virtual networks)

使用场景

以下是虚拟网络可能会派上用场的几个场景:

  • 管理使用相同地址空间的生产和暂存(staging)环境。
  • 管理使用相同地址空间的组织之间的收购或合并。
  • 允许 IT 专业服务出于各种行政和管理目的访问其客户的网络。
  • 允许开发人员或家庭实验室(homelab)用户确定性地通过其家庭网络路由流量,以实施附加的安全控制。
  • 出于安全原因,在网络和资源之间保证额外的隔离(不仅是策略执行),同时将所有配置保持在单个 Cloudflare 账户内。

前提条件

创建虚拟网络

在此示例中,“私有网络”是指具有自己重叠 IP 地址空间(暂存为 10.128.0.1/32,生产为 10.128.0.1/32)的独立环境(例如暂存环境或生产环境)。如果您的环境使用不重叠的 IP,则无需为每个环境创建单独的隧道。相反,您可以向单个隧道添加多个路由。

若要通过虚拟网络路由重叠的 IP:

  1. 创建两个唯一的虚拟网络:
    1. 在 Cloudflare 仪表板中,转到 Networking(网络) > Routes(路由) > Virtual networks(虚拟网络)

      Go to Virtual networks ↗
    2. 选择Create virtual network(创建虚拟网络)

    3. 将您的虚拟网络命名为 staging-vnet 并选择 Save(保存)

    4. 重复步骤 1a-1d 以创建另一个名为 production-vnet 的虚拟网络。

  2. 为每个具有重叠 IP 的私有网络创建一个 Cloudflare Tunnel(每个隔离环境一个隧道,例如暂存环境和生产环境):
    1. 转到 Networking(网络) > Tunnels(隧道)

    2. 选择Create a tunnel(创建隧道)

    3. 将您的隧道命名为 Staging tunnel 并选择 Create(创建)

    4. 在您的暂存环境中安装连接器。

    5. 转到 Networking(网络) > Routes(路由)

      Go to Routes ↗
    6. 选择Create route(创建路由) > Tunnel CIDR(隧道 CIDR)

    7. 选择 Staging tunnel,输入 10.128.0.1/32 作为网络,并选择 staging-vnet 作为虚拟网络。选择Create route(创建路由)

    8. 重复步骤 2a-2d 以创建另一个名为 Production tunnel 的隧道。请务必在您的生产环境中安装连接器。

    9. 重复步骤 2e-2g,为 Production tunnel 创建一个路由,并将 10.128.0.1/32 分配给 production-vnet

我们现在有两个分别路由到 staging-vnetproduction-vnet 的重叠 IP 地址。您可以使用 Cloudflare One 客户端在虚拟网络之间切换

若要通过虚拟网络路由重叠的 IP:

  1. 向您的 cloudflare_api_token 添加以下权限:

    • Cloudflare Tunnel Write
  2. 创建两个唯一的虚拟网络:

    resource "cloudflare_zero_trust_tunnel_cloudflared_virtual_network" "staging_vnet" {
    	account_id = var.cloudflare_account_id
    	name       = "staging-vnet"
    	comment    = "Staging virtual network"
    	is_default = false
    }
    
    resource "cloudflare_zero_trust_tunnel_cloudflared_virtual_network" "production_vnet" {
    	account_id = var.cloudflare_account_id
    	name       = "production-vnet"
    	comment    = "Production virtual network"
    	is_default = false
    }
  3. 为每个具有重叠 IP 的私有网络创建一个 Cloudflare Tunnel(每个隔离环境一个隧道,例如暂存环境和生产环境):

    resource "cloudflare_zero_trust_tunnel_cloudflared" "staging_tunnel" {
    	account_id = var.cloudflare_account_id
    	name       = "Staging tunnel"
    	config_src = "cloudflare"
    }
    
    resource "cloudflare_zero_trust_tunnel_cloudflared" "production_tunnel" {
    	account_id = var.cloudflare_account_id
    	name       = "Production tunnel"
    	config_src = "cloudflare"
    }
  4. 通过 Staging tunnel 路由 10.128.0.1/32 并将其分配给 staging-vnet。通过 Production tunnel 路由 10.128.0.1/32 并将其分配给 production-vnet

    resource "cloudflare_zero_trust_tunnel_cloudflared_route" "staging_tunnel_route" {
    	account_id         = var.cloudflare_account_id
    	tunnel_id          = cloudflare_zero_trust_tunnel_cloudflared.staging_tunnel.id
    	network            = "10.128.0.1/32"
    	comment            = "Staging tunnel route"
    	virtual_network_id = cloudflare_zero_trust_tunnel_cloudflared_virtual_network.staging_vnet.id
    }
    
    resource "cloudflare_zero_trust_tunnel_cloudflared_route" "production_tunnel_route" {
    	account_id         = var.cloudflare_account_id
    	tunnel_id          = cloudflare_zero_trust_tunnel_cloudflared.production_tunnel.id
    	network            = "10.128.0.1/32"
    	comment            = "Production tunnel route"
    	virtual_network_id = cloudflare_zero_trust_tunnel_cloudflared_virtual_network.production_vnet.id
    }
  5. 获取每个隧道的令牌

  6. 使用隧道令牌,在您的暂存环境中运行 Staging tunnel,在您的生产环境中运行 Production tunnel。参阅安装并运行隧道

若要为本地管理隧道通过虚拟网络路由重叠的 IP:

  1. 为每个具有重叠 IP 的私有网络创建一个 Cloudflare Tunnel(每个隔离环境一个隧道,例如暂存环境和生产环境):

    1. 在暂存环境中对 cloudflared 进行身份验证:

      cloudflared login
    2. 创建一个隧道以将您的暂存网络连接到 Cloudflare。

      cloudflared tunnel create staging-tunnel
    3. 在生产环境中对 cloudflared 进行身份验证:

      cloudflared login
    4. 创建一个隧道以将您的生产网络连接到 Cloudflare。

      cloudflared tunnel create production-tunnel

以下步骤可以从任何 cloudflared 实例执行。

  1. 创建两个唯一的虚拟网络。

    cloudflared tunnel vnet add staging-vnet
    cloudflared tunnel vnet add production-vnet
  2. 在继续之前,运行以下命令以验证新创建的虚拟网络是否正确列出:

    cloudflared tunnel vnet list
  1. 用您私有网络的 IP/CIDR 范围配置您的隧道,并将这些隧道分配给它们各自的虚拟网络。

    cloudflared tunnel route ip add --vnet staging-vnet 10.128.0.3/32 staging-tunnel
    cloudflared tunnel route ip add --vnet production-vnet 10.128.0.3/32 production-tunnel
  2. 验证 IP 路由是否正确列出:

    cloudflared tunnel route ip list

    我们现在有两个分别路由到 staging-vnetproduction-vnet 的重叠 IP 地址。

    1. 在您的暂存环境中,为 staging-tunnel 创建一个配置文件。该配置文件结构如下:

      tunnel: <Tunnel-UUID>
      credentials-file: /root/.cloudflared/credentials-file.json
      warp-routing:
         enabled: true
    2. 运行您的隧道。

      cloudflared tunnel run staging-tunnel
    3. 在您的生产环境中,对 production-tunnel 重复步骤 6 和 7。

    您现在可以使用 Cloudflare One 客户端在虚拟网络之间切换

删除虚拟网络

若要删除虚拟网络:

  1. 在 Cloudflare 仪表板中,转到 Networking(网络) > Routes(路由)

    Go to Routes ↗
  2. Routes(路由) 选项卡上,检查没有路由分配给您要删除的虚拟网络。如果虚拟网络正在使用中,请先删除这些路由或将它们重新分配给其他虚拟网络。

  3. 转到 Virtual networks(虚拟网络) 选项卡并找到您的虚拟网络。

  4. 选择三点菜单并选择 Delete(删除)

您可以选择删除与您的虚拟网络相关联的隧道。

若要为本地管理隧道删除虚拟网络:

  1. 删除虚拟网络中的所有 IP 路由。例如:

    cloudflared tunnel route ip delete --vnet staging-vnet 10.128.0.3/32
  2. (可选)删除与虚拟网络相关联的隧道。

    cloudflared tunnel delete staging-tunnel
  3. 删除虚拟网络。

    cloudflared tunnel vnet delete staging-vnet

您可以通过输入 cloudflared tunnel vnet list 来验证虚拟网络是否已被成功删除。

连接到虚拟网络

Windows、macOS 和 Linux

  1. 打开 Cloudflare One 客户端。
  2. 转到 Home(主页)
  3. VNET(虚拟网络) 下拉菜单中,选择您要连接的虚拟网络(例如,staging-vnet)。
  1. 打开 Cloudflare One 客户端。
  2. 转到 Settings(设置) > Traffic and DNS mode(流量和 DNS 模式) > Virtual networks(虚拟网络)
  3. 选择您要连接的虚拟网络,例如 staging-vnet

当您访问 10.128.0.3/32 时,Cloudflare One 客户端会将您的请求路由到暂存环境。

iOS、Android 和 ChromeOS

  1. 启动 Cloudflare One Agent 应用。
  2. 转到 Advanced(高级) > Connection options(连接选项) > Virtual networks(虚拟网络)
  3. 选择您要连接的虚拟网络,例如 staging-vnet

当您访问 10.128.0.3/32 时,Cloudflare One 客户端会将您的请求路由到暂存环境。

这篇文档对您有帮助吗?