使用 Pacemaker 搭建集群服务

背景

对于一些重要应用,我们希望实现高可用(High-Availability). 在 RHEL7 中,可以使用 Pacemaker 达到这样的效果。

Pacemaker 是一个集群资源管理器,它负责管理集群环境中资源(服务)的整个生命周期。除了传统意义上的 Active/Passive 高可用,Pacemaker 可以灵活管理各节点上的不同资源,实现如 Active/Active,或者多活多备等架构。

下图是一个经典的 Active/Passive 高可用例子, Host1/Host2 只有一台服务器在提供服务,另一台服务器作为备机时刻准备着接管服务。

Pacemaker Active Passive
Pacemaker Active Passive

为了节省服务器资源,有时候可以采取 Shared Failover 的策略,两台服务器同时提供不同的服务,留有一台备用服务器接管失效的服务。

Pacemaker Shared Failover
Pacemaker Shared Failover

通过共享文件系统如 GFS2,也可以实现 Active/Active 的多活形式。

Pacemaker Active Active
Pacemaker Active Active

继续阅读“使用 Pacemaker 搭建集群服务”

Where will log goes during logrotate.

During logroate, if there comes a log entry, where will this log entry goes? To the rotated file, lost, or to the newly created file?

It depends.

The logrotate process

In RHEL 6 (centos 6), logrotate works like below:

1. Rename the original file. For example, /var/log/messages --> /var/log/messages.1

2. Create a new file. In this example, create an empty /var/log/messages

3. Run post-rotate script. For rsyslog, it would send a HUP signal to rsyslogd.

继续阅读“Where will log goes during logrotate.”

Socket listen() backlog 是什么?

listen() backlog 是什么?

1. Linux 服务器上的一些程序会监听特定端口.

2. 当有 client 连接到服务器的相应端口,进行完 tcp 三次握手之后,相应的连接会放到服务器相应 socket 的队列,等待服务器的应用程序调用 accept() 来读取这个连接;

3. 这个队列的长度,就是 listen() backlog.

以 apache httpd 为例。

1. httpd 会监听 80 端口;

2. 当有用户端连接到服务器的 80 端口,首先会进行 tcp 的三次握手。进行完三次握手后,这个连接会被放到 80 端口这个 socket 的队列里,等待 httpd 去获取这个连接;

3. httpd 随后会调用 accept() 来获取这个连接。这个连接被获取之后,会从这个队列里移除;

4. 这个监听 80 端口的 socket 对应的队列长度,就是 listen() backlog.
继续阅读“Socket listen() backlog 是什么?”

如何使用 Linux 中的 TCP keepalive?

tcp-keepalive 是什么

1. tcp-keepalive,顾名思义,它可以尽量让 TCP 连接“活着”,或者让一些对方无响应的 TCP 连接“宣告死亡”。

2. 一些特定环境,防火墙会自动断开长期无活动的 TCP 连接,tcp-keepalive 可以在连接无活动一段时间后,发送一个空 ack,使 TCP 连接不会被防火墙关闭。

3. 一些时候,对方的服务器可能出现宕机或者网络中断等问题, tcp-keepalive 可以帮助断开这些无响应的连接。

4. tcp-keepalive 需要在应用程序层面针对其所用到的 Socket 进行开启。操作系统层面无法强制所有 socket 启用 tcp-keepalive. (本文在 CentOS/RHEL 6/7 环境进行测试)

继续阅读“如何使用 Linux 中的 TCP keepalive?”

umask 怎样影响新建文件的权限?

1. umask 设置的不是默认权限,而是针对新建文件的一种权限限制。

2. 应用程序在创建文件时,会指定文件的权限 (mode)。而最终这个新建的文件的权限会是 (mode & ~umask).

# man 2 open

O_CREAT
    If  the  file does not exist it will be created.  The owner (user ID) of the file is set to the effective user ID of the process.  The group
    ownership (group ID) is set either to the effective group ID of the process or to the group ID of the parent directory  (depending  on  file
    system  type and mount options, and the mode of the parent directory, see the mount options bsdgroups and sysvgroups described in mount(8)).

    mode specifies the permissions to use in case a new file is created.  This argument must be supplied when O_CREAT is specified in flags;  if
    O_CREAT is not specified, then mode is ignored.  The effective permissions are modified by the process’s umask in the usual way: The permis-
    sions of the created file are (mode & ~umask).  Note that this mode only applies to future accesses of the newly created  file;  the  open()
    call that creates a read-only file may well return a read/write file descriptor.

继续阅读“umask 怎样影响新建文件的权限?”