Sunday, January 28, 2018

capacity - Using all space on Amazon EC2 - Medium tier



I'm currently running an Amazon EC2 - Medium Tier reserved instance for hosting client websites. Recently it seems space is starting to run low on /dev/sda1 - so I thought I'd better prepare..



df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 8.0G 5.6G 2.5G 70% /

none 848M 116K 848M 1% /dev
none 853M 0 853M 0% /dev/shm
none 853M 56K 853M 1% /var/run
none 853M 0 853M 0% /var/lock
/dev/sda2 335G 195M 318G 1% /mnt


I've seen this Question : How to mount space in /dev/sda2 - which offers one solution, but how do you resize an active partition like sda1 so I don't have to change our current setup? Or is there better ways to utilize sda2?


Answer



DO NOT USE THE SPACE IN /mnt! This is ephemeral storage and will not persist across reboots - if you put stuff in there IT WILL BE LOST. Some linux distros mount the ephemeral storage up for you as a convenience, use it for temp or swap.




You have a couple options.



Increase Root Drive Size



You can't resize /dev/sda1 on the fly. You can resize it on launch, however. This will require downtime, but what you can do is




  1. Snapshot the existing instance into an AMI. This should give you ami-yyyyyy (I hope you are doing this or something like it already to make backups over time!)




    ec2-stop-instances i-xxxxxx



    ec2-create-image --name my-image-name-v1.0 --description "My lovely Web Server" i-xxxxxxx


  2. Run a new instance of that image with a larger root drive size



    ec2-run-instances -k ssh-key -z us-east-1b -t c1.medium -b "/dev/sda1=:50" ami-yyyyyy


  3. Now you are running instance i-zzzzz. Depending on what Linux you are using you may need to then resize the file system to get the additional space. On Ubuntu, on the box:



    sudo resize2fs /dev/sdf


  4. Now swap in i-zzzzzz for i-xxxxxx in your elastic IP or ELB or DNS or however you're advertising it to the world.





Add A Second Drive



This is probably better - marginally more expensive, but best practice is NOT to put a bunch of stuff on your root drive, as if it fills up with logs or files you're going to crash and have a sad time of recovery.




  1. Create an EBS volume of the desired size, let's say 20 GB. This gives you a volume, vol-yyyyyy.



    ec2-create-volume -z us-east-1b -s 20



  2. Attach the volume to your instance



    ec2-attach-volume vol-yyyyyy -i i-xxxxxx -d /dev/sdf


  3. On the instance, create a filesystem on it and mount it



    sudo mkfs -t ext3 /dev/sdf



    sudo mkdir -p /web



    sudo mount /dev/sdf /web



  4. Move your web root over to there.


  5. Add the new drive permanently to /etc/mnttab



    /dev/sdf /opt/apps ext3 defaults,rw 0 0


  6. Snapshot your new image to AMI as in step 1 - always a good practice.




This also has the benefit of being able to back up that EBS separately by just snapshotting the volume, and also if you need to kill one server and bring up another, you can detach the /web EBS volume from one and attach it to the other, making data migration easy.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...