• 在AWS部署海外节点, 图简单使用web控制台创建VPC和k8s集群出错(k8s), 使用cli命令行工具创建成功
  • 本实例为复盘, 记录aws命令行工具cli创建VPC网络, a区和b区分别创建公有子网、私有子网、nat网关+EIP、再创建ssh跳板机作为管理入口
  • 建议使用terraform管理云资源

公网出口nat网关创建在公有子网, 如下图:

#安装配置aws cli

#install
cd /tmp
curl -kL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version #配置aws key
aws configure #查看配置
aws configure list

#创建专用网络vpc

#env
Region=ap-southeast-1 #新加坡
vpc_name=test-vpc
vpc_cidr=10.20.0.0/16 aws ec2 create-vpc \
--region $Region \
--cidr-block ${vpc_cidr} \
--tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=${vpc_name}}]" \
|tee /tmp/vpc.log #get id
VpcId=$(jq -r ".Vpc.VpcId" /tmp/vpc.log)
OwnerId=$(jq -r ".Vpc.OwnerId" /tmp/vpc.log)

#在a区创建私有子网和公有子网

#env
zone_a=ap-southeast-1a lan_a=subnet-a
lan_a_cidr=10.20.0.0/21 lan_a_public=subnet-a-public
lan_a_public_cidr=10.20.81.0/24 aws ec2 create-subnet \
--region $Region \
--availability-zone ${zone_a} \
--vpc-id ${VpcId} --cidr-block ${lan_a_cidr} \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=${lan_a}}]" \
|tee /tmp/lan1.log aws ec2 create-subnet \
--region $Region \
--availability-zone ${zone_a} \
--vpc-id ${VpcId} --cidr-block ${lan_a_public_cidr} \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=${lan_a_public}}]" \
|tee /tmp/lan2.log #get id
lan_a_id=$(jq -r ".Subnet.SubnetId" /tmp/lan1.log)
lan_a_public_id=$(jq -r ".Subnet.SubnetId" /tmp/lan2.log)

#在b区创建私有子网和公有子网

#env
zone_b=ap-southeast-1b lan_b=subnet-b
lan_b_cidr=10.20.8.0/21 lan_b_public=subnet-b-public
lan_b_public_cidr=10.20.82.0/24 aws ec2 create-subnet \
--region $Region \
--availability-zone ${zone_b} \
--vpc-id ${VpcId} --cidr-block ${lan_b_cidr} \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=${lan_a}}]" \
|tee /tmp/lan3.log aws ec2 create-subnet \
--region $Region \
--availability-zone ${zone_b} \
--vpc-id ${VpcId} --cidr-block ${lan_b_public_cidr} \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=${lan_a_public}}]" \
|tee /tmp/lan4.log #get id
lan_b_id=$(jq -r ".Subnet.SubnetId" /tmp/lan2.log)
lan_b_public_id=$(jq -r ".Subnet.SubnetId" /tmp/lan4.log)

#创建互联网网关, 作为公有子网出口

igw_name=${vpc_name}-public-gatway
aws ec2 create-internet-gateway \
--region $Region \
--tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=${igw_name}}]" \
|tee /tmp/igw.log igw_id=$(jq -r ".InternetGateway.InternetGatewayId" /tmp/igw.log) #互联网网关,关联到vpc
aws ec2 attach-internet-gateway \
--region $Region \
--vpc-id ${VpcId} \
--internet-gateway-id ${igw_id} #创建公有子网的路由表
route_name=route-public
aws ec2 create-route-table \
--region $Region \
--vpc-id ${VpcId} \
--tag-specifications "ResourceType=route-table,Tags=[{Key=Name,Value=${route_name}}]" \
|tee /tmp/route.public.log
#get id
route_public_id=$(jq -r ".RouteTable.RouteTableId" /tmp/route.public.log) #添加路由规则,流量指向互联网网关
aws ec2 create-route \
--region $Region \
--route-table-id ${route_public_id} \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id ${igw_id} #路由表与2个公有子网关联
aws ec2 associate-route-table \
--region $Region \
--route-table-id ${route_public_id} \
--subnet-id "${lan_a_public_id}"
#
aws ec2 associate-route-table \
--region $Region \
--route-table-id ${route_public_id} \
--subnet-id "${lan_b_public_id}"

