<p>I'm trying to automate the process of horizontal scale up and scale down of elasticsearch nodes in kubernetes cluster.</p>

<p>Initially, I deployed an elasticsearch cluster (3 master, 3 data &amp; 3 ingest nodes) on a Kubernetes cluster. Where, <code>cluster.initial_master_nodes</code> was:</p>

<pre class="lang-yaml prettyprint-override"><code>cluster.initial_master_nodes:
  - master-a
  - master-b
  - master-c
</code></pre>

<p>Then, I performed scale down operation, reduced the number of master node 3 to 1 (unexpected, but for testing purpose). While doing this, I deleted <code>master-c</code>, <code>master-b</code> nodes and restarted <code>master-a</code> node with the following setting:</p>

<pre class="lang-yaml prettyprint-override"><code>cluster.initial_master_nodes:
  - master-a
</code></pre>

<p>Since the elasticsearch nodes (i.e. pods) use persistant volume, after restarting the node, the <code>master-a</code> slowing the following logs:</p>

<pre><code>"message": "master not discovered or elected yet, an election requires at least 2 nodes with ids from [TxdOAdryQ8GAeirXQHQL-g, VmtilfRIT6KDVv1R6MHGlw, KAJclUD2SM6rt9PxCGACSA], have discovered [] which is not a quorum; discovery will continue using [] from hosts providers and [{master-a}{VmtilfRIT6KDVv1R6MHGlw}{g29haPBLRha89dZJmclkrg}{10.244.0.95}{10.244.0.95:9300}{ml.machine_memory=12447109120, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 5, last-accepted version 40 in term 5"  }
</code></pre>

<p>Seems like it's trying to find <code>master-b</code> and <code>master-c</code>.</p>

<p>Questions:</p>

<ul>
<li>How to overwrite cluster settings so that <code>master-a</code> won't search for these deleted nodes?</li>
</ul>

<p>The <code>cluster.initial_master_nodes</code> setting only has an effect the first time the cluster starts up, but to avoid some very rare corner cases you should never change its value once you've set it and generally you should remove it from the config file as soon as possible. From <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/discovery-settings.html#initial_master_nodes" rel="noreferrer">the reference manual</a> regarding <code>cluster.initial_master_nodes</code>:</p>

<blockquote>
  <p>You should not use this setting when restarting a cluster or adding a new node to an existing cluster.</p>
</blockquote>

<p>Aside from that, Elasticsearch uses a <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery-quorums.html" rel="noreferrer">quorum-based election protocol</a> and says the following:</p>

<blockquote>
  <p>To be sure that the cluster remains available you <strong>must not stop half or more of the nodes in the voting configuration at the same time</strong>. </p>
</blockquote>

<p>You have stopped two of your three master-eligible nodes at the same time, which is more than half of them, so it's expected that the cluster no longer works.</p>

<p>The reference manual also contains <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery-adding-removing-nodes.html#modules-discovery-removing-nodes" rel="noreferrer">instructions for removing master-eligible nodes</a> which you have not followed:</p>

<blockquote>
  <p>As long as there are at least three master-eligible nodes in the cluster, as a general rule it is best to remove nodes one-at-a-time, allowing enough time for the cluster to automatically adjust the voting configuration and adapt the fault tolerance level to the new set of nodes.</p>
  
  <p>If there are only two master-eligible nodes remaining then neither node can be safely removed since both are required to reliably make progress. To remove one of these nodes you must first inform Elasticsearch that it should not be part of the voting configuration, and that the voting power should instead be given to the other node.</p>
</blockquote>

<p>It goes on to describe how to safely remove the unwanted nodes from the voting configuration using <code>POST /_cluster/voting_config_exclusions/node_name</code> when scaling down to a single node.</p>
