在CentOS/Redhat 5.4上使用openswan搭建数据中心之间的VPN

来源:互联网 发布:网页模版源码下载 编辑:程序博客网 时间:2024/06/05 20:36

最近由于主数据中心的流量过大,为了分流和成本,在国外购买了几个独立主机。这样就需要在2个数据中心之间进行
数据的同步,包括mysql,mongodb,以及反向代理等。通过在使用IPSec创建VPN从而将2个子网连接在一起是
最合适的方案。

在最初曾经考虑过使用OpenVPN,但是由于我们的应用涉及到较大的数据传输,OpenVPN的性能相对于IPSec的方案还是差,
我们也无法通过为openvpn部署硬件加速。出于成本考虑,最后选择纯软的vpn。

首先尝试使用CentOS自带的IPSec进行配置,不过很快失败。RedHat的文档描述的很不清楚,此外,配置比较繁琐,
按照其说明配置也无法连通,实在没有耐心,于是改用openswan来部署。

在部署openswan之前,先要简单画出2个子网的拓扑图,只要分出“左”《-》“右” 即可:

A(192.168.8.x/211.x.x.x) +————+ B(192.168.9.x/174.xx.xx.xx)

按照上图,A为left 节点,B 为 right节电。192.168.8.x和 192.168.9.x分别为2个网络的网段。
在2个网段分别选择一个主机作为网关,这样分别设置这个2个网关就可以了。
实际为:
gate_a(192.168.8.70/211.x.x.x) , gate_b(192.168.9.1/174.x.x.x)

这2个host都需要有2个网卡,分别是链接内网和公网,记住上面的内网和公网ip。

下面开始安装:(gate_a)

1. 安装openswan
gate_a.nightsailer.com> yum install ipsec-tools
gate_a.nightsailer.com> yum install openswan

注意:下面这步很关键,CentOS-5.4有个bug,安装openswan时自动创建cert db并不正确,所以首先需要
重新创建,否则下一步生成key的时候就会报错。

gate_a.nightsailer.com>certutil -N -d /etc/ipsec.d

2. 现在,为gate_a生成密钥:
gate_a.nightsailer.com>ipsec newhostkey –configdir /etc/ipsec.d/ –output /etc/ipsec.d/keys.secrets

3.显示输出gate_a 作为left的公钥:
gate_a.nightsailer.com>ipsec showhostkey –left
ipsec showhostkey nss directory showhostkey: /etc/ipsec.d
# rsakey Axxxx
leftrsasigkey=0sA………

将eftrsasigkey=0sA….. 这行输出记下,后面备用

4. 修改/etc/ip_sec.conf

# /etc/ipsec.conf – Openswan IPsec configuration file
#
# Manual: ipsec.conf.5
#
# Please place your own config files in /etc/ipsec.d/ ending in .conf

version 2.0 # conforms to second version of ipsec.conf specification

# basic configuration
config setup
# Debug-logging controls: “none” for (almost) none, “all” for lots.
# klipsdebug=none
# plutodebug=”control parsing”
# For Red Hat Enterprise Linux and Fedora, leave protostack=netkey
protostack=netkey
nat_traversal=yes
virtual_private=
oe=off
# Enable this if you see “failed to find any available worker”
nhelpers=0

#You may put your configuration (.conf) file in the “/etc/ipsec.d/” and uncomment this.
include /etc/ipsec.d/*.conf

5. 现在为这个vpn单独创建一个conf文件,放到/etc/ipsec.d/nightsailer.com_vpn.conf

conn nightsailer_vpn
#左节点的公网ip(gate_a)
left=211.x.x.x
#左节点的内网网段
leftsubnet=192.168.8.0/24
#左节点的网关的内网ip(可选)
leftsourceip=192.168.8.70
#左节点的id,可以是ip,也可以是域名:
# leftid=@gate_a.chinavisual.com
# 建议最好用ip,域名需要反向解析,dns没配好很容易出问题
leftid=211.x.x.x
# 上一步显示的gate_a作为left的公钥
leftrsasigkey=0sAQO…
leftnexthop=%defaultroute
#以下参数含义和上面的一样
right=174.x.x.x
rightsubnet=192.168.9.0/24
rightsourceip=192.168.9.1
rightid=174.x.x.x
rightrsasigkey=0sAQOp….
rightnexthop=%defaultroute
#是否在ipsec启动时自动启用这个链接
#auto = add ( 若这个选项,则需要手动up vpn链接)
auto=start

现在,登录到gate_b,重复上面的步骤1-5。

注意,第3步略有不同,因为gate_b是right节点,所以要显示它的right 密钥:
gate_b.nightsailer.com>ipsec showhostkey –right
ipsec showhostkey nss directory showhostkey: /etc/ipsec.d
# rsakey Axxxx
rightrsasigkey=0sA………

将rightrsasigkey=0sA….. 这行输出更新/etc/ipsec.d/nightsailer.com_vpn.conf(包括gate_a上的)

现在分别启动ipsec
gate_a.nightsailer.com>/etc/init.d/ipsec start
gate_b.nightsailer.com>/etc/init.d/ipsec start

好了,现在在gate_a和gate_b上相互ping,就可以ping通了。

网关通了,那么还需要再各自子网添加一个路由:
A网段添加:
route add -net 192.168.9.0 netmask 255.255.255.0 gw 192.168.8.70 eth1
#添加静态路由
在 /etc/sysconfig/network-scripts/route-eth1 追加:
192.168.9.0/24 via 192.168.8.70 dev eth1

B网段添加:
route add -net 192.168.8.0 netmask 255.255.255.0 gw 192.168.9.1 eth1
在 /etc/sysconfig/network-scripts/route-eth1 追加:
192.168.8.0/24 via 192.168.9.1 dev eth1

一切就大功告成了。

原创粉丝点击