需要调整并设置docker的最大虚拟内存。
linux
vim /etc/sysctl.conf
vm.max_map_count=262144
启用配置
sysctl -p
windows
wsl -d docker-desktop
sysctl -w vm.max_map_count=262144
exit
配置文件目录
mkdir es/config
mkdir es/node1/data
mkdir es/node2/data
mkdir es/node3/data
配置每个节点es.yml
es1.yml
# 集群名称
cluster.name: es-cluster
# 节点名称
node.name: es1
# 网络
network.bind_host: 0.0.0.0
# ip地址
network.publish_host: 192.168.1.105
# 端口配置,外部可访问端口
http.port: 9201
# 内部通信接口
transport.tcp.port: 9301
# 跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
# 是否可以成为主节点
node.master: true
# 数据存储
node.data: true
# 集群列表 ip:内部通信端口
discovery.zen.ping.unicast.hosts: ["192.168.1.105:9301","192.168.1.105:9302","192.168.1.105:9303"]
# 候选节点数, minimum_master_nodes = 总节点数 / 2 + 1
discovery.zen.minimum_master_nodes: 2
# 初始化时,主节点
cluster.initial_master_nodes: ["es1","es2","es3"]
# 不开启安全验证 。正式环境 记得开启
xpack.security.enabled: false
xpack.security.authc.accept_default_password: false
xpack.security.transport.ssl.enabled: false
#xpack.security.transport.ssl.verification_mode: certificate
#xpack.security.transport.ssl.keystore.path: /usr/share/elasticsearch/config/elastic-stack-ca.p12
#xpack.security.transport.ssl.truststore.path: /usr/share/elasticsearch/config/elastic-stack-ca.p12
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
es2.yml
cluster.name: es-cluster
node.name: es2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.1.105
http.port: 9202
transport.tcp.port: 9302
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.1.105:9303","192.168.1.105:9301","192.168.1.105:9302"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: ["es1","es2","es3"]
xpack.security.enabled: false
xpack.security.authc.accept_default_password: false
xpack.security.transport.ssl.enabled: false
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
es3.yml
cluster.name: es-cluster
node.name: es3
network.bind_host: 0.0.0.0
network.publish_host: 192.168.1.105
http.port: 9203
transport.tcp.port: 9303
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.1.105:9302","192.168.1.105:9301","192.168.1.105:9303"]
discovery.zen.minimum_master_nodes: 2
cluster.initial_master_nodes: ["es1","es2","es3"]
xpack.security.enabled: false
xpack.security.authc.accept_default_password: false
xpack.security.transport.ssl.enabled: false
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
启动es
docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d -p 9201:9201 -p 9301:9301 -v /e/docker/es/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /e/docker/es/node1/data:/usr/share/elasticsearch/data --name es01 elasticsearch:7.9.3
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9202:9202 -p 9302:9302 -v /e/docker/es/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /e/docker/es/node2/data:/usr/share/elasticsearch/data --name es02 elasticsearch:7.9.3
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9203:9203 -p 9303:9303 -v /e/docker/es/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /e/docker/es/node3/data:/usr/share/elasticsearch/data --name es03 elasticsearch:7.9.3
// 修改kibana.yml配置
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://192.168.1.105:9201","http://192.168.1.105:9202","http://192.168.1.105:9203" ]
monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: zh-CN
启动kibana
docker run --rm --name kibana1 -p 5601:5601 -v /e/docker/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:7.9.3
查看集群状态 访问
http://localhost:9201/_cat/health?v
#返回
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1684210866 04:21:06 es-cluster green 3 3 14 7 0 0 0 0 - 100.0%
获取集群主节点
GET _cat/nodesv
master 为*
的标识为主节点
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.1.105 58 78 3 0.04 0.16 0.35 dilmrt * es2
192.168.1.105 26 78 4 0.04 0.16 0.35 dilmrt - es1
192.168.1.105 56 78 1 0.04 0.16 0.35 dilmrt - es3
证书
节点1作为示例,其他节点将生成的证书复制到容器内即可
docker exec -it es01 bash
# 生成ca,默认名称为 elastic-stack-ca.p12
bin/elasticsearch-certutil ca
# 生成证书, 文件为:elastic-certificates.p12
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
# 将文件移动到 config文件夹下
mv elastic-certificates.p12 config/
# 设置文件权限,以及用户组
chmod 777 elastic-certificates.p12
chown elasticsearch elastic-certificates.p12
编辑 config目录下文件 elasticsearch.yml
# 打开注释掉的内容
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: /usr/share/elasticsearch/config/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: /usr/share/elasticsearch/config/elastic-certificates.p12
保存,重启重启节点1
将节点1的证书复制到其他节点
# 复制到宿主机器
docker cp es01:/usr/share/elasticsearch/config/elastic-certificates.p12 .
# 从宿主机器复制到节点es02
docker cp elastic-certificates.p12 es02:/usr/share/elasticsearch/config/elastic-certificates.p12
# 从宿主机器复制到节点es03
docker cp elastic-certificates.p12 es03:/usr/share/elasticsearch/config/elastic-certificates.p12
并记得打开节点2,节点3的注释内容
# 打开注释掉的内容
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: /usr/share/elasticsearch/config/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: /usr/share/elasticsearch/config/elastic-certificates.p12
重启即可
证书设置参考 搭建集群时配置证书_elasticsearch
找到主节点,设置密码,密码自定义
设置密码
bin/elasticsearch-setup-passwords interactive
分词器ik
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.3/elasticsearch-analysis-ik-7.9.3.zip
注意:如果证书有权限问题,可以尝试,再设置一下权限
chmod 777 elastic-certificates.p12
chown elasticsearch elastic-certificates.p12