NVIDIA 初创加速计划,免费加速您的创业启动 了解详情
写点什么

构建健壮的混合云网络——BJS VPN 篇

  • 2019-11-19
  • 本文字数:5089 字

    阅读完需:约 17 分钟

构建健壮的混合云网络——BJS VPN篇

背景介绍:


近年来,随着公有云的普及,一方面,越来越多的用户选择利用公有云在弹性、灵活性等方面的优势,在云上部署新的应用系统,另一方面,大量的企业有很多现有的本地基础设施投资,所以企业上云的过程并不是一触而就的,期间势必存在云应用与本地数据中心应用并存的局面,为了更好的融合云与本地数据中心的应用环境,实现整体应用系统的稳定性和高可用性,构建一个健壮的混合云网络至关重要。


在 AWS 上,用来连接 AWS 与本地数据中心的方式主要有以下 3 种:


  1. 纯 VPN 组网

  2. 纯专线组网

  3. VPN 与专线的混合组网


其中,对于 AWS 中国区来讲,由于 AWS 自身的 VPN 服务 VGW 目前尚未落地,客户急需要一个替代方案,能够达到类似于 VGW 的冗余及故障切换功能。


本篇主要讲述第一种组网方式,着眼点在于如何实现混合云网络的健壮性及故障自愈,并不会讲述太多的 IPSec VPN 知识。


对于第二,第三种组网方式的高可用实现,请参考:


《构建健壮的混合云网络——BJS DX 篇》


《构建健壮的混合云网络——BJS DX+VPN 篇》


对于如何实现对 VPN 流量的监控、故障告警及事件日志搜集,请参考:


《构建健壮的混合云网络——BJS DX+VPN 篇》


拓扑图:



Strongswan-1 和 Strongswan-2 分别作为 Region A 两个 AZ 内的 VPN 设备,与客户本地站点的 internet 出口设为分别建立两条 VPN 隧道,Private-1 和 Private-2 分别模拟 Region A 两个 AZ 中的两台内网设备。


本场景需要实现如下目的:


  1. Private-1,Private-2 能够与本地站点内的设备通过私网互通

  2. AWS 侧能够检测 VPN 连接的健康状态并实现自动的故障切换

  3. AWS 侧能够检测 VPN 设备的健康状态并实现自动的故障切换

  4. 本地站点侧能够检测 VPN 连接的健康状态并实现自动的故障切换

  5. 本地站点侧能够检测 VPN 设备的健康状态并实现自动的故障切换


系统工作流程:


  1. Strongswan-1 和 Strongswan-2 设置开机自动运行 vpn_monitor.sh 脚本,该脚本首先会将 Private-1 和 Private-2 去往本地站点的路由分别指向本 AZ 中的 VPN 设备,即:Strongswan-1 和 Strongswan-2

  2. 接着 Strongswan-1 和 Strongswan-2 会通过互相 ping 来检测对端的可达性,同时通过 ping 本地站点 VPN 设备的 tunnel 地址来检测 VPN 连接的可达性。

  3. 如果 Strongswan-1 的 VPN 连接发生故障,Strongswan-1 会将 Private-1 路由的 target 从 Strongswan-1 修改为指向 Strongswan-2,连接恢复后,Strongswan-1 会将路由切换回自身

  4. 如果 Strongswan-1 发生故障,Strongswan-2 的 ping 检测失败,Strongswan-2 会将 Private-1 路由的 target 从 Strongswan-1 修改为指向 Strongswan-2

  5. Strongswan-2 接着会对 Strongswan-1 做 stop,start 操作

  6. 当 Strongswan-1 在另外一台物理机上启动后,自动运行的脚本会将 Private-1 路由从 Strongswan-2 修改为指向 Strongswan-1


配置步骤:


  1. 为 Strongswan-1 和 Strongswan-2 创建合适的角色并关联


由于 Strongswan-1 和 Strongswan-2 需要对 VPC 路由表及 EC2 实例做操作,所以需要创建合适的角色并与实例关联。


在 IAM 服务中选择策略->创建策略



选择创建您自己的策略



使用如下内容设置策略文档,并点击创建策略



在 IAM 服务中选择角色->创建新角色



设置角色名称



选择 AWS 服务角色->Amazon EC2



选择筛选条件为客户管理的策略,并勾选之前创建的 Strongswan_Policy



审核配置并点击创建角色



在创建 Strongswan-1 和 Strongswan-2 的 EC2 时,关联 Strongswan_role 角色



需要注意为 Strongswan-1,Strongswan-2 关联 EIP,避免 IP 发生变化导致 VPN 无法建立,并且需要在控制台关闭源/目地址检查


  1. VPC 网络规划


