NAT, Network Address Translation,即网络地址转换。当内部网络的主机想要访问外网,但是又不想直接暴露给公网,可以通过NAT节点来访问外网。这样做有两个好处,第一是内网的主机无需拥有公网IP就可访问网络(NAT节点需要公网IP),节约了公网IP;第二是内网的主机由于没有公网IP,所以公网的电脑无法访问到它,这样就可以隐藏自己。一个很经典的示例是假如你有一台数据库服务器放置在内网中,为在同一个内网中的web服务器提供数据服务,为了安全性考虑你不会把它直接暴露在公网中。但是数据库服务器有时候自己是需要访问公网的,比如需要升级数据库服务器中的某些软件等。采用NAT方案可以很好的解决这个问题。

下图是NAT节点的功能示意图。

一些路由器或者装有特定软件的主机都可以作为NAT节点。在AWS中如果你想创建一个NAT节点的话那是非常的方便,因为AWS直接提供了预装了NAT软件的AMI,你只需直接使用该AMI在你的公共子网中实例化一台机器,并进行相应的配置即可。

下面的图展示了在AWS中的一个经典的VPC架构。该VPC里面建立了两个子网,一个是公共子网,通过Intenet Geteway和公网连接;一个是私有子网,无法直接访问公网。然后在公共子网中建立了一个EC2机器,使用的是AWS提供的具有NAT功能的AMI,并为它分配了一个弹性IP,这样该EC2就是一个NAT节点。在私有子网的所有机器都具有了通过该NAT节点访问外网的能力。

