diff --git a/cmd/internal/storage/elasticsearch/elasticsearch.go b/cmd/internal/storage/elasticsearch/elasticsearch.go index 40871a0e94..2b1ed1d043 100644 --- a/cmd/internal/storage/elasticsearch/elasticsearch.go +++ b/cmd/internal/storage/elasticsearch/elasticsearch.go @@ -18,6 +18,7 @@ import ( "flag" "fmt" "os" + "strings" "sync" "time" @@ -125,6 +126,10 @@ func newStorage( elasticHost string, enableSniffer bool, ) (storage.StorageDriver, error) { + // Remove all spaces to help user to configure + elasticHost = strings.ReplaceAll(elasticHost, " ", "") + hosts := strings.Split(elasticHost, ",") + // Obtain a client and connect to the default Elasticsearch installation // on 127.0.0.1:9200. Of course you can configure your client to connect // to other hosts and configure it in various other ways. @@ -132,7 +137,7 @@ func newStorage( elastic.SetHealthcheck(true), elastic.SetSniff(enableSniffer), elastic.SetHealthcheckInterval(30*time.Second), - elastic.SetURL(elasticHost), + elastic.SetURL(hosts...), ) if err != nil { // Handle error @@ -140,13 +145,20 @@ func newStorage( } // Ping the Elasticsearch server to get e.g. the version number - info, code, err := client.Ping().URL(elasticHost).Do() - if err != nil { - // Handle error - return nil, fmt.Errorf("failed to ping the elasticsearch - %s", err) - + // Just ping anyone of hosts successfully will be ok + var res *elastic.PingResult + var code int + for _, host := range hosts { + res, code, err = client.Ping().URL(host).Do() + if err == nil { + break + } + fmt.Printf("ping host %s failed, code: %d, err: %s", host, code, err) + } + if res == nil { + return nil, fmt.Errorf("failed to ping any host of the elasticsearch") } - fmt.Printf("Elasticsearch returned with code %d and version %s", code, info.Version.Number) + fmt.Printf("Elasticsearch returned with code %d and version %s", code, res.Version.Number) ret := &elasticStorage{ client: client, diff --git a/docs/storage/elasticsearch.md b/docs/storage/elasticsearch.md index fa679cf497..1145285a91 100644 --- a/docs/storage/elasticsearch.md +++ b/docs/storage/elasticsearch.md @@ -12,6 +12,8 @@ Specify ES host address: ``` -storage_driver_es_host="http://elasticsearch:9200" + # If you has several hosts, just use comma to separate it. + -storage_driver_es_host="http://elasticsearch1:9200,http://elasticsearch2:9200,http://elasticsearch3:9200" ``` There are also optional flags: