aws(学习笔记第九课) 使用AWS的网络存储EBS

aws(学习笔记第九课)

  • 使用AWS的网络存储EBS

学习内容:

  • 使用AWS的网络存储EBS

1.使用AWS的网络存储EBS

  1. EBS是什么
    EBSaws Elastic Block Store的缩写,就是AWS的弹性数据块存储。EBS有如下特点。
    • 它不属于EC2的一部分,独立存在。
    • 可以独立存在或者同一时间挂载到一个EC2实例上。
      注意,同一时间不能挂在到两个以及两个以上的EC2
    • 可以像普通的磁盘一样使用。
  2. 练习使用EBS
    • 创建一个EBS,之后用EC2进行挂载。

      json 复制代码
      {
      	"AWSTemplateFormatVersion": "2010-09-09",
      	"Description": "(EBS)",
      	"Parameters": {
      		"KeyName": {
      			"Description": "Key Pair name",
      			"Type": "AWS::EC2::KeyPair::KeyName",
      			"Default": "my-cli-key"
      		},
      		"VPC": {
      			"Description": "Just select the one and only default VPC",
      			"Type": "AWS::EC2::VPC::Id"
      		},
      		"Subnet": {
      			"Description": "Just select one of the available subnets",
      			"Type": "AWS::EC2::Subnet::Id"
      		},
      		"AttachVolume": {
      			"Description": "Should the volume be attached?",
      			"Type": "String",
      			"Default": "yes",
      			"AllowedValues": ["yes", "no"]
      		}
      	},
      	"Mappings": {
      		"EC2RegionMap": {
      			"ap-northeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-cbf90ecb"},
      			"ap-southeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-68d8e93a"},
      			"ap-southeast-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-fd9cecc7"},
      			"eu-central-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a8221fb5"},
      			"eu-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a10897d6"},
      			"sa-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-b52890a8"},
      			"us-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-1ecae776"},
      			"us-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-d114f295"},
      			"us-west-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-e7527ed7"}
      		}
      	},
      	"Conditions": {
      		"Attached": {"Fn::Equals": [{"Ref": "AttachVolume"}, "yes"]}
      	},
      	"Resources": {
      		"SecurityGroup": {
      			"Type": "AWS::EC2::SecurityGroup",
      			"Properties": {
      				"GroupDescription": "My security group",
      				"VpcId": {"Ref": "VPC"},
      				"SecurityGroupIngress": [{
      					"CidrIp": "0.0.0.0/0",
      					"FromPort": 22,
      					"IpProtocol": "tcp",
      					"ToPort": 22
      				}]
      			}
      		},
      		"IamRole": {
      			"Type": "AWS::IAM::Role",
      			"Properties": {
      				"AssumeRolePolicyDocument": {
      					"Version": "2012-10-17",
      					"Statement": [
      						{
      							"Effect": "Allow",
      							"Principal": {
      								"Service": ["ec2.amazonaws.com"]
      							},
      							"Action": ["sts:AssumeRole"]
      						}
      					]
      				},
      				"Path": "/",
      				"Policies": [
      					{
      						"PolicyName": "ec2",
      						"PolicyDocument": {
      							"Version": "2012-10-17",
      							"Statement": [{
      								"Effect" : "Allow",
      								"Action" : ["ec2:DescribeVolumes", "ec2:CreateSnapshot", "ec2:DescribeSnapshots", "ec2:DeleteSnapshot"],
      								"Resource": "*"
      							}]
      						}
      					}
      				]
      			}
      		},
      		"IamInstanceProfile": {
      			"Type": "AWS::IAM::InstanceProfile",
      			"Properties": {
      				"Path": "/",
      				"Roles": [{"Ref": "IamRole"}]
      			}
      		},
      		"Server": {
      			"Type": "AWS::EC2::Instance",
      			"Properties": {
      				"IamInstanceProfile": {"Ref": "IamInstanceProfile"},
      				"ImageId": {"Fn::FindInMap": ["EC2RegionMap", {"Ref": "AWS::Region"}, "AmazonLinuxAMIHVMEBSBacked64bit"]},
      				"InstanceType": "t2.micro",
      				"KeyName": {"Ref": "KeyName"},
      				"SecurityGroupIds": [{"Ref": "SecurityGroup"}],
      				"SubnetId": {"Ref": "Subnet"}
      			}
      		},
      		"Volume": {
      			"Type": "AWS::EC2::Volume",
      			"Properties": {
      				"AvailabilityZone": {"Fn::GetAtt": ["Server", "AvailabilityZone"]},
      				"Size": "5",
      				"VolumeType": "gp2"
      			}
      		},
      		"VolumeAttachment": {
      			"Type": "AWS::EC2::VolumeAttachment",
      			"Condition": "Attached",
      			"Properties": {
      				"Device": "/dev/xvdf",
      				"InstanceId": {"Ref": "Server"},
      				"VolumeId": {"Ref": "Volume"}
      			}
      		}
      	},
      	"Outputs": {
      		"PublicName": {
      			"Value": {"Fn::GetAtt": ["Server", "PublicDnsName"]},
      			"Description": "Public name (connect via SSH as user ec2-user)"
      		},
      		"VolumeId": {
      			"Value": {"Ref": "Volume"},
      			"Description": "Volume id"
      		}
      	}
      }
    • 创建结果

    • EBS上创建文件系统
      这里,还没有对EBS创建文件系统,接下来创建文件系统

      • 创建文件系统

        shell 复制代码
        sudo mkfs -t ext4 /dev/xvdf
      • 创建mount point进行挂载

        shell 复制代码
        mkdir /mnt/volume
        mount /dev/xvdf /mnt/volume/
      • 确认mount情况

      • touch文件在新的文件系统上

    • 尝试卸掉挂载

      • EBS最大的优点是独立于EC2,可以尝试将它umont

        shell 复制代码
        sudo umount /mnt/volume

        更新cloudformation,将AttachVolume修改为No

      • 可以查看更改集

      • 卸载之后,查看结果

        shell 复制代码
        fdisk -l
      • 更改AttachVolume,进行重新的Attach

      • 查看Attach之后的结果

      • 重新进行mount

        shell 复制代码
        mount /dev/xvdf /mnt/volume/
        cd /mnt/volume/
        cat ebs.txt
