写一个shell 脚本案例,关于Elasticsearch搜索引擎部署,案例需具备功能性,并且与实际场景具有关联性。
时间: 2024-11-05 19:27:40 浏览: 11
在Shell脚本中,我们可以创建一个简单的例子来演示如何部署Elasticsearch集群。请注意,这只是一个基本示例,实际生产环境部署可能会更复杂,涉及到更多的配置和错误检查。这里假设您已经安装了`curl`, `wget`, 和`sudo`。
```bash
#!/bin/bash
# 检查是否已安装 Elasticsearch
if ! command -v elasticsearch &> /dev/null; then
echo "Elasticsearch is not installed. Installing..."
sudo yum install -y elasticsearch # 对于基于RPM的系统如CentOS
# 或者
sudo apt-get install -y elasticsearch # 对于基于Debian的系统如Ubuntu
fi
# 配置Elasticsearch
echo "
cluster.name: my-elasticsearch-cluster
node.name: node-1
" > /etc/elasticsearch/elasticsearch.yml
# 启动Elasticsearch服务并设置开机自启动
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
# 确认Elasticsearch正在运行
if ! curl -s http://localhost:9200/_cat/nodes | grep 'green'; then
echo "Elasticsearch is not responding. Check logs for more details."
else
echo "Elasticsearch cluster started successfully."
echo "You can now access the web UI at http://localhost:9200/_plugin/kibana/"
fi
# 创建索引和文档示例 (仅作为示例)
echo '{"index": {"_id": "my-index"}}' | curl -XPUT http://localhost:9200/my-index
阅读全文