CoreOS: Is it possible to start an unit with cloud-init, which is manageable by fleet? - coreos

Can I start units with cloud-init, so that they are manageable by fleet afterwards?
It seems, that only normal systemd services can be started by cloud-unit, but without having them under fleet control. Is this right?
What would be a possible solution to automatically start specific services after the cluster is bootstrapped?
SOLVED:
#cloud-config
write_files:
- path: /home/core/foo.service
owner: core:core
permissions: 0644
content: |
[Unit]
Description=Foo
Requires=docker.service
After=docker.service
[Service]
User=core
TimeoutStartSec=0
KillMode=none
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill foo
ExecStartPre=-/usr/bin/docker rm foo
ExecStartPre=/usr/bin/docker pull registry.example.com/foo
ExecStart=/usr/bin/docker run --name foo registry.example.com/foo
ExecStop=/usr/bin/docker stop foo
coreos:
etcd:
discovery: https://discovery.etcd.io/<token>
addr: $private_ipv4:4001
peer-addr: $private_ipv4:7001
units:
- name: etcd.service
command: start
- name: fleet.service
command: start
- name: auto-start-foo.service
command: start
content: |
[Unit]
Description=Autostarts foo-service
Requires=docker.service
After=docker.service
[Service]
WorkingDirectory=/home/core/
ExecStart=/usr/bin/fleetctl start foo.service
Type=oneshot

A few weeks ago I was wondering the same thing. The approach presented by the OP used fleetctl to launch the process. I think that had the affect of being able to do fleetctl list-units and the launched process would be listed and managed by fleet.
You could put the content from the write_files down in the units: section and get rid of the write_files section, like this:
#cloud-config
coreos:
etcd:
discovery: https://discovery.etcd.io/<token>
addr: $private_ipv4:4001
peer-addr: $private_ipv4:7001
units:
- name: etcd.service
command: start
- name: fleet.service
command: start
- name: foo.service
command: start
content: |
[Unit]
Description=Foo
Requires=docker.service
After=docker.service
[Service]
User=core
TimeoutStartSec=0
KillMode=none
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill foo
ExecStartPre=-/usr/bin/docker rm foo
ExecStartPre=/usr/bin/docker pull registry.example.com/foo
ExecStart=/usr/bin/docker run --name foo registry.example.com/foo
ExecStop=/usr/bin/docker stop foo
but, that does mean that:
you won't see the unit when you do a fleetctl list-units, it is being
managed by the underlying systemd.
if the unit fails because this host fails it won't migrate to another
fleet host.
Thank you for the great question and answer #mbo :-)

Related

start .sh in .bash with a service or start the .sh file within the service

im currently setting up a minecraft server on my root but struggle with the startup on boot.
Before having this in startup i was starting the server with a .sh file which i had to start manually.
the .sh file also created a screen where i was able to check the console
.sh file:
screen -AmdS minecraft java -Xms4096M -Xmx4096M -jar /home/minecraft/server/server.jar nogui
But then i tried to have the server in startup of the root server so it starts automaticly
i created a service with a .bash file which starts the server with no problem on startup but without the screen option for the console
Service:
[Unit]
Description=Start Minecraft
After=network.target
[Service]
Type=simple
ExecStart=/root/start_minecraft_server.bash
TimeoutStartSec=0
[Install]
WantedBy=default.target
Bash:
#!/bin/bash
#Standard Minecraft
cd /home/minecraft/server/
exec java -Xmx4096M -Xms1024M -jar server.jar nogui
now i want to ask if you know any easy option for adding the screen option to the service or bash file?
Try this and make sure your screen is actually in /usr/bin/ by which screen
[Unit]
Description=Start Minecraft
After=network.target
[Service]
user=minecraft
Type=simple
ExecStart=/usr/bin/screen -S Minecraft_Server -d -m sh /root/start_minecraft_server.bash
TimeoutStartSec=0
[Install]
WantedBy=default.target
But you should also alter your startcript itself:
#!/bin/bash
#Standard Minecraft
cd /home/minecraft/server/
while true; do
exec java -Xmx4096M -Xms1024M -jar server.jar nogui
done;
And just for security reasons, you should never run your mc server as root - create another user for it with limited permissions and add something like user=minecraft below the SERVICE tag in the init startscript

