手把手教您建立Google Cloud VPN教程
建立Google Cloud VPN 步骤教学看这边
当你想要将服务转换到Google Cloud 上,却发现你无法将所有元件搬移上去,得要在分散于两个不同网路的元件之间传递资料时,你会开始在想「我要怎么在两个受信任的网路之间传递资料而不被窃听?帮每个元件之间的连线都改用SSL/TLS 连线吗?如果该元件不支援SSL/TLS 怎么办?」。这时我们可以采用IPsec VPN ,将两个网路之间的通讯进行加密,借此保护你的资料不会以明文的方式在网际网路上进行传输,也省去要对多个元件分别修改设定SSL /TLS 的困扰。而IPsec VPN 是被Google Cloud VPN 支援的,所以我们在此使用Google Cloud VPN 来为两端的网路间建立安全的通道。
需求与限制
• 两端(分别是in-house与Google Cloud端)都需要有VPN gateway 。in-house端可以是独立的VPN gateway设备,也可以在server上透过安装软体完成。Google Cloud端则由Google Cloud VPN提供。
• in-house端的VPN gateway需要支援IKEv1或IKEv2 pre-shared key authentication 。
• in-house端的VPN gateway需要支援ESP Tunnel Mode 。
• 两端的VPN gateway都需要有static public IP 。
• 如果in-house VPN gateway外有firewall ,请开放udp:500,4500这两个port以便让ESP & IKE流量通过
• 两端网路使用的IP range不可以重叠
• 准备好<secret-key> ,在设定两端的VPN gateway时会用到。
这里为了简化说明,我们会使用Google Cloud来建立in-house端的环境。
准备in-house 端与Google Cloud 端的网路
建立in-house 端网路
gcloud compute --project "in-house-project" networks create "vpn" --subnet-mode "custom"
gcloud compute --project "in-house-project" networks subnets create "vpn-asia-east1" --network "vpn" --region "asia-east1" --range "192.168.0.0/24"
gcloud compute --project "in-house-project" firewall-rules create "vpn-allow-ssh" --allow tcp:22 --network "vpn" --source-ranges "0.0.0.0/0"
gcloud compute --project "in-house-project" firewall-rules create "vpn-allow-internal" --allow tcp,udp,icmp --network "vpn" --source-ranges "192.168.0.0/24"
gcloud compute --project "in-house-project" firewall-rules create "vpn-allow-gcloud" --allow tcp,udp,icmp --network "vpn" --source-ranges "10.240.0.0/24"
接着建立Google Cloud端网路,这里将in-house端网路的IP range设定为192.168.0.0/24 ,同时设定了firewall让我们能透过SSH连上GCE instance进行操作、让in-house端网路内部互连以及与Google Cloud端网路能够互连。
gcloud compute --project "google-cloud-project" networks create "vpn" --subnet-mode "custom"
gcloud compute --project "google-cloud-project" networks subnets create "vpn-asia-east1" --network "vpn" --region "asia-east1" --range "10.240.0.0/24"
gcloud compute --project "google-cloud-project" firewall-rules create "vpn-allow-ssh" --allow tcp:22 --network "vpn" --source-ranges "0.0.0.0/0"
gcloud compute --project "google-cloud-project" firewall-rules create "vpn-allow-internal" --allow tcp,udp,icmp --network "vpn" --source-ranges "10.240.0.0/24"
gcloud compute --project "google-cloud-project" firewall-rules create "vpn-allow-inhouse" --allow tcp,udp,icmp --network "vpn" --source-ranges "192.168.0.0/24"
最后请执行以下指令,在两个project内各保留一个public IP供VPN gateway使用。这里将Google Cloud端网路的IP range设定为10.240.0.0/24 ,与in-house端的192.168.0.0/24 有所不同,这里也同时设定了firewall让我们能透过SSH连上GCE instance进行操作、让Google Cloud端网路内部互连以及与in-house端网路能够互连。
gcloud compute --project "in-house-project" addresses create "vpn-gateway" --region "asia-east1"
gcloud compute --project "google-cloud-project" addresses create "vpn-gateway" --region "asia-east1"
记下保留下来的IP address (可以到VPC network 的External IP addresses 那边查看刚刚设立的IP ),这里假设在in-house-project 跟google-cloud-project 保留下来的IP 分别是111.111.111.111 与111.111 .222.222,之后会用到。
准备in-house 端环境
step 1 在in-house 端网路建立VPN gateway
可以参考Set up a VPN gateway,在in-house端的网路内找一台Linux server ,安装strongSwan来当作VPN Gateway 。
执行以下指令,建立一个GCE instance 当作VPN gateway ,设定firewall 开放ESP & IKE 流量后,使用SSH 连上它
gcloud compute --project "in-house-project" instances create "vpn-gateway" \
--zone "asia-east1-b" --machine-type "f1-micro" \
--subnet "vpn-asia-east1" --private-network-ip "192.168.0.2" --address 111.111.111.111 \
--can-ip-forward --tags "vpn-gateway" --image "debian-8-jessie-v20180307" --image-project "debian-cloud"
gcloud compute --project "in-house-project" firewall-rules create "vpn-allow-ike-esp" \
--allow udp:500,udp:4500 --network "vpn" --source-ranges "111.111.222.222" \
--target-tags "vpn-gateway"
gcloud compute --project "in-house-project" ssh "vpn-gateway" --zone "asia-east1-b"
之后开始安装strongSwan ,这里先来准备设定档ipsec.conf 。这里可以使用你习惯的编辑器,将以下设定贴入设定档中。设定档里面的left指的是现在正在设定的VPN gateway所在的网路相关设定, right则是另一端网路的相关设定。
conn myconn
authby=psk
auto=route
dpdaction=hold
ike=aes256-sha1-modp2048,aes256-sha256-modp2048,aes256-sha384-modp2048,aes256-sha512-modp2048!
esp=aes256-sha1-modp2048,aes256-sha256-modp2048,aes256-sha384-modp2048,aes256-sha512-modp2048!
forceencaps=yes
keyexchange=ikev2
mobike=no
type=tunnel
left=%any
leftid=111.111.111.111
leftsubnet=192.168.0.0/24
leftauth=psk
leftikeport=4500
right=111.111.222.222
rightsubnet=10.240.0.0/24
rightauth=psk
rightikeport=4500
然后执行以下指令安装并设定strongSwan ,记得将<secret-key> 替换成你想要的字串
sudo apt-get update
sudo apt-get install strongswan -y
echo "%any : PSK \"<secret-key>\"" | sudo tee /etc/ipsec.secrets > /dev/null
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf > /dev/null
sudo sysctl -p
sudo cp ipsec.conf /etc
sudo ipsec restart
sudo ipsec up myconn
step 2 在in-house 端网路设定前往Google Cloud 端网路的route
gcloud compute --project "in-house-project" routes create "inhouse2gcloud" \
--destination-range "10.240.0.0/24" --network "vpn" --next-hop-instance "vpn-gateway" \
--next-hop-instance-zone "asia-east1-b" --priority "1000"
step 3 在in-house 端网路建立VPN client
gcloud compute --project "in-house-project" instances create "vpn-client" \
--zone "asia-east1-b" --machine-type "f1-micro" \
--subnet "vpn-asia-east1" --image "debian-8-jessie-v20180307" --image-project "debian-cloud"
gcloud compute --project "in-house-project" ssh "vpn-client" --zone "asia-east1-b"
然后安装Nginx
sudo apt-get update
sudo apt-get install nginx-light -y
echo "This is a response coming from in-house." | sudo tee /var/www/html/index.html > /dev/null
准备Google Cloud 端环境
步骤1设定Google Cloud VPN
记得将<secret-key> 替换成你想要的字串
gcloud compute --project "google-cloud-project" target-vpn-gateways create "vpn-gateway" \
--region "asia-east1" --network "vpn"
gcloud compute --project "google-cloud-project" forwarding-rules create "vpn-gateway-rule-esp" \
--region "asia-east1" --address "111.111.222.222" --ip-protocol "ESP" --target-vpn-gateway "vpn-gateway"
gcloud compute --project "google-cloud-project" forwarding-rules create "vpn-gateway-rule-udp500" \
--region "asia-east1" --address "111.111.222.222" --ip-protocol "UDP" --port-range "500" \
--target-vpn-gateway "vpn-gateway"
gcloud compute --project "google-cloud-project" forwarding-rules create "vpn-gateway-rule-udp4500" \
--region "asia-east1" --address "111.111.222.222" --ip-protocol "UDP" --port-range "4500" \
--target-vpn-gateway "vpn-gateway"
gcloud compute --project "google-cloud-project" vpn-tunnels create "tunnel2inhouse" \
--region "asia-east1" --peer-address "111.111.111.111" --shared-secret "" \
--ike-version "2" --target-vpn-gateway "vpn-gateway"
step 2 在Google Cloud 端网路设定前往in-house 端网路的route
gcloud compute --project "google-cloud-project" routes create "gcloud2inhouse" \
--network "vpn" --next-hop-vpn-tunnel "tunnel2inhouse" \
--next-hop-vpn-tunnel-region "asia-east1" --destination-range "192.168.0.0/24"
step 3 在Google Cloud 端网路建立VPN client
gcloud compute --project "google-cloud-project" instances create "vpn-client" \
--zone "asia-east1-b" --machine-type "f1-micro" --subnet "vpn-asia-east1" \
--image-family "debian-8"
gcloud compute --project "google-cloud-project" ssh "vpn-client" --zone "asia-east1-b"
然后安装Nginx
sudo apt-get update
sudo apt-get install nginx-light -y
echo "This is a response coming from Google Cloud." | sudo tee /var/www/html/index.html > /dev/null
执行测试
gcloud compute --project "in-house-project" ssh vpn-client \
--command 'curl http://10.240.0.2 2>/dev/null'
gcloud compute --project "google-cloud-project" ssh vpn-client \
--command 'curl http://192.168.0.3 2>/dev/null'
如果分别看到This is a response coming from Google Cloud. 和This is a response coming from in-house. 的话,就代表两边网路之间的VPN连线已经建立成功了!
VPC Network:GCP 和AWS 功能比较
本文整理GCP 的Virtual Private Cloud (VPC) 的笔记,重点在VPC Network、Shared VPC (XPN),同时跟AWS VPC 的设计比较。
摘要VPC Network
GCP的VPC Network整体来讲,有以下三个强项:
• Global
• Sharable
• Expandable
这三点大概都是AWS 比较没有的,也是很吸引我的因素之一。
基本概念
• VPC Network : a global entity spanning all GCP regions.在这个VPC Network里的VM (不管在哪一个Region)都可以直接透过Private IP沟通.
• entity spanning:实体跨越
• 每个Project会有一个预设的network
• 每个Project限制有5个network (同AWS的soft limit)
• Subnets:
• 在单一的regions里.每个region里可以有多个subnet
• 在GCP里,跟Subnetworks是指同一个东西。
• gcloud cli里会用subnetwork表示sub command
• GCP的Subnet概念等同于AWS Region里的一个VPC CIDR
• GCP没有类似AWS VPC Subnet的概念,要做类似概念就是在同一个Region直接规划很多Subnets
• Subnet的CIDR Space可以放大,但不能缩小。
• Gateway:
• 每个Subnet会有预设的Gateway: xxx.yyy.zzz.1
• 没有NAT概念的节点,透过Route、Firewall控制
配置与管理
• 建立VPC Network有两种方法:自动、手动
• 自动:会在每个region配置一个subnet (相当于开一个AWS Account,每个region都会有一个default VPC)
• 手动:可以自己规划subnet的CIDR,相当于自行规划AWS每个VPC的CIDR
• 预设自动的VPC Network CIDR都一样,所以project之间要透过网路互通,CIDR要避开Space,不然就无法串接。
• 详细参见: Types of VPC networks
• 手动规划Subnet CIDR,要符合 RFC1918 规范
• 每个subnet都会有以下四个保留位址:
• Network address (first address in the CIDR range)
• Default gateway address (second address in the CIDR range)
• Reserved address (second-to-last address in the CIDR range )
• Broadcast address (last address in the CIDR range)
Firewall rules
• Scope: Global,每个VPC Network有预设数个常用的firewalls,像是icmp, rdp, ssh,还有internal
• 预设的firewall rules只有default network才有,自己建出来的VPC network则不会有这些firewall rules,得自己设定
• 可以透过tag方式套用到不同的VM or resources
• 有allow、deny两种,每条rule只能是一种
Shared VPC Network
预设GCP 每个Project 都有独立的VPC Network,预设的CIDR 是一样的,换句话说,未来如果要资料交换,将会造成CIDR 碰撞问题,所以如果组织有一定规模,应该交由Network or Infra Team 负责网路CIDR 规划,避免未来因资料交换需求,造成路由复杂以及管理困难。
类似概念,在AWS VPC规划也是一样,更多请参阅:Plan and Design Multiple VPCs in Different Regions 笔记。
GCP 提供的功能称为Shared VPC (简称XPN),目的就是让不同的Project,共享同样的VPC Network。规划时就是在一个Project 里规划VPC Network,透过XPN 把Subnet 分享给其他Project。
Overview
下图是官方的文件描述的使用概念:
规划使用XPN要从organization开始看起:
1.权限(IAM Role):在organization里的IAM,授权特定User有Compute Shared VPC Admin的权限
2.规划Shared VPC host project:这个Project用途就是共享VPC Network,同时也只有Infra / Network Admin可以控管
• 规划CIDR分配范围(RFC1918)
• 考虑Regional, Project
3.规划Shared VPC service project:规划多个特定功能的Project,只要是团队、架构、预算独立,就可以独立一个project。
• 想像microservice每个service规划一个project
• 一个scrum team (PO, Dev, QA, Ops),他们有独立的商业目标、开发流程、预算考量。
• Infra & Security交由专门的Team统控
4. 特殊需求的,像是PCI DSS 需要的环境,则可以使用 Standalone project,换言之,他是独立的VPC Network,不跟XPN 共享。
规划案例– 依环境拆分
这是官方举的案例,考虑环境:
• 开发、正式环境切离
• 每个环境维度都有个XPN Host Project
• 每个服务不同的环境,都有独立的预算与权限
Limitation
• 每个VM Instance最多8张Network Interface,详细参阅 Creating Instances with Multiple Network Interfaces
• 每个Project最多5个VPC Network
• 每个Cloud Organization可以有100个shared VPC host projects
• 每个shared VPC host project可以attach 100个service projects
• GKE / GAE目前不支援Shared VPC.
Compare GCP VPC Network with AWS
简单整理GCP 跟AWS 的比较,如下图:
大家都知道Google 对于基础架构投入非常多资源,包含跨洲光缆、资料中心的建置,实体基础设施如此的强大,想见Cloud 上的Infra 不会太令人失望,果然!GCP 的强项就在于 Global 的概念,光这一点的基础架构,就让原本以IaaS 开始的AWS 加紧推出对应的服务,像是 Endpoint Service / Private Link、inter-region VPC peering connection 等功能。
虽然GCP VPC 完整性平心而论,还是有一些不足,但是未来依旧是可以期待的!
延伸阅读
• GCP: Experience GCP
• AWS: Study Notes – AWS VPC (Virtual Private Cloud)
• AWS: Plan and Design Multiple VPCs in Different Regions
参考资料
• Virtual Private Cloud (VPC) Network Overview
• Shared VPC Overview
• Google Cloud Platform for AWS Professionals: Networking
参考
• VPN简述Google Cloud VPN概述
• 建立VPN 创建Google Cloud VPN
• 将Cloud VPN与Palo Alto Networks PA-3020结合使用
• 将Cloud VPN与Amazon Web Services结合使用
• 将Cloud VPN与Fortinet FortiGate 300C结合使用
• 将Cloud VPN与Cisco ASA结合使用
• 将Cloud VPN与VyOS一起使用
• 将Cloud VPN与Microsoft AzureTM VPN网关一起使用
• 在GCE上方建立VPN网关在GCE实例上设置VPN网关



QQ咨询
旺旺咨询