<p>I'm trying to deploy <strong>scalable</strong> mariadb galera cluster in kubernetes or docker swarm. Since each pod or containers needs its own galera config, how should i create my deployment so i could be able to scale it without any manual work? I think we can't use ConfigMap cause for a 10 node cluster there have to be 10 configmaps!</p>

<p>Example of mariadb galera config of a node:</p>

<pre><code>wsrep_cluster_address="gcomm://ip_1,ip_2,ip_3"    
wsrep_node_address="ip_1"    
wsrep_node_name="node_1"
wsrep_cluster_name="mariadb-cluster"
</code></pre>

<p>For such applications which have different config for each node, what is the best way of deployment?</p>

<p>Note: I can create pods/containers and do the config my self (join new nodes to the cluster) but i think this isn't right way and i need it to be auto scalable.</p>

<p>You almost definitely want to use a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> to deploy this in Kubernetes.  Among other things, this has the property that each Pod will get its own PersistentVolumeClaim for storage, and that the names of individual Pods are predictable and sequential.  You should <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="nofollow noreferrer">create a matching headless Service</a> and then each Pod will have a matching DNS name.</p>

<p>That solves a couple of parts of the riddle:</p>

<pre><code># You pick this
wsrep_cluster_name="mariadb-cluster"

# You know what all of these DNS names will be up front
wsrep_cluster_address="gcomm://galera-0.galera.default.svc.cluster.local,...,galera-9.galera.default.svc.cluster.local"
</code></pre>

<p>For <code>wsrep_node_name</code>, the <a href="https://mariadb.com/kb/en/library/galera-cluster-system-variables/#wsrep_node_name" rel="nofollow noreferrer">MariaDB documentation</a> indicates that it defaults to the host name.  In Kubernetes, the host name defaults to the pod name, and the pod name is one of the sequential <code>galera-n</code> for pods managed by a StatefulSet, so you don't need to manually set this.</p>

<p><code>wsrep_node_address</code> is trickier.  Here <a href="https://mariadb.com/kb/en/library/galera-cluster-system-variables/#wsrep_node_address" rel="nofollow noreferrer">the documentation</a> indicates that there are heuristics to guess it (with a specific caveat that it might not be reliable for containers).  You can't know an individual pod's IP address before it's created.  You can in principle use the <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables" rel="nofollow noreferrer">downward API</a> to inject a pod's IP address into an environment variable.  I'd start by hoping the heuristics would guess the pod IP address and this works well enough (it is what the headless Service would ultimately resolve to).</p>

<p>That leaves you with the block above in the ConfigMap, and it's global across all of the replicas.  The other remaining per-Galera-node values should be automatically guessable.</p>