本场景的 VPC 的 CIDR 为 172.16.0.0/16,一共需要至少四个子网分别对应 4 台 EC2,Strongswan-1 和 Strongswan-2 可以共用一张路由表,默认路由到 IGW,Private-1 和 Private-2 分别需要一张独立的路由表,关于如何配置 VPC,子网,IGW,路由表等内容,可以参考如下文档:


Amazon Virtual Private Cloud 用户指南


http://docs.amazonaws.cn/AmazonVPC/latest/UserGuide/VPC_Introduction.html


Strongswan-1 和 Strongswan-2 所在子网的路由表设置如下



Private-1 所在子网的路由表设置如下,eni-23fbd87a 为 Strongswan-1 的网卡 ID,10.10.0.0/16 为本地站点网段



Private-2 所在子网的路由表设置如下,eni-2d75e606 为 Strongswan-2 的网卡 ID,10.10.0.0/16 为本地站点网段



  1. 配置 Strongswan


3.1 在 Strongswan-1 及 Strongswan-2 的/etc/sysctl.conf 文件中添加如下内容,开启转发,关闭重定向及反向路径过滤:


net.ipv4.ip_forward = 1


net.ipv4.conf.all.accept_redirects = 0


net.ipv4.conf.all.send_redirects = 0


net.ipv4.conf.default.rp_filter = 2


运行sysctl –p使修改生效


3.2 安装 Strongswan


修改/etc/yum.repos.d/epel.repo,将 enabled=0 修改成 enabled=1


wget ftp://fr2.rpmfind.net/linux/centos/6.8/os/x86_64/Packages/trousers-0.3.13-2.el6.x86_64.rpm


rpm -ivh trousers-0.3.13-2.el6.x86_64.rpm


yum install strongswan -y


chkconfig strongswan on


3.3 配置 Strongswan-1 的 strongswan


修改 Strongswan-1 的/etc/strongswan/ipsec.conf,添加如下内容,其中 172.16.1.157 为 Strongswan-1 的私网地址,54.223.192.238 为 Strongswan-1 的公网地址,52.50.130.77 为本地站点路由器的公网地址:


conn vpn1


type=tunnel


mobike=no


authby=secret


keyexchange=ikev1


ike=aes128-sha1-modp1024


esp=aes128-sha1-modp1024


left=172.16.1.157


leftid=54.223.192.238


leftsubnet=0.0.0.0/0


right=52.50.130.77


rightsubnet=0.0.0.0/0


auto=start


dpddelay=3


dpdtimeout=15


dpdaction=restart


mark=100


3.4 创建 Strongswan-1 的/etc/strongswan/ipsec.secrets 文件,添加如下内容:


54.223.192.238 52.50.130.77 : PSK "cisco"


3.5 配置 Strongswan-2 的 strongswan


修改 Strongswan-2 的/etc/strongswan/ipsec.conf,添加如下内容,其中 172.16.2.126 为 Strongswan-2 的私网地址,54.222.191.34 为 Strongswan-2 的公网地址,52.51.34.140 为本地站点路由器的公网地址:


conn vpn1


type=tunnel


mobike=no


authby=secret


keyexchange=ikev1


ike=aes128-sha1-modp1024


esp=aes128-sha1-modp1024


left=172.16.2.126


leftid=54.222.191.34


leftsubnet=0.0.0.0/0


right=52.51.34.140


rightsubnet=0.0.0.0/0


auto=start


dpddelay=3


dpdtimeout=15


dpdaction=restart


mark=100


3.6 创建 Strongswan-2 的/etc/strongswan/ipsec.secrets 文件,添加如下内容:


54.222.191.34 52.51.34.140 : PSK "cisco"


3.7 下载脚本到 Strongswan-1 和 Strongswan-2,修改如下内容


脚本下载地址:https://s3.cn-north-1.amazonaws.com.cn/junyublog/vpn_monitor.sh


VPN_ID="i-0430fe110cdec5835"


VPN_RT_ID="rtb-4b8f522f"


Remote_IP="169.254.40.105"


My_RT_ID="rtb-468f5222"


VPN_ID 为 peer 的 instance id,对于运行在 Strongswan-1 上的脚本,该值为 Strongswan-2 的 instance id,对于运行在 Strongswan-2 上的脚本,该值为 Strongswan-1 的 instance id


对于运行在 Strongswan-1 上的脚本,VPN_RT_ID 为 Private-2 所在子网关联的路由表,对于运行在 Strongswan-2 上的脚本,VPN_RT_ID 为 Private-1 所在子网关联的路由表


Remote_IP 为对端本地站点路由器的 tunnel 地址


