东亚银行、岚图汽车带你解锁 AIGC 时代的数字化人才培养各赛道新模式! 了解详情
写点什么

构建健壮的混合云网络——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:001699

评论

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

一文浅谈:我们为什么需要云原生

穿过生命散发芬芳

4月月更

Flutter 简单实用的 fluro 路由管理插件简介

岛上码农

flutter 大前端 ios开发 安卓开发 跨平台开发

津厦两地托育行业发展线上视频交流会成功召开

InfoQ 天津

阿里云与达摩院合作 AHPA 弹性预测论文被顶会 ICDE 录用

阿里巴巴云原生

Linux之lastb命令

入门小站

在线JSON压缩工具

入门小站

工具

计算机网络: IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解

喀拉峻

网络安全 IP

模块二:作业微信朋友圈的高性能复杂度

本人法海

「架构实战营」

PlatoFarm空投社区的逻辑,效仿无聊猿还是Dao理念使然

小哈区块

2022第12周-程序接盘侠

印哥爱学习

离职交接

Rust中值销毁前的清理动作

Shine

rust

用JAVA捋一下设计模式1-简单工厂模式

下雨了

设计模式 简单工厂模式 4月月更

在线正则表达式可视化测试工具

入门小站

工具

前端食堂技术周刊第 31 期:Vue 3、Vitest 中文文档上线、Pinia 正式成为 Vue 官方默认推荐的状态管理库、Vite v2.9.0

童欧巴

JavaScript 前端 Web web前端 前端工程师

《数据密集型型系统设计》LSM-Tree VS BTree

懒时小窝

哈希 B-tree 列式存储 LSM-Tree

好身体,从增加睡眠时间开始

石云升

睡眠 4月月更

探索SeekTiger生态,Tiger DAO VC有哪些新期待

西柚子

Linux驱动开发-proc接口介绍

DS小龙哥

4月月更

【限时免费】阿里云 ACR EE 增强型扫描引擎限时体验中,多维度保障容器镜像安全

阿里巴巴云原生

【PIMF】《伟大的计算原理》提炼“六脉神剑”认识OpenHarmony技术路线

离北况归

《伟大的计算原理》 技术路线 IMF

用JAVA捋一下设计模式23-解释器模式

下雨了

设计模式 4月月更 解释器模式

吹爆Python,解决了10个痛苦已久的难题

Jackpop

中国信通院联合OpenMLDB邀您参加《开源数据库发展研究报告》调研问卷

第四范式开发者社区

数据库 大数据 开源

用JAVA捋一下设计模式3-抽象工厂模式

下雨了

设计模式 抽象工厂模式 4月月更

完美结合,10款提升编程能力的游戏项目!

Jackpop

Redis集群架构剖析(4):槽位迁移,重新分配

非晓为骁

redis 分布式架构 redis cluster

Kubernetes官方java客户端之六:OpenAPI基本操作

程序员欣宸

4月月更

Redis(二)分布式锁与Redis集群搭建

神农写代码

探索SeekTiger生态,Tiger DAO VC有哪些新期待

小哈区块

用JAVA捋一下设计模式2-工厂方法模式

下雨了

设计模式 工厂方法模式 4月月更

用JAVA捋一下设计模式 4-单例模式

下雨了

设计模式 单例模式 4月月更

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