最近有合作伙伴遇到一个需求,总结下是这样的:用户在国内和海外Azure上都部署了应用,用户有一批扛着笔记本到处出差的人需要访问这些资源,但是由于应用设计原因,需要能够同时访问国内和海外Azure上的应用数据。通常对于这样的移动用户访问Azure的需求,我们会建议采用P2S VPN,实现单点到Azure上站点的VPN连接,就可以访问了。但是这次的问题在于用户希望可以同时访问国内和海外的Azure。很自然的一个想法就是能不能让用户先用P2S VPN连接到一个站点,比如美国的Azure,然后通过Azure站点间的VPN连接起来,使得终端用户可以通过一个站点作为跳板,访问另一个站点的应用。

在这其中涉及两段设置:1、站点到站点的IPSEC VPN,需要启用BGP。2、用户终端到某个Azure站点的P2S VPN。

具体配置如下:

首先,要Azure的VPN Gateway要开启BGP,是需要通过命令行来实现的,步骤记录如下:

首先建立2个VNET,一个记做 localvnet,一个记做remotevnet,创建gateway subnet,这些步骤可以在portal界面里完成,然后设置VPN GW所需的参数。

 

设置localvnet

 

$RG1 = "lyqvpn"

$Location1 = "China North"

$VNetName1 = "localvnet"

$GWSubName1 = "GatewaySubnet"

$GWIPName1 = "lyqGWIP01"

$GWIPconfName1 = "gwipconf1"

 

$gwpip1 = New-AzureRmPublicIpAddress -Name $GWIPName1 -ResourceGroupName $RG1 -Location $Location1 -AllocationMethod Dynamic

 

$vnet1 = Get-AzureRmVirtualNetwork -Name $VNetName1 -ResourceGroupName $RG1

$subnet1 = Get-AzureRmVirtualNetworkSubnetConfig -Name "GatewaySubnet" -VirtualNetwork $vnet1

$gwipconf1 = New-AzureRmVirtualNetworkGatewayIpConfig -Name $GWIPconfName1 -Subnet $subnet1 -PublicIpAddress $gwpip1

 

设置 remotevnet

$VNetName2 = "remotevnet"

$GWSubName2 = "GatewaySubnet"

$GWIPName2 = "lyqGWIP02"

$GWIPconfName2 = "gwipconf2"

 

$gwpip2 = New-AzureRmPublicIpAddress -Name $GWIPName2 -ResourceGroupName $RG1 -Location $Location1 -AllocationMethod Dynamic

 

$vnet2 = Get-AzureRmVirtualNetwork -Name $VNetName2 -ResourceGroupName $RG1

$subnet2 = Get-AzureRmVirtualNetworkSubnetConfig -Name "GatewaySubnet" -VirtualNetwork $vnet2

$gwipconf2 = New-AzureRmVirtualNetworkGatewayIpConfig -Name $GWIPconfName2 -Subnet $subnet2 -PublicIpAddress $gwpip2

 

以上步骤分别为localvnet和remotevnet的VPN网关创建了公网地址,并获得了对应的subnet信息和VPN设置。

 

接下来创建开启了BGP的VPN Gateway,注意不同vnet的ASN不能相同

 

$VNet1ASN = 65010

$VNet2ASN = 65020

$GWName1 = "localVNetGW"

$GWName2 = "remoteVNetGW"

 

New-AzureRmVirtualNetworkGateway -Name $GWName1 -ResourceGroupName $RG1 -Location $Location1 -IpConfigurations $gwipconf1 -GatewayType Vpn -VpnType RouteBased -GatewaySku HighPerformance -Asn $VNet1ASN

 

New-AzureRmVirtualNetworkGateway -Name $GWName2 -ResourceGroupName $RG1 -Location $Location1 -IpConfigurations $gwipconf2 -GatewayType Vpn -VpnType RouteBased -GatewaySku HighPerformance -Asn $VNet2ASN

 

创建VPN Gateway需要很长时间,耐心等待所有工作完成。

通过以下命令可以查看创建的结果:

$vnet1gw = Get-AzureRmVirtualNetworkGateway -Name $GWName1 -ResourceGroupName $RG1

$vnet1gw.BgpSettingsText

 

$vnet2gw = Get-AzureRmVirtualNetworkGateway -Name $GWName2 -ResourceGroupName $RG1

$vnet2gw.BgpSettingsText

 

看到的返回应当像下面这个格式:

{

  "Asn": 65010,

  "BgpPeeringAddress": "10.3.0.78",

  "PeerWeight": 0

}

 

创建完VPN Gateway后,需要为每段建立本地网关,来配合VPN GW。

$LNGName1 = "remoteLN"

$LNGPrefix1 = "10.6.0.78/32"

