#1.情况说明
#1.1分布式服务docker容器部署,内存溢出处理
# du - 报告磁盘空间使用情况
# du --max-depth=1 -h 在根目录执行,列出文件磁盘占用情况
[root@localhost /]# du --max-depth=1 -h
148M ./boot
0 ./dev
42G ./home
du: 无法访问"./proc/26052": 没有那个文件或目录
du: 无法访问"./proc/26179/task/26179/fd/3": 没有那个文件或目录
du: 无法访问"./proc/26179/task/26179/fdinfo/3": 没有那个文件或目录
du: 无法访问"./proc/26179/fd/4": 没有那个文件或目录
du: 无法访问"./proc/26179/fdinfo/4": 没有那个文件或目录
0 ./proc
3.2G ./run
0 ./sys
43M ./etc
1.1G ./root
40G ./var
40K ./tmp
9.0G ./usr
0 ./media
0 ./mnt
14G ./opt
0 ./srv
85G .//最后这个是总计
#1.2 /var/lib/docker
1.目录展示
[root@localhost docker]# pwd
/var/lib/docker
[root@localhost docker]# ll
总用量 20
drwx------. 2 root root 24 1月 5 2021 builder
drwx------. 4 root root 92 1月 5 2021 buildkit
drwx------. 3 root root 20 1月 5 2021 containerd
drwx------. 29 root root 4096 9月 18 10:01 containers
drwx------. 3 root root 22 1月 5 2021 image
drwxr-x---. 3 root root 19 1月 5 2021 network
drwx------. 120 root root 12288 9月 18 10:01 overlay2
drwx------. 4 root root 32 1月 5 2021 plugins
drwx------ 2 root root 6 8月 16 09:03 runtimes
drwx------. 2 root root 6 1月 5 2021 swarm
drwx------ 2 root root 6 9月 9 18:08 tmp
drwx------. 2 root root 6 1月 5 2021 trust
drwx------. 3 root root 97 9月 17 10:47 volumes
2.查看/var/lib/docker下的磁盘空间使用情况
[root@localhost docker]# du --max-depth=1 -h
1.4M ./containerd
2.3G ./containers
0 ./plugins
12G ./overlay2
7.8M ./image
56M ./volumes
0 ./trust
252K ./network
0 ./swarm
16K ./builder
56K ./buildkit
0 ./tmp
0 ./runtimes
15G .//最后这个是总计
3.查看containers每个容器使用空间,下面就是你创建所有容器的文件,文件名就是容器id
[root@localhost containers]# du --max-depth=1 -h
99M ./a8bcb70543d8b6792849e04aa5b7eb86cf61bd62fc94ca6d06fc231ffe9be539
1.3M ./570ab47f83f408021af329f4cbc3d37f8f948b68dc9f82508c2e2a53afc0249e
20M ./82e23003969fbe2ab30aaf30287c4f072a9d577981202c5071cbd052b620d351
185M ./784a19b6d06b1d78bd74f44d8af9404f98fbe2b1544836a089c72807a7f9b59b
1.3M ./34aa6db7c8a50f2819270e3bd1a8d42ea1723a5e7999e57b5e5197eefcb32204
163M ./2cb5b949740eec2695119c261d3c7071aaa9f5531e9bd61a2258ee7858827dd0
35M ./ce2dfe71c2d1162a9702be8c48b953a27895451c5cd46cce796711b964e7acfd
20M ./1b579993f447f6544621af9115d836601ac9884e7775044771d770900837bcf4
888K ./20843996d160ed82a99d911d23420c618eabee24634a94d639601f9994ff362f
5.8M ./0399a8f469893441a42f13120493a0cfd70b395f6ea33b91d5a8f1eae71a2174
2.6M ./0e3fccb3996c423cabcf076acac5a22dc87a1e7b17da2216d90444eae83cac3a
9.3M ./20c3a39e678cc4e2b9aa22a35a464e98454dbfc7b432ddd16a4dbe5fc2b41f4a
40K ./62b9e1c854a7e29d6290b0b5e38ba326c684f7596219281894c9f4307819da05
108K ./b28a4278b3b48277d1e148d55387f42fd592953ba8b384e5acca6f89d08e27d2
575M ./22b402958d413d912931f3274172b0f2c5356c3844c942c13330e2adcd20e269
828K ./14a87c1ad6b5fc526948b827eb94777a287a8be31821ec0b5bfcf9e58fd45a61
424M ./5a0f14246e7fce9a15207f6c6d0192a454c99eca3f16c713c218746c5fc94ecc
71M ./a8ca1cce2fa3f6099cd6bf2cea164d3a3adeaef109d9282d8b91df159db87137
173M ./e6e73e04a6f2b251a0750c011fbbb830648bab9e8c78ba5e1e542ad399c9a87f
68M ./fcb3248d62c879d47e8939feb96e4daa21a45dd37add7692f9d98067bf6bffed
24K ./71ccce45e73b881c2ece16bda45ac64ef6db7286ac5ce40988f469c635912ba0
2.5M ./721c770d2db027b7b15363572ad5948944bf60056586a8a5176bd652ec129253
96K ./ed9cb27e109265789da1d6e531c69ff2f93946fc199129d961fb2f187deaf597
2.8M ./9f684d980afc82cb83d6270f361d37308326c9b468dd89ae0a463a15791873a4
504M ./cad76ef9bb6beadf0c34af6cf614234eddd5dfb18e23a587b390cc10ba631da7
84K ./f600401f4af806ce8fa53e1fa31011f7a38cc13f5ce9e2df488f8f7fc7a42d1a
164K ./4fa020730940970a42c5dce9e802859c9372455d2b721ab7d7fb924316f9a0b3
2.4G . //最后这个是总计
4.随便进一个容器文件去
[root@localhost cad76ef9bb6beadf0c34af6cf614234eddd5dfb18e23a587b390cc10ba631da7]# tree -L 1
.
├── cad76ef9bb6beadf0c34af6cf614234eddd5dfb18e23a587b390cc10ba631da7-json.log
├── checkpoints
├── config.v2.json
├── hostconfig.json
├── hostname
├── hosts
├── mounts
├── resolv.conf
└── resolv.conf.hash
5.查看容器情况
说明:总容量504,全被*631da7-json.log这个文件占完了
实际情况是,这一堆容器的json.logs文件加起来有20个G,
现在看到的都是清空后的容量统计了
[root@localhost cad76ef9bb6beadf0c34af6cf614234eddd5dfb18e23a587b390cc10ba631da7]# ll -h
总用量 504M
-rw-r----- 1 root root 504M 9月 18 10:07 cad76ef9bb6beadf0c34af6cf614234eddd5dfb18e23a587b390cc10ba631da7-json.log
drwx------ 2 root root 6 9月 7 19:36 checkpoints
-rw------- 1 root root 5.3K 9月 10 11:01 config.v2.json
-rw-r--r-- 1 root root 1.7K 9月 10 11:01 hostconfig.json
-rw-r--r-- 1 root root 13 9月 10 11:01 hostname
-rw-r--r-- 1 root root 175 9月 10 11:01 hosts
drwx------ 3 root root 17 9月 7 19:36 mounts
-rw-r--r-- 1 root root 38 9月 10 11:01 resolv.conf
-rw-r--r-- 1 root root 71 9月 10 11:01 resolv.conf.hash
cad76ef9bb6beadf0c34af6cf614234eddd5dfb18e23a587b390cc10ba631da7-json.log
这个文件夹,就是docker容器的日志文件,该文件会不断的增长,最终导致你linux服务无法使用
无法为立即文档创建临时文件: 设备上没有空间
cd /ho table键都无法使用
#2.处理
#2.1清空该日志文件,*-json.log
正确姿势是:
1.cat /dev/null > *-json.log,(清空文件)
2.当然你也可以通过rm删除后重启docker(不建议)
#3.上述为查找文件流程
#4.清理脚本
!/bin/sh
echo "==================== start clean docker containers logs =========================="
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in logs
do
echo "cleanlogs:log"
cat /dev/null > $log
done
echo "==================== end clean docker containers logs =========================="