fix(nebius): persist firewall rules across instance stop/start cycles#106
fix(nebius): persist firewall rules across instance stop/start cycles#106
Conversation
is this solution an option?
UFW already persists its rules to /etc/ufw/*.rules and restores them on boot via its own service. The DOCKER-USER iptables rules are what don't survive. Instead of snapshotting the entire iptables state, you could:
This eliminates the need for iptables-persistent entirely. UFW handles everything, no race condition because there's only one service. |
Hi @patelspratik , Yeah, we did consider moving the DOCKER-USER rules into /etc/ufw/after.rules so everything could be managed under UFW. Though it can work, Docker’s documentation states that Docker and UFW are incompatible in how they use firewall rules, so adding those rules into UFW can make things a bit tricky to manage and easier to mess up. For now, we’re keeping UFW for general port rules and handling DOCKER-USER rules separately. This reduces coupling — if something goes wrong with UFW because of Docker-related rules, it doesn’t automatically affect the rest of the setup including SSH. Using iptables-persistent just ensures the custom Docker rules survive reboots, since Docker doesn’t persist those rules itself and UFW doesn’t manage them. |
Summary
After=netfilter-persistent.service) toufw.servicevia cloud-initruncmd, fixing a race condition where both services calliptables-restoreconcurrently on boot, causing UFW to fail withiptables-restore: line 4 failediptables-persistentand runsnetfilter-persistent saveto snapshot the complete iptables state (UFW chains + DOCKER-USER rules) so it survives stop/start cyclesnetfilter-persistentrestores the snapshot first, then UFW starts after it and re-applies cleanlyChanges
cloud/v1/providers/nebius/instance.go—generateCloudInitUserData()iptables-persistentto cloud-initpackageslist (installsnetfilter-persistentservice)printfpiped totee— writesAfter=netfilter-persistent.serviceto/etc/systemd/system/ufw.service.d/after-netfilter.conf, followed bysystemctl daemon-reloadsudo netfilter-persistent saveas the finalruncmdcommand to snapshot the complete iptables state to/etc/iptables/rules.v4runcmdentries: commands are now double-quoted with\and"properly escaped, preventing YAML-special characters from being misinterpreted by the cloud-init parserTest plan
host-sshhealth check passesjournalctlthat UFW starts afternetfilter-persistent(no race)sudo iptables -L DOCKER-USER -n -vshows all rules after rebootsudo ufw status verboseshows correct rules after reboot