|
1 | 1 | # Pruning images using CronJob
|
2 | 2 |
|
3 |
| -This example shows an image pruning happening in an automated fashion using the Kubernetes [CronJobs](https://docs.openshift.org/latest/dev_guide/cron_jobs.html) that |
4 |
| -are available in OpenShift Origin starting from version 3.5. |
| 3 | +This example shows an image pruning happening in an automated fashion using the |
| 4 | +Kubernetes [CronJobs](https://docs.openshift.org/latest/dev_guide/cron_jobs.html). |
5 | 5 | In this example, we will create a CronJob that will run image pruning every 1 hour.
|
6 | 6 |
|
7 | 7 | ## Requirements
|
8 | 8 |
|
9 |
| -In order to execute the pruning commands successfully, it is necessary to configure the |
10 |
| -authorization in a way that allows the `default` service account to perform the pruning |
11 |
| -against entire cluster (assuming you create the CronJob in the `default` project): |
| 9 | +In order to execute the pruning commands successfully, it is necessary to create a |
| 10 | +dedicated service account `image-pruner` with necessary privileges to perform pruning |
| 11 | +against the entire cluster. Make sure you run below commands with a user who has |
| 12 | +the power to assign cluster roles. Also double check the namespace you are invoking |
| 13 | +them in, if it is the one you desire. |
12 | 14 |
|
13 |
| -1. `oc adm policy add-cluster-role-to-user system:image-pruner system:serviceaccount:default:default --config=admin.kubeconfig` |
| 15 | +1. `oc create serviceaccount image-pruner` |
14 | 16 |
|
15 |
| - This command will grant the "image-pruner" role to service account in the `default` |
16 |
| - namespace. That will allow the service account to list all images in the cluster and |
17 |
| - perform the image pruning. |
| 17 | + This command creates an `image-pruner` [service account](https://docs.openshift.org/latest/admin_guide/service_accounts.html). |
| 18 | + |
| 19 | +2. `oc adm policy add-cluster-role-to-user system:image-pruner image-pruner` |
| 20 | + |
| 21 | + This command grants the `image-pruner` [role](https://docs.openshift.org/latest/admin_guide/manage_rbac.html) to that service account. |
18 | 22 |
|
19 | 23 | ## Creating the CronJob
|
20 | 24 |
|
21 |
| -2. `oc create -f examples/pruner/job.yaml -n default --config=admin.kubeconfig` |
| 25 | +2. `oc create -f examples/pruner/cronjob.yaml -n default --config=admin.kubeconfig` |
22 | 26 |
|
23 | 27 | This command creates the CronJob resource that runs the pruning job every 1 hour.
|
24 | 28 |
|
25 | 29 | Make sure, that you check the `oc adm prune --help` command and optionally tweak the
|
26 | 30 | CronJob arguments by specifying how much tag revisions you want to preserve on a single
|
27 |
| -tag or other options that might suit your environment. |
28 |
| - |
29 |
| -## Cleaning up old jobs |
30 |
| - |
31 |
| -To cleanup finished jobs, you can run this command: |
32 |
| - |
33 |
| -`oc delete jobs -l job=prune-images` |
34 |
| - |
35 |
| -Note that starting from Origin version 3.6, you will be able to specify `successfulJobsHistoryLimit` and `failedJobsHistoryLimit` |
36 |
| -options for the CronJob, so the cleanup command above won't be needed. |
| 31 | +tag or other options that might suit your environment. Full details about pruning images |
| 32 | +can be found in the [official documentation](https://docs.openshift.org/latest/admin_guide/pruning_resources.html#pruning-images). |
0 commit comments