Elasticsearch启用https访问

1.生成p12格式的CA证书

[sandwich@centos-elk elasticsearch-7.17.1]$ ./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 :

这里不改名,直接接受文件的默认名elastic-stack-ca.p12,然后输入密码: aaaaaa并记住。
执行完生成以下文件

[sandwich@centos-elk elasticsearch-7.17.1]$ ls *p12
elastic-stack-ca.p12

2.生成p12格式的certificate证书

接着用如下命令来生成一个新的证书

[sandwich@centos-elk elasticsearch-7.17.1]$ ./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.
    * By default, this generates a single certificate and key for use
       on a single instance.
    * The '-multiple' option will prompt you to enter details for multiple
       instances and will generate a certificate and key for each one
    * The '-in' option allows for the certificate generation to be automated by describing
       the details of each instance in a YAML file

    * An instance is any piece of the Elastic Stack that requires an SSL certificate.
      Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
      may all require a certificate and private key.
    * The minimum required value for each instance is a name. This can simply be the
      hostname, which will be used as the Common Name of the certificate. A full
      distinguished name may also be used.
    * A filename value may be required for each instance. This is necessary when the
      name would result in an invalid file or directory name. The name provided here
      is used as the directory name (within the zip) and the prefix for the key and
      certificate files. The filename is required if you are prompted and the name
      is not displayed in the prompt.
    * IP addresses and DNS names are optional. Multiple values can be specified as a
      comma separated string. If no IP addresses or DNS names are provided, you may
      disable hostname verification in your SSL configuration.

    * All certificates generated by this tool will be signed by a certificate authority (CA)
      unless the --self-signed command line option is specified.
      The tool can automatically generate a new CA for you, or you can provide your own with
      the --ca or --ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
    * The instance certificate
    * The private key for the instance certificate
    * The CA certificate

If you specify any of the following options:
    * -pem (PEM formatted output)
    * -keep-ca-key (retain generated CA key)
    * -multiple (generate multiple certificates)
    * -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files

Enter password for CA (elastic-stack-ca.p12) : 

这里需要输入前面生成CA证书的密码: aaaaaa
输入CA证书密码后,接受新证书的默认文件名,然后为新证书elastic-certificates.p12添加新密码: bbbbbb

Enter password for CA (elastic-stack-ca.p12) : 
Please enter the desired output file [elastic-certificates.p12]: 
Enter password for elastic-certificates.p12 : 

Certificates written to /home/sandwich/app/elk/elasticsearch-7.17.1/elastic-certificates.p12

This file should be properly secured as it contains the private key for 
your instance.

This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.

完成后,在相同路径下多了一个elastic-certificates.p12证书文件

[sandwich@centos-elk elasticsearch-7.17.1]$ ls *p12
elastic-certificates.p12  elastic-stack-ca.p12

3.把elastic-certificates.p12 证书拷入到 Elasticsearch 安装目录下的config子目录

[sandwich@centos-elk elasticsearch-7.17.1]$ mv elastic-certificates.p12 config/

4.生成pem格式的证书

[sandwich@centos-elk elasticsearch-7.17.1]$ openssl pkcs12 -in elastic-stack-ca.p12 -out ca.crt.pem -clcerts -nokeys
Enter Import Password:
MAC verified OK
[sandwich@centos-elk elasticsearch-7.17.1]$ ls
bin  ca.crt.pem  config  elastic-stack-ca.p12  jdk  lib  LICENSE.txt  logs  modules  NOTICE.txt  plugins  README.asciidoc  startup.log

注意,这里需要输入elastic-stack-ca.p12证书的密码

5.把pem证书拷到Kibana安装目录下的config目录中:

[sandwich@centos-elk elasticsearch-7.17.1]$ cp ca.crt.pem ../kibana-7.17.1-linux-x86_64/config/

6.修改Elasticsearch配置

config/elasticsearch.yml文件添加以下配置:

