问题:
K8s部署的服务会不断生成日志,最终导致/var/lib/docker/containers下生成很多日志,导致磁盘存满
解决:
1、自己清理
编写cleanlog.sh
#!/bin/sh
# drift
echo "========= start clean /var/lib/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 /var/lib/docker/containers/ logs =========="
2、根治
/etc/docker/daemon.json
添加log-driver 和log-opts
我的文件:
{
"log-driver":"json-file",
"log-opts": {"max-size":"20m", "max-file":"3"},
"exec-opts": ["native.cgroupdriver= cgroupfs"],
"insecure-registries":["172.16.10.160:80","172.16.10.160:88", "172.16.10.190:80", "quay.io", "k8s.gcr.io", "gcr.io", "docker.io"],
"registry-mirrors":["http://172.16.10.160:80"]
}
解释:
"max-size":"20m" 表示一个容器日志大小上限是20M,
"max-file":"3" 表示一个容器有三个日志,分别是id+.json、id+1.json、id+2.json。
重启docker
systemctl daemon-reload
systemctl restart docker