在AWS中创建定制的虚拟私有云(VPC)

创建定制VPC

AWS里一般默认有一个VPC,如果直接使用的话,可以省去很多麻烦。如果创建定制VPC,所有的组件需要各个创建,可以了解各个组件的细节。如果创建定制的VPC,会新创建一个默认的Route Table,Network ACL,和Security Group。不会创建Subnet和Internet Gateway。

创建一个定制的VPC并取名为XiongTestVPC, IPv4 CIDR block为10.0.0.0/16,如下图所示:
create VPC

这里默认创建了Network ACL是允许所有的流量进出, 也就是所有的Source和Destination都是0.0.0.0/0允许。

以下未知,是否要设置DNS resolution和DNS hostnames为Enabled?

DNS resolution       Enabled
DNS hostnames     Enabled

在VPC里创建两个Subnet

首先创建公用Subnet,名字为XiongPublicSubnet,VPC选择新定制的VPC–>XiongTestVPC,IPv4 CIDR block为10.0.1.0/24,这里Availability Zone选择ap-southeast-1a

注意: 这里选择了ap-southeast-1a,如果在另一个帐户里也选择了ap-southeast-1a,这两个Availability Zone并不一定是同一个,因为AWS会随机分配,这样可以保证不会有大多数用户选择同一个Availability Zone。创建公用Subnet如下图所示:

Create public Subnet

对于公有的Subnet,改变设置公有的Subnet的自动分配公有地址,在Action --> Modify auto-assign IP settings里,如下图所示:

Auto assign public IP

同理,创建另一个私有的Subnet,名字为XiongPrivateSubnet,VPC选择新定制的VPC–>XiongTestVPC,IPv4 CIDR block为10.0.2.0/24,这里Availability Zone选择ap-southeast-1b

这里创建的两个Subnet, 默认Route Table里只能访问本地,需要在后面关联能够访问外网的Route Table,Network ACL默认为前面新创建的VPC所默认生成的Network ACL。

创建Internet Gateway

创建一个名为XiongTestInternetGateway的Internet Gateway,并把它附加到新创建的定制VPC上。如下图所示:

Attach Internet GW

注意:一个VPC只能附加一个Internet gateway。

创建额外的Route table访问外部

创建额外的Route table

在创建定制VPC的时候,会默认创建一个Route table为主Route Table,这个默认创建的Route Table只能访问本地,是私有的(private),不能访问外部,这个Route Table专门用于内部私有的Subnet。那么这里我们创建一个额外的Route table可以访问外部,取名为XiongRouteTablePublic,用于外部的公有的Subnet。如下图所示:

Create public route table

添加路由到Internet Gateway

XiongRouteTablePublic这个公有的Route Table里添加两个(分别是IPV4和IPV6)指向前面新创建的可以访问外部的Internet Gateway, 也就是XiongTestInternetGateway0.0.0.0/0为IPV4, ::/0为IPV6,如下图所示:

Edit public route table

关联公有Subnet到额外的公有的Route table

关联公有Subnet也就是XiongPublicSubnet到额外的Route table也就是XiongRouteTablePublic,在Subnets > Edit route table association界面中,这样公有Subnet就可以访问外部公网我了。添加的地方如下图所示:

associate public subnet to public route table

在定制VPC里创建一个新的Security Group (安全组)

在定制VPC XiongTestVPC 里创建一个新的Security Group (安全组),取名为XiongInternalSecurityGroup,用途为内部的EC2使用,默认的Inbound Rules为空,Outbound Rules为所有。
所以在Inbound Rules中添加内网的Subnet地址可以访问以下端口。

  • ICMP --> 0~65536
  • HTTP --> 80
  • HTTPS --> 443
  • MYSQL/Aurora --> 3306
  • SSH --> 22

如下图所示:

create Internal SG

注意:Security Groups不能跨VPC。一个VPC里只能有一个Internet Gateway。
注意:Security Groups是stateful,也就是说添加到Inbound的规则,也自动添加到Outbound上去。

在定制VPC里创建两个EC2 (公有和私有)

在定制VPC里创建两个EC2 (公有和私有),公有EC2可以访问外网,也可以被外网访问,例如Web Server,私有EC2只能内部才能访问,例如数据库服务器。

创建公有EC2

使用以下启动脚本:

#!/bin/bash
yum install httpd -y
yum update -y
service httpd start
chkconfig httpd on
echo "<html><h1>Hello Xiong, this is public EC2 Instance!</h1></html>" > /var/www/html/index.html

这里要选择, XiongTestVPCXiongPublicSubnet,前面已经设置过XiongPublicSubnet会自动分配公网地址, 如下图所示:

Create public EC2

创建私有EC2

再创建一个私有EC2,假如用作内部数据服务器,这里要选择, XiongTestVPCXiongPrivateSubnet,前面已经设置过XiongPrivateSubnet 不会自动分配公网地址, 如下图所示:

Create private EC2
这里在创建私有EC2,在选择Security Group的时候,选择新创建的XiongInternalSecurityGroup,如下所示,这样就限制了只能从XiongPublicSubnet进行访问:

