如何更改 /etc/security/limits.d/ 中的堆栈深度限制并在启动时将更改应用于服务

Jam*_*ell 6 postgresql ubuntu ulimit ansible

我的系统

  • Ubuntu 14.04.5 (x86_64) 服务器系列,不断更新
  • 我的应用程序需要我增加 postgres 的堆栈深度
  • 我在/etc/security/limits.d/myapplication.conf 中创建了一个文件
  • myapplication.conf档案中有一行:* - stack 131072
  • 请注意,131072KB == 128MB
  • 制作此myapplication.conf文件后,我ulimit -s返回:131072
  • 然后我编辑了我的/etc/postgresql/9.3/main/postgresql.conf文件并附加了以下行:max_stack_depth = 126MB


我的问题

在引导期间,会出现以下消息:

 * The PostgreSQL server failed to start. Please check the log output:
2018-01-24 09:27:53 MST LOG:  invalid value for parameter "max_stack_depth": 129024
2018-01-24 09:27:53 MST DETAIL:  "max_stack_depth" must not exceed 7680kB.
2018-01-24 09:27:53 MST HINT:  Increase the platform's stack depth limit via "ulimit -s" or local equivalent.
2018-01-24 09:27:53 MST FATAL:  configuration file "/etc/postgresql/9.3/main/postgresql.conf" contains errors
 * Starting Mount network filesystems                                    [ OK ]
 * Starting PostgreSQL 9.3 database server        * Stopping Mount networ[ OK ]systems
                                                                                             [fail] 
Run Code Online (Sandbox Code Playgroud)

这反过来会导致我的应用程序服务失败,因为我依赖于我的数据库。开机后,如果我启动postgres服务,就没事了:

dev@wipn:~$ sudo service postgresql start
 * Starting PostgreSQL 9.3 database server                                                                                                                                                                                                                                                                             [ OK ] 
dev@wipn:~$ 
Run Code Online (Sandbox Code Playgroud)

我的猜测是/etc/security/limits.d/myapplication.conf的影响仅适用于启动期间的某个阶段,即我的系统尝试启动 postgres 之后的阶段。因此,也许一个明显的解决方案是在我启动 postgres 时进行更改,这很好,我可以处理。


我的问题

有什么方法可以改变内核的堆栈深度,这样我只需要对我的服务器做最小的改动?

我想要尽可能干净的东西。我希望它能够承受升级,并且最好适用于其他发行版。我通过 Ansible 剧本管理我的东西,所以我宁愿为此写一个干净的剧本。

可能只是更改我的服务的启动顺序是最佳解决方案。有人知道其他合适的选择吗?


我尝试过的事情

这是我尝试过的一些事情的列表,但没有成功。

/etc/security/limits.d/myapplication.conf 中

  • postgres - stack 131072
  • * - stack 131072 root - stack 131072

Jam*_*ell 1

在有人能给我一个干净的解决方案之前,这就是我想出的,而且很糟糕。我不会接受它作为我问题的答案,但它就是(笑)。至少它有效。


背景

似乎对/etc/security/limits.*的更改永远不会影响服务,而是会影响从 shell 执行的内容。因此,这使得我对/etc/security/limits.*的更改变得毫无意义。(此处插入咒骂)。我现在已经删除了/etc/security/limits.d/myapplication.conf


更改 postgres 的堆栈大小限制

这是一个垃圾解决方案。我恨它。

我编辑了“/usr/share/postgresql-common/init.d-functions”,特别是start()函数,显示为:

...
# start all clusters of version $1
# output according to Debian Policy for init scripts
start() {
    ulimit -s 131072    #JTS: To avoid Issue #XYZ 

    # create socket directory
    if [ -d /var/run/postgresql ]; then
        chmod 2775 /var/run/postgresql
    else
    ...
Run Code Online (Sandbox Code Playgroud)

显然我已经添加了 ulimit 行。修改这个文件对我来说很恶心,因为我希望它会通过更新而永久更改。至少我有一个 Ansible 规则来强制执行它。


我的 Ansible 解决方案

这是我创建的 Ansible 任务,用于强制执行此配置更改:

- blockinfile:
    dest: /usr/share/postgresql-common/init.d-functions
    block: |
          ulimit -s 131072
    backup: yes
    insertafter: '^start\(\) \{'
    state: present
Run Code Online (Sandbox Code Playgroud)

此 Ansible 任务导致函数如下所示:

...
# start all clusters of version $1
# output according to Debian Policy for init scripts
start() {
# BEGIN ANSIBLE MANAGED BLOCK
ulimit -s 131072
# END ANSIBLE MANAGED BLOCK
    # create socket directory
    if [ -d /var/run/postgresql ]; then
       ...
Run Code Online (Sandbox Code Playgroud)


值得注意的是:Upstart 服务忽略/etc/security/limits

Ubuntu 14.04 使用的 Upstart 似乎忽略了/etc/security/limits.* 我的应用程序服务实际上使用 upstart,并且可以为 upstart 插入一行,如下所示:

limit stack <softlimit> <hardlimit>
Run Code Online (Sandbox Code Playgroud)

Ubuntu 在 14.04 之后切换到 systemd,因此这个新贵的小玩意将逐渐变得无关紧要。

这与我的问题无关,因为在 14.04 上,postgresql不是由 upstart 管理的。