天天看點

在AWS中建立定制的虛拟私有雲(VPC)

文章目錄

  • ​​建立定制VPC​​
  • ​​在VPC裡建立兩個Subnet​​
  • ​​建立Internet Gateway​​
  • ​​建立額外的Route table通路外部​​
  • ​​建立額外的Route table​​
  • ​​添加路由到Internet Gateway​​
  • ​​關聯公有Subnet到額外的公有的Route table​​
  • ​​在定制VPC裡建立一個新的Security Group (安全組)​​
  • ​​在定制VPC裡建立兩個EC2 (公有和私有)​​
  • ​​建立公有EC2​​
  • ​​建立私有EC2​​
  • ​​私有EC2通過NAT通路外網​​
  • ​​NAT Gateway和NAT Instance的比較​​
  • ​​使用NAT Instance​​
  • ​​使用NAT Gateway​​
  • ​​添加定制ACL并設定ACL​​
  • ​​Endpoints測試​​
  • ​​術語​​
  • ​​Reference​​

建立定制VPC

AWS裡一般預設有一個VPC,如果直接使用的話,可以省去很多麻煩。如果建立定制VPC,所有的元件需要各個建立,可以了解各個元件的細節。如果建立定制的VPC,會新建立一個預設的Route Table,Network ACL,和Security Group。不會建立Subnet和Internet Gateway。

建立一個定制的VPC并取名為​

​XiongTestVPC​

​, IPv4 CIDR block為10.0.0.0/16,如下圖所示:

在AWS中建立定制的虛拟私有雲(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如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

對于公有的Subnet,改變設定公有的Subnet的自動配置設定公有位址,在Action --> ​

​Modify auto-assign IP settings​

​裡,如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

同理,建立另一個私有的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上。如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

注意:一個VPC隻能附加一個Internet gateway。

建立額外的Route table通路外部

建立額外的Route table

在建立定制VPC的時候,會預設建立一個Route table為主Route Table,這個預設建立的Route Table隻能通路本地,是私有的(private),不能通路外部,這個Route Table專門用于内部私有的Subnet。那麼這裡我們建立一個額外的Route table可以通路外部,取名為​

​XiongRouteTablePublic​

​,用于外部的公有的Subnet。如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

添加路由到Internet Gateway

在​

​XiongRouteTablePublic​

​​這個公有的Route Table裡添加兩個(分别是IPV4和IPV6)指向前面新建立的可以通路外部的Internet Gateway, 也就是​

​XiongTestInternetGateway​

​​,​

​0.0.0.0/0​

​​為IPV4, ​

​::/0​

​為IPV6,如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

關聯公有Subnet到額外的公有的Route table

關聯公有Subnet也就是​

​XiongPublicSubnet​

​​到額外的Route table也就是​

​XiongRouteTablePublic​

​​,在​

​Subnets > Edit route table association​

​界面中,這樣公有Subnet就可以通路外部公網我了。添加的地方如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

在定制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

如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

注意: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>" >      

這裡要選擇, ​

​XiongTestVPC​

​​和​

​XiongPublicSubnet​

​​,前面已經設定過​

​XiongPublicSubnet​

​會自動配置設定公網位址, 如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

建立私有EC2

再建立一個私有EC2,假如用作内部資料伺服器,這裡要選擇, ​

​XiongTestVPC​

​​和​

​XiongPrivateSubnet​

​​,前面已經設定過​

​XiongPrivateSubnet​

​ 不會自動配置設定公網位址, 如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

這裡在建立私有EC2,在選擇Security Group的時候,選擇新建立的​

​XiongInternalSecurityGroup​

​​,如下所示,這樣就限制了隻能從​

​XiongPublicSubnet​

​進行通路:

在AWS中建立定制的虛拟私有雲(VPC)

測試能從外網通路公用EC2.

在AWS中建立定制的虛拟私有雲(VPC)

使用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 [email protected] -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

這裡要選擇, ​

​XiongTestVPC​

​​和​

​XiongPublicSubnet​

​。

參考這個​​連結 ​​​, 需要關閉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.

如下圖所示,進行更改,

在AWS中建立定制的虛拟私有雲(VPC)

更改後如下:

在AWS中建立定制的虛拟私有雲(VPC)

下面從私有的EC2中建立一個路由到這個NAT Instance,在私有的Subnet中的路由表中添加。

在AWS中建立定制的虛拟私有雲(VPC)

以上再測試​

​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’,如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

結果是​

​ping 8.8.8.8​

​​和​

​yum update -y​

​都無法使用。

使用NAT Gateway

在VPC中直接建立一個NAT Gateway,在public Subnet中,如下圖所示,這裡要建立一個EIP,Elastic IP

在AWS中建立定制的虛拟私有雲(VPC)

直接從私有的EC2中建立一個路由到這個新建立的NAT GATEWAY,在私有的Subnet中的路由表中添加。如下圖所示:

在AWS中建立定制的虛拟私有雲(VPC)

測試的結果是​

​ping 8.8.8.8​

​​和​

​yum 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的規則 。

在AWS中建立定制的虛拟私有雲(VPC)

注意

我在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 [email protected] -i XiongEC2KeyPair.pem
^C
[root@ip-10-0-1-130 ec2-user]# ssh [email protected] -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通路權限的角色,

在AWS中建立定制的虛拟私有雲(VPC)

檢查下權限:

[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 [email protected] -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​

​,

在AWS中建立定制的虛拟私有雲(VPC)

可以檢視到在私有的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

繼續閱讀