1.将所有容器的输出路由到远程 rsyslog 服务器

        1.修改 rsyslog 配置文件/etc/rsyslog.conf, 从中找到 “# Provides UDP sysilog recepion"语句。并将该处的以下两行配置代码行首的“#”字符删除(取消注释)

[root@host1 ~]# vi /etc/rsyslog.conf
[root@host1 ~]# cat /etc/rsyslog.conf
# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# or latest version online at http://www.rsyslog.com/doc/rsyslog_conf.html 
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
global(workDirectory="/var/lib/rsyslog")

# Use default timestamp format
module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")

#### MODULES ####

module(load="imuxsock"   # provides support for local system logging (e.g. via logger command)
       SysSock.Use="off") # Turn off message reception via local log socket; 
  # local messages are retrieved through imjournal now.
module(load="imjournal"     # provides access to the systemd journal
       UsePid="system" # PID nummber is retrieved as the ID of the process the journal entry originates from
       FileCreateMode="0644" # Set the access permissions for the state file
       StateFile="imjournal.state") # File to store the position in the journal
#module(load="imklog") # reads kernel messages (the same are read from journald)
#module(load="immark") # provides --MARK-- message capability

# Include all config files in /etc/rsyslog.d/
include(file="/etc/rsyslog.d/*.conf" mode="optional")

# Provides UDP syslog reception
# for parameters see http://www.rsyslog.com/doc/imudp.html
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")

# Provides TCP syslog reception
# for parameters see http://www.rsyslog.com/doc/imtcp.html
#module(load="imtcp") # needs to be done just once
#input(type="imtcp" port="514")

#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log


# ### sample forwarding rule ###
#action(type="omfwd"  
# # An on-disk queue is created for this action. If the remote host is
# # down, messages are spooled to disk and sent when it is up again.
#queue.filename="fwdRule1"       # unique name prefix for spool files
#queue.maxdiskspace="1g"         # 1gb space limit (use as much as possible)
#queue.saveonshutdown="on"       # save messages to disk on shutdown
#queue.type="LinkedList"         # run asynchronously
#action.resumeRetryCount="-1"    # infinite retries if host is down
# # Remote Logging (we use TCP for reliable delivery)
# # remote_host is: name/ip, e.g. 192.168.0.1, port optional e.g. 10514
#Target="remote_host" Port="XXX" Protocol="tcp")

        2.保存该配置文件,执行 systemctl restart rsyslog 命令重启 rsyslog, 然后检查514端口是否
已开启

[root@host1 ~]# systemctl restart rsyslog
[root@host1 ~]# netstat -antup | grep 514
udp        0      0 0.0.0.0:514             0.0.0.0:*                           418069/rsyslogd     
udp6       0      0 :::514                  :::*                                418069/rsyslogd  

        3.启动Logspout 容器,将日志转发到 Docker 主机上的syslog日志系统中

[root@host1 ~]# docker run --rm --name="logspout" --volume=/var/run/docker.sock:/var/run/docker.sock 
gliderlabs/logspout syslog+udp://192.168.197.9:514
Unable to find image 'gliderlabs/logspout:latest' locally
latest: Pulling from gliderlabs/logspout
8572bc8fb8a3: Pull complete 
bd801371a862: Pull complete 
58100c398b34: Pull complete 
Digest: sha256:2d81c026e11ac67f7887029dbfd7d36ee986d946066b45c1dabd966278eb5681
Status: Downloaded newer image for gliderlabs/logspout:latest
2025/09/16 09:01:14 # logspout v3.2.14 by gliderlabs
2025/09/16 09:01:14 # adapters: udp multiline raw syslog tcp tls
2025/09/16 09:01:14 # options : 
2025/09/16 09:01:14 persist:/mnt/routes
2025/09/16 09:01:14 # jobs    : http[routes,health,logs]:80 pump routes
2025/09/16 09:01:14 # routes  :
#   ADAPTERADDRESSCONTAINERSSOURCESOPTIONS
#   syslog+udp192.168.197.9:514map[]

                接下来进行实际测试。

        4.打开另一个终端窗口(或者标签页,下同),执行tail-f/var/log/messages 命令使用 tail 工具实时监控系统日志文件/var/og/messages

