How do you add swap to an EC2 instance?
I'm currently running an ec2 micro instance and i've been finding that the instance occasionally runs out of memory.
Other than using a larger instance size, what else can be done?
A fix for this problem is to add swap
(ie paging
) space to the instance.
Paging works by creating an area on your hard drive and using it for extra memory, this memory is much slower than normal memory however much more of it is available.
To add this extra space to your instance you type:
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo chmod 600 /var/swap.1
sudo /sbin/swapon /var/swap.1
If you need more than 1024 then change that to something higher.
To enable it by default after reboot, add this line to /etc/fstab:
/var/swap.1 swap swap defaults 0 0
Swap should take place on the Instance Storage (ephemeral) disk and not an EBS device. Swapping will cause a lot of IO and will increase cost on EBS. EBS is also slower than the Instance Store and the Instance Store comes free with certain types of EC2 Instances.
It will usually be mounted to /mnt but if not run
sudo mount /dev/xvda2 /mnt
To then create a swap file on this device do the following for a 4GB swapfile
sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=4096
Make sure no other user can view the swap file
sudo chown root:root /mnt/swapfile
sudo chmod 600 /mnt/swapfile
Make and Flag as swap
sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile
Add/Make sure the following are in your /etc/fstab
/dev/xvda2 /mnt auto defaults,nobootwait,comment=cloudconfig 0 2
/mnt/swapfile swap swap defaults 0 0
lastly enable swap
sudo swapon -a
After applying the steps mentioned by ajtrichards you can check if your amazon free tier instance is using swap using this command
cat /proc/meminfo
result:
ubuntu@ip-172-31-24-245:/$ cat /proc/meminfo
MemTotal: 604340 kB
MemFree: 8524 kB
Buffers: 3380 kB
Cached: 398316 kB
SwapCached: 0 kB
Active: 165476 kB
Inactive: 384556 kB
Active(anon): 141344 kB
Inactive(anon): 7248 kB
Active(file): 24132 kB
Inactive(file): 377308 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 1048572 kB
SwapFree: 1048572 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 148368 kB
Mapped: 14304 kB
Shmem: 256 kB
Slab: 26392 kB
SReclaimable: 18648 kB
SUnreclaim: 7744 kB
KernelStack: 736 kB
PageTables: 5060 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1350740 kB
Committed_AS: 623908 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 7420 kB
VmallocChunk: 34359728748 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 637952 kB
DirectMap2M: 0 kB
链接地址: http://www.djcxy.com/p/54098.html
上一篇: 如何在c#中使用enyim memcached客户端与亚马逊elasticache
下一篇: 如何将交换添加到EC2实例?