Skip to content

Commit a17f3bb

Browse files
authored
Merge pull request #240200 from schaffererin/patch-34-2
New PR for Image Cleaner GA
2 parents 4724f03 + 2eb5c9b commit a17f3bb

File tree

1 file changed

+109
-132
lines changed

1 file changed

+109
-132
lines changed

articles/aks/image-cleaner.md

Lines changed: 109 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -5,184 +5,161 @@ ms.author: nickoman
55
author: nickomang
66
ms.topic: article
77
ms.custom: devx-track-azurecli
8-
ms.date: 03/02/2023
8+
ms.date: 06/02/2023
99
---
1010

11-
# Use Image Cleaner to clean up stale images on your Azure Kubernetes Service cluster (preview)
11+
# Use Image Cleaner to clean up stale images on your Azure Kubernetes Service (AKS) cluster
1212

13-
It's common to use pipelines to build and deploy images on Azure Kubernetes Service (AKS) clusters. While great for image creation, this process often doesn't account for the stale images left behind and can lead to image bloat on cluster nodes. These images can present security issues as they may contain vulnerabilities. By cleaning these unreferenced images, you can remove an area of risk in your clusters. When done manually, this process can be time intensive, which Image Cleaner can mitigate via automatic image identification and removal.
13+
It's common to use pipelines to build and deploy images on Azure Kubernetes Service (AKS) clusters. While great for image creation, this process often doesn't account for the stale images left behind and can lead to image bloat on cluster nodes. These images may contain vulnerabilities, which may create security issues. To remove security risks in your clusters, you can clean these unreferenced images. Manually cleaning images can be time intensive. Image Cleaner performs automatic image identification and removal, which mitigates the risk of stale images and reduces the time required to clean them up.
1414

1515
> [!NOTE]
1616
> Image Cleaner is a feature based on [Eraser](https://azure.github.io/eraser).
17-
> On an AKS cluster, the feature name and property name is `Image Cleaner` while the relevant Image Cleaner pods' names contain `Eraser`.
18-
19-
[!INCLUDE [preview features callout](./includes/preview/preview-callout.md)]
17+
> On an AKS cluster, the feature name and property name is `Image Cleaner`, while the relevant Image Cleaner pods' names contain `Eraser`.
2018
2119
## Prerequisites
2220

2321
* An Azure subscription. If you don't have an Azure subscription, you can create a [free account](https://azure.microsoft.com/free).
24-
* [Azure CLI][azure-cli-install] or [Azure PowerShell][azure-powershell-install] and the `aks-preview` 0.5.96 or later CLI extension installed.
25-
* The `EnableImageCleanerPreview` feature flag registered on your subscription:
26-
27-
### [Azure CLI](#tab/azure-cli)
28-
29-
First, install the aks-preview extension by running the following command:
30-
31-
```azurecli
32-
az extension add --name aks-preview
33-
```
34-
35-
Run the following command to update to the latest version of the extension released:
36-
37-
```azurecli
38-
az extension update --name aks-preview
39-
```
40-
41-
Then register the `EnableImageCleanerPreview` feature flag by using the [az feature register][az-feature-register] command, as shown in the following example:
42-
43-
```azurecli-interactive
44-
az feature register --namespace "Microsoft.ContainerService" --name "EnableImageCleanerPreview"
45-
```
46-
47-
It takes a few minutes for the status to show *Registered*. Verify the registration status by using the [az feature show][az-feature-show] command:
22+
* Azure CLI version 2.49.0 or later. Run `az --version` to find your version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install].
4823

49-
```azurecli-interactive
50-
az feature show --namespace "Microsoft.ContainerService" --name "EnableImageCleanerPreview"
51-
```
52-
53-
When the status reflects *Registered*, refresh the registration of the *Microsoft.ContainerService* resource provider by using the [az provider register][az-provider-register] command:
24+
## Limitations
5425

55-
```azurecli-interactive
56-
az provider register --namespace Microsoft.ContainerService
57-
```
26+
Image Cleaner doesn't yet support Windows node pools or AKS virtual nodes.
5827

