第一步:创建宿主文件夹,用于挂载相关文件
建议给每个文件夹授权:chmod 777 /sdyy/app/es-9201/config
chmod 777 elastic-stack-ca.p12
第二步:先创建一个实例
docker run --name escs -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 es:7.16.2
#--name表示镜像启动后的容器名称
#-d: 后台运行容器,并返回容器ID;
#-e: 指定容器内的环境变量
#-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
第三步:进入容器
docker exec -it escs bash
第四步:为 TLS 生成一个证书
./bin/elasticsearch-certutil ca
如上图,直接回车接受默认的文件名 elastic-stack-ca.p12
然后出现输入密码的提示,输入密码回车,退出容器
第五步:将文件复制到宿主机(切换到接下来部署集群时外挂的config文件下执行下面命令)
docker cp escs:/usr/share/elasticsearch/elastic-stack-ca.p12 .
docker cp escs:/usr/share/elasticsearch/config/elasticsearch.yml .
docker cp escs:/usr/share/elasticsearch/config/jvm.options .
三条命令执行完后,可以将临时的escs容器删掉了。
第六步:修改配置文件elasticsearch.yml(3节点,修改相应的ip和端口即可)
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 196.193.31.5
network.publish_host: 196.193.31.5
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["196.193.31.5:9300","196.193.31.4:9300","196.193.31.3:9300"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3"]
indices.fielddata.cache.size: 20%
indices.breaker.total.use_real_memory: false
indices.breaker.fielddata.limit: 40%
indices.breaker.request.limit: 40%
indices.breaker.total.limit: 95%
第七步:运行容器
docker run --name es-9201 --net=host -v /sdyy/app/es-9201/config/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /sdyy/app/es-9201/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /sdyy/app/es-9201/data:/usr/share/elasticsearch/data -v /sdyy/app/es-9201/logs:/usr/share/elasticsearch/logs -v /sdyy/app/es-9201/config/elastic-stack-ca.p12:/usr/share/elasticsearch/config/elastic-stack-ca.p12 -d es:7.16.2
第八步:如果在生成elastic-stack-ca.p12文件时录入了密码,需要进入容器bin执行以下命令以将密码存储在Elasticsearch密钥库中,提示录入的密码,即创建elastic-stack-ca.p12时录入的密码,每个节点都执行
(建议在容器config文件中执行 :chmod 777 elastic-stack-ca.p12)
./elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
./elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
第九步:修改配置文件elasticsearch.yml,增加如下配置,并重启es
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.keystore.type: PKCS12
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
xpack.security.transport.ssl.truststore.type: PKCS12
第十步:容器bin目录下,执行:
./elasticsearch-setup-passwords interactive