日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

快速搭建 ElasticSearch學習環境

作者:Devin_S 更新時間: 2022-09-26 編程語言

環境準備

  • ubuntu-22
  • docker 安裝

curl -sSL https://get.daocloud.io/docker | sh

  • docker-compose 安裝

sudo apt install docker-compose

  • 開始拉取鏡像

docker pull elasticsearch:7.17.1

拉取鏡像時間可能比較漫長,可以配置鏡像加速

單節點搭建

創建目錄

mkdir -p ./node/data ./node/conf
vim ./node/conf/elasticsearch.yml
# 配置文件elasticsearch.yml

配置文件elasticsearch.yml 寫入一下配置

cluster.name: "docker-cluster"
#節點的名稱
node.name: node
#此節點是否可以用作master節點
node.master: true
#此節點是否是存儲節點
node.data: true
#此節點是否是預處理節點 如果是master節點的話 建議這里是true
node.ingest: true
#設置主機IP 0.0.0.0時外網可以訪問到
network.host: 0.0.0.0
# 如果是虛擬機內安裝,建議配置虛擬機IP,主機可以訪問到
network.publish_host: 127.0.0.1 
# 配置端口
http.port: 9200
#cross 跨域訪問 配置這個之后 head就可以用了
http.cors.enabled:  true
http.cors.allow-origin:  "*"
path.logs: /usr/share/elasticsearch/logs

創建 docker-compose.yml

創建 docker-compose.yml 寫入一下內容

version: '3'
services:
  elasticsearch:
    image:  elasticsearch:7.17.1
    container_name: elasticsearch
    restart: always
    volumes:
      - ./node/data:/usr/share/elasticsearch/data:rw
      - ./node/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ./node/logs:/user/share/elasticsearch/logs:rw
    ports:
        - "9200:9200"
        - "9300:9300"
    environment:
        - "ES_JAVA_OPTS=-Xmx512m -Xmx512m"
        - discovery.type=single-node
        - TZ=Asia/Shanghai

docker-compose up 啟動ES

多節點搭建ES

創建目錄


mkdir -p /opt/elasticsearch/node-1/config /opt/elasticsearch/node-1/data
mkdir -p /opt/elasticsearch/node-2/config /opt/elasticsearch/node-2/data
mkdir -p /opt/elasticsearch/node-3/config /opt/elasticsearch/node-3/data
# 需要給data 目錄寫權限
chmod 777 node-1/data node-2/data node-3/data

創建elasticsearch.yml

vim /opt/elasticsearch/node-1/config/elasticsearch.yml

#寫入一下內容
# 集群的名稱)
cluster.name: "docker-cluster"
#節點的名稱
node.name: node-1
#此節點是否可以用作master節點
node.master: true
#此節點是否是存儲節點
node.data: true
#此節點是否是預處理節點
node.ingest: true
#設置主機IP 0.0.0.0時外網可以訪問到
network.host: 0.0.0.0
#publish_host 建議設置
network.publish_host: 127.0.0.1
# 配置端口
http.port: 9201
# 集群通信端口
transport.port: 9301
#集群內節點信息 每個節點會共享自己的此參數
discovery.seed_hosts: ["es1:9301","es2:9302","es3:9303"]
#集群的master候選節點目錄。只有在初始化的時候才生效。
#這里只寫node-1 并且配置這個參數 是用于快速搭建集群。集群已啟動自動node-1   是master
cluster.initial_master_nodes: ["node-1"]
#cross 跨域訪問 配置這個之后 head就可以用了
http.cors.enabled:  true
http.cors.allow-origin:  "*"

node-2,node-3對應的 elasticsearch.yml 與node-1基本一致,修改node.name http.port transport.port 與編號對應即可

創建 docker-compose.yml

vim docker-compose.yml

version: "3"
services:
  es1:
    image: elasticsearch:7.17.1
    container_name: es1
    environment:
      - "ES_JAVA_OPTS=-Xmx512m -Xmx512m"
    ports:
      - "9201:9201"
      - "9301:9301"
    volumes:
      - /opt/elasticsearch/node-1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /opt/elasticsearch/node-1/data:/usr/share/elasticsearch/data
    networks: 
      - es_net
      
  es2:
    image: elasticsearch:7.17.1
    container_name: es2
    environment:
      - "ES_JAVA_OPTS=-Xmx512m -Xmx512m"
    ports:
      - "9202:9202"
      - "9302:9302"
    volumes:
      - /opt/elasticsearch/node-2/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /opt/elasticsearch/node-2/data:/usr/share/elasticsearch/data
    networks: 
      - es_net
      
  es3:
    image: elasticsearch:7.17.1
    container_name: es3
    environment:
      - "ES_JAVA_OPTS=-Xmx512m -Xmx512m"
    ports:
      - "9203:9203"
      - "9303:9303"
    volumes:
      - /opt/elasticsearch/node-3/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /opt/elasticsearch/node-3/data:/usr/share/elasticsearch/data
    networks: 
      - es_net

networks:
  es_net:

連接測試

var ctx = context.Background()
var esUrl = "http://192.168.72.128:9201/"

client, err := elastic.NewClient(
		elastic.SetURL(esUrl),
	)
	if err != nil {
		log.Fatal("es 連接失敗:", err)
	}
	info, code, err := client.Ping(esUrl).Do(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println("Elasticsearch returned with code>: %d and version %s\n", code, info.Version.Number)

原文鏈接:https://blog.csdn.net/Devin_S/article/details/127042060

欄目分類
最近更新