![如何使用 systemd 在远程第二台机器上启动应用程序](https://linux22.com/image/168625/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20systemd%20%E5%9C%A8%E8%BF%9C%E7%A8%8B%E7%AC%AC%E4%BA%8C%E5%8F%B0%E6%9C%BA%E5%99%A8%E4%B8%8A%E5%90%AF%E5%8A%A8%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F.png)
问题是:可以systemd
在第二台远程机器上启动服务吗?
设想:
应用程序 3X 名称,我称之为 app1 app2 app3 机器 2X 名称,我称之为 ms1 和 ms2
----MS1---------MS2
----app1--------app3
----app2------------
我创建了一个类似的服务:
应用程序服务
[Unit]
Description=Application
[Service]
# The dummy program will exit
Type=oneshot
# Execute a dummy program
ExecStart=/bin/true
# This service shall be considered active after start
RemainAfterExit=yes
[Install]
# Components of this application should be started at boot time
WantedBy=multi-user.target
应用程序1.服务
[Unit]
Description=Application Component 1
# When systemd stops or restarts the app.service, the action is propagated to this unit
PartOf=app.service
# Start this unit after the app.service start
After=app.service
[Service]
# Pretend that the component is running
ExecStart=/bin/sleep infinity
# Restart the service on non-zero exit code when terminated by a signal other than SIGHUP, SIGINT, SIGTERM or SIGPIPE
Restart=on-failure
[Install]
# This unit should start when app.service is starting
WantedBy=app.service
应用2.服务
[Unit]
Description=Application Component 2
PartOf=app.service
After=app.service
[Service]
ExecStart=/bin/sleep infinity
Restart=on-failure
[Install]
WantedBy=app.service
应用程序3.服务
[Unit]
Description=Application Component 3
PartOf=app.service
After=app.service
# This unit should start after the app-component2 started
After=app-component2.service
[Service]
ExecStart=/bin/sleep infinity
Restart=on-failure
[Install]
WantedBy=app.service
我可以做什么来app3.service
在第二台机器上进行初始化?它是否可行systemd
或需要脚本吗?
答案1
根据https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/system_administrators_guide/index#sect-Managing_Services_with_systemd-Remote(更新的链接)你实际上可以这样做,但我从未尝试过。它是基于 ssh 的,我不知道如何在文件中使用它:(,但你app3.service
可能会执行类似ssh -i /path/to/ssh_key user@host systemctl start real_service
.
答案2
我通过使用本地服务来实现这一点,该服务启动netcat
以触发远程服务通过套接字启动。
local.service
:
[Unit]
Description=Local control of a remote service
[Service]
Type=simple
ExecStart=/bin/nc remote-machine remote-port
StandardOutput=journal
StandardError=journal
SuccessExitStatus=1
remote.socket
:
[Unit]
Description=Socket for allowing remote control of our service
[Socket]
ListenStream=listen-port
Accept=yes
[Install]
WantedBy=sockets.target
[Unit]
Description=A service running on another machine
Requires=remote.socket
[Service]
Type=simple
ExecStart=/path/to/application
StandardInput=socket
StandardOutput=socket
StandardError=socket
安装local.service
在您的主计算机上。在您要将这项工作卸载到的计算机上remote.socket
安装。[email protected]
在远程机器上开始监听
systemctl start remote.socket
在您的本地机器上进行测试(替代remote-ip
和remote-port
):
netcat remote-machine-ip remote-port
您应该看到远程进程正在运行。 stdout
并且stderr
应该出现在您的终端中。如果应用程序接受stdin
,您也可以在此处使用它。在本地终止netcat
,您应该看到远程服务停止。一旦您对此感到满意,请通过在远程计算机上运行以下命令来启用套接字,使其在启动后开始侦听:
systemctl enable remote.socket
最后,将它们放在一起
systemctl start local.sevice
您的远程服务应该正在运行
systemctl stop local.servce
您的远程服务应该停止。
现在您可以添加任何Requires=
WantedBy=
PartOf=
您想要的关系local.service
来触发远程服务。