Getting can't resolve host when curling dependencies in systemd
I'm using CoreOS with cloud-init and I'm trying to curl for flannel. I have a unit file as such:
[Unit]
Description=Run flannel
Documentation=https://github.com/coreos/flannel
Requires=etcd2.service
After=etcd2.service
[Service]
ExecStartPre=/usr/bin/curl -L -o /opt/bin/flannel-0.5.1-linux-amd64.tar.gz -z /opt/bin/flannel-0.5.1-linux-amd64.tar.gz https://github.com/coreos/flannel/releases/download/v0.5.1/flannel-0.5.1-linux-amd64.tar.gz
ExecStartPre=/usr/bin/tar -C /opt/bin -xzvf /opt/bin/flannel-0.5.1-linux-amd64.tar.gz
ExecStartPre=/usr/bin/mv /opt/bin/flannel-0.5.1/flanneld /opt/bin/flanneld
ExecStartPre=/usr/bin/rm -rf /opt/bin/flannel-0.5.1
ExecStartPre=/usr/bin/rm -rf /opt/bin/flannel-0.5.1-linux-amd64.tar.gz
ExecStart=/opt/bin/flanneld
when it goes to curl for flannel, I get cannot resolve host 'github.com'
and curl exits with a code of 1. Is there some issue where I cannot use dns at the time systemd starts this up?
For CoreOS you will need:
[Unit]
Requires=network-online.target
There is a difference between network.target
and network-online.target
. network-online.target
is what is pulled in when attempting to perform network-mounts in /etc/fstab
, and your dependency needs are closer to that. To use it.
In my case this answer works https://unix.stackexchange.com/questions/353179/why-isnt-my-systemd-service-waiting-for-the-network/356189#356189
You have two options
Variant 1
wait for network and dns is ready
but also blocks the boot process, if the network not available!!!
Steps:
- activate the
systemd
module (required)
systemctl enable systemd-networkd-wait-online.service
- change the config of your service
[Unit]
...
After=systemd-networkd-wait-online.service
Requires=systemd-networkd-wait-online.service
Variant 2
Restart the script on a failure and try again
Steps:
- Set a restart configuration for the service
[Service]
...
Restart=on-failure
RestartSec=5
- Example bash script with curl
#!/bin/sh
STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" https://webhook.example)
if test $STATUSCODE -ne 200; then
exit 1
fi
exit 0