用Supervisord运行PostgreSQL [英] Running PostgreSQL with Supervisord

查看:336
本文介绍了用Supervisord运行PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Ubuntu 10.04上使用Supervisor运行PostgreSQL 9.1。此刻,我使用init脚本手动启动PostgreSQL:

I want to run PostgreSQL 9.1 using Supervisor on Ubuntu 10.04. At the moment, I manually start PostgreSQL using the init script:

/etc/init.d/postgresql start

根据此帖子: http://nicksergeant.com/using-postgresql-with-supervisor-on-ubuntu-1010/ ,我需要修改PostgreSQL配置为了使PostgreSQL与Supervisor一起工作,它运行在TCP端口而不是Unix套接字上。

According to this post: http://nicksergeant.com/using-postgresql-with-supervisor-on-ubuntu-1010/, I need to modify the PostgreSQL config to make it run on TCP port instead of Unix socket, in order to make PostgreSQL work with Supervisor.

我对此方法有两个疑问:

I have two questions regarding this approach:


  1. 考虑到这更多的是hack,这样做有什么含义(例如安全性/权限,性能等)吗?

  1. Considering this is more of hack, is there any implication (e.g. security/permissions, performance, etc) of doing this?

为什么我们不能只在Supervisor配置中运行相同的初始化脚本 postgresql ?相反,如上面的链接所示,它运行 postmaster

Why cannot we just run the same init script postgresql in Supervisor config? Instead, as shown in the link above, it runs postmaster?

更新

由于以下两个答案的有用建议,我为Supervisor设置了一个脚本来直接调用PostgreSQL:

Thanks to the useful suggestions from both answers below, I have setup a script for Supervisor to invoke PostgreSQL directly:

#!/bin/sh

# This script is run by Supervisor to start PostgreSQL 9.1 in foreground mode

if [ -d /var/run/postgresql ]; then
    chmod 2775 /var/run/postgresql
else
    install -d -m 2775 -o postgres -g postgres /var/run/postgresql
fi

exec su postgres -c "/usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf"

我还设置了配置: /etc/postgresql/9.1/main/ start.conf manual ,这样PostgreSQL不会在启动时自动启动(但是,我不清楚是否已加载此配置)。然后,我为postgres设置Supervisor配置为:

I also set the config: /etc/postgresql/9.1/main/start.conf to manual so that PostgreSQL does not start automatically on boot (however, it's not clear to me whether this config is loaded). And then I setup the Supervisor config for postgres as:

[program:postgres]
user=root
group=root
command=/usr/local/bin/run_postgresql.sh
autostart=true
autorestart=true
stderr_logfile=/home/www-data/logs/postgres_err.log
stdout_logfile=/home/www-data/logs/postgres_out.log
redirect_stderr=true
stopsignal=QUIT

现在,我可以通过执行 start postgres supervisorctl 中启动PostgreSQL。 c>,运行正常。 但是,在我发出 stop postgres 之后,尽管 supervisorctl 声明postgres已停止,但服务器显然仍在运行

So now, I can start PostgreSQL in supervisorctl by doing start postgres, which runs fine. However, after I issue stop postgres, although supervisorctl declares postgres is stopped, the server apparently is still running as I can psql into it.

我想知道这是Supervisor配置问题还是PostgreSQL问题。欢迎任何建议!

I wonder if this is a Supervisor config issue, or a PostgreSQL issue. Any suggestion welcome!

推荐答案

该博客文章写得很糟糕。没有 TCP模式:帖子的建议方法仍将在Unix套接字上的另一个目录中侦听。帖子中的评论,例如外部pid文件-TCP模式不需要。

The blog post is rather badly written. There is no "TCP mode": the post's suggested method will still listen on a Unix socket, just in a different directory. Comments in the post such as "external pid file - not needed for TCP mode" are very misleading.

postmaster 是postgresql可执行文件的传统名称(以区分主分配过程和后端从属)。一段时间以来,没有单独的可执行文件,现在仅以 postgres形式安装。

postmaster is the traditional name for the postgresql executable (to distinguish the master dispatching process from the backend slaves). For some time now there has been no separate executable, and now it is installed as simply "postgres".

假定Supervisor大致类似于qmail / daemontools supervise 方案,完全有可能(实际上,很正常)让它运行一个设置目录和环境的脚本,然后执行带有必需参数(或传播给包装器脚本提供的参数,这在监督中是不寻常的,但是当您有一个配置文件将参数放入其中时更有意义)。

Assuming that Supervisor is broadly simliar to the qmail/daemontools supervise scheme, it would be entirely possible (in fact, quite normal) to have it run a script that sets up the directories and environment, and then execs postgres with the requisite arguments (or propagates arguments given to the wrapper script, which would be unusual with supervise but makes more sense when you have a config file to put arguments into).

supervise 起作用了(我将继续假设 Supervisor是相同的)是让主管进程按指定的方式运行一个子进程,并在退出时简单地重新启动一个新的子进程。这是基于以下想法:启动的进程是一个长期存在的守护进程,只有在出现严重错误时才退出,而仅将其重新启动才是有效的解决方案。相比之下,诸如 /etc/init.d 中的初始化脚本将运行子进程并将其分离,并将控制权返回给其调用者-如果子进程退出,则不会发生任何特殊情况,并且必须手动重新启动。如果您试图从supervise运行 /etc/init.d/postgresql start ,它将连续不断地生成postgresql守护程序,因为从init脚本返回的内容将解释为守护进程已经退出,而实际上它已经启动并脱离了。

The way supervise worked (and I'm going to continue to assume "Supervisor" is the same) is to have the supervisor process run a subprocess as specified, and simply relaunch a new subprocess if it exits. This is based on the idea that the process being launched is a long-lived daemon process that only exits when something goes badly wrong, and that simply restarting it is a valid fix. By contrast, init scripts such as in /etc/init.d run the subprocess and detach it, and return control to their caller- if the subprocess exits, nothing special happens, and it must be restarted manually. If you tried to simply run /etc/init.d/postgresql start from supervise, it would continuously keep spawning postgresql daemons, as the return from the init script would be interpreted as the daemon process having exited, when in fact it had been started and detached.

这篇关于用Supervisord运行PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