Discourse Docker HW 保留/使用(CPU、RAM、磁盘)及管理方法

本周末,我花时间在运行 Discourse 安装的虚拟机上安装了一些外部工具(grafana、prometheus、一些导出器)以及一些分析工具(matomo),并设置了外部位置(AWS S3)的备份。自迁移以来已经两个月了,我终于有空闲时间来做这件事了。

在让指标等数据沉淀几天后,我回去查看了一下,发现根据 cadvisor 的数据,运行 discourse 的容器占用了几乎所有的可用内存:

这让我感到奇怪,因为从其他来源查看时似乎并非如此。然而,我注意到其他方面仍有一些我想更好地理解的地方。

例如,CPU 使用率有峰值,但平均而言,几乎所有时间都远高于 100%:

然而,这是 Docker Stats 的输出:

CONTAINER ID   NAME                    CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O        PIDS
254d80933447   grafana                 0.02%     83.34MiB / 29.38GiB   0.28%     4.2MB / 10.1MB    88.2MB / 13MB    14
78d8a523c667   prometheus              0.00%     114.6MiB / 29.38GiB   0.38%     741MB / 50.7MB    190MB / 201MB    14
d602e2724c7a   cadvisor                1.48%     67.52MiB / 29.38GiB   0.22%     12.3MB / 691MB    166MB / 4.08MB   24
4718b3629c8e   docker_state_exporter   0.00%     11.54MiB / 29.38GiB   0.04%     2.85MB / 38.8MB   2.7MB / 90.1kB   14
c5a211185855   app                     337.52%   7.543GiB / 29.38GiB   25.67%    365MB / 883MB     360GB / 67.3GB   282
9b95fa3156bb   matomo_cron             0.00%     7.504MiB / 29.38GiB   0.02%     1.48kB / 0B       762MB / 0B       3
553a3e7389eb   matomo_web              0.11%     9.832MiB / 29.38GiB   0.03%     106MB / 203MB     8.89MB / 33MB    9
adf21bdea1e5   matomo_app              0.01%     113.3MiB / 29.38GiB   0.38%     166MB / 146MB     1.26GB / 153MB   4
96d873027990   matomo_db               0.03%     99.66MiB / 29.38GiB   0.33%     63.8MB / 126MB    118MB / 310MB    15
9d21fdde2ec9   node_exporter           0.00%     9.887MiB / 29.38GiB   0.03%     3MB / 48.9MB      10.5MB / 299kB   6

我在论坛上阅读了一些内容,尝试将 unicorn 进程的数量从自动检测的 (8) 减少到 4,但没有看到 CPU/内存使用量有任何显著变化。

最后但同样重要的是,当我们从 vbulletin3 迁移数据库到 discourse 时,数据库本身大约是 7GB。今天查看时,我发现它增长了十倍。

du -sh /var/discourse/shared/standalone/* | sort -hr | head -n 10
70G     /var/discourse/shared/standalone/postgres_data
1.6G    /var/discourse/shared/standalone/uploads
807M    /var/discourse/shared/standalone/log
69M     /var/discourse/shared/standalone/redis_data
200K    /var/discourse/shared/standalone/postgres_run
28K     /var/discourse/shared/standalone/state
12K     /var/discourse/shared/standalone/tmp
12K     /var/discourse/shared/standalone/ssl
8.0K    /var/discourse/shared/standalone/backups
4.0K    /var/discourse/shared/standalone/postgres_backup

我猜这是 postgresql 在后台运行并创建了大量额外数据,但有什么办法可以至少控制它吗?

如果能提供帮助,这是 Discourse Doctor 的输出:

DISCOURSE DOCTOR Mon May 15 09:44:17 AM CEST 2023
OS: Linux vmi1229594.OMITTED.net 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux


Found containers/app.yml

==================== YML SETTINGS ====================
DISCOURSE_HOSTNAME=OMITTED
SMTP_ADDRESS=OMITTED
DEVELOPER_EMAILS=OMITTED
SMTP_PASSWORD=OMITTED
SMTP_PORT=OMITTED
SMTP_USER_NAME=OMITTED
LETSENCRYPT_ACCOUNT_EMAIL=OMITTED

==================== DOCKER INFO ====================
DOCKER VERSION: Docker version 23.0.1, build a5ee5b1

DOCKER PROCESSES (docker ps -a)

CONTAINER ID   IMAGE                                     COMMAND                  CREATED        STATUS                 PORTS                                      NAMES
254d80933447   grafana/grafana:latest                    “/run.sh”        8 hours ago    Up 7 hours             0.0.0.0:8443->3000/tcp                     grafana
78d8a523c667   prom/prometheus:latest                    “/bin/prometheus --c…”   8 hours ago    Up 8 hours             0.0.0.0:9090->9090/tcp                     prometheus
d602e2724c7a   gcr.io/cadvisor/cadvisor:latest           “/usr/bin/cadvisor -…”   8 hours ago    Up 8 hours (healthy)                                              cadvisor
4718b3629c8e   karugaru/docker_state_exporter            “/go/bin/docker_stat…”   8 hours ago    Up 8 hours                                                        docker_state_exporter
c5a211185855   local_discourse/app                       “/sbin/boot”             9 hours ago    Up 7 hours             0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   app
9b95fa3156bb   matomo:fpm                                “bash -c 'bash -s <…”   20 hours ago   Up 20 hours            9000/tcp                                   matomo_cron
553a3e7389eb   nginx:alpine                              “/docker-entrypoint.…”   21 hours ago   Up 21 hours            80/tcp, 0.0.0.0:2053->443/tcp              matomo_web
adf21bdea1e5   matomo:fpm-alpine                         “/entrypoint.sh php-…”   21 hours ago   Up 21 hours            9000/tcp                                   matomo_app
96d873027990   mariadb                                   “docker-entrypoint.s…”   21 hours ago   Up 21 hours            3306/tcp                                   matomo_db
9d21fdde2ec9   quay.io/prometheus/node-exporter:latest   “/bin/node_exporter …”   36 hours ago   Up 36 hours                                                       node_exporter

c5a211185855   local_discourse/app                       “/sbin/boot”             9 hours ago    Up 7 hours             0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   app
adf21bdea1e5   matomo:fpm-alpine                         “/entrypoint.sh php-…”   21 hours ago   Up 21 hours            9000/tcp                                   matomo_app

Discourse container app is running


==================== PLUGINS ====================
          - git clone https://github.com/discourse/docker_manager.git
          - git clone https://github.com/discourse/discourse-spoiler-alert.git
          - git clone https://github.com/discourse/discourse-animated-avatars.git
          - git clone https://github.com/discourse/discourse-whos-online.git
          - git clone https://github.com/discourse/discourse-bbcode.git
          - git clone https://github.com/discourse/discourse-signatures.git
          - git clone https://github.com/discourse/discourse-reactions.git
          - git clone https://github.com/paviliondev/discourse-legal-tools.git
          - git clone https://github.com/discourse/discourse-patreon.git
          - git clone https://github.com/discourse/discourse-yearly-review.git
          - git clone https://github.com/discourse/discourse-user-notes.git
          - git clone https://github.com/merefield/discourse-user-network-vis.git
          - git clone https://github.com/discourse/discourse-calendar.git
          - git clone https://github.com/discourse/discourse-prometheus.git

WARNING:
You have what appear to be non-official plugins.
If you are having trouble, you should disable them and try rebuilding again.

See https://github.com/discourse/discourse/blob/main/lib/plugin/metadata.rb for the official list.

========================================
Discourse 3.1.0.beta4
Discourse version at OMITTED: Discourse 3.1.0.beta4
Discourse version at localhost: Discourse 3.1.0.beta4


==================== MEMORY INFORMATION ====================
RAM (MB): 31550

               total        used        free      shared  buff/cache   available
Mem:           30088        3958        1307        4269       24823       21475
Swap:           8191        1140        7051

==================== DISK SPACE CHECK ====================
---------- OS Disk Space ----------
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       194G   93G   91G  51% /

---------- Container Disk Space ----------
Filesystem      Size  Used Avail Use% Mounted on
overlay         194G   93G   91G  51% /
/dev/sda3       194G   93G   91G  51% /shared
/dev/sda3       194G   93G   91G  51% /var/log

==================== DISK INFORMATION ====================
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: OMITTED

Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   4194303   4190208    2G Linux filesystem
/dev/sda3  4194304 419428351 415234048  198G Linux filesystem

==================== END DISK INFORMATION ====================

==================== MAIL TEST ====================
For a robust test, get an address from http://www.mail-tester.com/
Or just send a test message to yourself.
Email address for mail test? ('n' to skip) [OMITTED]: n
Mail test skipped.
Replacing: SMTP_PASSWORD
Replacing: LETSENCRYPT_ACCOUNT_EMAIL
Replacing: DEVELOPER_EMAILS
Replacing: DISCOURSE_DB_PASSWORD
Replacing: Sending mail to

==================== DONE! ====================

感谢 @JammyDodger,没注意到我在 Support :+1:

1 个赞

简而言之,我会说:

  • 除非 CPU 使用率饱和所有核心,否则不必担心。
  • 不必担心 RAM 使用率,而要担心交换空间活动。
  • 可能会开一个新帖子讨论数据库大小增长 10 倍的问题。

如果您的系统提供交换空间活动指标,请使用它们。如果它不提供,请查看承载交换空间的设备的磁盘活动。

明白了,我读到内存(RAM)充足时,不必太担心 SWAP,所以我创建了 8GB,以防万一,如果这说得通的话。

您能否详细说明一下,在 SWAP 活动方面,我应该担心什么?

我在仪表板上能找到的与 SWAP 相关的内容如下:

这是完整的内存堆栈:

我习惯于在工作中处理容器/k8s,所以虚拟机层面的这些细节对我来说很模糊,不知道我应该关注什么。

如果您有一些入门级的链接,而您又不想/没时间在这里写长篇大论,我仍然很感激 :slight_smile:

感谢您提供的图片 - 我主要会关注第一张图,即每秒页数。如果您看到持续的活动,那么您就需要更多的内存。像您图片中显示的短暂峰值是可以接受的。您显示的峰值低于 1000,您的系统应该运行正常。因此,请留意每秒页数持续超过 500 的情况。

本质上,内存(RAM)速度快,而交换空间(swap)速度慢。操作系统会尽可能多地利用内存,这就是为什么“未使用的内存”难以衡量或思考的原因。在您的情况下,大块的红色部分是用于缓存磁盘内容的内存,这有助于提高应用程序性能。如果绿色部分(应用程序使用)增长到超过四分之三,那将是令人担忧的。对于某些工作负载来说,可能超过一半就会令人担忧。

但真正损害性能的是交换空间活动,因为交换空间速度慢。一定量的交换空间使用量并不重要:那是顶部的切片,紫色的部分。在您的情况下,最大交换空间使用量低于 2G,而您有 8G - 您有很大的容量。如果最大交换空间使用量接近您拥有的交换空间总量,那么您的系统可能即将崩溃。否则,则无需担心。

因此,请留意持续的交换空间活动,或者留意红色磁盘缓存被绿色应用程序使用所挤占的情况。

2 个赞

Ed,非常感谢您详细的解释,非常感激。我稍后会尝试使用警报,所以这真的很有帮助 :slight_smile:

2 个赞