这个问题与如何将非管理员命令安排为 systemd 服务有关?
我有以下 minikube.service 文件:
[Unit]
Description=Runs minikube on startup
After=vboxautostart-service.service vboxballoonctrl-service.service vboxdrv.service vboxweb-service.service
[Service]
Type=forking
ExecStart=/usr/local/bin/minikube start
User=mark
Group=mark
[Install]
WantedBy=multi-user.target
当我从 shell 手动启动它时它工作正常:
mark@minikube-vm:~$ sudo systemctl start minikube.service
mark@minikube-vm:~$ minikube ip
192.168.99.100
mark@minikube-vm:~$ systemctl status minikube.service
● minikube.service - Runs minikube on startup
Loaded: loaded (/etc/systemd/system/minikube.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-09-25 17:32:48 UTC; 34min ago
Process: 8388 ExecStart=/usr/local/bin/minikube start (code=exited, status=0/SUCCESS)
Tasks: 46 (limit: 19141)
CGroup: /system.slice/minikube.service
├─8434 /usr/lib/virtualbox/VBoxXPCOMIPCD
├─8439 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
├─8618 /usr/lib/virtualbox/VBoxHeadless --comment minikube --startvm 6ec91432-0f09-4fdc-9976-6f736b15c37f --vrde config
├─8632 /usr/lib/virtualbox/VBoxNetDHCP --comment HostInterfaceNetworking-vboxnet0 --config /tmp/dhcp-config-kiyf3.xml
└─8658 /usr/bin/pulseaudio --start --log-target=syslog
Sep 25 17:31:22 minikube-vm minikube[8388]: * Using the virtualbox driver based on existing profile
Sep 25 17:31:22 minikube-vm minikube[8388]: * Starting control plane node minikube in cluster minikube
Sep 25 17:31:22 minikube-vm minikube[8388]: * Restarting existing virtualbox VM for "minikube" ...
Sep 25 17:31:23 minikube-vm pulseaudio[8658]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSu
Sep 25 17:31:23 minikube-vm pulseaudio[8658]: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported:
Sep 25 17:32:16 minikube-vm minikube[8388]: * Preparing Kubernetes v1.19.0 on Docker 19.03.12 ...
Sep 25 17:32:43 minikube-vm minikube[8388]: * Verifying Kubernetes components...
Sep 25 17:32:48 minikube-vm minikube[8388]: * Enabled addons: dashboard, default-storageclass, storage-provisioner
Sep 25 17:32:48 minikube-vm minikube[8388]: * Done! kubectl is now configured to use "minikube" by default
Sep 25 17:32:48 minikube-vm systemd[1]: Started Runs minikube on startup.
mark@minikube-vm:~$
但是在机器启动时它失败了:
mark@minikube-vm:~$ systemctl status minikube.service
● minikube.service - Runs minikube on startup
Loaded: loaded (/etc/systemd/system/minikube.service; enabled; vendor preset: enabled)
Active: failed (Result: timeout) since Fri 2020-09-25 17:18:09 UTC; 12min ago
Process: 7140 ExecStart=/usr/local/bin/minikube start (code=killed, signal=TERM)
Sep 25 17:16:39 minikube-vm minikube[7140]: * Using the virtualbox driver based on existing profile
Sep 25 17:16:39 minikube-vm minikube[7140]: * Starting control plane node minikube in cluster minikube
Sep 25 17:16:39 minikube-vm minikube[7140]: * Restarting existing virtualbox VM for "minikube" ...
Sep 25 17:16:40 minikube-vm pulseaudio[7438]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSu
Sep 25 17:16:40 minikube-vm pulseaudio[7438]: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported:
Sep 25 17:17:34 minikube-vm minikube[7140]: * Preparing Kubernetes v1.19.0 on Docker 19.03.12 ...
Sep 25 17:18:06 minikube-vm minikube[7140]: * Verifying Kubernetes components...
Sep 25 17:18:08 minikube-vm systemd[1]: minikube.service: Start operation timed out. Terminating.
Sep 25 17:18:09 minikube-vm systemd[1]: minikube.service: Failed with result 'timeout'.
Sep 25 17:18:09 minikube-vm systemd[1]: Failed to start Runs minikube on startup.
mark@minikube-vm:~$
我错过了什么?
您已经启动了一个“服务”,它基本上只是一个运行一些命令然后退出的脚本。这种事情应该在 systemd 单元中使用
Type=oneshot
,RemainAfterExit=yes
以便 systemd 知道该进程将消失但仍认为“服务”处于活动状态。为了完整起见,您还应该添加一条停止 minikube 的匹配
ExecStop=
行。systemd 文档有另一个此类单元的示例。
Ubuntu 21.04,通过 deb 安装的 minikube:还需要设置 systemd 单元工作目录以匹配 minikube 从 cmdline 运行:
单位可以创建
systemctl edit --force --full minikube.service
~root/.kube/config
包括嵌入式证书,可以在主机/虚拟机之外使用。在 centos 8 和 7 上为我工作
创建文件位置 /usr/lib/systemd/system/minikube.service
运行下一个命令以启用和启动服务