[sandwich@centos-elk config]$ vi elasticsearch.yml
[sandwich@centos-elk config]$ tail -n 5 elasticsearch.yml
xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
xpack.security.authc.api_key.enabled: true
xpack.security.http.ssl.keystore.path: elastic-certificates.p12
xpack.security.http.ssl.truststore.path: elastic-certificates.p12
xpack.security.http.ssl.keystore.password: bbbbbb
xpack.security.http.ssl.truststore.password: bbbbbb

重启ES

7.配置Kibana

为了能让Kibana访问带有https的Elasticsearch。我们也需要做相应的配置,给config/kibana.yml添加如下配置

elasticsearch.ssl.certificateAuthorities: ["/home/sandwich/app/elk/kibana-7.17.1-linux-x86_64/config/ca.crt.pem"]
elasticsearch.ssl.verificationMode: none

并且把es的host改成https

elasticsearch.hosts: ["https://localhost:9200"]
<think>我们正在部署Elasticsearch 9.0.4并配置HTTP访问。根据用户需求,我们需要完成以下步骤: 1. 下载并安装Elasticsearch 9.0.4 2. 配置Elasticsearch以允许HTTP访问(即外部访问) 3. 启动Elasticsearch服务 参考之前的引用,我们可以发现配置HTTP访问通常需要修改`elasticsearch.yml`配置文件,设置`network.host`和`http.port`。在9.0.4版本中,配置方式可能类似。 步骤: 1. 下载Elasticsearch 9.0.4 从官网下载:https://www.elastic.co/downloads/elasticsearch 2. 解压安装包(假设安装在Linux系统) ```bash tar -xzf elasticsearch-9.0.4-linux-x86_64.tar.gz cd elasticsearch-9.0.4/ ``` 3. 修改配置文件 配置文件位于`config/elasticsearch.yml`。 我们需要设置: - `network.host: 0.0.0.0` # 允许所有IP访问 - `http.port: 9200` # HTTP访问端口,默认为9200,如果不需要修改可以保持,但也可以自定义 另外,根据Elasticsearch的安全更新,默认情况下启用了安全功能(包括TLS加密)。如果只需要简单的HTTP访问而不使用加密,可能需要调整安全设置。但请注意,从安全角度不推荐禁用安全功能。 在9.0版本中,默认会启用安全特性,因此如果我们只是希望允许HTTP访问,可能还需要配置安全部分。但用户需求是配置HTTP访问,我们可能希望禁用安全特性(仅用于测试环境,生产环境强烈建议使用HTTPS)。 在`elasticsearch.yml`中添加或修改以下配置: ```yaml # 允许所有网络访问 network.host: 0.0.0.0 # 配置HTTP端口(默认9200,可省略) http.port: 9200 # 禁用安全特性(仅用于开发或测试环境) xpack.security.enabled: false ``` 注意:禁用安全特性(`xpack.security.enabled: false`)可以让HTTP访问无需身份验证,但请确保只在安全的内网环境或测试环境中使用。 4. 启动Elasticsearch 由于Elasticsearch不能以root用户运行,需要创建一个非root用户(如果还没有的话)并赋予相应目录的权限。 ```bash # 创建用户组和用户 groupadd elasticsearch useradd -g elasticsearch elasticsearch # 将Elasticsearch目录的拥有者改为新用户 chown -R elasticsearch:elasticsearch /path/to/elasticsearch-9.0.4 # 切换到该用户启动 su elasticsearch ./bin/elasticsearch ``` 或者以后台方式启动: ```bash ./bin/elasticsearch -d ``` 5. 验证 在另一台机器或本机上使用curl测试: ```bash curl http://<服务器IP>:9200 ``` 应该会返回一个JSON格式的响应,包含集群信息等。 注意:如果服务器有防火墙,需要开放9200端口。 另外,也可以使用Docker部署,用户之前的问题中提到了Docker部署单点ES(引用[3])。我们可以提供Docker部署方式: 使用Docker部署Elasticsearch 9.0.4: 1. 拉取镜像: ```bash docker pull docker.elastic.co/elasticsearch/elasticsearch:9.0.4 ``` 2. 运行容器: ```bash docker run -d --name es \ -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" \ -e "xpack.security.enabled=false" \ docker.elastic.co/elasticsearch/elasticsearch:9.0.4 ``` 解释: - `-d`: 后台运行 - `--name es`: 容器名称 - `-p 9200:9200 -p 9300:9300`: 映射端口,9200是HTTP REST API端口,9300是内部节点通信端口 - `-e "discovery.type=single-node"`: 单节点模式 - `-e "ES_JAVA_OPTS=-Xms1g -Xmx1g"`: 设置JVM堆内存大小(根据实际情况调整) - `-e "xpack.security.enabled=false"`: 禁用安全特性(允许HTTP直接访问) 3. 验证同上。 注意:Docker方式部署时,数据默认存储在容器内部,如果需要持久化,可以使用卷映射,例如: ```bash -v es-data:/usr/share/elasticsearch/data ``` 根据用户需求,我们提供了两种部署方式:手动安装和Docker安装。 生成相关问题: 1. 如何为Elasticsearch 9.0.4配置HTTPS访问? 2. 如何启用Elasticsearch的安全特性并设置用户名密码? 3. Elasticsearch集群部署的步骤是什么? 4. 如何安装IK分词器插件到Elasticsearch 9.0.4? 注意:在生成回答时,我们参考了引用[2][3][5]中关于配置网络和端口的设置,但根据9.0.4版本的特点,增加了安全配置(xpack.security.enabled)的说明。</think>### 部署Elasticsearch 9.0.4并启用HTTP访问 #### 一、安装Elasticsearch 9.0.4 1. **下载安装包** ```bash wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-9.0.4-linux-x86_64.tar.gz tar -xzf elasticsearch-9.0.4-linux-x86_64.tar.gz cd elasticsearch-9.0.4/ ``` 2. **创建专用用户**(Elasticsearch禁止root运行) ```bash useradd elastic -m chown -R elastic:elastic /path/to/elasticsearch-9.0.4 su elastic ``` #### 二、配置HTTP访问 修改配置文件 `config/elasticsearch.yml`: ```yaml # 启用HTTP访问 network.host: 0.0.0.0 # 允许所有IP访问[^2][^5] http.port: 9200 # HTTP端口(默认) # 单节点模式配置 discovery.type: single-node # 单节点部署[^3] # 关闭安全功能(测试环境用,生产环境需配置TLS) xpack.security.enabled: false # 禁用认证[^3] ``` #### 三、启动服务 ```bash ./bin/elasticsearch -d # 后台启动 ``` #### 四、验证访问 ```bash curl http://服务器IP:9200 ``` 正常响应示例: ```json { "name" : "node-1", "cluster_name" : "elasticsearch", "version" : { "number" : "9.0.4", "build_flavor" : "default", "lucene_version" : "9.0.0" }, "tagline" : "You Know, for Search" } ``` #### 五、防火墙配置(如需) ```bash sudo ufw allow 9200 # 开放HTTP端口 sudo ufw reload ``` #### 六、Docker部署方案(可选) ```bash docker run -d --name es9 \ -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" \ -e "xpack.security.enabled=false" \ # 禁用认证[^3] docker.elastic.co/elasticsearch/elasticsearch:9.0.4 ``` --- ### 关键配置说明 1. `network.host: 0.0.0.0` 允许所有网络接口访问ES服务[^2][^4][^5] 2. `discovery.type: single-node` 单节点模式无需配置发现机制[^3] 3. `xpack.security.enabled: false` 禁用安全模块(生产环境需配置TLS证书)[^3] > ⚠️ **安全提醒**:生产环境必须启用安全配置(TLS+RBAC),此处禁用仅适用于测试环境[^3]。 --- ### 相关问题 1. 如何为Elasticsearch 9.0.4配置HTTPS加密传输? 2. 如何在开启安全认证后使用curl访问Elasticsearch API? 3. Elasticsearch集群部署时发现机制如何配置? 4. 如何安装IK分词器插件到Elasticsearch 9.0.4? 5. 生产环境中如何优化Elasticsearch的内存配置? [^1]: 插件安装示例(版本需匹配) [^2]: 网络主机配置 [^3]: Docker部署参数 [^4]: 网络绑定配置 [^5]: 集群网络设置
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT三明治

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值