Scheduling tasks
AHD VM uses Systemd as an init system.
You can leverage systemd in order to schedule periodic tasks.
A potential use case is scaling the number of replicas down to 0 for the restapi deployment during a certain maintenance period (Sunday 00:00:00 - Sunday 01:00:00)
1. Choose a name for the task
In this example, we'll use the name restapi-down
2. Create a timer file
Connect to AHD VM using SSH.
Using the nano
text editor, create a file named restapi-down.timer
sudo nano /etc/systemd/system/restapi-down.timer
Inside the file, paste the following content:
[Unit]
Description=
[Timer]
OnCalendar=
Persistent=true
[Install]
WantedBy=timers.target
Fill out two fields:
Description
: short description about the taskOnCalendar
: calendar event in which this timer is activated
You must use the following format to express the calendar event: DayOfWeek Year-Month-Day Hour:Minute:Second in UTC time (-2h w.r.t Italian time)
For example: since the maintenance window starts on Sunday 00:00:00 AM (Italian time):
[Unit]
Description= Scale down restapi deployment
[Timer]
OnCalendar=Sat 22:00:00
Persistent=true
[Install]
WantedBy=timers.target
3. Create a service file
Using the nano
text editor, create a file named restapi-down.service
Use the same name for the .timer and .service files
sudo nano /etc/systemd/system/restapi-down.service
Inside the file, paste the following content:
[Unit]
Description=
After=network.target
[Service]
Type=oneshot
ExecStart=
Fill out two fields:
Description
: short description about the taskExecStart
: command(s) that are executed when the service starts
For example: scale the number of replicas to 0 for a restapi deployment
[Unit]
Description= Kubectl command to scale down a restapi deployment
After=network.target
[Service]
Type=oneshot
ExecStart=kubectl scale deploy data-restapi --replicas=0
You can find the deployment's name using:
kubectl get deployment
4. Reload systemd manager configurations
sudo systemctl daemon-reload
5. Enable and start the timer unit
First
sudo systemctl enable restapi-down.timer
Then
sudo systemctl start restapi-down.timer
6. Check that the timer unit is active
systemctl status restapi-down.timer
You should see:
- the
Trigger
field, showing the time of the next schedulation - the
Triggers
field, showing which service to trigger (in this case, the restapi-down.service)
● restapi-down.timer - Scale down restapi deployment
Loaded: loaded (/etc/systemd/system/restapi-down.timer; enabled; preset: disabled)
Active: active (waiting) since Thu 2024-08-08 10:01:03 UTC; 24h ago
Trigger: Sat 2024-08-10 22:00:00 UTC; 1 day 11h left
Triggers: ● restapi-down.service
7. View logs of the systemd service and its execution history
journalctl -u restapi-down.service
The above example only shows the creation of a scheduled service for scaling restapi to zero.
To setup a maintenance window properly we must not forget about scaling the restapi deployment back to 1 by creating two additional files:
- restapi-up.timer
- restapi-up.service
sudo nano /etc/systemd/system/restapi-up.timer
[Unit]
Description= Rescale restapi
[Timer]
OnCalendar=Sat 23:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo nano /etc/systemd/system/restapi-up.service
[Unit]
Description= Kubectl command to scale a restapi deployment to 1 replica
After=network.target
[Service]
Type=oneshot
ExecStart=kubectl scale deploy data-restapi --replicas=1