1

I have some experience with systemd, but haven't done anything beyond the basics with it. I have some scripts that I've added to (AWS) "userdata", which I think gets executed by the systemd service cloud-final.service (cloud-init), but not sure. I want to make sure that the contents of userdata get executed before kubelet.service starts. (Basically, I need to setup do some setup steps for a CSI-based filesystem before kubelet boots).

Both cloud-final.service and kubelet.service are not installed by me; they are managed by external install processes. So, I don't know if it would be a bad practice (or difficult) to modify those unit files.

I'm wondering if there's some way to set a dependency between those services without editing their unit files. Or if there's a better way to setup a dependency relationship between code that runs in userdata and kubelet. I'd appreciate any advice that you have to offer.

1 Answer 1

1

To set up a dependency between the userdata execution (which is typically managed by cloud-final.service) and kubelet.service without modifying their unit files directly, you can create a custom systemd service that explicitly sets up this dependency. This custom service will ensure that your userdata script runs and completes before kubelet.service starts.

Create a new unit file for your custom service, for example, /etc/systemd/system/userdata-setup.service:

[Unit] Description=Run userdata script and set up dependencies After=network.target cloud-final.service Before=kubelet.service Wants=cloud-final.service [Service] Type=oneshot ExecStart=/usr/local/bin/your-userdata-script.sh RemainAfterExit=true [Install] WantedBy=multi-user.target 

This unit file does the following:

Specifies that the service should run after cloud-final.service and before kubelet.service.

Ensures the cloud-final.service is started before this service.

Runs your userdata script located at /usr/local/bin/your-userdata-script.sh.

Ensures that the service is considered active after the script has run, preventing kubelet.service from starting before it finishes.

Make sure your userdata script is available at /usr/local/bin/your-userdata-script.sh and is executable:

sudo mv your-userdata-script.sh /usr/local/bin/your-userdata-script.sh sudo chmod +x /usr/local/bin/your-userdata-script.sh 

Then create a drop-in file /etc/systemd/system/kubelet.service.d/custom-dependencies.conf with the following content:

[Unit] After=userdata-setup.service Requires=userdata-setup.service 

This makes sure that kubelet.service will wait for userdata-setup.service to complete before starting.

Test it:

sudo systemctl status userdata-setup.service sudo systemctl status kubelet.service 

Enable the service to start at boot:

sudo systemctl enable userdata-setup.service sudo systemctl start userdata-setup.service 

By doing this, you create a clear dependency chain that ensures your userdata script runs and completes before kubelet.service starts, without modifying the unit files of the cloud-final.service or kubelet.service.

This approach keeps the system's managed services intact while allowing you to introduce the necessary dependency for your setup tasks.

2
  • Thanks for your reply. However, I'm not able to get this approach to work. The "userdata-setup.service" immediately goes into an "inactive (dead)" state, and the kubelet.service does not wait for it - kubelet just starts running. I put a long sleep in "your-userdata-script.sh" for testing, and kubelet is for sure not waiting for that sleep. Commented Aug 7, 2024 at 22:29
  • That's strange, it should take a moment but not hang. I have added explicitly create a dependency on userdata-setup.service, ensuring that kubelet.service waits for userdata-setup.service to finish before it starts. This should resolve the issue of kubelet.service starting prematurely. Commented Aug 8, 2024 at 1:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.