Select internal SG
测试能从外网访问公用EC2.

Test EC2

使用SSH进行访问:

[c:\~]$

Connecting to 54.255.238.161:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-10-0-1-130 ~]$ sudo su
[root@ip-10-0-1-130 ec2-user]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=47 time=0.895 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=47 time=1.02 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=47 time=1.02 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=47 time=0.968 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=47 time=0.980 ms
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4014ms
rtt min/avg/max/mdev = 0.895/0.977/1.022/0.046 ms
[root@ip-10-0-1-130 ec2-user]#

在这里的私有地址是10.0.1.130

那么通过公有的EC2访问私有的EC2,上传所创建的私钥,名为XiongEC2KeyPair.pem,私有EC2的地址为:10.0.2.43,使用Bastion进行访问,通过SSH进行访问如下:

[root@ip-10-0-1-130 ec2-user]#
[root@ip-10-0-1-130 ec2-user]# ls
XiongEC2KeyPair.pem
[root@ip-10-0-1-130 ec2-user]# pwd
/home/ec2-user
[root@ip-10-0-1-130 ec2-user]# ssh ec2-user@10.0.2.43 -i XiongEC2KeyPair.pem
The authenticity of host '10.0.2.43 (10.0.2.43)' can't be established.
ECDSA key fingerprint is SHA256:vJAvER9E5hehP6KOEqDErZxWaDjlRnjLypdHSrQ4Fmk.
ECDSA key fingerprint is MD5:df:48:7d:eb:d6:11:10:9c:cc:d4:2f:1b:19:d4:59:6e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.2.43' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-10-0-2-43 ~]$ ifconfig
eth0      Link encap:Ethernet  HWaddr 06:26:BB:0C:73:8A
          inet addr:10.0.2.43  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::426:bbff:fe0c:738a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
          RX packets:582 errors:0 dropped:0 overruns:0 frame:0
          TX packets:637 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:63293 (61.8 KiB)  TX bytes:62320 (60.8 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:140 (140.0 b)  TX bytes:140 (140.0 b)

[ec2-user@ip-10-0-2-43 ~]$

私有EC2通过NAT访问外网

前面创建的私有的EC2是不能访问外网的,这样是安全的,但是如果需要进行软件更新怎么办呢?

[ec2-user@ip-10-0-2-43 ~]$ sudo su
[root@ip-10-0-2-43 ec2-user]# yum update -y
Loaded plugins: priorities, update-motd, upgrade-helper
Could not retrieve mirrorlist http://repo.ap-southeast-1.amazonaws.com/latest/main/mirror.list error was
...
...
[root@ip-10-0-2-43 ec2-user]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
time out...

这里需要用到NAT才能访问到外网。

NAT Gateway和NAT Instance的比较

NAT Instance是在Security Group后面的,本质上是一个虚拟机Instance,会有性能限制,可能成为瓶颈。 NAT Gateway是在Security Group前面的,但是是在ACL后面的,是可扩展的,性能优于NAT Instance,除了性能高以外,还不需要维护,不需要与安全组Security Group关联,不需要做Disabling Source/Destination Checks操作。

使用NAT Instance

创建一个NAT Instance,在EC2创建界面中的Community AMI中,搜索NAT,找到以下NAT AMI,

amzn-ami-vpc-nat-2018.03.0.20190826-x86_64-ebs - ami-0012a981fe3b8891f
Amazon Linux AMI 2018.03.0.20190826 x86_64 VPC HVM ebs

这里要选择, XiongTestVPCXiongPublicSubnet

参考这个链接 , 需要关闭Source/Destination检查。
Disabling Source/Destination Checks
Each EC2 instance performs source/destination checks by default. This means that the instance must be the source or destination of any traffic it sends or receives. However, a NAT instance must be able to send and receive traffic when the source or destination is not itself. Therefore, you must disable source/destination checks on the NAT instance.

如下图所示,进行更改,
Source Dest check
更改后如下:
Source Dest check disabled
下面从私有的EC2中创建一个路由到这个NAT Instance,在私有的Subnet中的路由表中添加。
route to NAT instance
以上再测试yum update -y可以成功了!

[root@ip-10-0-2-43 ec2-user]# yum update -y
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                                                                                                             | 2.1 kB  00:00:00
amzn-updates                                                                                                                                                          | 2.5 kB  00:00:00
(1/5): amzn-main/latest/group_gz                                                                                                                                      | 4.4 kB  00:00:00
(2/5): amzn-updates/latest/group_gz                                                                                                                                   | 4.4 kB  00:00:00
(3/5): amzn-updates/latest/updateinfo                                                                                                                                 | 611 kB  00:00:01
(4/5): amzn-updates/latest/primary_db                                                                                                                                 | 2.4 MB  00:00:01
(5/5): amzn-main/latest/primary_db                                                                                                                                    | 4.0 MB  00:00:02
Resolving Dependencies
--> Running transaction check
...
...

只要在Default VPC Security Group里放行ICMP,也是可以Ping通外网的。

[root@ip-10-0-2-43 ec2-user]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=46 time=2.08 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=46 time=2.12 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=46 time=2.02 ms
...
...

那么现在删除这个NAT Instance,下面来创建NAT Gateway可以达到同样的效果。
以上删除NAT Instance后,在路由表中会显示一个黑洞‘Blackhole’,如下图所示:

Blackhole
结果是ping 8.8.8.8yum update -y都无法使用。

使用NAT Gateway

在VPC中直接创建一个NAT Gateway,在public Subnet中,如下图所示,这里要创建一个EIP,Elastic IP
Create NAT Gateway

直接从私有的EC2中创建一个路由到这个新创建的NAT GATEWAY,在私有的Subnet中的路由表中添加。如下图所示:

Route to NAT Gateway

测试的结果是ping 8.8.8.8yum update -y都又可以正常使用了。

添加定制ACL并设置ACL

前面创建VPC XiongTestVPC的时候,会默认创建一个ACL, 这个ACL是默认允许所有的流量通过的。然而如果添加定制的ACL的时候,默认是禁止所有的流量通过的,需要手动添加规则。

创建一个定制的ACL, 在Network ACL里,点Create Network ACL,取名XiongCustomizedACL,选择在VPC XiongTestVPC里创建。可以看到新创建的XiongCustomizedACL Network ACL禁止所有进入和出去的流量。

如果把新创建的XiongCustomizedACL Network ACL与公有的Subnet XiongPublicSubnet进行关联,那么由于新创建的XiongCustomizedACL Network ACL禁止所有进入和出去的流量,立马的效果就是公用的EC2的HTTP服务器无法访问。SSH也断开了。

添加Inbound和outbound的规则,如下图所示,立马公用的EC2的HTTP服务器马上可以访问了:

下图关联ACL到公用的Subnet,并设置inbound和outbound的规则 。
Edit ACL

注意
我在Inbound的规则中添加了以下规则,SSH才可以使用,难道使用了Ephemeral port
600 Custom TCP Rule TCP (6) 1024 - 65535 0.0.0.0/0 ALLOW

使用Bastion进行访问。

[root@ip-10-0-1-130 ec2-user]# ssh ec2-user@10.0.2.43 -i XiongEC2KeyPair.pem
^C
[root@ip-10-0-1-130 ec2-user]# ssh ec2-user@10.0.2.43 -i XiongEC2KeyPair.pem
Last login: Sun Nov 17 07:19:47 2019 from ip-10-0-1-130.ap-southeast-1.compute.internal

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-10-0-2-43 ~]$ exit
logout