59-
### [Azure PowerShell](#tab/azure-powershell)
28+
## How Image Cleaner works
6029

61-
Register the `EnableImageCleanerPreview` feature flag by using the [Register-AzProviderPreviewFeature][register-azproviderpreviewfeature] cmdlet, as shown in the following example:
30+
When you enable Image Cleaner, it deploys an `eraser-controller-manager` pod, which generates an `ImageList` CRD. The eraser pods running on each node clean up any unreferenced and vulnerable images according to the `ImageList`. A [trivy][trivy] scan helps determine vulnerability and flags images with a classification of `LOW`, `MEDIUM`, `HIGH`, or `CRITICAL`. Image Cleaner automatically generates an updated `ImageList` based on a set time interval and can also be supplied manually. Once Image Cleaner generates an `ImageList`, it removes all images in the list from node VMs.
6231

63-
```azurepowershell-interactive
64-
Register-AzProviderPreviewFeature -ProviderNamespace Microsoft.ContainerService -Name EnableImageCleanerPreview
65-
```
32+
:::image type="content" source="./media/image-cleaner/image-cleaner.jpg" alt-text="Screenshot of a diagram showing ImageCleaner's workflow. The ImageCleaner pods running on the cluster can generate an ImageList, or manual input can be provided.":::
6633

67-
It takes a few minutes for the status to show *Registered*. Verify the registration status by using the [Get-AzProviderPreviewFeature][get-azproviderpreviewfeature] cmdlet:
34+
## Configuration options
6835

69-
```azurepowershell-interactive
70-
Get-AzProviderPreviewFeature -ProviderNamespace Microsoft.ContainerService -Name EnableImageCleanerPreview |
71-
Format-Table -Property Name, @{name='State'; expression={$_.Properties.State}}
72-
```
36+
With Image Cleaner, you can choose between manual and automatic mode and the following configuration options:
7337

74-
When ready, refresh the registration of the *Microsoft.ContainerService* resource provider by using the [Register-AzResourceProvider][register-azresourceprovider] command:
38+
|Name|Description|Required|
39+
|----|-----------|--------|
40+
|`--enable-image-cleaner`|Enable the Image Cleaner feature for an AKS cluster|Yes, unless disable is specified|
41+
|`--disable-image-cleaner`|Disable the Image Cleaner feature for an AKS cluster|Yes, unless enable is specified|
42+
|`--image-cleaner-interval-hours`|This parameter determines the interval time (in hours) Image Cleaner uses to run. The default value for Azure CLI is one week, the minimum value is 24 hours and the maximum is three months.|Not required for Azure CLI, required for ARM template or other clients|
7543

76-
```azurepowershell-interactive
77-
Register-AzResourceProvider -ProviderNamespace Microsoft.ContainerService
78-
```
44+
> [!NOTE]
45+
> After disabling Image Cleaner, the old configuration still exists. This means if you enable the feature again without explicitly passing configuration, the existing value is used instead of the default.
7946
80-
---
47+
## Enable Image Cleaner on your AKS cluster
8148

82-
## Limitations
49+
### Enable Image Cleaner on a new cluster
8350

84-
Image Cleaner does not support the following:
51+
* Enable Image Cleaner on a new AKS cluster using the [`az aks create`][az-aks-create] command with the `--enable-image-cleaner` parameter.
8552

