ES的優點:
- 1、分布式準實時
- 2、提供REST風格的API接口,是用戶可解借助任何語言使用https對ES執行請求來完成搜索任務;
- 3、提供聚合功能
1、Elasticsearch安裝
docker network create elastic
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.3.3
docker run --name es-node01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.3.3
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-> Elasticsearch security features have been automatically configured!
-> Authentication is enabled and cluster connections are encrypted.-> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):N-sf6R*O0Ur344otTfzc-> HTTP CA certificate SHA-256 fingerprint:bfd8e24f5c41dcd170aadb0f8dbae3fe27d99633738f2d9c99dd456955523a5d-> Configure Kibana to use this cluster:
* Run Kibana and click the configuration link in the terminal when Kibana starts.
* Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):eyJ2ZXIiOiI4LjMuMyIsImFkciI6WyIxNzIuMTguMC4yOjkyMDAiXSwiZmdyIjoiYmZkOGUyNGY1YzQxZGNkMTcwYWFkYjBmOGRiYWUzZmUyN2Q5OTYzMzczOGYyZDljOTlkZDQ1Njk1NTUyM2E1ZCIsImtleSI6InQxQUQ1SThCaWVkSFVsc3hFT3dlOnNoc1ZLVkl0UzB1R090S3EzUFotLXcifQ==-> Configure other nodes to join this cluster:
* Copy the following enrollment token and start new Elasticsearch nodes with `bin/elasticsearch --enrollment-token <token>` (valid for the next 30 minutes):eyJ2ZXIiOiI4LjMuMyIsImFkciI6WyIxNzIuMTguMC4yOjkyMDAiXSwiZmdyIjoiYmZkOGUyNGY1YzQxZGNkMTcwYWFkYjBmOGRiYWUzZmUyN2Q5OTYzMzczOGYyZDljOTlkZDQ1Njk1NTUyM2E1ZCIsImtleSI6InRWQUQ1SThCaWVkSFVsc3hELXp4OkJJSGx2YjQtU2pDWVBOVi11Y0VPVWcifQ==If you're running in Docker, copy the enrollment token and run:`docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.3.3`
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2、Kibana安裝及運行
通過Kibana可以可視化的管理es數據庫里的數據。
docker pull docker.elastic.co/kibana/kibana:8.3.3
docker run --name kib-01 --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.3.3
用戶名:elastic
密碼:N-sf6R*O0Ur344otTfzc
http://0.0.0.0:5601/app/home#/
http://localhost:5601
3、curl操作
注意證書,證書從容器中復制出來后,我存放在/Users/sunwenjun/data/elastic8/http_ca.crt
注意用戶名密碼最好加上雙引號。
The issue with zsh: no matches found: elastic:N-sf6R*O0Ur344otTfzc in your curl command is due to the * character being interpreted as a wildcard. To resolve this, you need to ensure the password is treated as a literal string. You can achieve this by quoting the password.
docker cp es-node01:/usr/share/elasticsearch/config/certs/http_ca.crt .
curl --cacert /Users/sunwenjun/data/elastic8/http_ca.crt -u "elastic:N-sf6R*O0Ur344otTfzc" https://localhost:9200/
4、Dev Tools可視化界面操作
5、python操作
pip install elasticsearch
from datetime import datetime
from elasticsearch import Elasticsearchclient = Elasticsearch(hosts=['https://localhost:9200'], # 服務地址與端口basic_auth=("elastic", "N-sf6R*O0Ur344otTfzc"), # 用戶名,密碼ca_certs="/Users/sunwenjun/data/elastic8/http_ca.crt" # 證書
)doc = {'author': 'author_name','text': 'Interesting content...','timestamp': datetime.now(),
}
resp = client.index(index="test-index", id=1, document=doc)
print(resp['result']) # created
6、關閉與啟動容器
docker start 容器id # 啟動容器
docker stop 容器id # 停止當前運行的容器
參考
- Elasticsearch:關于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x
- Elastic:使用 Docker 安裝 Elastic Stack 8.x 并開始使用
- 4.ELK之Elasticsearch常用curl命令
- Elasticsearch官網
- Elasticsearch官網例子
- kibana查看es存儲數據 kibana操作es數據