[root@host1 ~]# tail -f /var/log/messages
Sep 16 17:01:14 host1 NetworkManager[1037]: <info>  [1758013274.5256] device (veth56301f4): carrier: link connected
Sep 16 17:01:14 host1 NetworkManager[1037]: <info>  [1758013274.5263] device (docker0): carrier: link connected
Sep 16 17:01:16 host1 avahi-daemon[1038]: Joining mDNS multicast group on interface veth56301f4.IPv6 with address fe80::f83d:e5ff:fed9:8a10.
Sep 16 17:01:16 host1 avahi-daemon[1038]: New relevant interface veth56301f4.IPv6 for mDNS.
Sep 16 17:01:16 host1 avahi-daemon[1038]: Registering new address record for fe80::f83d:e5ff:fed9:8a10 on veth56301f4.*.
Sep 16 17:02:24 host1 systemd-logind[1049]: New session 5 of user root.
Sep 16 17:02:24 host1 systemd[1]: Started Session 5 of User root.
Sep 16 17:02:25 host1 systemd[1]: Starting Hostname Service...
Sep 16 17:02:25 host1 systemd[1]: Started Hostname Service.
Sep 16 17:02:55 host1 systemd[1]: systemd-hostnamed.service: Deactivated successfully.

        5.再打开另一个终端窗口,执行以下命令启动一个Redis 容器

[root@host1 ~]# docker run --rm -d --name redis redis
638a1d615856362d3315b99e3d55d31ff8ac821376925c46ed88c8957fabd4b6

        6.回到 tail 工具监控窗口,会发现该窗口显示了该容器输出的日志信息

Sep 16 17:04:56 host1 systemd[1]: Started libcontainer container 638a1d615856362d3315b99e3d55d31ff8ac821376925c46ed88c8957fabd4b6.
Sep 16 09:04:57 638a1d615856 redis[508446] Starting Redis Server
Sep 16 09:04:57 638a1d615856 redis[508446] 1:C 16 Sep 2025 09:04:57.222 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
Sep 16 09:04:57 638a1d615856 redis[508446] 1:C 16 Sep 2025 09:04:57.222 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
Sep 16 09:04:57 638a1d615856 redis[508446] 1:C 16 Sep 2025 09:04:57.223 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
Sep 16 09:04:57 638a1d615856 redis[508446] 1:C 16 Sep 2025 09:04:57.223 * Configuration loaded
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.223 * monotonic clock: POSIX clock_gettime
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.228 * Running mode=standalone, port=6379.
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.231 * <bf> RedisBloom version 8.2.0 (Git=unknown)
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> Registering configuration options: [
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ bf-error-rate       :      0.01 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ bf-initial-size     :       100 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ bf-expansion-factor :         2 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ cf-bucket-size      :         2 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ cf-initial-size     :      1024 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ cf-max-iterations   :        20 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ cf-expansion-factor :         1 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> #011{ cf-max-expansions   :        32 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * <bf> ]
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.232 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.239 * <search> Redis version found by RedisSearch : 8.2.1 - oss
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.239 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.239 * <search> Low level api version 1 initialized successfully
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.240 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, 
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.240 * <search> Initialized thread pools!
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.240 * <search> Disabled workers threadpool of size 0
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.240 * <search> Subscribe to config changes
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.240 * <search> Enabled role change notification
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.241 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.241 * <search> Register write commands
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.242 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> Registering configuration options: [
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-compaction-policy   :              }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-num-threads         :            3 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-retention-policy    :            0 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-duplicate-policy    :        block }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-chunk-size-bytes    :         4096 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-encoding            :   compressed }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-ignore-max-time-diff:            0 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> #011{ ts-ignore-max-val-diff :     0.000000 }
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> ]
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> Detected redis oss
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.243 * <timeseries> Enabled diskless replication
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.244 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.245 * <ReJSON> Created new data type 'ReJSON-RL'
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.245 * <ReJSON> version: 80200 git sha: unknown branch: unknown
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V1 API
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V2 API
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V3 API
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V4 API
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V5 API
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Enabled diskless replication
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Initialized shared string cache, thread safe: false.
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.246 * <search> Acquired RedisJSON_V5 API
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.247 * Server initialized
Sep 16 09:04:57 638a1d615856 redis[508446] 1:M 16 Sep 2025 09:04:57.247 * Ready to accept connections tcp
Sep 16 17:04:57 host1 kernel: docker0: port 2(veth98f6e8e) entered blocking state
Sep 16 17:04:57 host1 kernel: docker0: port 2(veth98f6e8e) entered disabled state
Sep 16 17:04:57 host1 kernel: veth98f6e8e: entered allmulticast mode
Sep 16 17:04:57 host1 kernel: veth98f6e8e: entered promiscuous mode
Sep 16 17:04:57 host1 NetworkManager[1037]: <info>  [1758013497.0074] manager: (veth98f6e8e): new Veth device (/org/freedesktop/NetworkManager/Devices/11)
Sep 16 17:04:57 host1 kernel: eth0: renamed from veth65d7b4f
Sep 16 17:04:57 host1 kernel: docker0: port 2(veth98f6e8e) entered blocking state
Sep 16 17:04:57 host1 kernel: docker0: port 2(veth98f6e8e) entered forwarding state
Sep 16 17:04:57 host1 NetworkManager[1037]: <info>  [1758013497.0394] device (veth98f6e8e): carrier: link connected
Sep 16 17:04:59 host1 avahi-daemon[1038]: Joining mDNS multicast group on interface veth98f6e8e.IPv6 with address fe80::88ee:d3ff:fee1:da20.
Sep 16 17:04:59 host1 avahi-daemon[1038]: New relevant interface veth98f6e8e.IPv6 for mDNS.
Sep 16 17:04:59 host1 avahi-daemon[1038]: Registering new address record for fe80::88ee:d3ff:fee1:da20 on veth98f6e8e.*.
Sep 16 17:05:15 host1 systemd[1]: Starting dnf makecache...
Sep 16 17:05:16 host1 dnf[516372]: CentOS Stream 9 - BaseOS (Aliyun)               5.3 kB/s | 3.9 kB     00:00
Sep 16 17:05:17 host1 dnf[516372]: CentOS Stream 9 - AppStream (Aliyun)            8.0 kB/s | 4.4 kB     00:00
Sep 16 17:05:18 host1 dnf[516372]: Docker CE Stable - x86_64                       3.8 kB/s | 2.0 kB     00:00
Sep 16 17:05:18 host1 dnf[516372]: 元数据缓存已建立。
Sep 16 17:05:18 host1 systemd[1]: dnf-makecache.service: Deactivated successfully.
Sep 16 17:05:18 host1 systemd[1]: Finished dnf makecache.
Sep 16 17:05:18 host1 systemd[1]: dnf-makecache.service: Consumed 1.607s CPU time.

        7.切换到另一个终端窗口,使用 docker logs 命令获取 Redis 容器的日志信息,会发现获取的日志信息与Logspout 容器收集到的完全一致

