k8s集群中经常被旧版本已经不再使用的镜像占完磁盘空间,出现

node has conditions: [DiskPressure]

或者是

failed to register layer: devmapper:Thin Pool has 107394 free data blocks which is less than minimum required 163840 free data blocks. Create more free space in thin pool or use dm.min_free_space option to change behavior

这样的问题,网上搜了一圈之后都是要手动登陆到Node然后执行下面命令,太过麻烦,

docker rm $(docker ps -q -f status=exited)
docker volume rm $(docker volume ls -qf dangling=true)
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

然后在github中看到有大神的镜像有实现,所有就拿来分享一下

apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    name: clean-up
  name: clean-up
spec:
  selector:
    matchLabels:
      app: clean-up
  template:
    metadata:
      labels:
        app: clean-up 
    spec:
      volumes:
        - name: docker-sock
          hostPath:
            path: /var/run/docker.sock
        - name: docker-directory
          hostPath:
            path: /var/lib/docker
      containers:
        - image: andyzhangx/docker-cleanup:latest  # based on https://hub.docker.com/r/meltwater/docker-cleanup/
          name: clean-up
          env:
            - name: CLEAN_PERIOD
              value: "60"
            - name: DELAY_TIME
              value: "1800"
          volumeMounts:
            - mountPath: /var/run/docker.sock
              name: docker-sock
              readOnly: false
            - mountPath: /var/lib/docker
              name: docker-directory
              readOnly: false

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