<p>CockroachDB has a relatively simple clustering mechanism, you initialize the DB with a command line option pointing at the host name of the other cockroach machines (but, this question is relevant really for any peer to peer clustered db).</p>

<p>One of the benefits of Cockroach is you can cluster across regions within a continent. Cockroach themselves published a good k8s config to standup a cockroach cluster on stateful sets. See <a href="https://github.com/cockroachdb/cockroach/blob/master/cloud/kubernetes/cockroachdb-statefulset.yaml" rel="nofollow noreferrer">this</a> config.</p>

<p>I'm trying to find a way to span the cockroach cluster across two GKE clusters in different regions. DNS and connectivity between the regions isn't really an issue, but I can't figure out how to address the stateful set instances. Internal to the cluster, they're cockroachdb-1.cockroach. Is there any way to allow these to be cross cluster addressable? One option would be to expose as a nodeport and point instances from the second cluster to machines with ports in the first cluster. That seems hacky and if the machine goes down represents a single point of failure. Any other ideas about how to do this? I also explored k8s federation, but I don't think it really addresses this issue either (though I could be wrong). </p>

<p>One final option would be exposing each instance through a load balancer...I don't really like that, but maybe it's the only way?</p>

<p>This is a good question that I've been meaning to play around with soon. You've been checking into a reasonable set of ideas. The core problem, as you allude to, is that every cockroach process needs to be able to individually address every other cockroach process.</p>

<p>I don't know how well cluster federation has developed over the last 12-18 months, but it seems like that's where this really should be solved.</p>

<p>Barring great developments in cluster federation, the "easiest" way that comes to mind is to use host networking for all the cockroachdb pods. You can specify a few known machine IPs as the join addresses for new pods to connect to, and then they'll all be able to talk to each other. I've made this work with StatefulSets before (by setting <code>dnsPolicy: ClusterFirstWithHostNet</code> along with <code>hostNetwork: true</code>), but I'm not sure it's a well-supported use case. You'd probably be better off using a DaemonSet (with a label selector to only run on certain nodes if you don't want it on them all). Something like this: <a href="https://gist.github.com/a-robinson/ec2b86783ccbf053c83ba83170673d63" rel="nofollow noreferrer">https://gist.github.com/a-robinson/ec2b86783ccbf053c83ba83170673d63</a></p>

<p>If that doesn't tickle your fancy, then creating a service for each StatefulSet instance unfortunately is probably the next best bet. As of a fairly recent change in Kubernetes, a separate label will be created for each pod, which should make this easier than it used to be: <a href="https://github.com/kubernetes/kubernetes/pull/55329" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/pull/55329</a></p>

<p>I'd love to see other suggestions for this, though, since it's all kind of manual or infrastructure-specific.</p>