$LNGIP1 = "139.xxx.xxx.xxx"

$LNGASN1 = 65020

$BGPPeerIP1 = "10.6.0.78"

 

New-AzureRmLocalNetworkGateway -Name $LNGName1 -ResourceGroupName $RG1 -Location $Location1 -GatewayIpAddress $LNGIP1 -AddressPrefix $LNGPrefix1 -Asn $LNGASN1 -BgpPeeringAddress $BGPPeerIP1

 

$LNGName2 = "localLN"

$LNGPrefix2 = "10.3.0.78/32"

$LNGIP2 = "139.xxx.xxx.xxx"

$LNGASN2 = 65010

$BGPPeerIP2 = "10.3.0.78"

 

New-AzureRmLocalNetworkGateway -Name $LNGName2 -ResourceGroupName $RG1 -Location $Location1 -GatewayIpAddress $LNGIP2 -AddressPrefix $LNGPrefix2 -Asn $LNGASN2 -BgpPeeringAddress $BGPPeerIP2

 

在这里要注意本地网关设置是代表了VPN GW需要去连接的那一端。

等待两个本地网关创建完成,用以下命令获得localvnet两个网关的设置

$vnet1gw = Get-AzureRmVirtualNetworkGateway -Name $GWName1  -ResourceGroupName $RG1

 

$lng1gw  = Get-AzureRmLocalNetworkGateway -Name $LNGName1 -ResourceGroupName $RG1

 

在localvnet上创建VPN连接

$Connection1 = "local2remote"

 

New-AzureRmVirtualNetworkGatewayConnection -Name $Connection1 -ResourceGroupName $RG1 -VirtualNetworkGateway1 $vnet1gw -LocalNetworkGateway2 $lng1gw -Location $Location1 -ConnectionType IPsec -SharedKey 'AzureA1b2C3' -EnableBGP $True

 

获得remotevnet的两个网关的设置

$vnet2gw = Get-AzureRmVirtualNetworkGateway -Name $GWName2  -ResourceGroupName $RG1

$lng2gw  = Get-AzureRmLocalNetworkGateway -Name $LNGName2 -ResourceGroupName $RG1

 

为remotevnet创建VPN连接

$Connection2 = "remote2local"

 

New-AzureRmVirtualNetworkGatewayConnection -Name $Connection2 -ResourceGroupName $RG1 -VirtualNetworkGateway1 $vnet2gw -LocalNetworkGateway2 $lng2gw -Location $Location1 -ConnectionType IPsec -SharedKey 'AzureA1b2C3' -EnableBGP $True

 

到这里,VPN Gateway的配置就完成了,等待一会,看到VPN连接状态为已连接就好了。

 

接下来在localvnet上做一个P2S的VPN配置,这个可以在Portal里实现:

找到以下位置,按要求输入地址池地址(与VNET地址不能重复),以及VPN所需的证书。

证书可以是企业已有的证书,或自签名的证书

 

 

获得自签名证书的方法如下:

在笔记本上用以下命令创建一个名为P2SRootCert的根证书

$cert = New-SelfSignedCertificate -Type Custom -KeySpec Signature `

-Subject "CN=P2SRootCert" -KeyExportPolicy Exportable `

-HashAlgorithm sha256 -KeyLength 2048 `

-CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign

把证书导出成cer文件,注意导出的文件里含有回车符,而Azure界面上只接受一个完整的字符串输入,所以要手工把回车符都去掉,才可以复制。

然后依据所生成的根证书,用以下命令生成客户端证书:

New-SelfSignedCertificate -Type Custom -KeySpec Signature `

-Subject "CN=P2SChildCert" -KeyExportPolicy Exportable `

-HashAlgorithm sha256 -KeyLength 2048 `

-CertStoreLocation "Cert:\CurrentUser\My" `

-Signer $cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")

 

这些都完成后,就可以从Azure的界面上下载一个配置安装文件,执行后自动为用户的笔记本创建了一个VPN连接。

 

所以,到现在为止,我们有了从国内到海外的一个VPN,支持BGP。有了从客户端到Azure的P2S VPN。开始测试,诶呀,为什么不通呢?

检查了下配置没问题,2个VPN也都打通了。再看一下笔记本的配置,发现笔记本的路由只有到Localvnet的路由,没有到remotevnet的路由,这是因为BGP并不会把路由推送到本地客户端。所以需要在笔记本上加一条去remotevnet的路由,指向到P2S VPN的网关。

route add 10.6.0.0 mask 255.255.255.0 192.168.10.128

 

这下就都好了,用户可以从笔记本上直接访问到2个Azure站点上的应用数据。这个方案可以满足用户要求。

 

posted on 2017-11-02 11:49  johntoo  阅读(319)  评论(0编辑  收藏  举报