By info290735
The requirement seems quite easy (before shutting down/rebooting remove a droplet tag) but getting it working just isn’t happening.
I have the following script which works for quickly adding/removing a droplet to the loadbalancer tag /root/SCRIPTS/loadbalancer.sh
#!/bin/bash
API=xyxyxyxyxyxyxyxyxyxyxyx
SID=`/snap/bin/doctl -t $API compute droplet list --format ID,Name | grep $HOSTNAME`
SID="$(echo $SID | cut -d ' ' -f1)"
function add {
/snap/bin/doctl -t $API compute droplet tag $SID --tag-name=WEB-LBD
}
function remove {
/snap/bin/doctl -t $API compute droplet untag $SID --tag-name=WEB-LBD
}
case "$1" in
add)
echo "Adding Server to LoadBalancer..."
add
;;
remove)
echo "Removing Server from LoadBalancer..."
remove
;;
debug)
echo "Output:"
echo "$SID"
;;
*)
echo "(add|remove|debug)"
;;
esac
This works great (and I’ve made another script calling this one before and after running updates). However what I was hoping to do, is to remove the server from the loadbalancer automatically before the server is reboot/shutdown
I’ve create /etc/systemd/system/loadbalancer-remove.service
[Unit]
Description=Remove Server from LoadBalancer on shutdown
After=network.target networking.service network-online.target nss-lookup.target systemd-resolved
Requires=network.target networking.service network-online.target nss-lookup.target systemd-resolved
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/root/SCRIPTS/loadbalancer.sh remove
[Install]
WantedBy=multi-user.target
The service is active but seems to run the exit after the network has already gone (or something it needs). According to some systemd programmer, all that’s needed to ensure this exits before the network is shutdown is “network.target” but the droplet hangs on reboot. I believe this is because doctl obviously needs the network up to communicate and the network is going down before this is stopped but it’s difficult to know this as I can’t see any output as it’s shutting down. I’ve tried various Wants, Requires & After but it’s the same result. I found a different way of doing it that uses halt.target reboot.target shutdown.target before starting this service, but that gave me the same issue : hang on reboot.
Just wondering if anyone has solved anything similar? it’s not specifically a doctl issue but I dont know if doctl taking a few seconds is allowing the network to be shutdown before it’s finished.
Any thoughts at all appreciated, I’ve been googling and trying stuff for 4 hours.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Doing some digging indicates that
After=networking.service
Should do the trick. If you remove all other items in After do you still encounter problems?
While this configuration should be achievable, it would not handle unplanned downtime such as crashes of the node. Are you monitoring your nodes externally as well to trigger the removal if one of your servers stops responding properly?
This comment has been deleted
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.