当前位置: 首页 > article >正文

让你的内网的GCE 也可以访问外网,Google Cloud Nat 介绍

场景

当我用下面的terrform 构建1个GCE Vm后

resource "google_compute_instance" "k8s-master" {
  name         = "k8s-master"
  project  = var.project_id
  zone = var.zone_id
  
  allow_stopping_for_update = true
  machine_type = "n2d-highmem-2" # 2cpu 16GB
  
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      size  = 20
    }
  }
  
  network_interface {
    network =  var.vpc
    subnetwork =  var.subnet
  }

  service_account {
    email  = "vm-common@jason-hsbc.iam.gserviceaccount.com"
    scopes = ["https://www.googleapis.com/auth/cloud-platform"]
  }

  # https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#provisioning_model
  # to reduce cost
  scheduling { 
    automatic_restart = false # Scheduling must have preemptible be false when AutomaticRestart is true.
    provisioning_model = "SPOT"
    preemptible         = true
  }
}

发现创建后的VM 并没有外网访问权限, 原因是没有分配外部ip, 导致一些操作(例如安装某些软件)不方便
在这里插入图片描述





解决方法一 分配外部ip

暴力解决方法就是分配1个外部ip, 但是外部ip是收费资源, 而且当vm很多的时候, 申请大量外部ip十分浪费。
而且不符合微服务的构建思想。

在微服务里, 通常只有load balancer or api gateway 才可以被外网访问。





解决方法二

使用 Google Cloud Nat

这个就是本文想解决的问题





什么是NAT gateway

Nat 的全程是 Network address tranformation , 网络地址转换…
通常是1台具有双网卡 (双 ip)地址的机器, 1个公网ip, 1个内网ip

功能是让局域网的各个内网主机可以通过这个Nat gatway , 去访问公网。
但是公网是不能通过这个Nat gateway去访问指定内网主机的。

是的, 我们的家用路由器实际上应该是1个Nat 网关, 而不是一台狭义的路由, 只不过名字叫路由器而已





GCP Cloud Nat 介绍

所以google的cloud Nat 就是的功能就是让没有公网ip的内网主机可以访问公网。

而CloudNat 是绑定在某个VPC 上的, 但是可以for all the subnet

图:
在这里插入图片描述




GCP Cloud Nat 配置

Cloud Nat 配置相当简单

首先需要1个Cloud Routes , 注意这个Cloud Routes 和 VPC Routes 并不是用1个东西

然后创建1个Cloud Nat, 模式选择 Auto only, source_subnetwork_ip_ranges_to_nat 设成 ALL_SUBNETWORKS_ALL_IP_RANGES

具体terraform代码:

## Create Cloud Router

resource "google_compute_router" "tf-router" {
  project = var.project_id
  name    = "tf-nat-router"
  network = google_compute_network.tf-vpc.name
  region  = var.region_id
}

## Create Nat Gateway

resource "google_compute_router_nat" "tf-cloud-nat" {
  name                               = "tf-cloud-nat"
  router                             = google_compute_router.tf-router.name
  region                             = var.region_id
  nat_ip_allocate_option             = "AUTO_ONLY"
  source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"

  log_config {
    enable = true
    filter = "ERRORS_ONLY"
  }
}

一旦创建后, 这个VPC的内网主机就可以访问外网了

我们也可以在下面页面查看Cloud Nat 的信息
在这里插入图片描述
在这里插入图片描述


http://www.kler.cn/a/157806.html

相关文章:

  • 大型语言模型在实体关系提取中的应用探索(二)
  • K8S 工具收集
  • 智跃人力资源管理系统GenerateEntityFromTable.aspx接口SQL注入漏洞复现 [附POC]
  • 微软 Power Platform 零基础 Power Pages 网页搭建教程学习实践(一)
  • 计算机网络之IP篇
  • C/C++---------------LeetCode第27. 移除元素
  • SSM框架(四):SSM整合 案例 + 异常处理器 +拦截器
  • 如何提高Pycharm的使用体验?
  • OpenGL学习(一)绘制一个图形
  • Redis高可用之Sentinel哨兵模式
  • Apache Doris 详细教程(三)
  • Django大回顾-4 自定义过滤器和标签、模型层
  • 某60区块链安全之JOP实战一学习记录
  • 重磅 | 国内首款带DESAT保护功能兼容光耦SLMi330CG-DG完美兼容ACPL-330J
  • 安卓apk抓包(apk抓不到包怎么办)
  • STM32(PWM、ADC)
  • GPC-数据鉴别(DAP)模式验证
  • 【华为OD机试python】分割数组的最大差值【2023 B卷|100分】
  • 【分布式算法】Raft算法详解
  • 「Go框架」gin框架是如何处理panic的?