반응형
우분투에서 Docker 데몬의 로그를 관리하는 방법
도커(Docker) 데몬의 로그를 관리하는 방법은 다양한 로그 관리 도구를 사용하여 가능합니다. 주로는 도커의 로깅 드라이버 설정 및 로그 파일 관리를 통해 로그를 관리합니다.
1. 로그 드라이버 설정(daemon.json 파일 생성)
Docker는 기본적으로 json-file 로그 드라이버를 사용하며, 로그 크기 및 보존 개수를 설정할 수 있습니다.
sudo vim /etc/docker/daemon.json
- daemon.json 파일이 없는 경우 새로 만들 수 있습니다.
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
- max-size: 로그 파일 하나당 최대 크기 (예: 10MB)
- max-file: 최대 보존 로그 파일 수 (예: 3개)
도커 데몬을 재시작합니다.
sudo systemctl restart docker
2. 로그 파일 위치
Docker 컨테이너 로그는 다음 위치에 저장됩니다
도커 컨테이너 로그 위치 : /var/lib/docker/containers/CONTAINER_ID/CONTAINER_ID-json.log
/var/lib/docker/containers/d2902eae/d2902eae-json.log
로그 확인
docker inspect <container_id> | grep LogPath
3. 긴급 용량 확보: 로그 비우기 (컨테이너 재시작 없이)
디스크가 가득 찬 상황에서는 아래 명령어로 컨테이너 로그를 안전하게 비울 수 있습니다
sudo truncate -s 0 /var/lib/docker/containers/<container_id>/<container_id>-json.log
※ rm 또는 echo "" > 방식은 Docker 동작에 문제를 일으킬 수 있으니 truncate 사용을 권장합니다.
728x90
4. logrotate를 통한 자동 로그 순환 설정
로그 파일 자동 순환을 설정하여 로그 축적을 방지할 수 있습니다.
설정 파일 생성 및 편집
sudo vim /etc/logrotate.d/docker
/var/lib/docker/containers/*/*.log
{
rotate 90
daily
maxsize 100M
dateext
#dateformat .%Y%m%d%H
missingok
notifempty
ifempty
compress
delaycompress
copytruncate
create
sharedscripts
}
- rotate 90 : 최대 90개까지 로그 보관
- daily : 매일 로그 순환
- maxsize 100M : 100MB 이상일 경우에도 순환
- compress : gzip 압축
- copytruncate : 로그 복사 후 원본 파일 비움(컨테이너 재시작 불필요)
logrotate 수동 실행 및 디버깅
강제 실행
logrotate -f /etc/logrotate.conf
디버그 실행
logrotate -d /etc/logrotate.conf
자세한 디버깅 + 강제 실행
logrotate -vdf /etc/logrotate.conf
$ logrotate -vdf /etc/logrotate.conf
...
rotating pattern: /var/lib/docker/containers/*/*.log
forced from command line (90 rotations)
empty log files are rotated, old logs are removed
considering log /var/lib/docker/containers/d290eae/d290eae-json.log
log needs rotating
rotating log /var/lib/docker/containers/d290eae/d290eae-json.log, log->rotateCount is 90
dateext suffix '-20211123'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
copying /var/lib/docker/containers/d290eae/d290eae-json.log to /var/lib/docker/containers/d290eae/d290eae-json.log-20211123
truncating /var/lib/docker/containers/d290eae/d290eae-json.log
...
logrotate 사용법
$ logrotate --help
Usage: logrotate [OPTION...] <configfile>
-d, --debug Don't do anything, just test (implies -v)
-f, --force Force file rotation
-m, --mail=command Command to send mail (instead of `/bin/mail')
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation
-l, --log=STRING Log file
--version Display version information
Help options:
-?, --help Show this help message
--usage Display brief usage message
logrotate 옵션
compress
Old versions of log files are compressed with gzip(1) by default. See also nocompress.
copytruncate
Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.
create mode owner group, create owner group
Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as the log file just rotated). mode specifies the mode for the log file in octal (the same as chmod(2)), owner specifies the user name who will own the log file, and group specifies the group the log file will belong to. Any of the log file attributes may be omitted, in which case those attributes for the new file will use the same values as the original log file for the omitted attributes. This option can be disabled using the nocreate option.
createolddir mode owner group
If the directory specified by olddir directive does not exist, it is created. mode specifies the mode for the olddir directory in octal(the same as chmod(2)), owner specifies the user name who will own the olddir directory, and group specifies the group the olddir direc‐tory will belong to. This option can be disabled using the nocreateolddir option. daily Log files are rotated every day.
dateext
Archive old versions of log files adding a date extension like YYYYMMDD instead of simply adding a number. The extension may be configured using the dateformat and dateyesterday options.
dateformat format_string
Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d %H and %s specifiers are allowed. The default value is -%Y%m%d except hourly, which uses -%Y%m%d%H as default value. Note that also the character separating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the date‐stamps generated by this format must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.
delaycompress
Postpone compression of the previous log file to the next rotation cycle. This only has effect when used in combination with compress. It can be used when some program cannot be told to close its logfile and thus might continue writing to the previous log file for some time.
ifempty
Rotate the log file even if it is empty, overriding the notifempty option (ifempty is the default).
maxsize size
Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with the time interval options, and it causes log files to be rotated without regard for the last rotation time. When maxsize is used, both the size and timestamp of a log file are con‐sidered.
missingok
If the log file is missing, go on to the next one without issuing an error message. See also nomissingok.
notifempty
Do not rotate the log if it is empty (this overrides the ifempty option).
postrotate/endscript
The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed (using /bin/sh) after the log file is rotated. These directives may only appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to the script. If sharedscripts is specified, whole pattern is passed to the script. See also prerotate. See sharedscripts and nosharedscripts for error handling.
prerotate/endscript
The lines between prerotate and endscript (both of which must appear on lines by themselves) are executed (using /bin/sh) before the log file is rotated and only if the log will actually be rotated. These directives may only appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to the script. If sharedscripts is specified, whole pattern is passed to the script. See also postrotate. See sharedscripts and nosharedscripts for error handling.
firstaction/endscript
The lines between firstaction and endscript (both of which must appear on lines by themselves) are executed (using /bin/sh) once before all log files that match the wildcarded pattern are rotated, before prerotate script is run and only if at least one log will actually be rotated. These directives may only appear inside a log file definition. Whole pattern is passed to the script as first argument. If the script exits with error, no further processing is done. See also lastaction.
rotate count
Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather than rotated.
size size
Log files are rotated only if they grow bigger then size bytes. If size is followed by k, the size is assumed to be in kilobytes. If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G are all valid.
sharedscripts
Normally, prerotate and postrotate scripts are run for each log which is rotated and the absolute path to the log file is passed as first argument to the script. That means a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscripts is specified, the scripts are only run once, no matter how many logs match the wildcarded pat‐tern, and whole pattern is passed to them. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. If the scripts exit with error, the remaining actions will not be executed for any logs. This option overrides the nosharedscripts option and implies create option.
su user group
Rotate log files set under this user and group instead of using default user/group (usually root). user specifies the user name used for rotation and group specifies the group used for rotation.
참고URL
- Docker Documentation : Configure logging driversConfigure logging drivers
728x90
반응형
'리눅스' 카테고리의 다른 글
Cloudflare 프록시 환경에서 Nginx의 액세스 로그에 클라이언트의 실제 IP 주소를 남기는 방법 (0) | 2023.09.13 |
---|---|
우분투에서 motd 메시지를 비활성화하는 방법(.hushlogin) (0) | 2023.09.13 |
Kafka Broker 및 ZooKeeper의 버전을 확인하는 방법 (0) | 2023.09.12 |
CentOS 7에서 ImageMagick을 설치하고 테스트하는 방법 (0) | 2023.09.12 |
MHA master_ip_failover 오류 (0) | 2023.09.12 |