[root@host1 ~]# docker logs redis
Starting Redis Server
1:C 16 Sep 2025 09:04:57.222 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 16 Sep 2025 09:04:57.222 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 16 Sep 2025 09:04:57.223 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
1:C 16 Sep 2025 09:04:57.223 * Configuration loaded
1:M 16 Sep 2025 09:04:57.223 * monotonic clock: POSIX clock_gettime
1:M 16 Sep 2025 09:04:57.228 * Running mode=standalone, port=6379.
1:M 16 Sep 2025 09:04:57.231 * <bf> RedisBloom version 8.2.0 (Git=unknown)
1:M 16 Sep 2025 09:04:57.232 * <bf> Registering configuration options: [
1:M 16 Sep 2025 09:04:57.232 * <bf> { bf-error-rate       :      0.01 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { bf-initial-size     :       100 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { bf-expansion-factor :         2 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { cf-bucket-size      :         2 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { cf-initial-size     :      1024 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { cf-max-iterations   :        20 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { cf-expansion-factor :         1 }
1:M 16 Sep 2025 09:04:57.232 * <bf> { cf-max-expansions   :        32 }
1:M 16 Sep 2025 09:04:57.232 * <bf> ]
1:M 16 Sep 2025 09:04:57.232 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
1:M 16 Sep 2025 09:04:57.239 * <search> Redis version found by RedisSearch : 8.2.1 - oss
1:M 16 Sep 2025 09:04:57.239 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
1:M 16 Sep 2025 09:04:57.239 * <search> Low level api version 1 initialized successfully
1:M 16 Sep 2025 09:04:57.240 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, 
1:M 16 Sep 2025 09:04:57.240 * <search> Initialized thread pools!
1:M 16 Sep 2025 09:04:57.240 * <search> Disabled workers threadpool of size 0
1:M 16 Sep 2025 09:04:57.240 * <search> Subscribe to config changes
1:M 16 Sep 2025 09:04:57.240 * <search> Enabled role change notification
1:M 16 Sep 2025 09:04:57.241 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
1:M 16 Sep 2025 09:04:57.241 * <search> Register write commands
1:M 16 Sep 2025 09:04:57.242 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
1:M 16 Sep 2025 09:04:57.243 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
1:M 16 Sep 2025 09:04:57.243 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
1:M 16 Sep 2025 09:04:57.243 * <timeseries> Registering configuration options: [
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-compaction-policy   :              }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-num-threads         :            3 }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-retention-policy    :            0 }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-duplicate-policy    :        block }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-chunk-size-bytes    :         4096 }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-encoding            :   compressed }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-ignore-max-time-diff:            0 }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> { ts-ignore-max-val-diff :     0.000000 }
1:M 16 Sep 2025 09:04:57.243 * <timeseries> ]
1:M 16 Sep 2025 09:04:57.243 * <timeseries> Detected redis oss
1:M 16 Sep 2025 09:04:57.243 * <timeseries> Enabled diskless replication
1:M 16 Sep 2025 09:04:57.244 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
1:M 16 Sep 2025 09:04:57.245 * <ReJSON> Created new data type 'ReJSON-RL'
1:M 16 Sep 2025 09:04:57.245 * <ReJSON> version: 80200 git sha: unknown branch: unknown
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V1 API
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V2 API
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V3 API
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V4 API
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Exported RedisJSON_V5 API
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Enabled diskless replication
1:M 16 Sep 2025 09:04:57.246 * <ReJSON> Initialized shared string cache, thread safe: false.
1:M 16 Sep 2025 09:04:57.246 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
1:M 16 Sep 2025 09:04:57.246 * <search> Acquired RedisJSON_V5 API
1:M 16 Sep 2025 09:04:57.247 * Server initialized
1:M 16 Sep 2025 09:04:57.247 * Ready to accept connections tcp

        8.实验完毕,停止 Redis 容器和 Logspout 容器(切换该容器运行的终端窗口,按CultC组合键),它们会被自动删除。

