ulimit “不生效”

有一台机器的在启动 ES 的时候始终报错

1
max file descriptors [65000] for elasticsearch process is too low

但是我已经在/etc/security/limits.conf里增加了如下配置,

1
2
3
4
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
elasticsearch hard nofile 65536
elasticsearch soft nofile 65536

按理说ulimit -n的时候应该是会到看到 65536 的,但是依旧输出的是 65000。郁闷

最后受这篇文章 ulimit 到底谁说了算? 的启发,我把排查目标放在了 profile 和 bashrc 文件上面。

最终发现原来还真是有人在这台机器的/etc/bashrc的文末加了一句ulimit -n 65000,把这行去掉后就正常了。

如果你也出现了这个问题,建议排查一下以下四个文件:

1
2
3
4
/etc/profile
/etc/bashrc
~/.profile
~/.bashrc

由 X-Pack 的认证机制引起的问题

X-Pack 是一个 Elastic Stack 的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中。在 Elasticsearch 5.0.0 之前,您必须安装单独的 Shield,Watcher 和 Marvel 插件才能获得在 X-Pack 中所有的功能。

ES、Kibana 和 X-Pack 的安装很简单,有需要的可以参看:安装 Elasticsearch、Kibana 和 X-Pack

HTTP REST API

在终端里访问 REST API 的时候

1
curl -XGET 'localhost:9200/_cat/health?v&pretty'

会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{"error": {"root_cause": [{"type": "security_exception","reason": "missing authentication token for REST request [/_cat/health?v&pretty]","header": {"WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""}}],"type": "security_exception","reason": "missing authentication token for REST request [/_cat/health?v&pretty]","header": {"WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""}},"status": 401
}

解决办法是添加上账号密码就好了

1
curl --user elastic:changeme -XGET 'localhost:9200/_cat/health?v&pretty'

这样就能正常访问了:

1
2
epoch      timestamp cluster  status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1487747024 15:03:44  myClusterName yellow          1         1     19  19    0    0       19             0                  -                 50.0%

这个账号密码实际上是 X-Pack 这个插件附带的认证功能

Java API

使用 X-Pack 后,使用 Java API 获取 Client 的时候会报一个类似这样的错:

1
2
3
4
5
...
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{pyXJL2PeTtGejUwbpycDUg}{127.0.0.1}{127.0.0.1:9300}]]
...
Caused by: org.elasticsearch.ElasticsearchSecurityException: missing authentication token for action [cluster:monitor/nodes/liveness]
...

其实看到Caused by的时候基本就已经能确定是因为 X-Pack 引入的认证机制而引起的错误。

既然知道了错误原因,那么就加上用户名和密码呗!但是事情却不是想的那么简单,ES 的官方文档里并没有提到在哪里可以加用户名和密码。

那么想必 Google 一下就能解决了吧,但是这个解决的过程其实并不顺利。首先 ES 的版本比较多,如果是 ES5 之前的版本是基本没见到这个问题的,其次也不是每个人都会装 X-Pack 的啊!最后 pass 掉了几种过气了的解决方案后,终于在 ES 的社区找到了个类似的问题,最终解决的办法其实 elastic 已经写到了它的文档中,只不过不是 ElasticSearch 的文档而是 X-Pack 的文档里(唉,其实我应该早想到的 [捂脸])

解决的关键是要引入一个 jar 包 x-pack-transport-5.2.1.jar,在构造 TransportClient 时用这个 jar 包里的 PreBuiltXPackTransportClient 替换掉 PreBuiltTransportClient,然后就可以在 Settings 里定义xpack.security.user了。

解决步骤如下(以使用 Maven 来管理依赖为例):

  1. 首先在 pom.xml 文件里添加仓库和依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<project ...><repositories><!-- add the elasticsearch repo --><repository><id>elasticsearch-releases</id><url>https://artifacts.elastic.co/maven</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository>...</repositories>...<dependencies><!-- add the x-pack jar as a dependency --><dependency><groupId>org.elasticsearch.client</groupId><artifactId>x-pack-transport</artifactId><version>{version}</version></dependency>...</dependencies>...</project>
  1. 然后在配置里加入xpack.security.user就好了
