# How to determine which node is the master (without using \_cat endpoint) in an application

**URL:** <https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672>\
**Category:** Elasticsearch\
**Created:** [March 4, 2024, 8:52pm UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672 "2024-03-04T20:52:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![buitcj](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@buitcj](https://discuss.elastic.co/u/buitcj)\
**Post date:** [March 4, 2024, 8:52pm UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672/1 "2024-03-04T20:52:31Z")

</div>

I am trying to determine if a node is the master elasticsearch node so that I can determine if my script should run. My script creates a user using the elasticsearch-cli binary and I want to ensure that this happens a single time.

\_cat/master and \_cat/nodes endpoints are not for application consumption (see important note at the top of [cat master API | Elasticsearch Guide [8.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-master.html))

Chatgpt says that using `curl -XGET 'http://localhost:9200/_cluster/health?pretty'` is another option but only if you know the total number of primary shards your cluster should have.

Is there an easy way to determine the master that is supported for applications?

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [March 4, 2024, 9:46pm UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672/2 "2024-03-04T21:46:44Z")

</div>

> [@buitcj](#):
>
> My script creates a user using the elasticsearch-cli binary and I want to ensure that this happens a single time.

Which cli binary? The `elasticsearch-users` ? Whic command are you running?

You can make requests to Elasticsearch on any node, there is no reason to just talk with the master.

> [@buitcj](#):
>
> Chatgpt says that using `curl -XGET 'http://localhost:9200/_cluster/health?pretty'` is another option but only if you know the total number of primary shards your cluster should have.

Regarding Elasticsearch ChatGPT is almost always wrong, you need to always check the documentation.

To get the current master you can use the `_nodes` API as explained in the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html).

Making a GET request to `_nodes/_master` will return the elected master node.

---

<div class="post-metadata">

**Author:** ![buitcj](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@buitcj](https://discuss.elastic.co/u/buitcj)\
**Post date:** [March 4, 2024, 11:28pm UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672/3 "2024-03-04T23:28:59Z")

</div>

> Which cli binary? The `elasticsearch-users` ? Whic command are you running?

Yes, `elasticsearch-users useradd ...`

> You can make requests to Elasticsearch on any node, there is no reason to just talk with the master.

I want there to be exactly one user created. Is it ok if I have three nodes all calling `elasticsearch useradd A` where A is the single user that I want? There won't be any race condition? I assume n-1 of the nodes will fail saying that the user already exists, which would be ok, so long as there's no race condition issue.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [March 5, 2024, 12:26am UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672/4 "2024-03-05T00:26:42Z")

</div>

> [@buitcj](#):
>
> I want there to be exactly one user created. Is it ok if I have three nodes all calling `elasticsearch useradd A` where A is the single user that I want?

To create a user you need to run the command **once** , it doesn't matter in which node you run it, assuming that you are not using a _file_ based realm.

It is not clear why you would have three node running this command as you didn't provide any context of what you are trying to achieve.

If you are trying to automate the creation of the users you need to automate this step based on the status of the cluster, regardless of how many nodes it have.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [March 5, 2024, 2:39am UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672/5 "2024-03-05T02:39:59Z")

</div>

> [@buitcj](#):
>
> Is it ok if I have three nodes all calling `elasticsearch useradd A`

Ok, now I understood.

`elasticsearch-users useradd` works using the [file based](https://www.elastic.co/guide/en/elasticsearch/reference/current/file-realm.html#file-realm) realm, so it will add the user on the **local** node, this means that the user will only work on the node where the command was executed.

> [@buitcj](#):
>
> I assume n-1 of the nodes will fail saying that the user already exists, which would be ok, so long as there's no race condition issue.

Not in this case, as the useradd command adds the user on the **local** node, if you want this change to be replicated to other nodes you need to do it yourself.

The [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/file-realm.html#file-realm) about the file based realm has this important note in the beginning:

> As the administrator of the cluster, it is your responsibility to ensure the same users are defined on every node in the cluster. The Elastic Stack security features do not deliver any mechanism to guarantee this.

Maybe what you want is the _native realm_ where the users are created and stored in the cluster? But for this you need to use the [Users API](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-put-user.html), not the CLI.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 2, 2024, 2:40am UTC](https://discuss.elastic.co/t/how-to-determine-which-node-is-the-master-without-using-cat-endpoint-in-an-application/354672/6 "2024-04-02T02:40:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