[root@host1 ~]# docker stop redis  
redis
[root@host1 ~]# docker stop logspout
Error response from daemon: No such container: logspout
[root@host1 ~]# docker ps -a
CONTAINER ID   IMAGE                          COMMAND                   CREATED        STATUS                      PORTS                                     NAMES
96632885a463   lagoudocker/cadvisor:v0.37.0   "/usr/bin/cadvisor -…"   18 hours ago   Exited (0) 18 hours ago                                               cadvisor-stdout
62b9981bf08e   php-nginx-supervisor:v1        "/usr/bin/supervisor…"   30 hours ago   Exited (255) 27 hours ago   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   php-web-app
7ecac05c7dac   centos-with-nginx:1.0          "nginx -g 'daemon of…"   5 days ago     Exited (255) 4 days ago     0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   test-nginx
0dbf7ea2178e   registry                       "/entrypoint.sh /etc…"   6 days ago     Exited (2) 39 minutes ago                                             myregistry
c8efe28b118e   busybox                        "sh"                      6 days ago     Exited (137) 6 days ago                                               stoic_raman
65a371a8cacb   httpd                          "httpd-foreground"        6 days ago     Exited (0) 6 days ago                                                 testweb
7ab5ac9bc007   ubuntu                         "/bin/bash"               6 days ago     Exited (0) 6 days ago                                                 inspiring_joliot
b97f426e8369   ubuntu                         "/bin/echo"               6 days ago     Exited (0) 6 days ago                                                 friendly_chaplygin
9ac8245b5b08   img-layers-test                "python /app/app.py"      8 days ago     Exited (0) 8 days ago                                                 crazy_kowalevski
49e91767b48d   ubuntu                         "/bin/bash"               8 days ago     Exited (0) 8 days ago                                                 frosty_lichterman
38b001022f7a   hello-world                    "/hello"                  8 days ago     Exited (0) 8 days ago                                                 intelligent_brahmagupta
[root@host1 ~]# 

2.通过 HTTP 查看 Logspout 收集的日志

        1.只需读取 HTTP数据流,即可实时查看生成的聚合日志。可以使用 Logspout 的 HTTP流模块,实时查看由它聚合的本地日志,而不用提供日志路由的URI,如执行以下操作:

[root@host1 ~]# docker run --rm -d --name="logspout" \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --publish=127.0.0.1:8000:80 \
  gliderlabs/logspout
