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.
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?
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.
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.
elasticsearch-users useradd works using the file based 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.
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 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, not the CLI.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.