为了创建这样一套网络及机器,最简便的方式当然是使用AWS提供的CloudFormation了。如果不了解CloudFormation,可以看我以前写过的一篇文章 《亚马逊云服务之CloudFormation》。下面展示的是创建该整个VPC的CloudFormation脚本。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  300. 300
  301. 301
  302. 302
  303. 303
  304. 304
  305. 305
  306. 306
  307. 307
  308. 308
  309. 309
  310. 310
  311. 311
  312. 312
  313. 313
  314. 314
  315. 315
  316. 316
  317. 317
  318. 318
  319. 319
  320. 320
  321. 321
  322. 322
  323. 323
  324. 324
  1. {
  2. "AWSTemplateFormatVersion": "2010-09-09",
  3. "Description": "Setup a vpc, which contains two subnets and one NAT machine",
  4. "Parameters": {
  5. "KeyName": {
  6. "Description": "Name of and existing EC2 KeyPair to enable SSH access to the instance",
  7. "Type": "String"
  8. },
  9. "VpcCidr": {
  10. "Description": "CIDR address for the VPC to be created.",
  11. "Type": "String",
  12. "Default": "10.2.0.0/16"
  13. },
  14. "AnyCidr": {
  15. "Description": "CIDR address for Any Where.",
  16. "Type": "String",
  17. "Default": "0.0.0.0/0"
  18. },
  19. "AvailabilityZone1": {
  20. "Description": "First AZ.",
  21. "Type": "String",
  22. "Default": "cn-north-1a"
  23. },
  24. "PublicSubnetCidr": {
  25. "Description": "Address range for a public subnet to be created in AZ1.",
  26. "Type": "String",
  27. "Default": "10.2.1.0/24"
  28. },
  29. "PrivateSubnetCidr": {
  30. "Description": "Address range for private subnet.",
  31. "Type": "String",
  32. "Default": "10.2.2.0/24"
  33. },
  34. "NATInstanceType": {
  35. "Description": "Instance type for NAT",
  36. "Type": "String",
  37. "Default": "t1.micro"
  38. }
  39. },
  40. "Mappings": {
  41. "AWSNATAMI": {
  42. "cn-north-1": {
  43. "AMI": "ami-eab220d3"
  44. }
  45. }
  46. },
  47. "Resources": {
  48. "VPC": {
  49. "Type": "AWS::EC2::VPC",
  50. "Properties": {
  51. "CidrBlock": {
  52. "Ref": "VpcCidr"
  53. },
  54. "Tags": [
  55. {
  56. "Key": "Name",
  57. "Value": "VPC"
  58. }
  59. ]
  60. }
  61. },
  62. "InternetGateWay": {
  63. "Type": "AWS::EC2::InternetGateway",
  64. "Properties": {
  65. "Tags": [
  66. {
  67. "Key": "Name",
  68. "Value": "INTERNET_GATEWAY"
  69. }
  70. ]
  71. }
  72. },
  73. "GatewayToInternet": {
  74. "Type": "AWS::EC2::VPCGatewayAttachment",
  75. "Properties": {
  76. "InternetGatewayId": {
  77. "Ref": "InternetGateWay"
  78. },
  79. "VpcId": {
  80. "Ref": "VPC"
  81. }
  82. }
  83. },
  84. "PublicSubnet": {
  85. "Type": "AWS::EC2::Subnet",
  86. "Properties": {
  87. "CidrBlock": {
  88. "Ref": "PublicSubnetCidr"
  89. },
  90. "AvailabilityZone": {
  91. "Ref": "AvailabilityZone1"
  92. },
  93. "Tags": [
  94. {
  95. "Key": "Name",
  96. "Value": "PUBLIC_SUBNET"
  97. }
  98. ],
  99. "VpcId": {
  100. "Ref": "VPC"
  101. }
  102. }
  103. },
  104. "PrivateSubnet": {
  105. "Type": "AWS::EC2::Subnet",
  106. "Properties": {
  107. "CidrBlock": {
  108. "Ref": "PrivateSubnetCidr"
  109. },
  110. "AvailabilityZone": {
  111. "Ref": "AvailabilityZone1"
  112. },
  113. "Tags": [
  114. {
  115. "Key": "Name",
  116. "Value": "PRIVATE_SUBNET"
  117. }
  118. ],
  119. "VpcId": {
  120. "Ref": "VPC"
  121. }
  122. }
  123. },
  124. "DefaultSecurityGroup": {
  125. "Type": "AWS::EC2::SecurityGroup",
  126. "Properties": {
  127. "GroupDescription": "Default Instance SecurityGroup",
  128. "SecurityGroupIngress": [
  129. {
  130. "IpProtocol": "-1",
  131. "CidrIp": {
  132. "Ref": "VpcCidr"
  133. }
  134. }
  135. ],
  136. "Tags": [
  137. {
  138. "Key": "Name",
  139. "Value": "DEFAULT_SECURITY_GROUP"
  140. }
  141. ],
  142. "VpcId": {
  143. "Ref": "VPC"
  144. }
  145. }
  146. },
  147. "PublicRouteTable": {
  148. "Type": "AWS::EC2::RouteTable",
  149. "Properties": {
  150. "VpcId": {
  151. "Ref": "VPC"
  152. },
  153. "Tags": [
  154. {
  155. "Key": "Name",
  156. "Value": "PUBLIC_ROUTE_TABLE"
  157. }
  158. ]
  159. }
  160. },
  161. "PublicRoute": {
  162. "Type": "AWS::EC2::Route",
  163. "Properties": {
  164. "DestinationCidrBlock": {
  165. "Ref": "AnyCidr"
  166. },
  167. "GatewayId": {
  168. "Ref": "InternetGateWay"
  169. },
  170. "RouteTableId": {
  171. "Ref": "PublicRouteTable"
  172. }
  173. }
  174. },
  175. "PublicSubnetRouteTableAssociation": {
  176. "Type": "AWS::EC2::SubnetRouteTableAssociation",
  177. "Properties": {
  178. "RouteTableId": {
  179. "Ref": "PublicRouteTable"
  180. },
  181. "SubnetId": {
  182. "Ref": "PublicSubnet"
  183. }
  184. }
  185. },
  186. "NATEIP": {
  187. "Type": "AWS::EC2::EIP",
  188. "Properties": {
  189. "InstanceId": {
  190. "Ref": "NATInstance"
  191. }
  192. }
  193. },
  194. "NATInstance": {
  195. "Type": "AWS::EC2::Instance",
  196. "Properties": {
  197. "InstanceType": {
  198. "Ref": "NATInstanceType"
  199. },
  200. "KeyName": {
  201. "Ref": "KeyName"
  202. },
  203. "SubnetId": {
  204. "Ref": "PublicSubnet"
  205. },
  206. "SourceDestCheck": false,
  207. "ImageId": {
  208. "Fn::FindInMap": [
  209. "AWSNATAMI",
  210. {
  211. "Ref": "AWS::Region"
  212. },
  213. "AMI"
  214. ]
  215. },
  216. "Tags": [
  217. {
  218. "Key": "Name",
  219. "Value": "NAT"
  220. }
  221. ],
  222. "SecurityGroupIds": [
  223. {
  224. "Ref": "NATSecurityGroup"
  225. }
  226. ]
  227. }
  228. },
  229. "PrivateSubnetRouteTable": {
  230. "Type": "AWS::EC2::RouteTable",
  231. "Properties": {
  232. "VpcId": {
  233. "Ref": "VPC"
  234. },
  235. "Tags": [
  236. {
  237. "Key": "Name",
  238. "Value": "PRIVATE_SUBNET_ROUTE_TABLE"
  239. }
  240. ]
  241. }
  242. },
  243. "PrivateSubnetRoute": {
  244. "Type": "AWS::EC2::Route",
  245. "Properties": {
  246. "DestinationCidrBlock": {
  247. "Ref": "AnyCidr"
  248. },
  249. "InstanceId": {
  250. "Ref": "NATInstance"
  251. },
  252. "RouteTableId": {
  253. "Ref": "PrivateSubnetRouteTable"
  254. }
  255. }
  256. },
  257. "PrivateSubnetRouteTableAssociation": {
  258. "Type": "AWS::EC2::SubnetRouteTableAssociation",
  259. "Properties": {
  260. "RouteTableId": {
  261. "Ref": "PrivateSubnetRouteTable"
  262. },
  263. "SubnetId": {
  264. "Ref": "PrivateSubnet"
  265. }
  266. }
  267. },
  268. "NATSecurityGroup": {
  269. "Type": "AWS::EC2::SecurityGroup",
  270. "Properties": {
  271. "GroupDescription": "NAT Instance SecurityGroup",
  272. "SecurityGroupIngress": [
  273. {
  274. "IpProtocol": "-1",
  275. "CidrIp": {
  276. "Ref": "VpcCidr"
  277. }
  278. }
  279. ],
  280. "Tags": [
  281. {
  282. "Key": "Name",
  283. "Value": "NAT_SECURITY_GROUP"
  284. }
  285. ],
  286. "VpcId": {
  287. "Ref": "VPC"
  288. }
  289. }
  290. }
  291. },
  292. "Outputs": {
  293. "VPCId": {
  294. "Description": "VPC id",
  295. "Value": {
  296. "Ref": "VPC"
  297. }
  298. },
  299. "PublicSubnetId": {
  300. "Description": "public subnet id",
  301. "Value": {
  302. "Ref": "PublicSubnet"
  303. }
  304. },
  305. "PrivateSubnetId": {
  306. "Description": "private subnet id",
  307. "Value": {
  308. "Ref": "PrivateSubnet"
  309. }
  310. },
  311. "NATSecurityGroupId": {
  312. "Description": "NAT SG id",
  313. "Value": {
  314. "Ref": "NATSecurityGroup"
  315. }
  316. },
  317. "NATEIP": {
  318. "Description": "NAT Server EIP.",
  319. "Value": {
  320. "Ref": "NATEIP"
  321. }
  322. }
  323. }
  324. }

