LoginSignup
4
3

More than 5 years have passed since last update.

CoreOSのメモリ不足解消のためにswap領域を設定する

Posted at

CoreOSのメモリ不足解消のためにswap設定をする

VPSでCoreOSを動かしているとわりとすぐメモリ不足に陥ります。
その解決策のひとつとしてswapを設定する手順メモ。

swapファイルの作成と有効化

  1. swapファイルの作成
    ここでは1024MBのファイルを作成します。

    # dd if=/dev/zero of=/swapfile bs=1M count=1024
    
  2. mkswapでスワップ領域を作成

    # mkswap /swapfile
    
  3. swaponでスワップ領域を有効にする

    # swapon /swapfile
    

systemdのサービスとして追加

  1. 下記の内容でファイルを作成

    /etc/systemd/system/swap.service
    [Unit]
    Description=Turn on swap
    
    [Service]
    Type=oneshot
    Environment="SWAPFILE=/swapfile"
    RemainAfterExit=true
    ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
    ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
    
    [Install]
    WantedBy=multi-user.target
    
  2. サービスの有効化

    # systemctl enable --now /etc/systemd/system/swap.service
    

確認

下記コマンドで追加されていることを確認

# free -h
             total       used       free     shared    buffers     cached
Mem:          2.0G       1.9G       100M        66M        23M       264M
-/+ buffers/cache:       1.6G       388M
Swap:         1.0G       308M       715M

Swap:の項目があればOKです。

参考サイト

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3