Docker容器日志输出到syslog

编程

检查是否安装syslog

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

rsyslogd -v
rsyslogd 8.24.0, compiled with:
    PLATFORM:                x86_64-redhat-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
    Number of Bits in RainerScript integers: 64

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

 

开启 Rsyslog 服务

vim /etc/rsyslog.conf

    #module(load="imtcp")

    #input(type="imtcp" port="516")

去掉前面的注释符 #

vim /etc/rsyslog.d/rsyslog_docker.conf

添加以下配置

# Rsyslog configuration file.

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

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

 

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

重启服务

systemctl restart rsyslog

查看是否正常启动

$ netstat -anpt | grep 516 

tcp 0 0 0.0.0.0:516 0.0.0.0:* LISTEN 7290/rsyslogd

tcp6 0 0 :::516 :::* LISTEN 7290/rsyslogd

 

运行容器

docker-compose up -d

 

 

解读:

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

version: "3.7"

services:

auth-swagger:

image: auth-swagger:v0.0.8

restart: always

ports:

- 3008:8080

environment:

- URL=swagger.json

depends_on:

- auth

networks:

- defnet

auth:

image: auth:v0.0.8

restart: always

ports:

- 5001:5000

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

version: "3.7"

services:

auth-swagger:

image: 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: auth:v0.0.8

restart: always

ports:

- 5001:5000

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/syslogs# ls auth*

auth.log auth-swagger.log

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

以上是 Docker容器日志输出到syslog 的全部内容, 来源链接: utcz.com/z/518684.html

回到顶部