环境准备
- 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)