1
2
3
4
5
6
7
8
9
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
...TransportClient client = new PreBuiltXPackTransportClient(Settings.builder().put("cluster.name", "myClusterName").put("xpack.security.user", "elastic:changeme")....build()).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

正常的话做完上边两步后代码就能顺利执行了。不过我因为用的 Maven 库是公司的私服,在第一步添加依赖的时候始终加不上,而 x-pack-transport-5.2.1.jar 这个包直接以及间接依赖的包多达二十多个,一个个手动添加是不现实的,这时候只能开始处理 Maven 的配置问题。由于 Maven 平时我也只是直接使用,settings.xml 配置文件都是从前辈那里传下来的,没有仔细研究过,这次算是补补课了。

我的 ${HOME}/.m2/settings.xml 里之前 mirrors 节点的配置如下:

1
2
3
4
5
6
7
8
9
10
<mirrors><mirror>...</mirror><mirror><id>nexus</id><mirrorOf>*</mirrorOf><url>http://my.host.com/nexus/groups/public</url></mirror>
</mirrors>

问题是由<mirrorOf>*</mirrorOf>这个配置引起的,mirrorOf的含义如下:

mirrorOf:用来表示该 mirror 是关联的哪一个仓库,其值为其关联仓库的 id。当要同时关联多个仓库时,这多个仓库之间可以用逗号隔开;当要关联所有的仓库时,可以使用 “” 表示;当要关联除某一个仓库以外的其他所有仓库时,可以表示为 “,!repositoryId”;当要关联不是 localhost 或用 file 请求的仓库时,可以表示为 “external:*”。

既然找到了问题所在,那么对应的改之即可,即 id 为 elasticsearch-releases 的这个 repository 不再会关联到 nexus 这个镜像上

1
2
3
4
5
6
7
8
9
10
<mirrors><mirror>...</mirror><mirror><id>nexus</id><mirrorOf>*,!elasticsearch-releases</mirrorOf><url>http://my.host.com/nexus/groups/public</url></mirror>
</mirrors>

P.S.

You can also add an Authorization header to each request. If you’ve configured global authorization credentials, the Authorization header overrides the global authentication credentials. This is useful when an application has multiple users who access Elasticsearch using the same client. You can set the global token to a user that only has the transport_client role, and add the transport_client role to the individual users.

代码片段如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.security.authc.support.SecuredString;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;import static org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
...TransportClient client = new PreBuiltXPackTransportClient(Settings.builder().put("cluster.name", "myClusterName").put("xpack.security.user", "transport_client_user:changeme")....build()).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301))String token = basicAuthHeaderValue("elastic", new SecuredString("changeme".toCharArray()));client.filterWithHeader(Collections.singletonMap("Authorization", token)).prepareSearch().get();

关于 SSL 可以参考 Java Client and Security

