📜 ⬆️ ⬇️

Once again about SWAP in Linux "on hot" on AWS EC2 Instance

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


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


A bit of theory: what is SWAP?


SWAP is one of the mechanisms of virtual memory, in which individual fragments of memory (usually inactive) are moved from RAM to secondary storage (a separate partition or file), freeing up RAM for loading other active fragments of memory. SWAP is also used when organizing a sleep mode. But in the context of this article we will not consider it. More information about SWAP can be found in Wikipedia .


Consider two ways to add SWAP:



SWAP Volume


To implement this solution, we will need:



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


Screenshot

image


Specify the size we need and create EBS Volume


Screenshot

image


Waiting for EBS Volume State to become available.


Screenshot

image


Then choose our EBS Volume and Actions -> Attach Volume


Screenshot

image


And choosing the server to which we will mount the disk is completed with the preparation of the EBS Volume


Screenshot

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 : don't 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: don't forget to replace / dev / xvdf with your value
NOTE 2: adding entries 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 

Check that SWAP is activated in 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:



But what if we don't have the ability to connect another EBS Volume to the server, but there is free space on the current EBS Volume? In this case, we can make 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 a 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, convert the file for use as SWAP through 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 access rights to swapfile


 ubuntu@testinstance:~$ sudo chmod 0400 /swapfile 

And activate it


  ubuntu@testinstance:~$ sudo swapon /swapfile 

Making 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 

Check that SWAP is activated in the system.


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

')

Source: https://habr.com/ru/post/346778/


All Articles