#创建2个nat网关+EIP, 分别作为a区和b区私有子网出口

#申请2个弹性IP地址,标记为eip-nat-a,eip-nat-b
aws ec2 allocate-address --region $Region |tee /tmp/eip1.log
eip1_id=$(jq -r ".AllocationId" /tmp/eip1.log)
aws ec2 create-tags --resources ${eip1_id} --tags Key=Name,Value=eip-nat-a aws ec2 allocate-address --region $Region |tee /tmp/eip2.log
eip2_id=$(jq -r ".AllocationId" /tmp/eip2.log)
aws ec2 create-tags --resources ${eip2_id} --tags Key=Name,Value=eipnat-b #分别创建a、b区nat网关nat-a,nat-b, 关联eip和公有子网
aws ec2 create-nat-gateway \
--region $Region \
--subnet-id ${lan_a_public_id} \
--allocation-id ${eip1_id} \
--tag-specifications "ResourceType=natgateway,Tags=[{Key=Name,Value=nat-a}]" \
|tee /tmp/nat1.log aws ec2 create-nat-gateway \
--region $Region \
--subnet-id ${lan_b_public_id} \
--allocation-id ${eip2_id} \
--tag-specifications "ResourceType=natgateway,Tags=[{Key=Name,Value=nat-b}]" \
|tee /tmp/nat2.log nat1_id=$(jq -r ".NatGateway.NatGatewayId" /tmp/nat1.log)
nat2_id=$(jq -r ".NatGateway.NatGatewayId" /tmp/nat2.log) #创建路由表, 把私有子网和nat网关关联,作为访问公网出口
#创建a区私有子网路由表
route_name=route-a
aws ec2 create-route-table \
--region $Region \
--vpc-id ${VpcId} \
--tag-specifications "ResourceType=route-table,Tags=[{Key=Name,Value=${route_name}}]" \
|tee /tmp/route.a.log
#get id
route_a_id=$(jq -r ".RouteTable.RouteTableId" /tmp/route.a.log) #添加路由规则,流量指向nat-a网关
aws ec2 create-route \
--region $Region \
--route-table-id ${route_a_id} \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id ${nat1_id} #路由表与lan-a子网关联
aws ec2 associate-route-table \
--region $Region \
--route-table-id ${route_a_id} \
--subnet-id ${lan_a_id} #创建b区私有子网路由表
route_name=route-b
aws ec2 create-route-table \
--region $Region \
--vpc-id ${VpcId} \
--tag-specifications "ResourceType=route-table,Tags=[{Key=Name,Value=${route_name}}]" \
|tee /tmp/route.b.log
#get id
route_b_id=$(jq -r ".RouteTable.RouteTableId" /tmp/route.a.log) #添加路由规则,流量指向nat-a网关
aws ec2 create-route \
--region $Region \
--route-table-id ${route_b_id} \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id ${nat2_id} #路由表与lan-a子网关联
aws ec2 associate-route-table \
--region $Region \
--route-table-id ${route_b_id} \
--subnet-id ${lan_b_id}

#公有子网中启动ec2实例测试

#创建一个密钥对
aws ec2 create-key-pair --region $Region \
--key-name MySshKey --query "KeyMaterial" \
--output text > MySshKey.pem
chmod 400 MySshKey.pem #创建安全组,并容许ssh访问
gname=SSHAccess
aws ec2 create-security-group \
--region $Region --vpc-id ${VpcId} \
--group-name $gname --description "SSH access" \
|tee /tmp/gid.log gid=$(jq -r ".GroupId" /tmp/gid.log) aws ec2 authorize-security-group-ingress \
--group-id $gid --protocol tcp --port 22 --cidr 0.0.0.0/0 #启动ec2
aws ec2 run-instances \
--region $Region --count 1 \
--instance-type t2.micro \
--subnet-id ${lan_a_public_id} \
--security-group-ids $gid \
--key-name MySshKey \
--image-id ami-04ff9e9b51c1f62ca \
|tee /tmp/ec2.log ec2_id=$(jq -r ".Instances[0].InstanceId" /tmp/ec2.log) #查看ec2启动状态
aws ec2 describe-instances --instance-id ${ec2_id} \
--query "Reservations[*].Instances[*].{State:State.Name,Address:PublicIpAddress}" #等待实例状态为running,若没公网IP,可以申请EIP后绑定
aws ec2 allocate-address --region $Region |tee /tmp/eip3.log
eip3=$(jq -r ".PublicIp" /tmp/eip2.log)
eip3_id=$(jq -r ".AllocationId" /tmp/eip2.log)
aws ec2 associate-address --instance-id ${ec2_id} --allocation-id ${eip3_id} #ssh登录
ssh -i MySshKey.pem ubuntu@${eip3}

#若为测试, 则删除创建的实例

#ec2
aws ec2 terminate-instances --instance-ids ${ec2_id}
#security-group
aws ec2 delete-security-group --group-id $gid
#ssh key
aws ec2 delete-key-pair --region $Region --key-name MySshKey
#解除EIP关联
aws ec2 disassociate-address --association-id ${eip1_id} --region $Region
aws ec2 disassociate-address --association-id ${eip2_id} --region $Region
aws ec2 disassociate-address --association-id ${eip3_id} --region $Region
#删除EIP
aws ec2 release-address --allocation-id ${eip1_id} --region $Region
aws ec2 release-address --allocation-id ${eip2_id} --region $Region
aws ec2 release-address --allocation-id ${eip3_id} --region $Region
#删除子网
aws ec2 delete-subnet --region $Region --subnet-id ${lan_a_id}
aws ec2 delete-subnet --region $Region --subnet-id ${lan_a_public_id}
aws ec2 delete-subnet --region $Region --subnet-id ${lan_b_id}
aws ec2 delete-subnet --region $Region --subnet-id ${lan_b_public_id}
#删除nat网关
aws ec2 delete-nat-gateway --nat-gateway-id ${nat1_id} --region $Region
aws ec2 delete-nat-gateway --nat-gateway-id ${nat2_id} --region $Region
#删除路由表
aws ec2 delete-route-table --region $Region --route-table-id ${route_b_id}
aws ec2 delete-route-table --region $Region --route-table-id ${route_a_id}
#互联网网关与VPC分离
aws ec2 detach-internet-gateway --region $Region --internet-gateway-id ${igw_id} --vpc-id ${VpcId}
#删除互联网网关
aws ec2 delete-internet-gateway --region $Region --internet-gateway-id ${igw_id}
#删除vpc
aws ec2 delete-vpc --region $Region --vpc-id ${VpcId} #若删除失败, 可到web控制台操作

#参考

aws命令行cli创建VPC网络、公有子网、私有子网、nat网关+EIP的更多相关文章

  1. NET Core 环境搭建和命令行CLI入门

    NET Core 环境搭建和命令行CLI入门 2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文 ...

  2. NET Core 环境搭建和命令行CLI入门[转]

      NET Core 环境搭建和命令行CLI入门 时间:2016-07-06 01:48:19      阅读:258      评论:0      收藏:0      [点我收藏+]   标签: N ...

  3. 创建vpc网络

    vpc相关功能点: 模块 功能点 描述 备注 VPC 创建vpc网络 创建vpc网络,指定vpc网络名称   修改vpc网络 修改vpc网络名称   删除vpc网络 删除vpc网络   vpc相关命令 ...

  4. 如何在Linux命令行中创建以及展示演示稿

    导读 你在准备一场演讲的时候,脑海可能会先被图文并茂.形象华丽的演示图稿所占据.诚然,没有人会否认一份生动形象的演讲稿所带来的积极作用.然而,并非所有的演讲都需要TED Talk的质量.更多时候,演讲 ...

  5. windows cmd命令行下创建文件和文件夹

    在window下无法通过 右键=>新建 命令来创建以点开头的文件和文件夹 比如 .log ,会提示必须输入文件名. 可以通过命令行来创建 新建文件夹 mkdir .vs 新建文件 type NU ...

  6. go如何编写命令行(cli)程序

    创建一个命令行程序 问题 如何使用golang创建可以在命令行当中传递参数的程序?go如何带参数执行程序? 比如我们期望使用hello -version来查看hello程序的版本号码.或者输入hell ...

  7. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门

    2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...

  8. 【转】使用命令行方式创建和启动android模拟器

    原文网址:http://blog.csdn.net/tiandinilv/article/details/8953001 1.Android模拟器介绍 Android中提供了一个模拟器来模拟ARM核的 ...

  9. ASP.NET Core:CMD命令行+记事本 创建Console程序和Web Application

    今天看了Scott关于ASP.NET Core的介绍视频,发现用命令行一步一步新建项目.添加Package.Restore.Build.Run 执行的实现方式,更让容易让我们了解.NET Core的运 ...

  10. 使用Maven命令行快速创建项目骨架(archetype)

      > mvn archetype:generate 接下来就会输出一些列带索引变化的archetype项可供我们选择,然后提示我们选择一个编号,可以直接回车选择默认的编号(392),然后就跟着 ...

