Fetching Cluster Images
It can be difficult to know exactly what images are actively running in your environment. Below is a simple way to generate a list of every image in a running cluster, store it as an environment variable, and turn that list into a Hauler manifest you can sync into your store.
Capturing your cluster's images as a manifest makes the set reproducible: you can version-control it, review changes over time, and re-sync the exact same content on demand - so rebuilding a haul or standing up the cluster again in a disconnected environment is repeatable rather than a manual scramble.
Fetch Cluster Images​
# fetch all images in the cluster, remove duplicates, alphabetize, and put on a newline
export IMAGE_LIST=$({
kubectl get pods --all-namespaces -o jsonpath="{range .items[*]}{.spec.containers[*].image} {.spec.initContainers[*].image} {.spec.ephemeralContainers[*].image}{'\n'}{end}"
kubectl get cronjobs --all-namespaces -o jsonpath="{range .items[*]}{.spec.jobTemplate.spec.template.spec.containers[*].image} {.spec.jobTemplate.spec.template.spec.initContainers[*].image}{'\n'}{end}"
} | tr ' ' '\n' | sort -u | sed '/^$/d')
Example Output​
# view the list of images from the cluster
echo $IMAGE_LIST
index.docker.io/rancher/hardened-etcd:v3.5.1-k3s1-build20220112
index.docker.io/rancher/hardened-kubernetes:v1.23.5-rke2r1-build20220316
index.docker.io/rancher/rke2-cloud-provider:v0.0.3-build20211118
rancher/hardened-calico:v3.21.4-build20220228
rancher/hardened-cluster-autoscaler:v1.8.5-build20211119
rancher/hardened-coredns:v1.9.1-build20220318
rancher/hardened-flannel:v0.17.0-build20220317
rancher/hardened-k8s-metrics-server:v0.5.0-build20211119
rancher/klipper-helm:v0.7.0-build20220315
rancher/nginx-ingress-controller:nginx-1.0.2-hardened4
Generate Hauler Manifest​
# add the required formatting for the image list
export IMAGE_LIST_MODIFIED=$(echo "${IMAGE_LIST}" | sed 's/^/ - name: /')
# create the hauler manifest with the updated image list
cat << EOF >> hauler-manifest.yaml
apiVersion: content.hauler.cattle.io/v1
kind: Images
metadata:
name: hauler-cluster-images
spec:
images:
$IMAGE_LIST_MODIFIED
EOF
Resulting Hauler Manifest​
hauler-manifest.yaml
apiVersion: content.hauler.cattle.io/v1
kind: Images
metadata:
name: hauler-cluster-images
spec:
images:
- name: index.docker.io/rancher/hardened-etcd:v3.5.1-k3s1-build20220112
- name: index.docker.io/rancher/hardened-kubernetes:v1.23.5-rke2r1-build20220316
- name: index.docker.io/rancher/rke2-cloud-provider:v0.0.3-build20211118
- name: rancher/hardened-calico:v3.21.4-build20220228
- name: rancher/hardened-cluster-autoscaler:v1.8.5-build20211119
- name: rancher/hardened-coredns:v1.9.1-build20220318
- name: rancher/hardened-flannel:v0.17.0-build20220317
- name: rancher/hardened-k8s-metrics-server:v0.5.0-build20211119
- name: rancher/klipper-helm:v0.7.0-build20220315
- name: rancher/nginx-ingress-controller:nginx-1.0.2-hardened4
Sync the Manifest​
With the manifest generated, sync its images into your store with hauler store sync:
hauler store sync --filename hauler-manifest.yaml