Skip to content

Instantly share code, notes, and snippets.

@alexeldeib
Created October 22, 2021 16:01
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save alexeldeib/2a02ccb3db02ddb828a9c1ef04f2b955 to your computer and use it in GitHub Desktop.
Save alexeldeib/2a02ccb3db02ddb828a9c1ef04f2b955 to your computer and use it in GitHub Desktop.
crictl image cleanup
apiVersion: v1
kind: ConfigMap
metadata:
name: imagecleanupconfig
namespace: default
labels:
component: imagecleanup
data:
# removes all images unused by a running container
cronjobs: 0 0 * * * crictl rmi --prune 2>&1
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: imagecleanup
namespace: default
spec:
selector:
matchLabels:
component: imagecleanup
template:
metadata:
labels:
component: imagecleanup
spec:
volumes:
- name: containerd
hostPath:
path: /var/run/containerd/containerd.sock
type: Socket
- name: imagecleanupconfig
configMap:
name: imagecleanupconfig
containers:
- name: imagecleanup
image: alpine:latest
imagePullPolicy: Always
env:
- name: CRICTL_VERSION
value: v1.20.0
- name: CONTAINER_RUNTIME_ENDPOINT
value: unix:///var/run/containerd/containerd.sock
- name: IMAGE_SERVICE_ENDPOINT
value: unix:///var/run/containerd/containerd.sock
# details of the command:
# - install crictl
# - add a cron job from the config map
# - execute crond in the foregroud
command:
[
"sh",
"-c",
"wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$CRICTL_VERSION/crictl-$CRICTL_VERSION-linux-amd64.tar.gz && tar zxvf crictl-$CRICTL_VERSION-linux-amd64.tar.gz -C /usr/local/bin && crontab /config/cronjobs && crond -f -d 8",
]
resources:
requests:
cpu: 100m
memory: 50Mi
volumeMounts:
- name: containerd
mountPath: /var/run/containerd/containerd.sock
- name: imagecleanupconfig
mountPath: /config
---
@vbratkev
Copy link

vbratkev commented Nov 22, 2022

Nice. It helped me. Thank you. It is useful when you have an old crictl version in k8s cluster and can't update.
In my case crictl was old version

# crictl rmi --prune
Incorrect Usage: flag provided but not defined: -prune

NAME:
   crictl rmi - Remove one or more images

USAGE:
   crictl rmi [command options] IMAGE-ID [IMAGE-ID...]

OPTIONS:
   --all, -a  Remove all images
   
FATA[0000] flag provided but not defined: -prune        
# crictl --version
crictl version v1.16.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment