环境准备
curl -sSL https://get.daocloud.io/docker | sh
sudo apt install docker-compose
docker pull elasticsearch:7.17.1
拉取镜像时间可能比较漫长,可以配置镜像加速
单节点搭建
创建目录
mkdir -p ./node/data ./node/conf
vim ./node/conf/elasticsearch.yml
配置文件elasticsearch.yml 写入一下配置
cluster.name: "docker-cluster"
node.name: node
node.master: true
node.data: true
node.ingest: true
network.host: 0.0.0.0
network.publish_host: 127.0.0.1
http.port: 9200
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
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
node.master: true
node.data: true
node.ingest: true
network.host: 0.0.0.0
network.publish_host: 127.0.0.1
http.port: 9201
transport.port: 9301
discovery.seed_hosts: ["es1:9301","es2:9302","es3:9303"]
cluster.initial_master_nodes: ["node-1"]
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)