The method of setting the timed sequential startup of the KVM virtual machine-systemd method

Preface: The KVM virtual machine system is a good way to extract the maximum performance of the server, but no one wants to wait for 5 minutes or more when the server starts. In order to reduce the number of tasks when the server is powered on, it is necessary to start the virtual machines sequentially within a period of time after the server is powered on, so as to achieve the dependence and functional connection between various services.

1. First upload the code: kvm-vm.service

[Unit]
Description=Start KVM Guests
After=network-online.target

[Service]
Type=simple
ExecStart=/bin/bash -c 'while ! ping -c1 192.168.1.240 &>/dev/null; do sleep 1; done; for guest in NFS Mysql PHP Nginx; do virsh start $guest; sleep 1m; done'
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target

illustrate:

a. ping -c1 192.168.1.240 , this sentence indicates that after the ip response of the KVM host is pinged, subsequent commands will be executed; 240 is the IP of the KVM host.

b. NFS Mysql PHP Nginx These are the names of my four VMs. Because these services have certain dependencies, they are started in order. NFS provides storage functions for the other three services; Mysql provides database functions for wordpress and nextcloud; the PHP server opens multiple pools, and is finally reverse-proxyed by nginx.

c. sleep 1m means to start a virtual machine every 1 minute.

Special note: This service is always running. If you enter the virtual machine and run poweroff, the virtual machine will be started after 1 minute. The following is the advanced version, the server starts running kvm-vm.service after 5 minutes, and after running for 5 minutes, it will shut down by itself, and the status of systemctl status kvm-vm.service is also inactive

 2. Advanced version

[Unit]
Description=Start KVM Guests
After=network-online.target

[Service]
TimeoutStartSec=infinity
ExecStartPre=/bin/sleep 300

ExecStart=/bin/bash -c 'while ! ping -c1 192.168.1.240 &>/dev/null; do sleep 1; done; for guest in NFS Mysql PHP Nginx; do virsh start $guest; sleep 1m; done'

TimeoutStopSec=1 
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

The above content sets that the specific function of the service will start to execute 300 seconds after the KVM system is turned on, and the service will be automatically closed after one minute after the task is completed.

​​​​​​​

 3. Sequence startup script of rc.local system

1. Of course, the system must first have the rc.local program, or the system itself is managed by rc.local, such as Alpine Linux.

Semi-automatic installation of Alpine Linux and configuration of desktop, remote login, frp, aria2 service, etc.

2.  vim vms-start.sh

#!/bin/bash
# 一个控制KVM虚拟机在KVM宿主机启动后,按照1.5分钟启动一个的频率,顺序启动的脚本;
# 总计6个虚拟机,名称分别是: NFS,Rides,Mysql,Only,PHP,Nginx;
# 用虚拟机的名称设定一个数组,要按照启动顺序编写,空格隔开;
guests=(NFS Rides Mysql PHP Nginx)
# 轮询数组中的虚拟机名称,间隔1.5分钟启动一次;
for guest in "${guests[@]}";
do
# 使用virsh 命令启动虚拟机
virsh start "$guest"
# 检查退出状态,并输出一个消息
if [ $? -eq 0 ];
then
echo "Started $guest successfully."
else
echo "Failed to start $guest!"
fi
# 在开始新一个虚拟机的轮询前,等待90秒;
sleep 90
done
# 退出该脚本
exit 0

3. Add rc.local startup item

 update-rc.d kvm-start

Guess you like

Origin blog.csdn.net/lggirls/article/details/130419581