程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Elasticsearch 8.2: multi node cluster building and python connection

編輯:Python

Elasticsearch build

There are currently two servers ,ip Respectively 192.168.171.81 and 192.168.171.25, We need to build a es colony .

First download elasticsearch-8.2.0-linux-x86_64.tar.gz, Copy to these two servers respectively , unpack .

It should be noted that , We need to start Elasticsearch Before service , You need to configure elasticsearch.yml.

Modify on the first server config/elasticsearch.yml, The contents are as follows :

network.host: 192.168.171.81
transport.host: 192.168.171.81
cluster.name: my_cluster # Don't write , The default is elasticsearch
node.name: node1 # Don't write , The default is the current server name 
# node.roles: [ data, master ] # Don't write , The default is cdfhilmrstw

The list of roles is as follows :

Then modify the file config/jvm.options, The contents are as follows :

-Xms4g
-Xmx4g

Start the first server es:./bin/elasticsearch
The following , Indicating successful startup :

The default password is a little hard to remember , We change the password :
./bin/elasticsearch/bin/elasticsearch-reset-password -u elastic -i

Verify success :
curl --insecure -u elastic:123456 -XGET "https://192.168.171.81:9200/", among 123456 It's the password I just set , Return as follows :

This indicates that the node is started normally .

Then check the cluster nodes :curl --insecure -u elastic:123456 -XGET "https://192.168.171.81:9200/_cat/nodes"

You can see that there is only one node at present .

At present elasticsearch.yml The file is automatically updated :

If you want to add a new server , Need to generate a token, With this token A new node can be automatically added to the cluster ,./bin/elasticsearch-create-enrollment-token -s node

Next, configure the second server . It is the same as the first one , modify ./config/elasticsearch.yml

network.host: 192.168.171.25
transport.host: 192.168.171.25
cluster.name: my_cluster
node.name: node2
node.roles: [ data, master ] # Don't write , The default is cdfhilmrstw

modify config/jvm.options:

-Xms4g
-Xmx4g

And then we'll talk about adding... To this node es In the cluster :./bin/elasticsearch --enrollment-token eyJ2ZXIiOiI4LjIuMCIsImFkciI6WyIxOTIuMTY4LjE3MS44MTo5MjAwIl0sImZnciI6Ijk5MGI4NDllNGVkNDk0NmEwYzAzZDdkZDk4YzQwMTg0MzllNGQ5M2E0NDExNjM5MTAxY2QwOTdkZGEzNmI5NTkiLCJrZXkiOiI0dVVjQUlFQktyU0dWdy1YSXlfVTpLYjBWaW1zZ1FkZTdxQ3dHamZJYUNnIn0=

And then display on the first node terminal :

Check node2 The state of :
curl --insecure -u elastic:123456 -XGET "https:/192.168.171.25:9200/"

You can see node2 And successfully started .

see node2 Configuration file for elasticsearch.yml:

Instruction use enrollment-token It can be configured automatically . Not next time .

Check the cluster nodes :

You can see that there are two nodes in the cluster .

Python Connect

install pip install elasticsearch

Check version :pip list | grep elasticsearch

elasticsearch 8.2.0

take es Nodes in the config/cert/http_ca.crt Copy to local .

from elasticsearch import Elasticsearch
es = Elasticsearch('https://elastic:[email protected]:9200',
ca_certs = './http_ca.crt',
verify_certs=True)
data = {

"name": " Extinction teacher ",
"age": "49",
"sex": "f",
"address": " Mount Emei ",
"sect": " Emei ",
"skill": " net ",
"power": "70",
"create_time": "2022-05-08 23:16:53",
"modify_time": "2022-05-08 23:16:59"
}
resp = es.index(index="example_index", id=1, body=data)
print(resp)

You can return that the data was added successfully .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved