+86 135 410 16684Mon. - Fri. 10:00-22:00

Amazon EC2实例添加交换分区

Amazon EC2实例添加交换分区

Amazon EC2实例添加交换分区

想必大家都知道Linux系统中,SWAP分区的重要性。那么今天在这里就和大家分享一下Amazon EC2实例如何添加交换分区。

创建Swap分区

1.登录AWS管理页面,点击「EC2」。

Amazon-Web-Services2.点击「Instances」,选择需要追加Swap的实例。这里实例为「test」。

Instances-ec23.点击左侧菜单栏的「ELASTIC BLOCK STORE」选择「Volumes」,然后点击「Create Volumes」。

ELASTIC-BLOCK-STORE4.如下图,Type默认即可,Size根据需要填写。这里需要注意的是Availability Zone要选择和实例同一可用区域。

Creat-Volume

5.确认volume卷已经创建完成。

ELASTIC-BLOCK-STORE16.选择新创建的卷,点击「Attach Volumes」。

AttachVolume7.Instance为选择需要创建Swap分区的实例。输入Device名称。点击「Attach」。

Attach-Volumes18.确认新建的Volume卷已连接到实例上。

Volumes-State19.卷关联到实例后,SSH登录该实例,执行如下命令。确认连接的卷被创建成功。

$ sudo fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/xvda: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
#         Start          End    Size  Type            Name
 1         4096     16777182      8G  Linux filesyste Linux
128         2048         4095      1M  BIOS boot parti BIOS Boot Partition
Disk /dev/xvdf: 5368 MB, 5368709120 bytes,    # 新做成的卷。
10485760 sectorsUnits = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

10.格式化做成的卷。

$ sudo mkswap -f /dev/xvdf
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=626c9a77-47d6-428f-8bb0-8912d0a50c74

11.挂载SWAP分区。

$ sudo swapon /dev/xvdf

12.确认Swap分区创建成功。

$ free -m
            total       used       free     shared    buffers     cached
Mem:           996        424        571          0         10        357
-/+ buffers/cache:         56        939
Swap:         5119          0       5119

13.设定实例重启有效化。

$ sudo vi /etc/fstab
#
LABEL=/     /           ext4    defaults,noatime  1   1
tmpfs       /dev/shm    tmpfs   defaults        0   0
devpts      /dev/pts    devpts  gid=5,mode=620  0   0
sysfs       /sys        sysfs   defaults        0   0
proc        /proc       proc    defaults        0   0
/dev/xvdf   swap        swap    defaults        0   0  ←添加该信息

服务器重启,确认SWAP分区

1.如图,选择实例,点击「Actions」,选择「Reboot」。

Instances-Reboot2.点击「Yes,Reboot」重启实例。

Reboot-Instances

3.服务器重启后,确认SWAP分区是有效的。

$ free -m
           total       used       free     shared    buffers     cached
Mem:           996        114        881          0          7         61
-/+ buffers/cache:         45        950
Swap:         5119          0       5119

注:也可是使用EC2的Ephemeral Disk创建交换分区。