相关推荐
安冬的码畜日常10 分钟前
【Vim Masterclass 笔记22】S09L40 + L41:同步练习11:Vim 的配置与 vimrc 文件的相关操作(含点评课内容)
笔记·vim·vim配置·vim同步练习·vim options·vim option-list
五味香14 分钟前
Java学习,List 元素替换
android·java·开发语言·python·学习·golang·kotlin
佛州小李哥1 小时前
在亚马逊云科技上用AI提示词优化功能写出漂亮提示词(上)
人工智能·科技·ai·语言模型·云计算·aws·亚马逊云科技
追Star仙1 小时前
基于Qt中的QAxObject实现指定表格合并数据进行word表格的合并
开发语言·笔记·qt·word
Clockwiseee2 小时前
docker学习
学习·docker·eureka
安冬的码畜日常2 小时前
【Vim Masterclass 笔记24】S10L43 + L44:同步练习10 —— 基于 Vim 缓冲区的各类基础操作练习(含点评课)
笔记·vim·自学笔记·vim同步练习·vim缓冲区·vim buffer·vim缓冲区练习
lozhyf2 小时前
Go语言-学习一
开发语言·学习·golang
一只码代码的章鱼2 小时前
粒子群算法 笔记 数学建模
笔记·算法·数学建模·逻辑回归
圆圆滚滚小企鹅。2 小时前
刷题笔记 贪心算法-1 贪心算法理论基础
笔记·算法·leetcode·贪心算法
mascon2 小时前
U3D的.Net学习
学习