你可以通过AWS提供的图形化界面AWS Management Console来使用该CloudFormation脚本,也可以通过AWS CLI来使用。使用以上的CloudFormation脚本创建的VPC可以一键创建你AWS中的基础网络架构。从此再也不用为配置网络发愁了。

在AWS中创建NAT节点的更多相关文章

  1. linux中创建gpio节点

    转自:http://blog.chinaunix.net/uid-29165999-id-4296162.html #define GPIO_MAJOR 230 // major device NO. ...

  2. Labview中创建属性节点和调用节点的用法

    创建属性节点 个人感觉有点像C中的指针 创建调用节点

  3. javascript中创建新节点的方法 标签: javascript 2016-12-25 11:38 55人阅读 评论(0)

    一. var newnode=document.createElement("i"); var newnodeText=document.createTextNode(" ...

  4. platform型设备在/dev目录下自动创建设备节点的分析【转】

    转自:http://blog.csdn.net/rockrockwu/article/details/7357648 系统启动过程中platform设备.驱动注册完毕,为什么在/dev目录下就自动创建 ...

  5. js中创建html标签、加入select下默认的option的value和text、删除select元素节点下全部的OPTION节点

    <pre name="code" class="java"> jsp 中的下拉框标签: <s:select name="sjx&qu ...

  6. jQuery中的查找节点、创建节点、插入节点、删除节点、替换节点、复制节点操作方法

    jQuery操作节点我们可以分六点来讲,查找节点.创建节点.插入节点.删除节点.替换节点.复制节点. 一.查找节点 text() - 设置或返回所选元素的文本内容   ,html() - 设置或返回所 ...

  7. js动态创建style节点(js文件中添加css)

    ie6 不能 document.createElement('style') 然后append到head标签里.所以就找到这样个好文章 --------------------- 有很多提供动态创建 ...

  8. 在AWS中部署OpenShift平台

    OpenShift是RedHat出品的PAAS平台.OpenShift做为PAAS平台最大的特点是它是完全容器化的PAAS平台,底层封装了Docker和Kubernetes,上层暴露了对开发者友好的接 ...

  9. aws中的路由表

    参考官方文档: 由表中包含一系列被称为路由的规则,可用于判断网络流量的导向目的地. 在您的 VPC 中的每个子网必须与一个路由表关联:路由表控制子网的路由.一个子网一次只能与一个路由表关联,但您可以将 ...

随机推荐

  1. javascript父级鼠标移入移出事件中的子集影响父级的处理方法

    一.我们先分析其产生的原因: 1.当鼠标从父级移入子集时触发了父级的两个事件:a.父级的mouseout事件(父级离开到子集):b.由于事件冒泡影响,又触发了父级的mouseover事件(父级移入父级 ...

  2. TColor 与 RGB 的转换函数

    function RGB2TColor(const R, G, B: Byte): Integer;begin  // convert hexa-decimal values to RGB  Resu ...

  3. map阶段动态获取CombineTextInputFormat各输入文件路径

    老mr程序中map中conf的map.input.file参数只能获取获取CombineTextInputFormat的第一个输入文件,而新版mr程序则连第一个输入文件也无法获取,这是因为create ...

  4. css js 的引入方式和书写位置

    css 的引入方式 1.行内样式 <div id="div1" style="width:100px; height:100px; background:red&q ...

  5. 51nod 1138 连续整数的和(数学公式)

    1138 连续整数的和 #include <iostream> #include <cmath> #include <cstdio> using namespace ...

  6. Hibernate-list()与iterate()方法的区别

    对于list方法而言,实际上Hibernate是通过一条Select SQL获取所有的记录.并将其读出,填入到POJO中返回.而iterate 方法,则是首先通过一条Select SQL 获取所有符合 ...

  7. shell数组操作

    1.数组定义,shell使用一对括号表示数组,数组元素间用"空格"分隔 # 空数组arr1 arr1=() # 数组arr2,成员分别是1, 2, 3, 4, 5, 6 arr2= ...

  8. 简单的css js控制table隔行变色

    (1)用expression 鼠标滑过变色: <style type="text/css"><!-- table { background-color:#0000 ...

  9. 将 project.json 项目转换为 Visual Studio 2015 解决方案

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  10. js组件之间的通信

    应用场景: 1.在刷微博的时候,滑到某个头像上,会出现一张usercard(用户名片), 微博列表区有这个usercard, 推荐列表也有这个usercard,评论区也有. 2.在网上购物时,购物车安 ...