86-
* ARM64 node pools. For more information, see [Azure Virtual Machines with ARM-based processors][arm-vms].
87-
* Windows node pools.
53+
```azurecli-interactive
54+
az aks create -g myResourceGroup -n myManagedCluster \
55+
--enable-image-cleaner
56+
```
8857
89-
## How Image Cleaner works
58+
### Enable Image Cleaner on an existing cluster
9059
91-
When enabled, an `eraser-controller-manager` pod is deployed, which generates an `ImageList` CRD. The eraser pods running on each nodes will clean up the unreferenced and vulnerable images according to the ImageList. Vulnerability is determined based on a [trivy][trivy] scan, after which images with a `LOW`, `MEDIUM`, `HIGH`, or `CRITICAL` classification are flagged. An updated `ImageList` will be automatically generated by Image Cleaner based on a set time interval, and can also be supplied manually.
60+
* Enable Image Cleaner on an existing AKS cluster using the [`az aks update`][az-aks-update] command.
9261
62+
```azurecli-interactive
63+
az aks update -g myResourceGroup -n myManagedCluster \
64+
--enable-image-cleaner
65+
```
9366
67+
### Update the Image Cleaner interval on a new or existing cluster
9468
95-
Once an `ImageList` is generated, Image Cleaner will remove all the images in the list from node VMs.
69+
* Update the Image Cleaner interval on a new or existing AKS cluster using the `--image-cleaner-interval-hours` parameter.
9670
97-
:::image type="content" source="./media/image-cleaner/image-cleaner.jpg" alt-text="Screenshot of a diagram showing ImageCleaner's workflow. The ImageCleaner pods running on the cluster can generate an ImageList, or manual input can be provided.":::
71+
```azurecli-interactive
72+
# Update the interval on a new cluster
73+
az aks create -g myResourceGroup -n myManagedCluster \
74+
--enable-image-cleaner \
75+
--image-cleaner-interval-hours 48
76+
# Update the interval on an existing cluster
77+
az aks update -g myResourceGroup -n myManagedCluster \
78+
--image-cleaner-interval-hours 48
79+
```
9880
99-
## Configuration options
81+
After you enable the feature, the `eraser-controller-manager-xxx` pod and `collector-aks-xxx` pod are deployed. The `eraser-aks-xxx` pod contains *three* containers:
10082
101-
In addition to choosing between manual and automatic mode, there are several options for Image Cleaner:
83+
- **Scanner container**: Performs vulnerability image scans
84+
- **Collector container**: Collects nonrunning and unused images
85+
- **Remover container**: Removes these images from cluster nodes
10286
103-
|Name|Description|Required|
104-
|----|-----------|--------|
105-
|--enable-image-cleaner|Enable the Image Cleaner feature for an AKS cluster|Yes, unless disable is specified|
106-
|--disable-image-cleaner|Disable the Image Cleaner feature for an AKS cluster|Yes, unless enable is specified|
107-
|--image-cleaner-interval-hours|This parameter determines the interval time (in hours) Image Cleaner will use to run. The default value for Azure CLI is one week, the minimum value is 24 hours and the maximum is three months.|Not required for Azure CLI, required for ARM template or other clients|
87+
Image Cleaner generates an `ImageList` containing nonrunning and vulnerable images at the desired interval based on your configuration. Image Cleaner automatically removes these images from cluster nodes.
10888
109-
> [!NOTE]
110-
> After disabling Image Cleaner, the old configuration still exists. This means that if you enable the feature again without explicitly passing configuration, the existing value will be used rather than the default.
89+
## Manually remove images using Image Cleaner
11190
112-
## Enable Image Cleaner on your AKS cluster
91+
1. Create an `ImageList` using the following example YAML named `image-list.yml`.
11392
114-
To create a new AKS cluster using the default interval, use [az aks create][az-aks-create]:
93+
```yml
94+
apiVersion: eraser.sh/v1alpha1
95+
kind: ImageList
96+
metadata:
97+
name: imagelist
98+
spec:
99+
images:
100+
- docker.io/library/alpine:3.7.3 # You can also use "*" to specify all non-running images
101+
```
115102
116-
```azurecli-interactive
117-
az aks create -g MyResourceGroup -n MyManagedCluster \
118-
--enable-image-cleaner
119-
```
103+
2. Apply the `ImageList` to your cluster using the `kubectl apply` command.
120104
121-
To enable on an existing AKS cluster, use [az aks update][az-aks-update]:
105+
```bash
106+
kubectl apply -f image-list.yml
107+
```
122108
123-
```azurecli-interactive
124-
az aks update -g MyResourceGroup -n MyManagedCluster \
125-
--enable-image-cleaner
126-
```
109+
Applying the `ImageList` triggers a job named `eraser-aks-xxx`, which causes Image Cleaner to remove the desired images from all nodes. Unlike the `eraser-aks-xxx` pod under autoclean with *three* containers, the eraser-pod here has only *one* container.
127110
128-
The `--image-cleaner-interval-hours` parameter can be specified at creation time or for an existing cluster. For example, the following command updates the interval for a cluster with Image Cleaner already enabled:
111+
## Image exclusion list
129112
130-
```azurecli-interactive
131-
az aks update -g MyResourceGroup -n MyManagedCluster \
132-
--image-cleaner-interval-hours 48
133-
```
113+
Images specified in the exclusion list aren't removed from the cluster. Image Cleaner supports system and user-defined exclusion lists. It's not supported to edit the system exclusion list.
134114
135-
After the feature is enabled, the `eraser-controller-manager-xxx` pod and `collector-aks-xxx` pod will be deployed.
136-
Based on your configuration, Image Cleaner will generate an `ImageList` containing non-running and vulnerable images at the desired interval. Image Cleaner will automatically remove these images from cluster nodes.
115+
### Check the system exclusion list
137116
138-
## Manually remove images
117+
* Check the system exclusion list using the following `kubectl get` command.
139118
140-
To manually remove images from your cluster using Image Cleaner, first create an `ImageList`. For example, save the following as `image-list.yml`:
119+
```bash
120+
kubectl get -n kube-system cm eraser-system-exclusion -o yaml
121+
```
141122
142-
```yml
143-
apiVersion: eraser.sh/v1alpha1
144-
kind: ImageList
145-
metadata:
146-
name: imagelist
147-
spec:
148-
images:
149-
- docker.io/library/alpine:3.7.3 # You can also use "*" to specify all non-running images
150-
```
123+
### Create a user-defined exclusion list
151124
152-
And apply it to the cluster:
125+
1. Create a sample JSON file to contain excluded images.
153126
154-
```bash
155-
kubectl apply -f image-list.yml
156-
```
127+
```bash
128+
cat > sample.json <<EOF
129+
{"excluded": ["excluded-image-name"]}
130+
EOF
131+
```
157132
158-
A job named `eraser-aks-xxx`will be triggered which causes Image Cleaner to remove the desired images from all nodes.
133+
2. Create a `configmap` using the sample JSON file using the following `kubectl create` and `kubectl label` command.
159134
160-
## Disable Image Cleaner
135+
```bash
136+
kubectl create configmap excluded --from-file=sample.json --namespace=kube-system
137+
kubectl label configmap excluded eraser.sh/exclude.list=true -n kube-system
138+
```
161139
162-
To stop using Image Cleaner, you can disable it via the `--disable-image-cleaner` flag:
140+
3. Verify the images are in the exclusion list using the following `kubectl logs` command.
163141
164-
```azurecli-interactive
165-
az aks update -g MyResourceGroup -n MyManagedCluster
166-
--disable-image-cleaner
167-
```
142+
```bash
143+
kubectl logs -n kube-system <eraser-pod-name>
144+
```
168145
169-
## Logging
146+
## Image Cleaner image logs
170147
171-
Deletion image logs are stored in `eraser-aks-nodepool-xxx` pods for manually deleted images, and in `collector-aks-nodes-xxx` pods for automatically deleted images.
148+
Deletion image logs are stored in `eraser-aks-nodepool-xxx` pods for manually deleted images and in `collector-aks-nodes-xxx` pods for automatically deleted images.
172149
173-
You can view these logs by running `kubectl logs <pod name> -n kubesystem`. However, this command may return only the most recent logs, since older logs are routinely deleted. To view all logs, follow these steps to enable the [Azure Monitor add-on](./monitor-aks.md) and use the Container Insights pod log table.
150+
You can view these logs using the `kubectl logs <pod name> -n kubesystem` command. However, this command may return only the most recent logs, since older logs are routinely deleted. To view all logs, follow these steps to enable the [Azure Monitor add-on](./monitor-aks.md) and use the Container Insights pod log table.
174151
175-
1. Ensure that Azure monitoring is enabled on the cluster. For detailed steps, see [Enable Container Insights for AKS cluster](../azure-monitor/containers/container-insights-enable-aks.md#existing-aks-cluster).
152+
1. Ensure Azure Monitoring is enabled on your cluster. For detailed steps, see [Enable Container Insights on AKS clusters](../azure-monitor/containers/container-insights-enable-aks.md#existing-aks-cluster).
176153
177-
1. Get the Log Analytics resource ID:
154+
2. Get the Log Analytics resource ID using the [`az aks show`][az-aks-show] command.
178155
179156
```azurecli
180-
az aks show -g <resourceGroupofAKSCluster> -n <nameofAksCluster>
157+
az aks show -g myResourceGroup -n myManagedCluster
181158
```
182159

183-
After a few minutes, the command returns JSON-formatted information about the solution, including the workspace resource ID:
160+
After a few minutes, the command returns JSON-formatted information about the solution, including the workspace resource ID.
184161

185-
```json
162+
```json
186163
"addonProfiles": {
187164
"omsagent": {
188165
"config": {
@@ -191,11 +168,11 @@ You can view these logs by running `kubectl logs <pod name> -n kubesystem`. Howe
191168
"enabled": true
192169
}
193170
}
194-
```
171+
```
195172

196-
1. In the Azure portal, search for the workspace resource ID, then select **Logs**.
173+
3. In the Azure portal, search for the workspace resource ID, then select **Logs**.
197174

198-
1. Copy this query into the table, replacing `name` with either `eraser-aks-nodepool-xxx` (for manual mode) or `collector-aks-nodes-xxx` (for automatic mode).
175+
4. Copy this query into the table, replacing `name` with either `eraser-aks-nodepool-xxx` (for manual mode) or `collector-aks-nodes-xxx` (for automatic mode).
199176

200177
```kusto
201178
let startTimestamp = ago(1h);
@@ -217,23 +194,23 @@ You can view these logs by running `kubectl logs <pod name> -n kubesystem`. Howe
217194
| order by TimeGenerated desc
218195
```
219196

220-
1. Select **Run**. Any deleted image logs will appear in the **Results** area.
197+
5. Select **Run**. Any deleted image logs appear in the **Results** area.
221198

222199
:::image type="content" source="media/image-cleaner/eraser-log-analytics.png" alt-text="Screenshot showing deleted image logs in the Azure portal." lightbox="media/image-cleaner/eraser-log-analytics.png":::
223200

201+
## Disable Image Cleaner
202+
203+
* Disable Image Cleaner on your cluster using the [`az aks update`][az-aks-update] command with the `--disable-image-cleaner` parameter.
204+
205+
```azurecli-interactive
206+
az aks update -g myResourceGroup -n myManagedCluster \
207+
--disable-image-cleaner
208+
```
209+
224210
<!-- LINKS -->
225211
226212
[azure-cli-install]: /cli/azure/install-azure-cli
227-
[azure-powershell-install]: /powershell/azure/install-az-ps
228-
229213
[az-aks-create]: /cli/azure/aks#az_aks_create
230214
[az-aks-update]: /cli/azure/aks#az_aks_update
231-
[az-feature-register]: /cli/azure/feature#az-feature-register
232-
[register-azproviderpreviewfeature]: /powershell/module/az.resources/register-azproviderpreviewfeature
233-
[az-feature-show]: /cli/azure/feature#az-feature-show
234-
[get-azproviderpreviewfeature]: /powershell/module/az.resources/get-azproviderpreviewfeature
235-
[az-provider-register]: /cli/azure/provider#az-provider-register
236-
[register-azresourceprovider]: /powershell/module/az.resources/register-azresourceprovider
237-
238-
[arm-vms]: https://azure.microsoft.com/blog/azure-virtual-machines-with-ampere-altra-arm-based-processors-generally-available/
239215
[trivy]: https://github.com/aquasecurity/trivy
216+
[az-aks-show]: /cli/azure/aks#az_aks_show

0 commit comments

Comments
 (0)