ElasticSearch 实践过程中遇到的几个小问题相关推荐

  1. elasticsearch安装过程中的license问题解决办法

    elasticsearch安装过程中的license问题解决办法 参考文章: (1)elasticsearch安装过程中的license问题解决办法 (2)https://www.cnblogs.co ...

  2. 【Elasticsearch】Elasticsearch 查询过程中的 pre-filter 原理

    1.概述 转载:添Elasticsearch 查询过程中的 pre-filter 原理 大家都知道在对索引执行查询的时候,需要在所有的分片上执行查询,因为无法知道被查询的关键词位于哪个分片,对于全文查 ...

  3. python数据分析实训心得_Python代码在实践过程中的经验总结

    Python代码在实践过程中的经验总结 关于Python脚本,在具体的实践过程中经常会遇到一些问题,下面将其总结,便于使用.考虑使用 Logger(logger 怎么配置,需要输出哪些信息 - 可以反 ...

  4. linux中license路径,Elasticsearch安装过程中的license问题解决办法

    Elasticsearch安装过程中的license问题解决办法 1.git clone git://github.com/mobz/elasticsearch-head.git 2.cd elast ...

  5. 给定一个由n个圆盘组成的塔,这些圆盘按照大小递减的方式套在第一根桩柱上。现要将整个塔移动到另一根桩柱上,每次只能移动一个圆盘,且较大的圆盘在移动过程中不能放置在较小的圆盘上面

    对汉诺塔问题详解,给定一个由n个圆盘组成的塔,这些圆盘按照大小递减的方式套在第一根桩柱上.现要将整个塔移动到另一根桩柱上,每次只能移动一个圆盘,且较大的圆盘在移动过程中不能放置在较小的圆盘上面: 输入 ...

  6. Vue 实践过程中的几个问题

    前言 本篇是我在使用vue过程中,遇到的几个小问题和之前不了解的东西,记录下来,希望能够帮助各位踩坑.如果喜欢的话可以点波赞,或者关注一下,希望本文可以帮到大家. 本文首发于我的个人blog:obko ...

  7. elasticsearch部署过程中各种报错解析

    elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as r ...

  8. 盘点一个JS逆向过程中中文编解码的小案例

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 空山不见人,但闻人语响. 大家好, ...

  9. 设计过程中常见的 10 个小问题

    重读<别让我思考> 「好的设计是不言而喻的」(good design is obvious)这句话已经提出来很久了.而我相信在过去很长一段时间里,从事食品.音乐.建筑.服装.哲学和其它等领 ...

最新文章

  1. 图像处理之基础---极坐标系及其与直角坐标系的关系
  2. ubuntu下小键盘不能用
  3. LeetCode-二分查找-374. 猜数字大小
  4. 《深入理解Java虚拟机》笔记4——类文件结构
  5. Linux下判断字符串长度
  6. Android 使用LocationLstenser获取位置信息
  7. datetime 日期_用Hamcrest验证DateTime和日期
  8. python flask快速入门与进阶-Flask基础进阶与python flask实战bbs教程
  9. Linux驱动总结3- unlocked_ioctl和堵塞(waitqueue)读写函数的实现 【转】
  10. python django flask介绍,Django/Flask简介
  11. mysql cluster error 4010 157_关于mysql-cluster的一些问题想讨教各位经验人事
  12. ***YZJ的牛肉干***
  13. git/gitbub入门使用记录
  14. 美国三大股指再创新高:纳指开盘上涨0.29%
  15. 3D场景建模学习必备的基础知识
  16. #创新应用#飞流下载:下载娱乐两不误
  17. 小程序如何从0裂变开始获客?
  18. 用计算机弄音乐,视频剪辑添加音乐,如何在电脑上給视频添加音乐,什么添加音乐软件比较好用...
  19. 在嘲笑与冷眼下成长,并不断在泥潭中站起
  20. 负载均衡技术全攻略(大全)

热门文章

  1. html flash 循环播放,在网页中插入flv格式的flash视频怎么让其循环播放_html/css_WEB-ITnose...
  2. ngixn+tomcat负载均衡 动静分离配置 (nginx反向代理)
  3. html标签名缩写与英文全称对照表
  4. 如何查看一个现有的keil工程之前由什么版本的keil IDE编译
  5. Socket网络通讯开发总结之:Java 与 C进行Socket通讯
  6. 计算机三级网络操作题,计算机等级考试三级网络技术考试模拟试题
  7. pdf转图片 java_有将pdf文件转图片的格式方法吗?
  8. git bash here创建项目无法选择m_git 版本控制初学者指南
  9. html文档(.htm)不能正常阅读,HTM或HTML图标变成无法显示和识别的解决方法大全
  10. oracle := 和=,oracle a:=100 和 b=:c 区别