Skip to content

Commit 4ced04c

Browse files
committed
chore: update swap
1 parent 6d87771 commit 4ced04c

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

infra/terraform/user-data.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ apt-get install -y \
1616
systemctl enable --now docker
1717
usermod -aG docker ubuntu # takes effect on next login for 'ubuntu'
1818

19-
# Add swap space (2GB) to help with memory pressure
20-
fallocate -l 2G /swapfile
21-
chmod 600 /swapfile
22-
mkswap /swapfile
23-
swapon /swapfile
24-
echo '/swapfile none swap sw 0 0' >> /etc/fstab
25-
26-
# Configure swappiness (how aggressively to use swap - 10 is conservative)
27-
echo 'vm.swappiness=10' >> /etc/sysctl.conf
19+
# Add swap space (2GB) to help with memory pressure (idempotent)
20+
if ! swapon --show | grep -q '^/swapfile'; then
21+
SWAP_GB=2
22+
fallocate -l ${SWAP_GB}G /swapfile || dd if=/dev/zero of=/swapfile bs=1M count=$((SWAP_GB*1024))
23+
chmod 600 /swapfile
24+
mkswap /swapfile
25+
swapon /swapfile
26+
# persist across reboots
27+
grep -q '^/swapfile ' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
28+
fi
29+
# Conservative swappiness
30+
sysctl -w vm.swappiness=10
31+
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf
32+
sysctl --system >/dev/null
2833

2934
# K3s (pin to stable; adjust as you like)
3035
curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL=stable sh -s - --write-kubeconfig-mode 640

0 commit comments

Comments
 (0)