My_RT_ID 为本机负责转发的 EC2 的路由表 ID,对于运行在 Strongswan-1 上的脚本,该值为 Private-1 的路由表 ID,对于运行在 Strongswan-2 上的脚本,该值为 Private-2 的路由表 ID


  1. 创建 tunnel 接口及路由表


在/etc/rc.d/rc.local 文件中添加如下内容:


Strongswan-1:


litterbin=$(ip link add tunnel1 type vti local 172.16.1.157 remote 52.50.130.77 key 100)


litterbin=$(ip add add 169.254.100.2/30 remote 169.254.100.1/30 dev tunnel1)


litterbin=$(ip link set tunnel1 up mtu 1436)


litterbin=$(ip rule add priority 10 from all lookup 10)


ip route add 169.254.100.0/30 via 169.254.100.1 table 10


ip route add 10.10.0.0/16 via 169.254.100.1 table 10


/bin/bash ./home/ec2-user/vpn_monitor.sh > /tmp/log


其中 169.254.100.2 为 Strongswan-1 的 tunnel 口地址,169.254.100.1 为对端本地站点路由器的 tunnel 口地址,172.16.1.157 为 Strongswan-1 的私网地址,52.50.130.77 为对端本地站点路由器的出口地址


Strongswan-2


litterbin=$(ip link add tunnel1 type vti local 172.16.2.126 remote 52.51.34.140 key 100)


litterbin=$(ip add add 169.254.200.2/30 remote 169.254.200.1/30 dev tunnel1)


litterbin=$(ip link set tunnel1 up mtu 1436)


litterbin=$(ip rule add priority 10 from all lookup 10)


ip route add 169.254.200.0/30 via 169.254.200.1 table 10


ip route add 10.10.0.0/16 via 169.254.200.1 table 10


/bin/bash ./home/ec2-user/vpn_monitor.sh > /tmp/log


其中 169.254.200.2 为 Strongswan-2 的 tunnel 口地址,169.254.200.1 为对端本地站点路由器的 tunnel 口地址,172.16.2.126 为 Strongswan-1 的私网地址,52.51.34.140 为对端本地站点路由器的出口地址


  1. 本地站点路由器参考配置


此处以 Cisco 路由器为例,思路如下:


a. 创建 tunnel-based ipsec vpn


b. 通往 VPC 网段的路由指向 tunnel 接口,并且使用 ip sla track


c. 设置 ip sla 检测 Strongswan 的 tunnel ip 可达性


d. 本地内网配置 IGP,将静态路由重分发到 IGP 中


Cisco Router 1:


track 100 ip sla 10 reachability


!


crypto keyring KEYRING


local-address 10.10.1.100


pre-shared-key address 54.223.192.238 key cisco


!


crypto isakmp policy 100


encr aes


authentication pre-share


group 2


lifetime 28800


crypto isakmp keepalive 10 10


crypto isakmp profile isakmp-profile


keyring KEYRING


match identity address 54.223.192.238 255.255.255.255


local-address 10.10.1.100


!


crypto ipsec security-association replay window-size 128


!


crypto ipsec transform-set TRAN esp-aes esp-sha-hmac


mode tunnel


crypto ipsec df-bit clear


!


crypto ipsec profile ipsec-profile


set transform-set TRAN


set pfs group2


!


interface Tunnel100


ip address 169.254.100.1 255.255.255.0


ip tcp adjust-mss 1387


tunnel source 10.10.1.100


tunnel mode ipsec ipv4


tunnel destination 54.223.192.238


tunnel protection ipsec profile ipsec-profile


ip virtual-reassembly


!


router ospf 100


router-id 1.1.1.1


redistribute static subnets route-map static2ospf


!


ip route 172.16.0.0 255.255.0.0 Tunnel100 169.254.100.2 tag 100 track 100


!


ip sla 10


icmp-echo 169.254.100.2 source-ip 169.254.100.1


frequency 5


ip sla schedule 10 start-time now


!


route-map static2ospf permit 10


match tag 100


Cisco Router 2:


track 100 ip sla 10 reachability


!


crypto keyring KEYRING


local-address 10.10.2.100


pre-shared-key address 54.222.191.34 key cisco


!


crypto isakmp policy 100


encr aes


authentication pre-share


group 2


lifetime 28800


crypto isakmp keepalive 10 10


crypto isakmp profile isakmp-profile


keyring KEYRING


match identity address 54.222.191.34 255.255.255.255


local-address 10.10.2.100


!


crypto ipsec security-association replay window-size 128


!


crypto ipsec transform-set TRAN esp-aes esp-sha-hmac


mode tunnel


crypto ipsec df-bit clear


!


crypto ipsec profile ipsec-profile


set transform-set TRAN


set pfs group2


!


interface Tunnel100


ip address 169.254.200.1 255.255.255.0


