<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>服务器安全维护工作室 &#187; 阿里云VPC网络之间通过GRE隧道打通</title>
	<atom:link href="https://www.fuwuqiok.com/tag/%e9%98%bf%e9%87%8c%e4%ba%91vpc%e7%bd%91%e7%bb%9c%e4%b9%8b%e9%97%b4%e9%80%9a%e8%bf%87gre%e9%9a%a7%e9%81%93%e6%89%93%e9%80%9a/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fuwuqiok.com</link>
	<description></description>
	<lastBuildDate>Sun, 01 Mar 2020 07:28:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.26</generator>
	<item>
		<title>阿里云VPC网络之间通过GRE隧道打通</title>
		<link>https://www.fuwuqiok.com/%e9%98%bf%e9%87%8c%e4%ba%91vpc%e7%bd%91%e7%bb%9c%e4%b9%8b%e9%97%b4%e9%80%9a%e8%bf%87gre%e9%9a%a7%e9%81%93%e6%89%93%e9%80%9a/</link>
		<comments>https://www.fuwuqiok.com/%e9%98%bf%e9%87%8c%e4%ba%91vpc%e7%bd%91%e7%bb%9c%e4%b9%8b%e9%97%b4%e9%80%9a%e8%bf%87gre%e9%9a%a7%e9%81%93%e6%89%93%e9%80%9a/#comments</comments>
		<pubDate>Mon, 14 Mar 2016 14:39:10 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[aliyun服务器代维]]></category>
		<category><![CDATA[服务器代维]]></category>
		<category><![CDATA[阿里云VPC网络之间通过GRE隧道打通]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=3094</guid>
		<description><![CDATA[<p>阿里云服务器如何配置Snat让整个VPC网络都通过一个EIP访问公网，今天介绍一下如何通过GRE隧道将两个VP [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/%e9%98%bf%e9%87%8c%e4%ba%91vpc%e7%bd%91%e7%bb%9c%e4%b9%8b%e9%97%b4%e9%80%9a%e8%bf%87gre%e9%9a%a7%e9%81%93%e6%89%93%e9%80%9a/">阿里云VPC网络之间通过GRE隧道打通</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>阿里云服务器如何配置Snat让整个VPC网络都通过一个EIP访问公网，今天介绍一下如何通过GRE隧道将两个VPC网络的私网打通。</p>
<p><span id="more-517"></span>测试环境如下：</p>
<p>2个VPC(VPC1,VPC2)代表两个完全隔离的物理网络</p>
<p>每个VPC网络交换机，路由器，公网IP各一个，ECS2台，一台作为VPC网络的网关，一台作为局域网内部的服务器。</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/14.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/14.jpg" alt="14" width="510" height="106" /></a></p>
<p>VPC1:   {EIP:112.74.32.191    GW：172.16.1.1    VPC1-Client:   172.16.1.2}</p>
<p>VPC2:   {EIP:112.74.32.161    GW:172.16.2.1     VPC2-Client:   172.16.2.2}</p>
<p>拓扑如下：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/23.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/23.jpg" alt="23" width="640" height="364" /></a></p>
<p><strong>VPC1-GW操作</strong><br />
加载gre模块</p>
<div id="highlighter_781333" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># modprobe ip_gre</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>2</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># lsmod |grep gre</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>3</code></td>
<td class="content"><code class="plain">ip_gre 9575 0</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>4</code></td>
<td class="content"><code class="plain">ip_tunnel 12597 1 ip_gre</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>创建隧道tun1</p>
<div id="highlighter_62166" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># ip tunnel add tun1 mode gre remote 112.74.32.161 local 172.16.1.1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>激活隧道tun1</p>
<div id="highlighter_289411" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># ip link set tun1 up</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>为隧道配置互联地址</p>
<div id="highlighter_315281" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># ip addr add 192.168.2.1 peer 192.168.2.2 dev tun1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>将访问VPC2网络的数据都丢到tun1接口</p>
<div id="highlighter_4529" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># route add -net 172.16.2.0/24 dev tun1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>#VPC2-GW配置和VPC1一样，只是链路反过来，按照实际情况修改一下。</p>
<div id="highlighter_216491" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># modprobe ip_gre</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>2</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># lsmod |grep gre</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>3</code></td>
<td class="content"><code class="plain">ip_gre 9575 0</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>4</code></td>
<td class="content"><code class="plain">ip_tunnel 12597 1 ip_gre</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>5</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># ip tunnel add tun1 mode gre remote 112.74.32.191 local 172.16.2.1</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>6</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># ip link set tun1 up</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>7</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># ip addr add 192.168.2.2 peer 192.168.2.1 dev tun1</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>8</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># route add -net 172.16.1.0/24 dev tun1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>查看隧道，属于激活状态<br />
<a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/33.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/33.jpg" alt="33" width="651" height="398" /></a><br />
从VPC2网络测试VPC1的GW，已经可以联通，从VPC1测试VPC2的GW也可以联通了</p>
<div id="highlighter_951535" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># nc -zv 172.16.1.1 22</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>2</code></td>
<td class="content"><code class="plain">Connection to 172.16.1.1 22 port [tcp/</code><code class="functions">ssh</code><code class="plain">] succeeded!</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>3</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># nc -vz 172.16.2.1 22</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>4</code></td>
<td class="content"><code class="plain">Connection to 172.16.2.1 22 port [tcp/</code><code class="functions">ssh</code><code class="plain">] succeeded!</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>至此，两个VPC网络的网关都已经能互通了，但是测试两个VPC网络的其他服务器，目前依然不能互通，如下：<br />
<a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/61.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/61.jpg" alt="61" width="622" height="104" /></a><br />
这是因为路由还不通，这里以VPC2的client访问VPC1的client为例：</p>
<p>登陆VPC2的控制台为VPC2路由器配置默认路由，指向配置了EIP的VPC2-GW<br />
<a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/42.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/42.jpg" alt="42" width="615" height="293" /></a></p>
<p>&nbsp;</p>
<p>登陆VPC2-GW，开启IP转发，并配置SNAT，让VPC1client的数据包转换为GRE隧道可以通讯的192.168.2.2这个IP：</p>
<div id="highlighter_515085" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># echo 1 &gt; /proc/sys/net/ipv4/ip_forward</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>2</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># iptables -t nat -I POSTROUTING -s 172.16.2.0/24 -d 172.16.1.0/25 -j SNAT --to 192.168.2.2</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>这时，VPC2-client的数据包已经可以到达VPC1-GW了（VPC2-client-&gt;VPC2路由-&gt;VPC2-G-&gt;<strong>GRE</strong>-&gt;VPC1-GW），但是依然Ping不通，抓包看一下：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/111.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/111.jpg" alt="111" width="430" height="41" /></a></p>
<p>源地址是VPC1的互联地址，目标地址是172.16.1.2，这个路径显然是不通的，需要VPC1-GW将这个数据包转换为VPC1网络的地址：</p>
<div id="highlighter_336754" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>1</code></td>
<td class="content"><code class="plain">[root@VPC2-GW ~]</code><code class="comments"># echo 1 &gt; /proc/sys/net/ipv4/ip_forward</code></td>
</tr>
</tbody>
</table>
</div>
<div class="line alt2">
<table>
<tbody>
<tr>
<td class="number"><code>2</code></td>
<td class="content"><code class="plain">[root@VPC1-GW ~]</code><code class="comments"># iptables -t nat -I POSTROUTING -j SNAT --to 172.16.1.1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>配置完毕后，VPC1-&gt;VPC2反向也做同样的配置。</p>
<p>至此再做测试，VPC1和VPC2的网络就完全互通了：</p>
<p><a href="https://www.fuwuqiok.com/wp-content/uploads/2016/03/3333.jpg"><img class="attachment-medium" src="https://www.fuwuqiok.com/wp-content/uploads/2016/03/3333.jpg" alt="3333" width="1034" height="273" /></a></p>
<p>&nbsp;</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/%e9%98%bf%e9%87%8c%e4%ba%91vpc%e7%bd%91%e7%bb%9c%e4%b9%8b%e9%97%b4%e9%80%9a%e8%bf%87gre%e9%9a%a7%e9%81%93%e6%89%93%e9%80%9a/">阿里云VPC网络之间通过GRE隧道打通</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/%e9%98%bf%e9%87%8c%e4%ba%91vpc%e7%bd%91%e7%bb%9c%e4%b9%8b%e9%97%b4%e9%80%9a%e8%bf%87gre%e9%9a%a7%e9%81%93%e6%89%93%e9%80%9a/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
