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.