4

Using the cos-stable container optimized OS on GCE. Micro instance so ram is pretty sparse. Tried to enable swap to prevent locking due to OOM during docker pulls, but can't get it to work.

I realize most folders are stateless, so I put the swapfile under home:

sudo fallocate -l 1G /home/user/swapfile
sudo chmod 600 /home/user/swapfile
sudo mkswap /home/user/swapfile

results in:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

But sudo swapon /home/user/swapfile gives the error:

swapon: /home/user/swapfile: swapon failed: Invalid argument

Any ideas how to enable swap on COS?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
odogggg
  • 43
  • 3

2 Answers2

11

Disk based swap is disabled in the COS image. You can enable disk based swap with

sysctl vm.disk_based_swap=1

I have the following in my cloud-init:

bootcmd:
- sysctl vm.disk_based_swap=1
- fallocate -l 1G /var/swapfile
- chmod 600 /var/swapfile
- mkswap /var/swapfile
- swapon /var/swapfile
StAdmin
  • 126
  • 4
  • Woah, nice. I'll have to try that out. Thanks so much. – odogggg Oct 30 '19 at 21:03
  • 1
    Is this an older version of COS? When I run the first line, I get the following: ```-bash: sysctl: command not found``` – AreToo Mar 03 '20 at 21:38
  • @AreToo After executing all the commands under ssh, I've added custom metadata key `startup-script` to the COS instance with these value: `sysctl vm.disk_based_swap=1; sudo swapon /PATH_TO_YOUR/swapfile` – mu3 Jul 14 '20 at 15:51
3

Swap is not supported in container optimized OS

Swap would effectively destroy much of the behavioral isolation Google offers between containers.

Guaranteed pods should never require swap. Burstable pods should have their requests met without requiring swap. BestEffort pods have no guarantee.

I highly suggest you use a bigger instance as a f1-micro only has 600MB of RAM and you still need to run the OS on the instance it addition with your containers

Ernesto U
  • 786
  • 3
  • 14
  • Thanks for the answer, but I'm not using k8s. It's just a single vm running a few docker containers managed by docker compose. Can't switch to a bigger instance because the project requires to be in the always free tier. I find it interesting that such a minimal OS with hardly any packages built in includes swapon but doesn't support swap. Perhaps I should use a different distro as swap is critical during those rare memory spikes – odogggg Oct 04 '19 at 03:03
  • I understand your limitations, you should use a different OS I’ve tested the steps to add swap memory in GCP with Ubuntu and CoreOS, it works as you expect https://help.ubuntu.com/community/SwapFaq – Ernesto U Oct 04 '19 at 13:16
  • "I highly suggest you use to switch from free tier to paid tier" - okay. Personally I just switched to Ubuntu. – Nakilon Sep 07 '21 at 07:07