Fork me on GitHub

MongoDB 生产环境笔记

MongoDB 生产环境笔记

在生产环境中,我们配置MongoDB需要注意点有很多,而不是一安装就可以使用。我们需要配置一些内核和系统参数。因为这些参数是会影响到我们 MongoDB 的性能的。
如果你的MongoDB 实例所在的服务器还有其它业务和应用,那么修改下面的参数需要注意是否会影响其它应用的性能和运行状态。

官方文档链接:https://docs.mongodb.com/manual/administration/production-notes/

需要配置的点有:

一、vm.zone_reclaim_mode 参数

设置内核参数 vm.zone_reclaim_mode ,该参数是设置当一个内存区域的内存耗尽的时候,是从内部回收,还是去下一个内存区域寻找,0为去下一个区域寻找,非0表示当前区域回收。

# 修改,重启后失效
sysctl -w vm.zone_reclaim_mode=0
# 永久修改
echo "vm.zone_reclaim_mode = 0" >> /etc/sysctl.conf 
sysctl -p
# 查询当前参数配置
sysctl -a |grep vm.zone_reclaim_mode

二、添加 swap 分区

如果内存空间不是那么充足的话,我们可以为系统配置 swap 分区。具体配置见文章 linux系统添加swap(虚拟内存)分区

对于 WiredTiger 储存引擎,在压力比较大的情况下,WiredTiger 会将数据放置在 swap 分区里。

三、设置 swappiness 参数

在 Linux 系统中,可以通过查看 /proc/sys/vm/swappiness 内容的值来确定系统对 SWAP 分区的使用原则。当swappiness 内容的值为 0 时,表示最大限度地使用物理内存,物理内存使用完毕后,才会使用 SWAP 分区。当swappiness 内容的值为 100 时,表示积极地使用 SWAP 分区,并且把内存中的数据及时地置换到 SWAP 分区。
默认值为 0,表示需要在物理内存使用完毕后才会使用 SWAP 分区,

如果我们运行的主机系统 RHEL / CentOS 的内核版本在 2.6.32-303 及以上,我们可以把该值设置为 1。

# 临时修改
sysctl -w vm.swappiness=1
# 永久修改
cat "vm.swappiness = 1" >> /etc/sysctl.conf 
sysctl -p
# 查看配置参数
sysctl -a |grep vm.swappiness

四、内核和文件系统版本

在 Linux 系统上运行 MongoDB ,我们建议使用 Linux内核版本2.6.36或者更高版本,使用 XFS 或者是 EXT4 文件系统,强烈建议使用 XFS 文件系统。因为 EXT4 和 WiredTiger 一起使用会有可能出现性能问题。

MongoDB 需要使用 glibc 库,最好版本至少是 2.13 上。

五、禁用 Transparent Huge Pages (THP)

在启动的时候我们可以看到类似的日志:

2019-04-12T10:11:26.665+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-04-12T10:11:26.665+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-04-12T10:11:26.665+0800 I CONTROL  [initandlisten]
2019-04-12T10:11:26.665+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-04-12T10:11:26.665+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

上面的警告就是意味着我们需要设置 /sys/kernel/mm/transparent_hugepage/defrag/sys/kernel/mm/transparent_hugepage/enabled 两个参数设置为 never ,上面参数的设置就是代表着 Transparent Huge Pages(THP), 在了解 THP 前,我们需要先了解下 Huge Pages(标准页),Huge Pages是从Linux Kernel 2.6后被引入的。目的是使用更大的内存页面(memory page size) 以适应越来越大的系统内存,让操作系统可以支持现代硬件架构的大页面容量功能。而THP(Transparent Huge Pages) 是从RHEL6 开始引入的一个功能,THP 是一个抽象层, 可以自动创建、管理和使用传统大页的大多数方面。

Huge pages can be difficult to manage manually, and often require significant changes to code in order to be used effectively. As such, Red Hat Enterprise Linux 6 also implemented the use of transparent huge pages(THP). THP is an abstraction layer that automates most aspects of creating, managing, and using huge pages.

THP hides much of the complexity in using huge pages from system administrators and developers. As the goal of THP is improving performance, its developers (both from the community and Red Hat) have tested and optimized THP across a wide range of systems, configurations, applications, and workloads. This allows the default settings of THP to improve the performance of most system configurations. However, THP is not recommended for database workloads.

在官方文档中最后一行写到: THP is not recommended for database workloads. 也就是 THP 不适用于在数据库上。因为数据库是不连续的内存访问模式,我们需要禁用THP以确保使用MongoDB获得最佳性能。

详细的有关 Huge Pages 和Transparent Huge Pages的介绍见 :文章

查看 Transparent Huge Pages 状态

cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
#如果输出结果为[always]表示 THP 启用了。[never]表示 THP 禁用、[madvise]表示(只在MADV_HUGEPAGE标志的VMA中使用THP

脚本禁用 THP

创建 init.d 脚本 /etc/init.d/disable-transparent-hugepages

#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
chmod 755 /etc/init.d/disable-transparent-hugepages # 设置具有可执行权限
chkconfig --add disable-transparent-hugepages # 设置开机自启

上面的配置需要重启主机才能生效。

生效配置

/etc/init.d/disable-transparent-hugepages start

配置 tuned 和 ktune

tunedktune是Red Hat和CentOS上可用的动态内核调优工具,可以禁用 THP 。

要在 tunedktune 中禁用 THP, 需要我们将配置文件的THP值设置为 never,否则 tuned 或者 ktune 会更改我们设置的值。

When RHEL 7 / CentOS 7 run in a virtual environment, the tuned tool automatically invokes a performance profile derived from performance throughput, which automatically sets the readahead settings to 4MB. This can negatively impact performance.

CentOS 6

cp -r /etc/tune-profiles/default /etc/tune-profiles/no-thp
echo  "set_transparent_hugepages never"  >>/etc/tune-profiles/no-thp/ktune.sh
tuned-adm profile no-thp

CentOS 7

mkdir /etc/tuned/no-thp
cat << EOF >>/etc/tuned/no-thp/tuned.conf
[main]
include=virtual-guest

[vm]
transparent_hugepages=never
EOF
tuned-adm profile no-thp

测试修改是否生效

cat /sys/kernel/mm/transparent_hugepage/enabled 
cat /sys/kernel/mm/transparent_hugepage/defrag  

生效配置:
always madvise [never]

六、ulimit 设置

通常系统默认给用户的最大进程数和最大可以打开的文件数是比较低的,所以在启动 MongoDB 的时候我们会看到以下警告。

WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.

官网的推荐配置是:

# 推荐配置
-f (file size): unlimited
-t (cpu time): unlimited
-v (virtual memory): unlimited [1]
-l (locked-in-memory size): unlimited
-n (open files): 64000
-m (memory size): unlimited [1] [2]
-u (processes/threads): 64000

我们可以直接更改用户的默认配置,也可以通过配置 systemd 服务,并参数写入 Service

cat <<EOF >>/usr/lib/systemd/system/mongodb.service
[Unit]
Description= mongodb service manager
[Service]
# Other directives omitted
# (file size)
LimitFSIZE=infinity
# (cpu time)
LimitCPU=infinity
# (virtual memory size)
LimitAS=infinity
# (locked-in-memory size)
LimitMEMLOCK=infinity
# (open files)
LimitNOFILE=64000
# (processes/threads)
LimitNPROC=64000
Type=forking
User=mongodb
Group=mongodb
PIDFile=/opt/mongodb/logs/mongod.pid
ExecStart= /opt/mongodb/bin/mongod -f /opt/mongodb/mongodb.conf
ExecStop= /opt/mongodb/bin/mongod --shutdown   --dbpath /opt/mongodb/data
Restart=always
[Install]
WantedBy=multi-user.target
EOF

七、tcp_keepalive_time

该参数用于 TCP 发送 keepalive 探测消息的间隔时间(秒),用于确认 TCP 连接是否有效。

查看系统的默认值:

# 通过sysctl 查看
sysctl net.ipv4.tcp_keepalive_time 
# 通过查看文件的值
cat /proc/sys/net/ipv4/tcp_keepalive_time
# 一般系统的默认值是 7200

Mongodb 官网建议将该值设置为 300 。

临时更改

systemctl  -w   net.ipv4.tcp_keepalive_time=300

永久更改,

echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf
sysctl -p

八、同步时间

如果我们部署的是副本集群,我们需要配置脚本,让这几台节点的的时间同步。

posted @ 2019-04-19 09:48  自由早晚乱余生  阅读(3030)  评论(0编辑  收藏  举报