Can I test if my cluster is initialized?

We run a kubernetes job to init our cluster. It just runs “/cockroach/cockroach init --insecure --host=cockroachdb-0.cockroachdb” and retries until we get a non-zero return.

The problem is that when if the cluster is already initialized then we get an error but really I want to treat that as success.

Is there some way I can say init cluster only of it doesn’t already exist?

There is not a direct way to query for initialization status. You can try to connect to a node and perform a query (such as SELECT 1), and if it succeeds, the cluster is initialized. If not, it’s ambiguous: you might be connecting to a newly-provisioned node that hasn’t connected to the initialized parts of the cluster yet.

Running init too many times could create multiple clusters (each node would pick one of the clusters to join and would be unable to talk to nodes that joined another cluster), so we do not recommend retrying the init command. If cockroach init fails, tear down everything you’ve done (specifically, stop all the cockroach processes and delete their store directories) and start over from scratch instead of trying to re-run init on the same nodes and stores.

One thing we’ve considered to make this easier in future releases is to make the init command take the number of expected nodes as an argument. Then it could (safely) fail if run against a node with fewer than the expected number of peer connections.