注意
一个Subnet只能在一个ACL里面,然而,一个Network ACL可以与多个Subnets相关联。
至少需要两个Subnets才能创建Loader Balancer

Endpoints测试

将私有的EC2移回到默认的VPC XiongTestVPC默认的ACL,这样测试会更容易些。

添加一个有S3访问权限的角色,

Add s3 access role

检查下权限:

[root@ip-10-0-1-130 ec2-user]# aws s3 ls
Unable to locate credentials. You can configure credentials by running "aws configure".
[root@ip-10-0-1-130 ec2-user]# ssh ec2-user@10.0.2.43 -i XiongEC2KeyPair.pem
Last login: Sun Nov 17 07:20:51 2019 from ip-10-0-1-130.ap-southeast-1.compute.internal

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-10-0-2-43 ~]$ aws s3 ls
2019-01-06 13:16:41 xiong-polly
2019-01-06 13:17:07 xiong-polly-mp3
2019-01-01 01:55:31 xiong2019
[ec2-user@ip-10-0-2-43 ~]$

为了测试需要,删除NAT Gateway以测试私有的EC2不能访问S3。记得删除NAT Gateway的时候删除EIP。

再测试,已经不能访问S3了,因为NAT Gateway删除后,私有的EC2已经不能访问外网了。

[ec2-user@ip-10-0-2-43 ~]$ aws s3 ls
...

创建一个S3 Gateway的Endpoint,com.amazonaws.ap-southeast-1.s3 amazon Gateway

S3 Gateway

可以查看到在私有的Subnet的Route Table里添加了一条路由到这个新创建的可以访问S3 的 Endpoint,测试结果如下,这里需要加上Region,否则不成功。

[ec2-user@ip-10-0-2-43 ~]$ aws s3 ls
^C
[ec2-user@ip-10-0-2-43 ~]$ aws s3 ls --region ap-southeast-1
2019-01-06 13:16:41 xiong-polly
2019-01-07 03:44:18 xiong-polly-mp3
2019-01-06 13:18:34 xiong2019
[ec2-user@ip-10-0-2-43 ~]$

术语

NAT: Network Address Translation (NAT)
Bastions: Jumbox,
Endpoints 通过VPC可以直接访问AWS的一些服务,不需要通过外网。
EIPElastic IP

本次实验的拓扑图。
拓扑图

Reference

AWS Network ACLs
AWS NAT Instances
Securely Connect to Linux Instances Running in a Private Amazon VPC
Ephemeral port Wkipedia

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值