随机推荐

  1. manim边学边做--图形间集合关系

    几何图形间的集合关系,是数学和几何学中的一个基本概念, 通过计算不同形状(如圆形.矩形.三角形等)的交集和并集等关系,可以实现复杂的图形处理和视觉效果. manim中提供了4种计算几何形状间集合关系的 ...

  2. Web前端技术丛书代码下载

    我是清华社编辑,这些下载资源供读者个人学习使用,禁止商用. IE/Chrome下载,或者微信扫描二维码,按提示发邮箱下载. 二维码用微信扫码,可填写邮箱,把链接转发邮箱下载. <微信小程序开发从 ...

  3. 开源项目更新|WPF/Uno Platform/WinUI 3三个版本的《英雄联盟客户端》

    ​ 哈喽大家好! 我们是中韩Microsoft MVP夫妇 Vicky&James^^很高兴能加入博客园和大家分享我们的技术! 自2008年以来,我们一直深耕于WPF技术,积累了丰富的经验.这 ...

  4. 有封装过v-model相关的双向绑定组件吗?

    有的,之前在做⼀个通⽤的上传组件的时候,⼀开始是通过传统的⽗⼦通信实现的,⽗传⼦实现的图⽚ 回显,⼦传⽗实现的图⽚url抛出传给给接⼝,后来使⽤v-model做了⼀点优化,简单了不少 追问: 具体是怎 ...

  5. 04 统计语言模型(n元语言模型)

    博客配套视频链接: https://space.bilibili.com/383551518?spm_id_from=333.1007.0.0 b 站直接看 配套 github 链接:https:// ...

  6. js常用函数-02 _关于$(function () { })的运行时机

    js常用函数-02 _关于$(function () { })的运行时机 代码模式: $(function() { //执行操作 }); 查找到的结果: (function() {}),即 $(doc ...

  7. linu管理文本文件

    vi\vim编辑器的三种工作模式 命令模式(Command mode):按键编辑器都理解为命令,以命令驱动执行不同的功能.此模型下,不能自由进行文本编辑. 输入模式(Insert mode):也就是所 ...

  8. 洛谷:P5707 【深基2.例12】上学迟到 (纯净的顺序结构方法)

    本内容纯作者吃饱了没事干做出来的,仅供娱乐和思路参考(当然代码肯定是AC了) 最近我想重新提升一下自己的编程能力,想选一个题量比较精炼的平台,所以就用了洛谷. 题目描述 学校和 yyy 的家之间的距离 ...

  9. UE5笔记:虚幻引擎反射系统和对象

    虚幻引擎反射系统 使用宏提供引擎和编辑器各种功能,封装你的类.使用虚幻时,可以使用标准的C++类,函数和变量 虚幻中对象的基类是UObject,UCALSS宏的作用是标记UObject的子类,以便UO ...

  10. quartz集群增强版🎉

    quartz集群增强版 转载请著名出处https://www.cnblogs.com/funnyzpc/p/18534034 这是除了mee_admin之外,投入时间精力最多的一次开源了,quartz ...