Systemd ExecStart with arguments

I have process, that i run in this way :
sudo RADIODEV=/dev/spidev0.0 /opt/basicstation/build-rpi-std/bin/station -d -h /opt/basicstation/build-rpi-std/bin
I would like to lunch it on raspberry boot with systemctl like that :
[Unit]
Description=Basic station secure websocket
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
ExecStart= RADIODEV=/dev/spidev0.0 /opt/basicstation/build-rpi-std/bin/station -d -h /opt/basicstation/build-rpi-std/bin
[Install]
WantedBy=multi-user.target
Alias=basic_station.service
So i want to know how put the argument
RADIODEV=/dev/spidev0.0
-d
-h /opt/basicstation/build-rpi-std/bin
because wheni just put :
ExecStart= RADIODEV=/dev/spidev0.0 /opt/basicstation/build-rpi-std/bin/station -d -h /opt/basicstation/build-rpi-std/bin
That's not work
I already check some issue like :
issue systemd
But i can't reproduce what they propose.

Could not edit systemd service file

Need to edit following entries:
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
in /lib/systemd/system/docker.service file
$ sudo -E systemctl edit docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
did not update the service file after restart(sudo systemctl restart docker.service)
Edit
On AWS EC2, below is the issue:
$ nano /etc/systemd/system/docker.service.d/override.conf
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
/lib/systemd/system/docker.service still shows unmodified
1) What is the recommended approach to edit service file(docker.service)?
2) Why /lib/systemd/system/docker.service cannot be edited directly?
You need to create a systemd drop-in file for docker.service.
Create /etc/systemd/system/docker.service.d/override.conf file with contents
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
Reload systemd unit files.
systemctl daemon-reload
NOTE: Reloading systemctl daemon is required after editing any systemd unit file that's as per systemd design. For more info check this.
Restart docker daemon.
systemctl restart docker
You need to restart docker daemon to pick up the latest updated systemd unit file.
For more info check this.

Adding a shell command inside/inline of a systemd service file

I am running the gunicorn server as a service via systemd, Here is the sample service file:
[Unit]
Description=Gunicorn NGINX
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/test
ExecStart=/usr/local/bin/gunicorn --workers 8 --threads 8 --backlog 100 --bind 10.0.0.20:5000 -m 777 abc:app
Restart=always
[Install]
WantedBy=multi-user.target
I want now to replace the number near --workers and --threads by number of cores using the shell command so that it will dynamically pick the number of cores
nproc --all
Can someone help me how to do this
You can explicitly invoke a shell to get shell parsing.
ExecStart=/bin/bash -c '/usr/local/bin/gunicorn --workers "$(nproc --all)" --threads "$(nproc --all)" --backlog 100 --bind 10.0.0.20:5000 -m 777 abc:app'

systemd-path service not working

I have added systemd service to monitor a path. But it is not working. I touched a .txt file under /tmp/test/. But it is not kicking in my service. I cant see "/tmp/testlog.txt" getting generated. Is there anything wrong in my service?
myservice.path
[Unit]
Description=Path Exists
[Path]
PathExistsGlob=/tmp/test/*.txt
PathChanged=/tmp/test/
[Install]
WantedBy=multi-user.target
myservice.service
[Unit]
Description=Test
[Service]
ExecStartPre=/bin/sh -c 'mkdir /tmp/test && sleep 60'
ExecStart=/bin/sh -c 'echo "Test Success" >> /tmp/testlog.txt & '
[Install]
WantedBy=multi-user.target
tmp dir:
# ls /tmp/test/
ab.txt
#
What could be the reason for the failure?
That was a timing issue. I added dependency and made this service to start as the very last one. That one solved the issue.

Resources