# CentOS中怎么使用Elasticsearch搭建可視化服務
## 前言
Elasticsearch作為一款強大的分布式搜索和分析引擎,在企業級數據檢索、日志分析等場景中廣泛應用。但原生Elasticsearch僅提供REST API接口,直接操作不夠直觀。本文將詳細介紹在CentOS系統中部署Elasticsearch并集成Kibana可視化平臺的完整流程,包含性能優化和安全配置建議。
---
## 一、環境準備
### 1.1 系統要求
- **操作系統**:CentOS 7/8(本文以CentOS 7.9為例)
- **硬件配置**:
- 最低2核CPU,4GB內存(生產環境建議8GB+)
- 磁盤空間根據數據量預估(SSD推薦)
- **網絡要求**:
- 開放9200(ES)、5601(Kibana)端口
```bash
firewall-cmd --permanent --add-port={9200,5601}/tcp
firewall-cmd --reload
Elasticsearch依賴Java運行環境:
# 安裝OpenJDK 11
yum install -y java-11-openjdk
# 驗證安裝
java -version
# 導入GPG密鑰
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 創建repo文件
cat <<EOF > /etc/yum.repos.d/elasticsearch.repo
[elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
# 安裝Elasticsearch
yum install -y elasticsearch
編輯/etc/elasticsearch/elasticsearch.yml
:
cluster.name: my-es-cluster
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node # 單節點模式
xpack.security.enabled: true # 啟用安全功能
# 設置開機自啟
systemctl daemon-reload
systemctl enable elasticsearch
# 啟動服務
systemctl start elasticsearch
# 檢查狀態
curl -XGET "http://localhost:9200/_cluster/health?pretty" -u elastic
首次運行會輸出默認密碼,請妥善保存。
yum install -y kibana
編輯/etc/kibana/kibana.yml
:
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
elasticsearch.username: "kibana_system"
elasticsearch.password: "自動生成的密碼"
i18n.locale: "zh-CN" # 中文界面
# 生成Kibana系統用戶密碼
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u kibana_system
# 啟動服務
systemctl enable kibana
systemctl start kibana
生成自簽名證書:
mkdir /etc/elasticsearch/certs
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/elasticsearch/certs/elasticsearch.key \
-out /etc/elasticsearch/certs/elasticsearch.crt
修改ES配置:
xpack.security.http.ssl:
enabled: true
keystore.path: certs/elasticsearch.crt
keystore.password: ""
通過Kibana界面操作:
1. 訪問http://<服務器IP>:5601
2. 進入”Stack Management” > “安全” > “角色”
3. 創建只讀角色示例:
- 集群權限:monitor
- 索引權限:read
, view_index_metadata
# 下載航班數據樣本
wget https://download.elastic.co/demos/kibana/gettingstarted/flights.json
# 導入ES(需認證)
curl -XPOST "http://localhost:9200/flights/_bulk?pretty" \
-H "Content-Type: application/json" \
--data-binary @flights.json \
-u elastic:<password>
創建索引模式:
flights*
作為模式名稱構建可視化圖表:
編輯/etc/elasticsearch/jvm.options
:
-Xms4g # 最小堆內存
-Xmx4g # 最大堆內存(不超過物理內存50%)
# 創建索引時指定分片
PUT /logs-2023.08.01
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
indices.queries.cache.size: 10%
indices.fielddata.cache.size: 30%
max virtual memory areas vm.max_map_count [65530] is too low
sysctl -w vm.max_map_count=262144
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
curl -XGET localhost:9200
journalctl -u kibana -f
通過本文的詳細指導,您已在CentOS系統上成功搭建了Elasticsearch搜索服務與Kibana可視化平臺。建議進一步探索: - 集成Logstash構建ELK日志系統 - 使用APM實現應用性能監控 - 通過Watcher功能設置告警機制
注意事項:生產環境務必配置完備的安全策略,包括定期密碼輪換、網絡ACL限制等。官方文檔是獲取最新信息的最佳途徑:Elastic官方文檔 “`
該文檔包含以下特點: 1. 結構化分步指南,適合不同基礎用戶 2. 包含安全配置、性能優化等進階內容 3. 提供實際可執行的命令和配置片段 4. 中英文混合的關鍵術語標注 5. 典型問題排查方案 6. 擴展學習建議 7. 符合Markdown語法規范
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。