<p>Currently I deployed Cassandra in k8s without multi-rack in single/multiple data-centers using single rack.</p>
<p>Now I am planning to deploy Cassandra across multiple racks in single/multiple DCs. I am planning to use <code>topologySpreadConstraints</code> for this. I will define to constraints one for <code>zone</code> and another for <code>node</code> and will add nodes label accordingly. Here is the <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/" rel="nofollow noreferrer">link</a> which I am referring for above implementation.</p>
<p>The idea behind this is to achieve High Availability (HA) so that if one rack goes down my service will be available &amp; pods should not be scheduled on other racks. When it's restored, pods should be restored back on it.</p>
<p>But I am not sure how many <code>statefulset (sts)</code> I should use?</p>
<ol>
<li>Should I use one <code>sts</code> if I have one DC and N <code>sts</code> if I have N DC?</li>
<li>Or, I should always use N <code>sts</code> if I have N <code>Racks</code> in each DC?</li>
</ol>
<p><strong>Sample Code</strong>
Consider, I have 3 nodes, 3 racks and am trying to deploy 2 pods on each rack and node. And added zone &amp; node labels on all nodes.</p>
<pre><code>apiVersion: v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
      foo: bar
  serviceName: &quot;nginx&quot;
  replicas: 6 # by default is 1
  topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: node-pu
    whenUnsatisfiable: DoNotSchedule
    labelSelector:
      matchLabels:
        foo: bar
  - maxSkew: 1
    topologyKey: zone-pu
    whenUnsatisfiable: DoNotSchedule
    labelSelector:
      matchLabels:
        foo: bar
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
   ... # removed other config
</code></pre>

Pushpendra
<p>I'm going to assume that you're not using the cass-operator in <a href="https://dtsx.io/3znMa05" rel="nofollow noreferrer">K8ssandra</a> since the <code>CassandraDatacenter</code> in cass-operator owns the StatefulSets.</p>
<p>You don't need to create a StatefulSet for each logical Cassandra rack. It should be able to schedule pods in different availability zones.</p>
<p>But I would suggest creating a different StatefulSet for each logical Cassandra DC so you can control how the pods get scheduled across racks/zones. Cheers!</p>
