[Terraform] 03 - VPC + NAT

结构目标

网络搭建设计图如下。 

 

Ref: [AWS] 扫盲,AWS的网络组件介绍 [基本概念介绍,赞比较多]

 

 

一、CIDR

CIDR 参考:CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍)

  • 子网划分

子网划分实际上就是将原来的两级IP地址转变为三级IP地址,表示如下:
IP地址 ::= {<网络号>, <子网号>, <主机号>} 

 

  • CIDR概述及其地址块计算

IP地址 ::= {<网络前缀>, <主机号>} / 网络前缀所占位数
例如:已知一个IP地址是:128.14.35.7/20,那么这个已知条件告诉大家的并不仅仅是一个IP地址这么简单,我们来分析一下。
128.14.35.7/20 = 10000000  00001110  00100011  00000111

我们通过令主机号分别为全0和全1就可以得到一个CIDR地址块的最小地址和最大地址。

最小地址是:128.14.32.0    = 10000000  00001110  00100000  00000000 
最大地址是:128.14.47.255  = 10000000  00001110  00101111  11111111     
子网掩码是:255.255.240.0  = 11111111  11111111  11110000  00000000 

 

 

二、NAT (Network Address Translation)

利用 Address:port 来定位一台主机。

 

  • NAT 网关(NAT Gateway)

一种支持 IP 地址转换服务,提供 SNAT 和 DNAT 能力,可为私有网络(VPC)内的资源提供安全、高性能的 Internet 访问服务。

我需要一个具备双向通信的能力,也就是同时具备:SNAT and DNAT。

Goto: [AWS] NAT Gateway & Security Group

 

 

 

代码分析


一、公网 VPC

资源: https://www.terraform.io/docs/configuration/resources.html

Ref: 经典网络还是VPC,开发者作何选择?

  • VPC 特点

经典网络公有云上所有用户共享公共网络资源池,用户之间未做逻辑隔离。用户的内网IP由系统统一分配,相同的内网IP无法分配给不同用户。

VPC是在公有云上为用户建立一块逻辑隔离的虚拟网络空间。在VPC内,用户可以自由定义网段划分、IP地址和路由策略,安全可提供网络ACL及安全组的访问控制,因此,VPC有更高的灵活性和安全性。

# Internet VPC
resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  instance_tenancy     = "default"
  enable_dns_support   = "true"
  enable_dns_hostnames = "true"
  enable_classiclink   = "false"
  tags = {
    Name = "main"
  }
}

 

  • 创建子网

map_public_ip_on_launch

  • true: 这是我在可用区 ap-south-1a创建的公共子网。 我已将map_public_ip_on_launch的值设置为true以便在该子网中启动实例时, map_public_ip_on_launch一个Public IP与之关联 。 使用此IP,客户端将能够访问实例以查看网页。 
  • false: 在此子网中,我将启动MySQL数据库,并且我不希望任何人都可以进入我的数据库并破坏数据。 这样做是为了将MySQL数据库保留在私有世界中。

Ref: 带有公有和私有子网的 VPC (NAT) 【官方参考】

Ref: AWS-创建具有公有子网和私有子网的VPC 【配置过程】

# Subnets
resource "aws_subnet" "main-public-1" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = "true"
  availability_zone       = "eu-west-1a"

  tags = {
    Name = "main-public-1"
  }
}

resource "aws_subnet" "main-public-2" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.2.0/24"
  map_public_ip_on_launch = "true"
  availability_zone       = "eu-west-1b"

  tags = {
    Name = "main-public-2"
  }
}


resource "aws_subnet" "main-private-1" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.4.0/24"
  map_public_ip_on_launch = "false"
  availability_zone       = "eu-west-1a"

  tags = {
    Name = "main-private-1"
  }
}

resource "aws_subnet" "main-private-2" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.5.0/24"
  map_public_ip_on_launch = "false"
  availability_zone       = "eu-west-1b"

  tags = {
    Name = "main-private-2"
  }
}

 

  • 创建 Internet Gateway

Public subnet 访问 Internet.

# Internet GW
resource "aws_internet_gateway" "main-gw" {
  vpc_id = aws_vpc.main.id  # 这里需要关联到一个 VPC

  tags = {
    Name = "main"
  }
}

 

[路由表] 跟公网网关相关的“路由表”设置。

# route tables
resource "aws_route_table" "main-public" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.main-gw.id
  }

  tags = {
    Name = "main-public-1"
  }
}

  

  • 关联 “子网” 与 “路由表”

将子网和路由表绑定起来。

# route associations public
resource "aws_route_table_association" "main-public-1-a" {
  subnet_id      = aws_subnet.main-public-1.id
  route_table_id = aws_route_table.main-public.id
}

resource "aws_route_table_association" "main-public-2-a" {
  subnet_id      = aws_subnet.main-public-2.id
  route_table_id = aws_route_table.main-public.id
}

 

 

二、单独配置 NAT以及 Gateway

  • 弹性 IP 地址

先配置一个“静态IP”。专为动态云计算设计的静态 IPv4 地址。弹性 IP 地址与 AWS 账户关联。

借助弹性 IP 地址,您可以快速将地址重新映射到您的账户中的另一个实例,从而屏蔽实例故障。

# nat gw
resource "aws_eip" "nat" {
  vpc = true
}

resource "aws_nat_gateway" "nat-gw" {
  allocation_id = aws_eip.nat.id
  subnet_id     = aws_subnet.main-public-1.id    # 该网关在子网中 <--
  depends_on    = [aws_internet_gateway.main-gw]    # 该网关 与 internet网关 关联起来 <--
}

 

[路由表] 跟内网网关相关的 “路由表” 设置。

# VPC setup for NAT
resource "aws_route_table" "main-private" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block     = "0.0.0.0/0"
    nat_gateway_id = aws_nat_gateway.nat-gw.id
  }

  tags = {
    Name = "main-private-1"
  }
}

 

将子网和路由表绑定起来。

# route associations private
resource "aws_route_table_association" "main-private-1-a" {
  subnet_id      = aws_subnet.main-private-1.id
  route_table_id = aws_route_table.main-private.id
}

resource "aws_route_table_association" "main-private-2-a" {
  subnet_id      = aws_subnet.main-private-2.id
  route_table_id = aws_route_table.main-private.id
}

 

 

三、查看配置结果

多出了一个main的vpc。

 

四个 subnet。

 

两个路由表。

 

Internet Gateway。

 

NAT Gateway

 

End.

posted @ 2020-11-10 18:27  郝壹贰叁  阅读(265)  评论(0编辑  收藏  举报