Magento Search: Could not validate a connection to elasticsearch. No alive nodes found in your cluster. Magento Setup Upgrade Error.

How to fix Magento Could not validate a connection to elastic search. No alive nodes found in your cluster Magento 2?

I have faced an error while working with Magento locally. I used Warden to run local Magento. I got an error while running the command from CLI,

bin/magento setup:upgrade

Error on the command line:

Could not validate a connection to Elasticsearch. No alive nodes found in your cluster.

Based on the error, I have checked my elastic search configuration in the app/etc/env.php file, I have managed the catalog search configuration from the env.php file,

'catalog' => [
    'search' => [
        'engine' => 'elasticsearch7',
        'elasticsearch7_server_hostname' => 'elasticsearch',
        'elasticsearch7_server_port' => '9200',
        'elasticsearch7_index_prefix' => 'magento2',
        'elasticsearch7_enable_auth' => '0',
        'elasticsearch7_server_timeout' => '15'
    ]
]

If you haven’t configured the elastic search setting in env.php you can check it from the Admin panel.

Stores -> Configuration -> Catalog -> Catalog Search

Click on the button Test Connection and you will get the result of whether your elastic search is working or not.

You have still an issue while running the setup upgrade command, You need to reset your elastic search volume on docker.

First Stop your docker/warden project,

warden env down

You need to remove the elastic search volume from the docker. If you do not know the elastic search volume you can find it by running the command,

docker volume ls 
local     magento243_bashhistory
local     magento243_dbdata
local     magento243_esdata
local     magento243_rabbitmq
local     magento243_redis
local     magento243_sshdirectory

You can see the result that has words like esdata (magento243_esdata) in it.

Now remove the volume by running the given command,

docker volume rm <elastic-search-volume-name> (Example, docker volume rm magento243_esdata)

Again start the warden/docker project (warden env up) and try to run the setup upgrade command working for you.

I have followed the above steps to fix the issue for elastic search on the Magento development machine.