cfd9130ed802c6d28dd2e67f85899510016d9f423586802bc6a6d447bd727e4a

        2.使用 curl 观察容器的日志流

[root@host1 ~]# curl http://127.0.0.1:8000/logs

        3.打开另一个终端窗口,执行容器操作。这里启动 Redis 容器,切回前面的终端窗口,会发现关于 Redis 容器启动的日志流,如下所示

[root@host1 ~]# curl http://127.0.0.1:8000/logs
           redis|Starting Redis Server
           redis|1:C 16 Sep 2025 14:32:02.923 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
           redis|1:C 16 Sep 2025 14:32:02.924 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
           redis|1:C 16 Sep 2025 14:32:02.924 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
           redis|1:C 16 Sep 2025 14:32:02.924 * Configuration loaded
           redis|1:M 16 Sep 2025 14:32:02.925 * monotonic clock: POSIX clock_gettime
           redis|1:M 16 Sep 2025 14:32:02.926 * Running mode=standalone, port=6379.
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> RedisBloom version 8.2.0 (Git=unknown)
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> Registering configuration options: [
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { bf-error-rate       :      0.01 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { bf-initial-size     :       100 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { bf-expansion-factor :         2 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { cf-bucket-size      :         2 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { cf-initial-size     :      1024 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { cf-max-iterations   :        20 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { cf-expansion-factor :         1 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> { cf-max-expansions   :        32 }
           redis|1:M 16 Sep 2025 14:32:02.927 * <bf> ]
           redis|1:M 16 Sep 2025 14:32:02.927 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
           redis|1:M 16 Sep 2025 14:32:02.932 * <search> Redis version found by RedisSearch : 8.2.1 - oss
           redis|1:M 16 Sep 2025 14:32:02.932 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Low level api version 1 initialized successfully
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, 
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Initialized thread pools!
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Disabled workers threadpool of size 0
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Subscribe to config changes
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Enabled role change notification
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
           redis|1:M 16 Sep 2025 14:32:02.933 * <search> Register write commands
           redis|1:M 16 Sep 2025 14:32:02.933 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> Registering configuration options: [
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-compaction-policy   :              }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-num-threads         :            3 }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-retention-policy    :            0 }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-duplicate-policy    :        block }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-chunk-size-bytes    :         4096 }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-encoding            :   compressed }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-ignore-max-time-diff:            0 }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> { ts-ignore-max-val-diff :     0.000000 }
           redis|1:M 16 Sep 2025 14:32:02.934 * <timeseries> ]
           redis|1:M 16 Sep 2025 14:32:02.935 * <timeseries> Detected redis oss
           redis|1:M 16 Sep 2025 14:32:02.935 * <timeseries> Enabled diskless replication
           redis|1:M 16 Sep 2025 14:32:02.935 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Created new data type 'ReJSON-RL'
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> version: 80200 git sha: unknown branch: unknown
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Exported RedisJSON_V1 API
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Exported RedisJSON_V2 API
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Exported RedisJSON_V3 API
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Exported RedisJSON_V4 API
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Exported RedisJSON_V5 API
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Enabled diskless replication
           redis|1:M 16 Sep 2025 14:32:02.936 * <ReJSON> Initialized shared string cache, thread safe: false.
           redis|1:M 16 Sep 2025 14:32:02.936 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
           redis|1:M 16 Sep 2025 14:32:02.936 * <search> Acquired RedisJSON_V5 API
           redis|1:M 16 Sep 2025 14:32:02.937 * Server initialized
           redis|1:M 16 Sep 2025 14:32:02.937 * Ready to accept connections tcp
[root@host1 ~]# curl http://127.0.0.1:8000/logs
           redis|1:signal-handler (1758033244) Received SIGTERM scheduling shutdown...
           redis|1:M 16 Sep 2025 14:34:04.256 * User requested shutdown...
           redis|1:M 16 Sep 2025 14:34:04.256 * Saving the final RDB snapshot before exiting.
           redis|1:M 16 Sep 2025 14:34:04.259 * DB saved on disk
           redis|1:M 16 Sep 2025 14:34:04.259 # Redis is now ready to exit, bye bye...
^C

        如果收集到多个容器的日志,则将以不同颜色显示不同容器的日志。

        4.实验完毕,停止Redis 容器和 Logspout容器,它们会被自动删除。

[root@host1 ~]# docker stop redis
redis

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