Docker部署Elasticsearch 7.6集群并配置X-Pack安全认证
自Elasticsearch 6.8版本起,基础版(Basic)许可便集成了X-Pack的安全特性,此前版本通常缺乏内置的安全防护。以下记录在Docker环境下配置安全认证的具体方法。
为简化部署流程,本方案采用Docker容器化方式安装和运行服务。
前置环境配置
在使用Docker运行ES之前,需调整Linux内核的相关参数以满足其运行要求。
修改系统参数vm.max_map_count=262144
sudo vim /etc/sysctl.conf
vm.max_map_count=262144
执行命令后无需重启系统,当前配置即可立即生效
sysctl -w vm.max_map_count=262144
ES的数据目录和日志目录需要授权给UID为1000的用户。假设构建一个包含三个节点的ES集群,首先创建相应的数据存储路径。
mkdir -p es01/data
mkdir -p es01/logs
mkdir -p es02/data
mkdir -p es02/logs
mkdir -p es03/data
mkdir -p es03/logs
## es的用户id为1000,这里暂且授权给所有人好了
sudo chmod 777 es* -R
镜像版本选择
Elasticsearch提供多种许可证类型,其中Open Source和Basic为免费版本。自6.8版本起,安全功能被集成到Basic许可中。

对应的Docker镜像名称为
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.6.2
Docker Hub上同步的镜像为elasticsearch。直接拉取elasticsearch:7.6.2版本即可。
部署实施
所有安装配置文件已托管于GitHub: https://github.com/Ryan-Miao/docker-china-source/tree/master/docker-elasticsearch
首先,编写docker-compose.yml文件
version: '2.2'
services:
es01:
image: elasticsearch:7.6.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es01/data:/usr/share/elasticsearch/data
- ./es01/logs:/usr/share/elasticsearch/logs
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12
ports:
- 9200:9200
networks:
- elastic
es02:
image: elasticsearch:7.6.2
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es02/data:/usr/share/elasticsearch/data
- ./es02/logs:/usr/share/elasticsearch/logs
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12
ports:
- 9201:9200
networks:
- elastic
es03:
image: elasticsearch:7.6.2
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es03/data:/usr/share/elasticsearch/data
- ./es03/logs:/usr/share/elasticsearch/logs
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12
ports:
- 9202:9200
networks:
- elastic
kib01:
depends_on:
- es01
image: kibana:7.6.2
container_name: kib01
ports:
- 5601:5601
environment:
ELASTICSEARCH_URL: http://es01:9200
ELASTICSEARCH_HOSTS: http://es01:9200
volumes:
- ./kibana.yml:/usr/share/kibana/config/kibana.yml
networks:
- elastic
networks:
elastic:
driver: bridge
关于elasticsearch.yml配置详解
核心配置内容如下
network.host: 0.0.0.0
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: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.type: PKCS12
xpack.security.audit.enabled: true
- network.host设置为0.0.0.0以允许外部IP访问,解除默认的本地IP绑定限制
- xpack.security部分负责安全相关配置,其中SSL证书需自行生成
关于证书elastic-certificates.p12
ES提供了专用的证书生成工具elasticsearch-certutil。我们可以在Docker实例内部生成该证书,随后将其复制出来供后续统一使用。
首先启动一个临时的ES实例
sudo docker run -dit --name=es elasticsearch:7.6.2 /bin/bash
进入该实例内部
sudo docker exec -it es /bin/bash
生成CA证书: elastic-stack-ca.p12
[root@25dee1848942 elasticsearch]# ./bin/elasticsearch-certutil ca
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.
Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority
By default the 'ca' mode produces a single PKCS#12 output file which holds:
* The CA certificate
* The CA's private key
If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key
Please enter the desired output file [elastic-stack-ca.p12]:
Enter password for elastic-stack-ca.p12 :
基于CA再生成节点证书: elastic-certificates.p12
[root@25dee1848942 elasticsearch]# ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'cert' mode generates X.509 certificate and private keys.
生成的elastic-certificates.p12即为集群通信所需的证书文件。
将证书复制到宿主机,然后按Ctrl+D退出容器内部
sudo docker cp es:/usr/share/elasticsearch/elastic-certificates.p12 .
# 关闭这个容器
sudo docker kill es
sudo docker rm es
至此成功获取了必要的证书文件。
用户密码生成
首先需要启动完整的ES集群,以便在集群内部生成初始用户密码。
sudo docker-compose up
然后登录进入其中任意一台节点容器
sudo docker exec -it es01 /bin/bash
使用auto参数可自动生成随机密码,若需自定义密码则使用interactive模式
[root@cfeeab4bb0eb elasticsearch]# ./bin/elasticsearch-setup-passwords -h
Sets the passwords for reserved users
Commands
--------
auto - Uses randomly generated passwords
interactive - Uses passwords entered by a user
Non-option arguments:
command
Option Description
------ -----------
-E Configure a setting
-h, --help Show help
-s, --silent Show minimal output
-v, --verbose Show verbose output
[root@cfeeab4bb0eb elasticsearch]# ./bin/elasticsearch-setup-passwords auto
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
The passwords will be randomly generated and printed to the console.
Please confirm that you would like to continue [y/N]y
Changed password for user apm_system
PASSWORD apm_system = YxVzeT9B2jEDUjYp66Ws
Changed password for user kibana
PASSWORD kibana = 8NnThbj0N02iDaTGhidU
Changed password for user logstash_system
PASSWORD logstash_system = 9nIDGe7KSV8SQidSk8Dj
Changed password for user beats_system
PASSWORD beats_system = qeuVaf1VEALpJHfEUOjJ
Changed password for user remote_monitoring_user
PASSWORD remote_monitoring_user = DtZCrCkVTZsinRn3tW3D
Changed password for user elastic
PASSWORD elastic = q5f2qNfUJQyvZPIz57MZ
验证密码访问
通过浏览器访问localhost:9200/9201/9202端口时,系统将提示输入账号凭证
默认情况下,输入用户名elastic及刚才设置的password即可登录
同样地,访问Kibana界面 localhost:5601

密码遗忘处理
如果生成密码后不慎遗忘,可以进入容器内部进行重置操作。
进入ES容器内部
sudo docker exec -it es01 /bin/bash
创建一个临时的超级用户RyanMiao
./bin/elasticsearch-users useradd ryan -r superuser
Enter new password:
ERROR: Invalid password...passwords must be at least [6] characters long
[root@cfeeab4bb0eb elasticsearch]# ./bin/elasticsearch-users useradd ryan -r superuser
Enter new password:
Retype new password:
使用该临时超级用户权限来修改elastic用户的密码:
curl -XPUT -u ryan:ryan123 http://localhost:9200/_xpack/security/user/elastic/_password -H "Content-Type: application/json" -d '
{
"password": "q5f2qNfUJQyvZPIz57MZ"
}'
参考资料
http://codingfundas.com/setting-up-elasticsearch-6-8-with-kibana-and-x-pack-security-enabled/index.html
到此这篇关于docker安装Elasticsearch7.6集群并设置密码的文章就介绍到这了,更多相关docker安装Elasticsearch集群内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!