Lightsail の最も安いプランだと月額 3.50 USD でインスタンスが運用できます。小規模なウェブサイトであればこれで全然運用できるのではないでしょうか。
- Amazon Lightsail の料金 | AWS
- https://aws.amazon.com/jp/lightsail/pricing/
当然ながら格安な分、サーバのスペックも低めなわけで、特にメモリがなんとも心許ないです。
- 512 MB メモリ
- 1 コアプロセッサ
- 20 GB SSD ディスク
- 1 TB 転送(上限を超えたアウトバウンドデータ転送分は超過分として課金)
対応として、スワップ領域を作成してメモリを割り当てる方法が考えられます。今回はそれを試してみましょう。
FYI:そもそもスワップ領域とは?
- スワップとは|「分かりそう」で「分からない」でも「分かった」気になれる IT 用語辞典
- IT エンジニアの豆知識 メモリの SWAP ホントに理解してる…?
1. 現在の使用状況を確認する #
メモリやファイルシステムについて、まずは現在の使用状況を確認します。
1-1. メモリ使用状況を確認する #
現在のメモリ使用状況を確認します。free コマンドを使います。
free -h
>>               total        used        free      shared  buff/cache   available
>> Mem:          481Mi       299Mi        17Mi        39Mi       165Mi       130Mi
>> Swap:         634Mi       241Mi       393Mi
上記から、純粋なメモリの容量は 481 MiB で、スワップ領域は 634 MiB ということがわかります。この通り、スワップ領域は初期状態でも割り当てられているようですね。
補足:free の出力の見方は次の記事がわかりやすいです。 #
- 【Linux】free コマンドの内容を図解化してわかりやすく説明します - フラミナル
- https://blog.framinal.life/entry/2020/04/23/080124
補足:free コマンドのオプションは次の通りです。 #
free --help
>> Usage:
>>  free [options]
>>
>> Options:
>>  -b, --bytes         show output in bytes
>>      --kilo          show output in kilobytes
>>      --mega          show output in megabytes
>>      --giga          show output in gigabytes
>>      --tera          show output in terabytes
>>      --peta          show output in petabytes
>>  -k, --kibi          show output in kibibytes
>>  -m, --mebi          show output in mebibytes
>>  -g, --gibi          show output in gibibytes
>>      --tebi          show output in tebibytes
>>      --pebi          show output in pebibytes
>>  -h, --human         show human-readable output
>>      --si            use powers of 1000 not 1024
>>  -l, --lohi          show detailed low and high memory statistics
>>  -t, --total         show total for RAM + swap
>>  -s N, --seconds N   repeat printing every N seconds
>>  -c N, --count N     repeat printing N times, then exit
>>  -w, --wide          wide output
>>
>>      --help     display this help and exit
>>  -V, --version  output version information and exit
補足:top コマンドなら使用状況をリアルタイムに確認できます。 #
top
>> top - 03:55:59 up 7 days,  2:31,  1 user,  load average: 0.00, 0.00, 0.00
>> Tasks:  81 total,   1 running,  80 sleeping,   0 stopped,   0 zombie
>> %Cpu(s):  0.3 us,  0.0 sy,  0.0 ni, 99.3 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
>> MiB Mem :    481.9 total,     15.8 free,    300.9 used,    165.3 buff/cache
>> MiB Swap:    634.8 total,    393.4 free,    241.4 used.    129.0 avail Mem
>>
>>   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
>>     1 root      20   0  170464   5268   3760 S   0.0   1.1   0:05.72 systemd
>>     .........
1-2. ファイルシステムの使用状況を確認する #
現在のファイルシステムの使用状況を確認します。df コマンドを使います。
df -h
>> Filesystem      Size  Used Avail Use% Mounted on
>> .........
>> /dev/xvda1       20G  3.2G   16G  18% /
>> .........
20GB のうち 18% しか使われておらず、残り 16G が利用可能です。スワップ領域を大きくする余裕があることがわかりました。
補足:df コマンドのオプションは次の通りです。 #
df --help
>> Usage: df [OPTION]... [FILE]...
>> Show information about the file system on which each FILE resides,
>> or all file systems by default.
>>
>> Mandatory arguments to long options are mandatory for short options too.
>>   -a, --all             include pseudo, duplicate, inaccessible file systems
>>   -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
>>                            '-BM' prints sizes in units of 1,048,576 bytes;
>>                            see SIZE format below
>>   -h, --human-readable  print sizes in powers of 1024 (e.g., 1023M)
>>   -H, --si              print sizes in powers of 1000 (e.g., 1.1G)
>>   -i, --inodes          list inode information instead of block usage
>>   -k                    like --block-size=1K
>>   -l, --local           limit listing to local file systems
>>       --no-sync         do not invoke sync before getting usage info (default)
>>       --output[=FIELD_LIST]  use the output format defined by FIELD_LIST,
>>                                or print all fields if FIELD_LIST is omitted.
>>   -P, --portability     use the POSIX output format
>>       --sync            invoke sync before getting usage info
>>       --total           elide all entries insignificant to available space,
>>                           and produce a grand total
>>   -t, --type=TYPE       limit listing to file systems of type TYPE
>>   -T, --print-type      print file system type
>>   -x, --exclude-type=TYPE   limit listing to file systems not of type TYPE
>>   -v                    (ignored)
>>       --help     display this help and exit
>>       --version  output version information and exit
1-3. スワップファイルを確認する #
初期状態からスワップ領域が割り当てられていたようですので、そのスワップファイルを確認しておきます。
swapon コマンドを使います。sudo で実行しましょう。
sudo swapon -s
>> Filename                  Type    Size    Used    Priority
>> /mnt/.bitnami.swap        file    649996  256992  -2
あるいは次のコマンドでも同じです。
cat /proc/swaps
>> Filename                  Type    Size    Used    Priority
>> /mnt/.bitnami.swap        file    649996  256992  -2
2. 新しいスワップファイルを作成する #
スワップ領域を拡張するために、まずは新規割り当て用のスワップファイルを作成します。
2-1. スワップファイルを作成する #
スワップ領域用のファイルを作成します。fallocate コマンドを使います。sudo で実行しましょう。
スワップ領域のサイズは任意です。以下では 4GB としています。
sudo fallocate -l 4G /mnt/.myswap.swap
ファイルが作られたことを確認します。
ls -lha /mnt
>> total 4.7G
>> drwxr-xr-x  2 root root 4.0K Aug 27 04:35 .
>> drwxr-xr-x 19 root root 4.0K Aug 20 01:25 ..
>> -rw-------  1 root root 635M Aug 20 01:26 .bitnami.swap
>> -rw-r--r--  1 root root 4.0G Aug 27 04:35 .myswap.swap
なおこの時点で df コマンドを実行すると、4GB 分が Used の状態となったことが確認できます。
df -h
>> Filesystem      Size  Used Avail Use% Mounted on
>> .........
>> /dev/xvda1       20G  7.2G   12G  39% /
>> .........
補足:fallocate コマンドと dd コマンド #
fallocate ではなく dd コマンドを実行しても同様のことが行えますが、処理速度の面から fallocate を使用した方が良いでしょう。
- Linux でサイズ指定してダミーファイルを作成する方法 - conf t
- https://monaski.hatenablog.com/entry/2016/02/21/225849
dd コマンドで実行する例
sudo dd if=/dev/zero of=/mnt/.myswap.swap bs=1M count=4096
2-2. スワップファイルの権限を変更する #
デフォルトのスワップファイル .bitnami.swap に設定されていた権限に合わせておきましょう。
sudo chmod 600 /mnt/.myswap.swap
変更されたことを確認します。
ls -lha /mnt
>> total 4.7G
>> drwxr-xr-x  2 root root 4.0K Aug 27 04:35 .
>> drwxr-xr-x 19 root root 4.0K Aug 20 01:25 ..
>> -rw-------  1 root root 635M Aug 20 01:26 .bitnami.swap
>> -rw-------  1 root root 4.0G Aug 27 04:35 .myswap.swap
2-3. スワップファイルのファイル属性を変更する #
ファイルがスワップファイルとして使えるように、ファイル属性を変更します。
mkswap コマンドを使います。sudo で実行しましょう。
sudo mkswap /mnt/.myswap.swap
3. 新しいスワップ領域を割り当てる #
新しいスワップファイルをスワップ領域として割り当てます。
3-1. スワップ領域を割り当てる #
swapon コマンドを使います。sudo で実行しましょう。
sudo swapon /mnt/.myswap.swap
スワップ領域が割り当てられたことを確認します。
free -h
>>               total        used        free      shared  buff/cache   available
>> Mem:          482Mi       360Mi       9.0Mi        39Mi       113Mi        69Mi
>> Swap:         4.6Gi       6.0Mi       4.6Gi
sudo swapon -s
>> Filename                  Type    Size    Used    Priority
>> /mnt/.bitnami.swap        file    649996  6656    -2
>> /mnt/.myswap.swap         file    4194300 0       -3
3-2. インスタンス再起動時もスワップ領域を自動で割り当てるようする #
いまのままだとインスタンスを再起動した際は、追加分のスワップ領域の割り当てが行われません。そのため再起動時も自動で割り当てられるように設定します。
fstab のファイルを開きます。
sudo vim /etc/fstab
ファイルの下部に以下を追記します。
# ADD
/mnt/.myswap.swap swap swap defaults 0 0
その後、インスタンスを再起動してから free コマンドなどで確認してみてください。スワップ領域が自動で確保されていることが確認できます。
参考 #
- スワップファイルを使用して Amazon EC2 インスタンスのスワップ領域としてメモリを割り当てる
- Lightsail のメモリが足りないからスワップ追加 | Narumium Blog
- AWS Lightsail のインスタンスで、スワップ領域の追加方法 | ex1-lab
- Lightsail メモリ不足で Swap を確保したよ [Amazon Linux] | 苔茎科テラリウム