1.准备linux服务器,ip地址为192.168.137.116
2.安装docker-ce
yum -y install docker-ce
配置docker镜像
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://harbor:443", "https://985ecq8w.mirror.aliyuncs.com",
"https://docker.registry.cyou",
"https://docker-cf.registry.cyou",
"https://docker.jsdelivr.fyi",
"https://dockercf.jsdelivr.fyi",
"https://dockertest.jsdelivr.fyi",
"https://dockerpull.com",
"https://dockerproxy.cn",
"https://hub.uuuadc.top",
"https://docker.1panel.live",
"https://docker.anyhub.us.kg",
"https://docker.chenby.cn",
"https://dockerhub.jobcher.com",
"https://dockerhub.icu",
"https://docker.ckyl.me",
"https://docker.awsl9527.cn",
"https://docker.hpcloud.cloud",
"https://docker.m.daocloud.io"
],
"insecure-registries":["harbor:443"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
harbor安装什么的就不赘述了,docker pull能够正常使用就行
vim /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.137.30 harbor
192.168.137.116 ELK
启动docker
systemctl enable --now docker
3.docker pull镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.3.0
dcoker pull containerize/elastichd
推送到harbor
#登录及推送镜像到harbor
docker login harbor:443
docker tag docker.elastic.co/elasticsearch/elasticsearch:7.3.0 harbor:443/library/elasticsearch:7.3.0
docker push harbor:443/library/elasticsearch:7.3.0
docker tag containerize/elastichd:latest harbor:443/library/containerize/elastichd:latest
docker push harbor:443/library/containerize/elastichd:latest
[root@ELK ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.elastic.co/elasticsearch/elasticsearch 7.3.0 bdaab402b220 5 years ago 806MB
harbor:443/library/elasticsearch 7.3.0 bdaab402b220 5 years ago 806MB
containerize/elastichd latest c2202f76db37 7 years ago 28.1MB
harbor:443/library/containerize/elastichd latest c2202f76db37 7 years ago 28.1MB
4.创建一个网络
docker network create esnet
5.启动elasticsearch容器
docker run --name es -p 9200:9200 -p 9300:9300 --network esnet -e "discovery.type=single-node" bdaab402b220
验证是否成功启动
[root@ELK ~]# curl -X GET "http://localhost:9300/"
This is not ancurl -X GET "http://localhost:9200/"
{
"name" : "dbda370d76cd",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "pN-AU14rTSudsqG1yU4TkA",
"version" : {
"number" : "7.3.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "de777fa",
"build_date" : "2019-07-24T18:30:11.767338Z",
"build_snapshot" : false,
"lucene_version" : "8.1.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
6.启动可视化容器ElasticHD
docker run -p 9800:9800 -d --link es:demo --network esnet -e "discovery.type=single-node" containerize/elastichd
7.浏览器打开ElasticHD
8.使用goland开发go查询elasticsearch示例
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
)
func main() {
// 创建 Elasticsearch 客户端
_, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
// 设置 Elasticsearch URL
cfg := elasticsearch.Config{
Addresses: []string{"http://192.168.137.116:9200"},
}
esClient, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client with custom config: %s", err)
}
// 创建索引
indexName := "new_index"
if err := createIndex(esClient, indexName); err != nil {
log.Fatalf("Error creating index: %s", err)
}
// 插入一些数据
if err := insertData(esClient, indexName); err != nil {
log.Fatalf("Error inserting data: %s", err)
}
// 执行一个简单的搜索请求
res, err := search(esClient, indexName)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
// 检查响应状态
if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
} else {
// Print the response status and error information.
log.Fatalf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}
// 解析并打印搜索结果
var result map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
fmt.Printf("%s\n", result)
}
// createIndex 函数创建索引
func createIndex(client *elasticsearch.Client, indexName string) error {
// 构建创建索引的请求
req := esapi.IndicesCreateRequest{
Index: indexName,
Body: strings.NewReader(`
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"title": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
}`),
Pretty: true,
}
// 执行请求
res, err := req.Do(context.Background(), client)
if err != nil {
return err
}
defer res.Body.Close()
if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return err
} else {
// Print the response status and error information.
return fmt.Errorf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}
return nil
}
// insertData 函数插入数据
func insertData(client *elasticsearch.Client, indexName string) error {
// 构建插入数据的请求
req := esapi.IndexRequest{
Index: indexName,
DocumentID: "1",
Body: strings.NewReader(`{"title": "Example Title", "content": "Example Content"}`),
Refresh: "true",
}
// 执行请求
res, err := req.Do(context.Background(), client)
if err != nil {
return err
}
defer res.Body.Close()
if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return err
} else {
// Print the response status and error information.
return fmt.Errorf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}
return nil
}
// search 函数执行搜索请求
func search(client *elasticsearch.Client, indexName string) (*esapi.Response, error) {
// 构建搜索请求
q := `
{
"query": {
"match_all": {}
}
}`
// 发送搜索请求
req := esapi.SearchRequest{
Index: []string{indexName},
Body: strings.NewReader(q),
Pretty: true,
}
// 执行请求
res, err := req.Do(context.Background(), client)
if err != nil {
return nil, err
}
return res, nil
}
9.浏览器查看数据是否改变
10.创建elasticsearch集群
注意: 先删除之前创建的容器,以免端口冲突
(1)相同的ip,不同的端口
192.168.137.116服务器
es1.yml,es2.yml,es3.yml文件见压缩包资源
原理和下面的(2)逻辑差不多,只要区分不同的服务就行,ip或者端口不同只是区分的条件
不过因为是在一个服务器里面执行,为了端口不冲突,就要映射不同的端口,更加麻烦罢了
在/root目录下执行
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=262144
EOF
sysctl -p
mkdir data1
chmod 777 data1
mkdir data2
chmod 777 data2
mkdir data3
chmod 777 data3
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d \
-p 9200:9200 \
-p 9300:9300 \
-v ./es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ./data1:/usr/share/elasticsearch/data \
--name ES01 \
harbor:443/library/elasticsearch:7.3.0
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d \
-p 9201:9201 \
-p 9301:9301 \
-v ./es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ./data2:/usr/share/elasticsearch/data \
--name ES02 \
harbor:443/library/elasticsearch:7.3.0
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d \
-p 9202:9202 \
-p 9302:9302 \
-v ./es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ./data3:/usr/share/elasticsearch/data \
--name ES03 \
harbor:443/library/elasticsearch:7.3.0
安装elasticsearch-head进行集群可视化查看
docker pull mobz/elasticsearch-head:5
docker run -it --name="es-admin" -p 9100:9100 mobz/elasticsearch-head
(2)不同的ip,相同的端口
192.168.137.21服务器
es1.yml文件
cluster.name: es-cluster
node.name: es-0001
network.bind_host: 0.0.0.0
network.publish_host: 192.168.137.21
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.137.21:9300","192.168.137.22:9300","192.168.137.23:9300"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: ["es-0001","es-0002","es-0003"]
在/root目录下执行
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=262144
EOF
sysctl -p
mkdir data1
chmod 777 data1
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d \
-p 9200:9200 \
-p 9300:9300 \
-v ./es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ./data1:/usr/share/elasticsearch/data \
--name ES01 \
harbor:443/library/elasticsearch:7.3.0
192.168.137.22服务器
cluster.name: es-cluster
node.name: es-0002
network.bind_host: 0.0.0.0
network.publish_host: 192.168.137.22
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.137.21:9300","192.168.137.22:9300","192.168.137.23:9300"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: ["es-0001","es-0002","es-0003"]
在/root目录下执行
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=262144
EOF
sysctl -p
mkdir data1
chmod 777 data1
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d \
-p 9200:9200 \
-p 9300:9300 \
-v ./es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ./data1:/usr/share/elasticsearch/data \
--name ES01 \
harbor:443/library/elasticsearch:7.3.0
192.168.137.23服务器
cluster.name: es-cluster
node.name: es-0003
network.bind_host: 0.0.0.0
network.publish_host: 192.168.137.23
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.137.21:9300","192.168.137.22:9300","192.168.137.23:9300"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: ["es-0001","es-0002","es-0003"]
在/root目录下执行
cat >> /etc/sysctl.conf << EOF
vm.max_map_count=262144
EOF
sysctl -p
mkdir data1
chmod 777 data1
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d \
-p 9200:9200 \
-p 9300:9300 \
-v ./es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v ./data1:/usr/share/elasticsearch/data \
--name ES01 \
harbor:443/library/elasticsearch:7.3.0
使用浏览器查看