目录
清理docker日志
编写查询脚本
编写清理脚本
设置docker日志大小
服务器的应用突然不能访问了,系统提示: No space left on device,得知是由于服务器磁盘满了所致
使用df -h 查看空间
[root@m72 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 104G 104G 20K 100% /
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 32G 2.0G 30G 7% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 497M 159M 339M 32% /boot
tmpfs 6.3G 0 6.3G 0% /run/user/0
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/3f17bc7ecc97a62a2f9dee897e309952fde7d9405420311a3136d8946bae90f9/merged
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/9c25266154cda67be2cfe68fc4772fd462c8232434f62d33ce1c63ff833b718c/merged
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/27827ad4a1a57d5db7a5ec60228719fae33aba44b740a7b940ccd562dd801f0e/merged
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/c7c3d258820f23330fb91a702c745e45fa4bc66386833d1f6cb7257a099e2ef0/merged
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/6b7680181c55df3c961489706f094a0225fab2722c17a608e2bb3c70e6b2415a/merged
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/4c07b02860762d5a5f92c7239032560041d5da9c93ffc2cdf7e019cd5132a590/merged
overlay 104G 104G 20K 100% /var/lib/docker/overlay2/b9016d58a90894a65a9863dc8f93de4071761d011b167de4a37638b57d2eeb10/merged
看到磁盘占用100%了,下面overlay那里可以看到是docker镜像的原因,排查docker日志
docker容器日志一般默认位于/var/lib/docker/containers/xxxxx/中,xxx-json.log的文件
[root@m72 /]# du -h --max-depth=1 /var/lib/docker/containers/
76G /var/lib/docker/containers/6d525e609df974dc9c021619f31676ab2804a3d87f47d8a933ef44999589f80a
24K /var/lib/docker/containers/cf12d6f6d13879fa98cb5a3a9f0e5b258dcb6c692831bf15fd7663116ee7c596
24K /var/lib/docker/containers/72d2190f7ac03a3c39f796658475d8d0d2dbb99e9563654a52bcae7dbd3588fe
24K /var/lib/docker/containers/01baa3897dc4982d4446e5a6a5b352e2ab0840a895d6756d27fec27bcd942b39
28K /var/lib/docker/containers/00dc49f7f836f18d61fc16f4ce1123800b0b0ea87d7c751a326f41e2f8bc4151
28K /var/lib/docker/containers/f2c3a9f843690c0a29454b309f8e03a6228445616a696acf42430922cc0f3917
24K /var/lib/docker/containers/5552fc25f27ce77f9b05dc301e2dce558f4d5549f66fc493305d1c79b29fa3b2
24K /var/lib/docker/containers/f5886a54904287707656710e1e7768eaf987608d6b46b7845437e349c8546eed
76G /var/lib/docker/containers/
有个镜像占用76G,应该就是他了,cd 进入该目录
-rw-r-----. 1 root root 81423171584 Aug 1 10:02 6d525e609df974dc9c021619f31676ab2804a3d87f47d8a933ef44999589f80a-json.log
drwx------. 2 root root 6 Jun 7 16:07 checkpoints
-rw-------. 1 root root 27382 Aug 1 09:59 config.v2.json
-rw-r--r--. 1 root root 1419 Aug 1 09:59 hostconfig.json
-rw-r--r--. 1 root root 13 Jun 21 15:23 hostname
-rw-r--r--. 1 root root 174 Jun 21 15:23 hosts
-rw-r--r--. 1 root root 84 Jun 21 15:23 resolv.conf
-rw-r--r--. 1 root root 71 Jun 21 15:23 resolv.conf.hash
drwxrwxrwt. 3 root root 80 Jun 21 15:29 shm
然后
cat /dev/null > *-json.log
注意不要使用 rm -rf,rm 的话会从文件系统目录结构上解除链接,如果运行的进程正在使用该文件的话,磁盘空间会一直被占用。或者使用rm -rf后重新启动docker。
为了方便我们以后排查和处理类似问题,我们编写一个脚本,以后进行自动化处理
[root@m72 ops]# touch find_docker_logs.sh
[root@m72 ops]# vi find_docker_logs.sh
增加如下内容
#!/bin/sh
echo "------------------docker containers logs size----------------------"
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs
do
ls -lh $log
done
echo "-------------------------------------------------------------------"
添加权限并执行查看
[root@m72 ops]# chmod +x find_docker_logs.sh
[root@m72 ops]# sh find_docker_logs.sh
------------------docker containers logs size----------------------
-rw-r-----. 1 root root 37M Aug 1 10:15 /var/lib/docker/containers/6d525e609df974dc9c021619f31676ab2804a3d87f47d8a933ef44999589f80a/6d525e609df974dc9c021619f31676ab2804a3d87f47d8a933ef44999589f80a-json.log
-rw-r-----. 1 root root 0 Aug 1 10:01 /var/lib/docker/containers/cf12d6f6d13879fa98cb5a3a9f0e5b258dcb6c692831bf15fd7663116ee7c596/cf12d6f6d13879fa98cb5a3a9f0e5b258dcb6c692831bf15fd7663116ee7c596-json.log
-------------------------------------------------------------------
[root@m72 ops]# touch clean_docker_logs.sh
[root@m72 ops]# vi clean_docker_logs.sh
添加如下内容
#!/bin/sh
echo "------------------------clean docker containers logs------------------------"
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs
do
echo "clean logs : $log"
cat /dev/null > $log
done
echo "---------------------end clean docker containers logs------------------------"
添加权限并执行
[root@m72 ops]# chmod +x clean_docker_logs.sh
[root@m72 ops]# sh clean_docker_logs.sh
------------------------clean docker containers logs------------------------
clean logs : /var/lib/docker/containers/6d525e609df974dc9c021619f31676ab2804a3d87f47d8a933ef44999589f80a/6d525e609df974dc9c021619f31676ab2804a3d87f47d8a933ef44999589f80a-json.log
clean logs : /var/lib/docker/containers/cf12d6f6d13879fa98cb5a3a9f0e5b258dcb6c692831bf15fd7663116ee7c596/cf12d6f6d13879fa98cb5a3a9f0e5b258dcb6c692831bf15fd7663116ee7c596-json.log
---------------------end clean docker containers logs------------------------
如果想根治日志过大的问题,可以修改docker配
执行
vim /etc/docker/daemon.json
增加log-driver,log-opts两项配置,我这里设置的最大文件大小300m,最多生成3个日志文件
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"log-driver":"json-file",
"log-opts":{"max-size":"300m","max-file":"3"}
}
重启docker
[root@m72 ops]# systemctl daemon-reload
[root@m72 ops]# systemctl restart docker