Ubuntu service script to start a CockroachDB node on boot

I have completed the migration of my application from PostgreSQL to CockroachDB. I now need to deploy my application. I need to start a CockroachDB node when the server is started / rebooted.
I have just spent many hours trying to create a cockroach.service script to be installed in /etc/systemd/system/ but with no success.
Has anyone been able to successfully start a CockroachDB node automatically when the OS Ubuntu boots?
Any help will be greatly appreciated.
Ashok

Sure.

My systemd service file looks like this:

# /etc/systemd/cocroachdb.service
#   Starts/Stops a cockroach database node.

[Unit]
Description=Cockroach Database cluster node
Requires=network.target

[Service]
Type=simple
WorkingDirectory=/home/me
ExecStart=/usr/local/bin/cockroach start --certs-dir=certs  --max-sql-memory=25%% --cache=50%%
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=cockroach
User=rsm
[Install]
WantedBy=multi-user.target

Assuming the roach nodes have all been up correctly before then they remember where their peers are and all works well.

OK, that is on Debian not Ubuntu, but I think it should be much the same.

1 Like

Thank you very much for your systemd service file. I have modified it to fit my environment and am currently testing it.

Question: Why is such information not available under Docs->Deployment?

Ashok

Hi @ullal!

We don’t currently use systemd in our testing, so we just hadn’t gotten to it yet, but we’ll look at adding it – Thanks for the suggestion!

After some modifications to the systemd service file that heater had posted, my cockroach.service is as follows:

/etc/systemd/system/cockroach.service

Starts/Stops a cockroach database node.

[Unit]
Description=Cockroach Database cluster node
Requires=network.target

[Service]
Type=simple
WorkingDirectory=/home/ullal
ExecStartPre=/bin/sleep 30
ExecStart=/usr/local/bin/cockroach start --certs-dir=certs --host=192.168.0.82 --http-host=192.168.0.82 --join=192.168.0.80:26257,192.168.0.81:26257,192.168.0.82:26257 --cache=25%% --max-sql-memory=25%%
ExecStop=/usr/local/bin/cockroach quit --certs-dir=certs --host=192.168.0.82
Restart=always
RestartSec=10
RestartPreventExitStatus=0
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=cockroach
User=ullal

[Install]
WantedBy=default.target

I have changed/added:

  1. WantedBy=default.target and ExecStartPre=/bin/sleep 30

This was done because syslog showed an time sync error after booting. Now I am delaying the start so that NTP has time, on a slow network connection, to sync the clock.

  1. ExecStop= …

This enables me to stop the node using systemctl stop cockroach.

  1. RestartPreventExitStatus=0

This prevents a restart after I have manually stopped the node. Without this, the the node was started again immediately after I had stopped in manually, as defined by Restart=always

I hope that this information is useful to someone who might be using Ubuntu servers.

Ashok

4 Likes