an approach to handle multi node elastic search - unix1998/technical_notes GitHub Wiki

When using curl, you can't directly query or change settings on multiple nodes simultaneously in a single command. However, you can use a script to iterate over multiple node endpoints and execute the same curl command on each node. This approach can be useful for querying or updating settings across all nodes in your Elasticsearch cluster.

Using a Shell Script to Iterate Over Nodes

Here's an example of how you can achieve this using a shell script:

#!/bin/bash

# List of node endpoints
nodes=("node1_ip:9200" "node2_ip:9200" "node3_ip:9200")

# The API endpoint you want to query or change
api_endpoint="/_cluster/settings"
http_method="PUT"  # Change to GET, POST, DELETE as needed
content_type="Content-Type: application/json"
data='{
  "persistent": {
    "cluster.routing.allocation.enable": "all"
  }
}'

# Iterate over each node and execute the curl command
for node in "${nodes[@]}"; do
  echo "Querying $node"
  curl -X $http_method "http://$node$api_endpoint" -H "$content_type" -d "$data"
done

Explanation:

  1. Define Node Endpoints: You specify the IP addresses or hostnames of the nodes in the nodes array.
  2. Set API Endpoint and Method: Define the API endpoint you want to interact with (/api_endpoint), the HTTP method (PUT in this case), and any necessary headers (Content-Type: application/json).
  3. Data to Send: The data variable contains the JSON payload you want to send with the request.
  4. Iterate and Execute: The script iterates over each node in the nodes array and executes the curl command for each one.

Example Usage

Query Cluster Health Across All Nodes

#!/bin/bash

nodes=("node1_ip:9200" "node2_ip:9200" "node3_ip:9200")
api_endpoint="/_cluster/health"
http_method="GET"

for node in "${nodes[@]}"; do
  echo "Querying $node"
  curl -X $http_method "http://$node$api_endpoint?pretty"
done

Update Cluster Settings Across All Nodes

#!/bin/bash

nodes=("node1_ip:9200" "node2_ip:9200" "node3_ip:9200")
api_endpoint="/_cluster/settings"
http_method="PUT"
content_type="Content-Type: application/json"
data='{
  "persistent": {
    "cluster.routing.allocation.enable": "all"
  }
}'

for node in "${nodes[@]}"; do
  echo "Updating settings on $node"
  curl -X $http_method "http://$node$api_endpoint" -H "$content_type" -d "$data"
done

Advanced Example with HTTPS and Authentication

If your Elasticsearch cluster is secured with HTTPS and requires basic authentication, you can modify the script as follows:

#!/bin/bash

nodes=("node1_ip:9200" "node2_ip:9200" "node3_ip:9200")
api_endpoint="/_cluster/settings"
http_method="PUT"
content_type="Content-Type: application/json"
data='{
  "persistent": {
    "cluster.routing.allocation.enable": "all"
  }
}'
username="your_username"
password="your_password"

for node in "${nodes[@]}"; do
  echo "Updating settings on $node"
  curl -u $username:$password -X $http_method "https://$node$api_endpoint" -H "$content_type" -d "$data"
done

This script can be saved as a .sh file and executed from the command line to perform operations across all specified nodes.

Conclusion

While you can't directly use curl to interact with multiple nodes in a single command, you can easily achieve the desired effect using a shell script to iterate over each node and execute the necessary curl commands. This method provides flexibility and allows you to automate the process of querying or updating settings across all nodes in your Elasticsearch cluster.