- 필요없는 컨테이너 이미지를 계속 저장하면 비용이 늘어 남.
- GCP Container Registry 의 태깅하지 않은 이미지를 제거. ( 또는 옵션을 사용하여 특정 태그를 삭제 가능 > 문서 참고.)
- https://github.com/sethvargo/gcr-cleaner 은 이미지 제거 프로그램.
- Cloud Run 을 통해 gcr-cleaner 를 실행.
- Cloud Scheduler 를 이용하여 주기적으로 gcr-cleaner 를 실행.
1. API 기능을 활성화.
gcloud services enable --project "${PROJECT_ID}" \
appengine.googleapis.com \
cloudscheduler.googleapis.com \
run.googleapis.com
2. Cloud Run (gcr-cleaner) 의 service account 생성.
gcloud iam service-accounts create "gcr-cleaner" \
--project "${PROJECT_ID}" \
--display-name "gcr-cleaner"
3. Cloud Run 을 배포.
gcloud --quiet run deploy "gcr-cleaner" \
--async \
--project ${PROJECT_ID} \
--platform "managed" \
--service-account "gcr-cleaner@${PROJECT_ID}.iam.gserviceaccount.com" \
--image "us-docker.pkg.dev/gcr-cleaner/gcr-cleaner/gcr-cleaner" \
--region "us-central1" \
--timeout "60s"
- region 은 본인이 사용한 곳을 변경.
4. Service Account 에 컨테이너 이미지가 저장되는 Cloud Storage bucket의 write 권한을 부여.
gsutil acl ch -u gcr-cleaner@${PROJECT_ID}.iam.gserviceaccount.com:W gs://artifacts.${PROJECT_ID}.appspot.com
- bucket 주소 변경.
5. Cloud Run 을 실행 할 service account 생성한 뒤, Cloud Run(gcr-cleaner) 을 실행 할 권한을 부여.
gcloud iam service-accounts create "gcr-cleaner" \
--project "${PROJECT_ID}" \
--display-name "gcr-cleaner"
gcloud run services add-iam-policy-binding "gcr-cleaner" \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "us-central1" \
--member "serviceAccount:gcr-cleaner-invoker@${PROJECT_ID}.iam.gserviceaccount.com" \
--role "roles/run.invoker"
6. App Engin 생성
gcloud app create \
--project "${PROJECT_ID}" \
--region "us-central" \
--quiet
- 이미 있다면 생략 가능.
7. image repository, servcie url 환경 변수 생성.
# Replace this with the full name of the repository for which you
# want to cleanup old references, for example:
export REPO="gcr.io/${PROJECT_ID}/my-image"
export REPO="us-docker-pkg.dev/${PROJECT_ID}/my-repo/my-image"
# Capture the URL of the Cloud Run service:
export SERVICE_URL=$(gcloud run services describe gcr-cleaner --project "${PROJECT_ID}" --platform "managed" --region "us-central1" --format 'value(status.url)')
- REPOT : 이미지를 제거할 대상 repository
- SERVICE_URL : cgr-cleaner 를 실행할 end-point
8. 스케쥴러 생성.
gcloud scheduler jobs create http "gcrclean-myimage" \
--project ${PROJECT_ID} \
--description "Cleanup ${REPO}" \
--uri "${SERVICE_URL}/http" \
--message-body "{\"repo\":\"${REPO}\"}" \
--oidc-service-account-email "gcr-cleaner-invoker@${PROJECT_ID}.iam.gserviceaccount.com" \
--schedule "0 8 * * 2" \
--time-zone="US/Eastern"
- message-body 에 몇가지 옵션을 추가 할 수 있다. 옵션들은 github 에 설명되어 있음.
- schedule 은 cront job
'Google Cloud Platform' 카테고리의 다른 글
Login with Service Account (0) | 2020.08.03 |
---|---|
[VM] autoscaling (0) | 2020.06.11 |
[VM] start up script to install apache and index page showing information of client, vn (0) | 2020.06.11 |
[VM] Authorize the VM with credentials (0) | 2020.06.02 |
[VM] upload file (0) | 2020.06.02 |