ElasticSearch web端口安全访问问题,测试了以下方案:

防火墙限制访问ip 生产上可用,开发环境需要不停修改ip,不可行
nginx反向代理 最终使用了该方案
x-pack插件 1个月试用期,license过期后免费续费但鉴权功能不可用【巨坑】
http_basic插件 仅支持到es1.5.1版本,实测es5.x版本不可用
search_guard插件 安装后9300端口也需要基于SSL协议访问了,由于我们有应用用到该端口,需要修改应用,不可行

使用x-pack插件

安装插件

elasitcsearch目录下

  1. bin/elasticsearch-plugin install x-pack

添加配置

elasitcsearch目录下,vi config/elasticsearch.yml

  1. cluster.routing.allocation.disk.threshold_enabled: false
  2. http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
  3. xpack.security.enabled: true

重启

重启es后生效,xpack默认账户为elastic,默认密码为changeme。

卸载过期的x-pack并使用nginx反向代理做basic auth

卸载过期的x-pack

elasitcsearch目录下

  1. bin/elasticsearch-plugin remove x-pack

修改web端口配置

elasitcsearch目录下, vi config/elasticsearch.yml

  1. #network.host: 172.31.197.24
  2. # tcp(9300)端口绑定的ip为对外的ip,不做改变
  3. transport.bind_host: 172.31.197.24
  4. # web端口绑定的ip为localhost,只允许本机访问
  5. network.bind_host: 127.0.0.1
  6. # web端口修改为9201,9200由nginx转发
  7. #http.port: 9200
  8. http.port: 9201
  9. # 删除掉x-pack的相关配置
  10. ##cluster.routing.allocation.disk.threshold_enabled: false
  11. ##http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
  12. ##xpack.security.enabled: true

配置nginx服务器

本机nginx配置中添加:

  1. server {
  2. listen 9200;
  3. # 服务器对外的ip
  4. server_name 172.31.197.24;
  5. location / {
  6. proxy_http_version 1.1;
  7. proxy_set_header Connection "";
  8. proxy_set_header Host $host;
  9. proxy_set_header X-Real-IP $remote_addr;
  10. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  11. auth_basic "login";
  12. # 存放账号密码的文件
  13. auth_basic_user_file /etc/nginx/pass/es;
  14. autoindex on;
  15. proxy_pass http://localhost:9201/;
  16. }
  17. }

其中,存放账号密码的文件/etc/nginx/pass/es需要手动创建。
访问https://www.sojson.com/htpasswd.html,输入用户名和密码,将加密算法修改为Crypt,点击生成按钮,将结果内容填入该文件即可。可分行填写多个用户。

重启

依次重启es和ngxin,可通过wget localhost:9201/_cat和访问页面http://ip:9200/_cat对两个组件分别进行测试。

使用Search Guard插件

安装插件

elasitcsearch目录下

  1. bin/elasticsearch-plugin install com.floragunn:search-guard-5:5.6.3-19.1

下载完成后,进入plugin/search_guard5/bin 目录,执行install_demo.sh。

添加jar包

如果运行环境JRE为java8以上,则需要找一个javax.xml.bind.jar放到es的lib目录下,否则会报错
java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter

启动es并初始化

重启es,并执行plugin/search_guard5/bin目录下的sg_admin_demo.sh,如果报错访问不到172.0.0.1:9300端口,则需要编辑sg_admin_demo.sh在最后加上参数 -h 本机ip。执行完成后9200和9300都需要鉴权后访问。

防火墙的设定方法:

修改配置

CentOS默认防火墙是firewalld ,修改配置文件vi /etc/firewalld/zones/public.xml

  1. <!--
  2. 开放除9200外所有端口,对9200只允许指定ip访问
  3. -->
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <zone>
  6. <short>Public</short>
  7. <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  8. <service name="ssh"/>
  9. <service name="dhcpv6-client"/>
  10. <port protocol="tcp" port="1-9199"/>
  11. <port protocol="tcp" port="9201-65535"/>
  12. <port protocol="udp" port="1-9199"/>
  13. <port protocol="udp" port="9201-65535"/>
  14. <rule family="ipv4">
  15. <source address="10.5.119.16"/>
  16. <port protocol="tcp" port="9200"/>
  17. <accept/>
  18. </rule>
  19. </zone>

启动防火墙

启动firewall并设置开机启动:

  1. systemctl start firewalld
  2. systemctl enable firewalld