ip tcp adjust-mss 1387


tunnel source 10.10.2.100


tunnel mode ipsec ipv4


tunnel destination 54.222.191.34


tunnel protection ipsec profile ipsec-profile


ip virtual-reassembly


!


router ospf 100


router-id 2.2.2.2


redistribute static subnets route-map static2ospf


!


ip route 172.16.0.0 255.255.0.0 Tunnel100 169.254.200.2 tag 100 track 100


!


ip sla 10


icmp-echo 169.254.200.2 source-ip 169.254.200.1


frequency 5


ip sla schedule 10 start-time now


!


route-map static2ospf permit 10


match tag 100


作者介绍:


余骏


亚马逊 AWS 解决方案架构师,负责基于 AWS 的云计算方案架构的咨询和设计,同时致力于 AWS 云服务在国内的应用和推广。在加入 AWS 之前,在思科中国担任系统工程师,负责方案咨询和架构设计,在企业私有云和基础网络方面有丰富经验。


本文转载自 AWS 技术博客。


原文链接:


https://amazonaws-china.com/cn/blogs/china/hybrid-bjs-vpn/


2019-11-19 08:001880

评论

发布
暂无评论
发现更多内容

巧用Android网络通信技术,在网络上直接传输对象 (1),Android工程师面试题

android 程序员 移动开发

开发8年程序员:你是不是疯了,为什么字节跳动的Offer都不要了

android 程序员 移动开发

往事只能回味!春招 Android 开发岗:我居然三天就拿到了offer

android 程序员 移动开发

开发部署提速8倍!这款IDE插件了解一下?,211本硕如何通过字节跳动、百度、美团Android面试

android 程序员 移动开发

当中国诗词大会邂逅了Flutter,从此我的眼里只有你!(1)

android 程序员 移动开发

小企业呆了3年多,人都呆废了!准备跳槽字节跳动,2021年不想被公司优化

android 程序员 移动开发

带了一个实习生程序员,这波操作搞得我人都傻了,android开发者网站

android 程序员 移动开发

开发者须知:女性玩家和手机游戏注意事项(1),flutter面试题2021

android 程序员 移动开发

官方推荐Flow,LiveData:那我走,绝对干货

android 程序员 移动开发

建造者模式,android原生开发技术

android 程序员 移动开发

当中国诗词大会邂逅了Flutter,从此我的眼里只有你!,附架构师必备技术详解

android 程序员 移动开发

客2消,客1消,客0消,android热修复视频

android 程序员 移动开发

带着问题重学Android事件分发,移动端内嵌h5页面

android 程序员 移动开发

当事人:现在就是非常后悔,开工那天没去上班,Flutter中网络图片加载和缓存源码分析

android 程序员 移动开发

彻底理解Android架构,移动应用开发就业工资

android 程序员 移动开发

安卓指纹对称加密及登录功能的实现,android面试简历

android 程序员 移动开发

实战|Android后台启动Activity实践之路续,2021年Android者未来的出路在哪里

android 程序员 移动开发

实践App内存优化:如何有序地做内存分析与优化,flutter免费视频教程

android 程序员 移动开发

当 Android 的 Compat 库不能拯救你的时候,覆盖所有面试知识点

android 程序员 移动开发

微信小程序之加载更多(分页加载)实例 ,flutter瀑布流列表

android 程序员 移动开发

工作两年裸辞备战一个多月收割offer,一二线互联网公司Android面试题汇总

android 程序员 移动开发

工作都是公司的,技术才是自己的,【2021Android最新学习路线

android 程序员 移动开发

年底被裁,程序员 2020 年的出路在哪里?,Android开发实用必备的几款插件

android 程序员 移动开发

干了5年Android开发,突然感觉自己啥也不会,啥也不想干

android 程序员 移动开发

开发八年的程序员,在字节跳动5年,30岁的我被刚升职后辈一句话惊醒

android 程序员 移动开发

当你面试的时候,被问到关于Fragment的种种,5年经验Android程序员面试27天

android 程序员 移动开发

当面试官要你说一下Activity的启动模式时,怎么回答最合适?标准答案在这里

android 程序员 移动开发

年后想跳槽涨薪?你想要的面试题全在这里,kotlinmap初始化

android 程序员 移动开发

开发者须知:女性玩家和手机游戏注意事项,安卓开发中遇到最难的问题

android 程序员 移动开发

开源爆款,阿里P7Android技术笔记,图文并茂,Android经典面试题详解

android 程序员 移动开发

带你高效入门 Flutter,安卓开发入门书籍推荐

android 程序员 移动开发

构建健壮的混合云网络——BJS VPN篇_语言 & 开发_亚马逊云科技 (Amazon Web Services)_InfoQ精选文章