安装
安装es
使用https,下面配置会默认生成https证书
bash
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.2
privileged: true
container_name: es_node_1
ports:
- 9200:9200
restart: always
environment:
- cluster.name=elasticsearch #设置集群名称为elasticsearch
- discovery.type=single-node #以单一节点模式启动
- ES_JAVA_OPTS=-Xms4g -Xmx4g #设置使用jvm内存大小
- xpack.security.enrollment.enabled=true
volumes:
- /Users/mac/Desktop/dockerWorkspace/data:/usr/share/elasticsearch/data
- /Users/mac/Desktop/dockerWorkspace/plugins:/usr/share/elasticsearch/plugins
- /Users/mac/Desktop/dockerWorkspace/logs:/usr/share/elasticsearch/logs
不使用https,不能使用 api key连接es
bash
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.2
privileged: true
container_name: es_node_1
ports:
- 9200:9200
restart: always
environment:
- cluster.name=elasticsearch #设置集群名称为elasticsearch
- discovery.type=single-node #以单一节点模式启动
- ES_JAVA_OPTS=-Xms4g -Xmx4g #设置使用jvm内存大小
- xpack.security.http.ssl.enabled=false
volumes:
- /Users/mac/Desktop/dockerWorkspace/data:/usr/share/elasticsearch/data
- /Users/mac/Desktop/dockerWorkspace/plugins:/usr/share/elasticsearch/plugins
- /Users/mac/Desktop/dockerWorkspace/logs:/usr/share/elasticsearch/logs
docker-compose up -d elasticsearch
如果想要挂载所有配置文件,首先要先创建一个预备容器然后将配置文件拷贝到本地命令如下,然后停止并删除容器,然后数据卷加上配置文件夹,运行容器。下面的kibana如同。
bash
docker copy es_node_1:/usr/share/elasticsearch/config /Users/mac/Desktop/dockerWorkspace
安装kibana
使用 api key连接es
bash
version: '3'
services:
kibana:
image: docker.elastic.co/kibana/kibana:8.12.2
container_name: kibana
ports:
- 5601:5601
restart: always
environment:
- elasticsearch.hosts=http://127.0.0.1:9200 # 设置es的地址
- I18N_LOCALE=zh-CN # 设置为中文
volumes:
- /Users/mac/Desktop/dockerWorkspace/kibana/logs:/usr/share/kibana/logs
- /Users/mac/Desktop/dockerWorkspace/kibana/data:/usr/share/kibana/data
docker-compose up -d elasticsearch
使用密码连接es
先获取kibana的es账号密码
bash
docker exec -it es_node_1 /bin/bash
elasticsearch-reset-password -u kibana_system --auto
bash
version: '3'
services:
kibana:
image: docker.elastic.co/kibana/kibana:8.12.2
container_name: kibana
ports:
- 5601:5601
restart: always
environment:
- elasticsearch.hosts=http://127.0.0.1:9200 # 设置es的地址
- I18N_LOCALE=zh-CN # 设置为中文
volumes:
- /Users/mac/Desktop/dockerWorkspace/kibana/logs:/usr/share/kibana/logs
- /Users/mac/Desktop/dockerWorkspace/kibana/data:/usr/share/kibana/data
docker-compose up -d elasticsearch
删除kibana容器中的/usr/share/kibana/config/node.options的 --openssl-legacy-provider ,在/usr/share/kibana/config/node.options/kibana.yaml添内容如下, 并重启容器
yaml
elasticsearch.username: kibana_system
elasticsearch.password: zVq7FEQr*It0_SpKl6mm
xpack.reporting.roles.enabled: false
使用 api key连接es(es使用https才能使用)
获取es访问密码和token
bash
# 进入es容器
docker exec -it es_node_1 /bin/bash
# 重置密码
elasticsearch-reset-password -u elastic
# 生成token
elasticsearch-create-enrollment-token -s kibana
填写token
填写验证码
docker logs -f kibana
输入获取到的es密码登录
下载安装ik
bash
# 进入容器内部
docker exec -it es_node_1 /bin/bash
# 在线下载并安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.12.2/elasticsearch-analysis-ik-8.12.2.zip
#退出
exit
#重启容器
docker restart es_node_1
修改ik的切分词汇和禁止切分词汇配置
bash
# 安装vim需要使用root用户
docker exec -it -u root es_node_1 /bin/bash
# 安装vim
apt-get update && apt-get install vim
# 修改配置文件
vim /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
我的配置是远程的文件
退出并重启容器
bash
exit
docker restart es_node_1
扩展词汇dic支持热更新,修改之后需要等一段时间才会生效
ik支持两种分词器模式
ik_smart
:最少切分ik_max_word
:最细切分
测试
bash
GET /_analyze
{
"analyzer": "ik_max_word",
"text": "黑马程序员学习java太棒了"
}
GoLang连接es
首先将es中的证书从容器中拷贝出来,证书的地址可以从elasticsearch.yml看到
bash
docker cp es_node_1:/usr/share/elasticsearch/config/certs/http_ca.crt ./
连接es
go
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net/http"
"github.com/elastic/go-elasticsearch/v8"
)
var ESClient *elasticsearch.Client
func main() {
// https安全配置
cert, err := ioutil.ReadFile("http_ca.crt")
if err != nil {
log.Fatalf("Failed to load certificate: %v", err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cert)
cfg := elasticsearch.Config{
Addresses: []string{
"https://localhost:9200",
},
Username: "elastic",
Password: "VB2dd2tB=BJPuxCR*l9-",
Transport: &http.Transport{
MaxIdleConnsPerHost: 20,
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
},
}
if ESClient, err = elasticsearch.NewClient(cfg);err != nil{
panic(err)
}
res, err := ESClient.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
log.Println(res)
}