elasticsearch安装与使用(1)-使用docker安装Elasticsearch

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 Elasticsearch

client =  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           # 停止当前运行的容器

参考

  • 16
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值