프리티어의 아마존 웹서비스를 사용하는 경우 메모리가 1G 제한되어 있기 때문에 WAS 여러개 운영하거나 무거운 어플리케이션을 디플로이 할때 특히 컴파일타임에 일시적으로 충분하지 못한 메모리 문제로 여러 문제를 보게된다. 아무리 JVM 메모리 옵션을 튜닝해도 한계가 있기 때문에 속도가 느리더라도 Swap 영역을 잡아서 Virtual Memory 활용하는 것도 하나의 대안이 되겠다. 다만 이것이 디스크 IO 유발시켜서 추가 요금이 발생하는 지는 다시 컴토해야 것같다.

 

EC2 micro instances 같이 Free Tire에서 제공하는 리소스는 무척 제한적이다. 천성적으로 64bit OS 보다 넓은 메모리 어드레싱이나 데이터처리가 빠르게 처리되지만 32bit 비해서 메모리 리소스 소비가 많다고 한다. 따라서EC2 micro instances 위해서는 64bit OS 이미지를 선택해서 사용하는 것보다는 32bit OS이미지 선택이 바람직하다는 생각이 든다.

 

다음은 디스크 볼륨을 OS 머신에 Mount하고 스왑 영역을 설정하는 과정이다. 동일한 디스크 볼륨에 스왑 영역을 잡을 수도 있지만 IO 분산하기위해서 다음과 같이 Swap 볼륨을  따로 설정하였다.

 

1.       lsblk 명령을 통해 현재 가용 볼륨을 확인한다.

root@cosmos:~# lsblk

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

xvda    202:0    0   8G  0 disk

└─xvda1 202:1    0   8G  0 part /

xvdf    202:80   0   3G  0 disk

xvdg    202:96   0  19G  0 disk

root@cosmos:~#

 

 

2.       볼륨을 ext4방식으로 포맷하고 /mnt/data  마운트한다.

root@cosmos:~# mkfs -t ext4 /dev/xvdg

root@cosmos:~# cd /mnt

root@cosmos:/mnt# mkdir data

root@cosmos:/mnt# mount /dev/xvdg data

 

3.       부팅후에서 변경 사항을 반영하기 위하여 /etc/fstab 백업하고  추가 반영 한다.

root@cosmos:/mnt# cd /etc

root@cosmos:/etc# cp fstab fstab.org

root@cosmos:/etc# vi fstab

# 다음행을 추가 한다.

/dev/xvdg       /mnt/data   ext4   

 

4.       Swap 반영하기 전에 메모리 상태를 확인한다.

root@cosmos:~# free -m

             total       used       free     shared    buffers     cached

Mem:           992        264        728          0          9        190

-/+ buffers/cache:         63        928

Swap:            0          0          0

 

5.       디스크 볼륨을 스왑영역으로 설정한다.

root@cosmos:~# swapon /dev/xvdf

 

6.       스왑 적용 메모리 상태 확인

root@cosmos:~# free -m

             total       used       free     shared    buffers     cached

Mem:           992        264        727          0          9        190

-/+ buffers/cache:         64        928

Swap:         3071          0       3071

 

7.       재부팅 후에도 변경사항을 유지하기 위해서 /etc/fstab  다음 행을 추가한다.

/dev/xvdf       none    swap    sw  0       0

 

8.       적용된 결과를 다시 확인한다.

root@cosmos:~# lsblk

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

xvda    202:0    0   8G  0 disk

└─xvda1 202:1    0   8G  0 part /

xvdf    202:80   0   3G  0 disk [SWAP]

xvdg    202:96   0  19G  0 disk /mnt/data

 

 


How do you add swap to an EC2 instance?

http://stackoverflow.com/questions/17173972/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?

Answers 1

A fix for this problem is to add swap or 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 this is much more of it 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 /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

 

COMMENTS

Thanks! Really simple and great to know. ?  ashpriom Feb 25 at 14:12

Could you please explain what does it mean "if=...", "of=...", bs=1M and count=1024 because I've never seen if, of and = in the argument list. ?  Alexander Supertramp Mar 18 at 3:53

if means 'input file', of means 'output file', bs means 'block size' and count is the number of blocks you want to allocate… you can read the man page of the command for more info: linux.die.net/man/1/dd ?  Fabio Batista May 19 at 22:23  

 

Answers 2

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 the EC2 Instance.

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

 

Answers 3

You can add a 1 GB swap to your instance with these commands:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo mkswap /swapfile
sudo swapon /swapfile

To enable it by default after reboot, add this line to /etc/fstab:

/swapfile swap swap defaults 0 0

 

Answers 4

 

Most distros have it packaged.

On EC2 you might want to change "swappath" to /mnt or high-iops disk.

 

You can create swap space using the following steps Here we are creating swap at /home/

1) dd if=/dev/zero of=/home/swapfile1 bs=1024 count=8388608 Here count is kilobyte count of swap space

2) mkswap /home/swapfile1

3) vi /etc/fstab make entry : /home/swapfile1 swap swap defaults 0 0

4) run: swapon -a

 

 

Posted by Steven J.S Min
,