golang 操作es
docker 操作安装es
1. 创建网络
因为我们还需要部署kibana容器,因此需要让es和kibana容器互联,这里先创建一个网络。
使用FinalShell登录自己的Linux云服务器客户端(阿里云或腾讯云)
docker network create es-net
2.加载镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.13.4 docker pull docker.elastic.co/kibana/kibana:8.13.4
3.运行容器
在服务器 /home/elasticsearch 创建 data 、plugins、logs文件夹,并chmod 777 文件夹
docker run -d --restart=always -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -e "discovery.type=single-node" -v es-data:/home/elasticsearch/data -v es-plugins:/home/elasticsearch/plugins -v es-logs:/home/elasticsearch/logs --privileged --network es-net -p 9200:9200 -p 9300:9300 --name es docker.elastic.co/elasticsearch/elasticsearch:8.13.4
命令解释:
-e "cluster.name=es-docker-cluster":设置集群名称
-e "http.host=0.0.0.0":监听的地址,可以外网访问
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m":分配内存大小
-e "discovery.type=single-node":单节点模式
-v es-data:/usr/local/elasticsearch7.12.1/data:挂载逻辑卷,绑定es的数据目录
-v es-logs:/usr/local/elasticsearch7.12.1/logs:挂载逻辑卷,绑定es的日志目录
-v es-plugins:/usr/local/elasticsearch7.12.1/plugins:挂载逻辑卷,绑定es的插件目录
--privileged:授予逻辑卷访问权
--network es-net :加入一个名为es-net的网络中
-p 9200:9200:端口映射配置
# 开放9200端口用于http通信 firewall-cmd --zone=public --add-port=9200/tcp --permanent # 开放9300端口用于ES集群间通信 firewall-cmd --zone=public --add-port=9300/tcp --permanent # 重新加载防火墙使开放的端口即时生效 firewall-cmd --reload
先配置 elasticsearch.yml
docker exec -it es bash cat config/elasticsearch.yml // elasticsearch.yml文件配置如下 cluster.name: "docker-cluster" network.host: 0.0.0.0 #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 09-07-2024 05:32:02 # # -------------------------------------------------------------------------------- # Enable security features xpack.security.enabled: true xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: false keystore.path: certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: false verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 #----------------------- END SECURITY AUTO CONFIGURATION -------------------------
验证安装结果
在浏览器地址栏中输入URL:http://<你的云服务器公网IP地址>:9200/
4.下载分词
docker exec -it es bash cd bin ./elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.13.4 exit
然后重启es容器
docker restart es
5.设置密码
docker exec -it es bash // 设置密码 ./elasticsearch-setup-passwords interactive
分别设置相应的帐号密码就可以了
6.创建用户并赋权 (因为默认的用户elastic很多情况不能用,所以要添加新用户以便操作es)
// 进入es容器 docker exec -it es bash // 添加root 用户 bin/elasticsearch-users useradd root // 设置密码后赋权 bin/elasticsearch-users roles -a kibana_admin root
7. 部署Kibana
Kibana 使操作ES数据的可视化界面,使用Kibana 操作ES非常方便。
在/usr/local目录执行如下docker命令
docker run -d --name kibana -e ELASTICSEARCH_HOSTS=http://es:9200 --network=es-net -p 5601:5601 docker.elastic.co/kibana/kibana:8.13.4
修改kibana.yml配置 ,配置 es 的链接 及用户名密码
//先在 /home/kibana下建一个kibana.yml 文件 // kibana.yml内容为 # # Default Kibana configuration for docker target server.host: "0.0.0.0" server.shutdownTimeout: "5s" elasticsearch.hosts: [ "http://localhost:9200" ] monitoring.ui.container.elasticsearch.enabled: true elasticsearch.username: "root" elasticsearch.password: "123456" // 然后 将文件 copy到kibana容器 docker cp ./kibana.yml kibana:/usr/share/kibana // 进入kibana容器 docker exec -it kibana bash // 用拷进来的kibana.yml文件强制替换config文件夹里的kibana.yml mv -f kibana.yml ./config // 退出 exit // 重启kibana容器 docker restart a043