Once Again About SWAP on Linux Hot on AWS EC2 Instance

We all know how important SWAP is. And how bad it is without him. Especially when there is not so much RAM, and a "gluttonous" process has appeared on the server. Yes, and end-to-end disk space on the server. And performance needs to be restored right now ...


In this article I want to consider ways to add SWAP on AWS EC2 servers "hot", without rebooting the server.


A bit of theory: what is SWAP?


SWAP is one of the mechanisms of virtual memory in which individual pieces of memory (usually inactive) are moved from RAM to secondary storage (a separate section or file), freeing up RAM to load other active pieces of memory. Also, SWAP is used in organizing sleep patterns. But in the context of this article, we will not consider this. You can read more about SWAP on Wikipedia .


Consider two ways to add SWAP:


  • SWAP as Volume
  • SWAP as FIle

SWAP Volume


To implement this solution, we will need:


  • create ebs volume
  • mount it to our server
  • prepare the mounted partition for use under SWAP
  • update / etc / fstab

First you need to create an EBS Volume of the right size. To do this, go to
Services -> EC2 -> Volumes in the AWS Console


Screen shot

image


Specify the size we need and create EBS Volume


Screen shot

image


We are waiting for EBS Volume State to become available.


Screen shot

image


Then select our EBS Volume and Actions -> Attach Volume


Screen shot

image


And having chosen the server to which we mount the disk, we complete with the preparation of EBS Volume


Screen shot

image


Now connect to our server and execute the lsblk command


ubuntu@testinstance:~$ lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  16G  0 disk
└─xvda1 202:1    0  16G  0 part /
xvdf    202:80   0   8G  0 disk

We see that our EBS Volume is successfully connected as / dev / xvdf .
Now we need to format the connected EBS Volume via mkswap
NOTE : do not forget to replace / dev / xvdf with your value


ubuntu@testinstance:~$ sudo mkswap /dev/xvdf
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=f2fe6c31-e8c5-4c28-b00f-99205cf2b04b

And activate it


ubuntu@testinstance:~$ sudo swapon /dev/xvdf

Making changes to / etc / fstab
NOTE: do not forget to replace / dev / xvdf with your own value
NOTE 2: adding an entry to / etc / fstab is necessary so that after rebooting the server you do not have to reconfigure SWAP



ubuntu@testinstance:~$ echo -ne "/dev/xvdf\tswap\tswap\tdefault\t0\t0\n" | sudo tee -a /etc/fstab

Verify that SWAP is activated on the system.


ubuntu@testinstance:~$ free
              total        used        free      shared  buff/cache   available
Mem:        1014648       45824      705668        3152      263156      809168
Swap:       8388604           0     8388604

Swap file


To implement this solution, we will need:


  • create and prepare a SWAP file for use
  • update / etc / fstab

But what if we do not have the opportunity to connect another EBS Volume to the server, but there is free space on the current EBS Volume? In this case, we can make a SWAP file


First, let's see how much free space we have on the disk through df


ubuntu@testinstance:~$ df --block-size=G
Filesystem     1G-blocks  Used Available Use% Mounted on
udev                  1G    0G        1G   0% /dev
tmpfs                 1G    1G        1G   4% /run
/dev/xvda1           16G    1G       15G   7% /
tmpfs                 1G    0G        1G   0% /dev/shm
tmpfs                 1G    0G        1G   0% /run/lock
tmpfs                 1G    0G        1G   0% /sys/fs/cgroup
tmpfs                 1G    0G        1G   0% /run/user/1000

Create an 8Gb swapfile



ubuntu@testinstance:~$ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
8192+0 records in
8192+0 records out
8589934592 bytes (8.6 GB, 8.0 GiB) copied, 130.955 s, 65.6 MB/s

After that we convert the file for use as SWAP via mkswap


ubuntu@testinstance:~$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=5f3d0d1c-1ece-4e0d-aa58-16b093891438

Changing swapfile permissions


ubuntu@testinstance:~$ sudo chmod 0400 /swapfile

And activate it



ubuntu@testinstance:~$ sudo swapon /swapfile

We make changes to / etc / fstab
NOTE: adding an entry to / etc / fstab is necessary so that after rebooting the server you do not have to reconfigure SWAP


ubuntu@testinstance:~$ echo -ne "/swapfile\tswap\tswap\tdefault\t0\t0\n" | sudo tee -a /etc/fstab

Verify that SWAP is activated on the system.


ubuntu@testinstance:~$ free
              total        used        free      shared  buff/cache   available
Mem:        1014648       45824      705668        3152      263156      809168
Swap:       8388604           0     8388604

Also popular now: