Whoosy's Blog

藏巧于拙 用晦而明 寓清于浊 以屈为伸

0%

Docker容器日志输出到syslog

Docker容器日志输出到syslog

检查是否安装syslog

syslog 是 linux 强大的日志系统,Rsyslog 是 syslog 的标准实现,linux 已经默认安装,查看一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ rsyslogd -v
rsyslogd 8.32.0, compiled with:
PLATFORM: x86_64-pc-linux-gnu
PLATFORM (lsb_release -d):
FEATURE_REGEXP: Yes
GSSAPI Kerberos 5 support: Yes
FEATURE_DEBUG (debug build, slow code): No
32bit Atomic operations supported: Yes
64bit Atomic operations supported: Yes
memory allocator: system default
Runtime Instrumentation (slow code): No
uuid support: Yes
systemd support: Yes
Number of Bits in RainerScript integers: 64

See http://www.rsyslog.com for more information.

开启 Rsyslog 服务

vim /etc/rsyslog.conf

1
2
#module(load="imtcp")
#input(type="imtcp" port="514")

去掉前面的注释符 #

vim /etc/rsyslog.d/rsyslog_docker.conf

添加以下配置

1
2
3
4
5
6
7
# Rsyslog configuration file.

template(name="DynaFile" type="string" string="/var/log/docker/%programname%.log")

if $programname != "rsyslogd" then {
action(type="omfile" dynaFile="DynaFile")
}

日志会输出到/var/log/docker/目录下

重启服务

1
systemctl restart rsyslog

查看是否正常启动

1
2
3
$ netstat -anpt | grep 514
tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 7290/rsyslogd
tcp6 0 0 :::514 :::* LISTEN 7290/rsyslogd

运行容器

docker run

1
2
3
4
5
6
docker run -d -p 8888:80 \
--name nginx_container \
--log-driver syslog \
--log-opt syslog-address=tcp://172.26.182.230:514 \
--log-opt tag="soc_nginx" \
nginx:1.16.1

解读:

1.IP为宿主机的IP

2.输出日志名为: {$tag}.log

docker-compose

以这个docker-compose.yml为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
version: '3.7'

services:
auth-swagger:
image: harbor.socmap.org/bs-auth/auth-swagger:v0.0.8
restart: always
ports:
- 3008:8080
environment:
- URL=swagger.json
depends_on:
- auth
networks:
- defnet

auth:
image: harbor.socmap.org/bs-auth/auth:v0.0.8
restart: always
ports:
- 5001:5000
env_file:
- auth-prod.env
environment:
- FLASK_APP=project:create_app
networks:
- defnet

networks:
defnet:
ipam:
driver: default
config:
- subnet: "172.60.0.0/16"

修改之后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
version: '3.7'

services:
auth-swagger:
image: harbor.socmap.org/bs-auth/auth-swagger:v0.0.8
restart: always
ports:
- 3008:8080
environment:
- URL=swagger.json
depends_on:
- auth
logging:
driver: "syslog"
options:
syslog-address: "tcp://192.168.17.184:514"
tag: auth-swagger
networks:
- defnet

auth:
image: harbor.socmap.org/bs-auth/auth:v0.0.8
restart: always
ports:
- 5001:5000
env_file:
- auth-prod.env
environment:
- FLASK_APP=project:create_app
logging:
driver: "syslog"
options:
syslog-address: "tcp://192.168.17.184:514"
tag: auth-swagger
networks:
- defnet

networks:
defnet:
ipam:
driver: default
config:
- subnet: "172.60.0.0/16"

查看日志

1
2
root@HP-OMEN:/var/log/docker# ls auth*
auth.log auth-swagger.log

日志名就是刚才设置的tag的值

最后

关于流程上

运维负责配置好服务器上的syslog服务,确定日志存放位置;因为落地的项目syslog部署位置都不一样,所以需要在docker-compose.yml文件中改一下IP,改成物理机的内网地址即可,也就是这个配置:syslog-address: "tcp://192.168.17.184:514" 只需在部署时把docker-compose.yml文件按上述配置修改一下即可!