티스토리 뷰

Cloud/Kubernetes

Kaniko

Jacob_baek 2022. 12. 26. 14:43

Introduce Kaniko

kubernetes cluster 혹은 container 내에서 Dockerfile을 사용하여 container image를 만드는 도구이다.

다음과 같은 특징을 가진다.

  • Docker daemon을 요구하지 않는다.
  • google 에서 개발했지만 google에 종속적이지 않다.
    (실제 google에 의해 관리되어지지 않는다는 github 메인페이지 문구가 있다.)
  • windows container에 대한 빌드는 제공하지 않는다.

실제 build 및 push 과정을 kaniko executor를 사용하여 pod상에서 진행시킬수 있다.
아래는 push 없이 이미지를 만드는 과정과 Azure Container Registry에 push하는 과정을 기술하였다.

How it works

kaniko는 별도의 docker daemon의 연결을 요구하지 않는다.
이는 base image를 가져와 압축해제하고 Dockerfile에 있는 각 명령들을 실행하여 파일시스템의 스냅샷을 만들게 되고
base image에 변경된 파일의 layer를 추가하고 image의 metadata를 업데이트하여 이미지를 빌드하는 과정을 수행하게 된다.

without push

먼저 간단히 configmap으로 이미지를 만드는 Dockerfile을 mount 시켜 이미지를 만들도록 해보았다.
(실제 production 환경에서는 별개의 storage(ex. local, s3, blob storage 등)에 저장된 압축된 Dockerfile을 가지고 이미지를 만드는 방법을 사용하는것을 권장한다.)

apiVersion: v1
kind: ConfigMap
metadata:
  name: dockerfile-example
data:
  Dockerfile: |
    FROM ubuntu
    ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
---
apiVersion: v1
kind: Pod
metadata:
  name: kaniko
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:latest
    args: ["--dockerfile=/workspace/Dockerfile", "--context=dir://workspace", "--no-push"]
    volumeMounts:
      - name: dockerfile-cm
        mountPath: /workspace
  restartPolicy: Never
  volumes:
    - name: dockerfile-cm
      configMap:
        name: dockerfile-example

실제 kaniko pod에는 다음과 같은 로그가 출력되며 이미지가 만들어진다.

INFO[0000] Retrieving image manifest ubuntu
INFO[0000] Retrieving image ubuntu from registry index.docker.io
INFO[0006] Built cross stage deps: map[]
INFO[0006] Retrieving image manifest ubuntu
INFO[0006] Returning cached image manifest
INFO[0006] Executing 0 build triggers
INFO[0006] Building stage 'ubuntu' [idx: '0', base-idx: '-1']
INFO[0006] Skipping unpacking as no commands require it.
INFO[0006] ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
INFO[0006] Skipping push to container registry due to --no-push flag

다만 --no-push 옵션을 주었기 때문에 pod상에서 이미지는 만들어지지만 실제 push를 통한 registry에 저장되지는 않는다.
해당 이미지는 해당 pod상에서만 만들어진것이기에 host에서 확인되지 않는다.

with push

Dockerhub 부터 GCR/ECR/ACR 등 다양한 registries에 대한 push가 가능하다.
실제 registry에 대한 인증정보는 /kaniko/.docker/kaniko.json파일을 통해 사용되어진다.

echo -n USER:PASSWORD | base64
cat << EOF >> kaniko.json
{
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "xxxxxxxxxxxxxxx"
    }
  }
}
EOF

Azure Container Registry 기준

아래와 같이 config.json 파일을 service principal 정보를 기반으로 만들어내고
이를 configmap으로 생성한다.(가급적 kevault와 같은 secret manager를 사용하는것을 추천한다.)

cat << EOF >> config.json
{
  "auths": {
    "jacobk8sacreg.azurecr.io": {
      "username": "[app_id]",
      "password": "[cliennt_secret]"
    }
  }
}
EOF

kubectl create configmap docker-config --from-file=./config.json

생성된 config.json 파일을 사용하도록 volumemount를 하여 kaniko로 만들어진 image를 ACR에 push 한다.

apiVersion: v1
kind: ConfigMap
metadata:
  name: dockerfile-example
data:
  Dockerfile: |
    FROM ubuntu
    ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
---
apiVersion: v1
kind: Pod
metadata:
  name: kaniko
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:latest
    args: ["--dockerfile=/workspace/Dockerfile", "--context=dir://workspace", "--destination=jacobk8sacreg.azurecr.io/helloworld:v1.0"]
    volumeMounts:
      - name: dockerfile
        mountPath: /workspace
      - name: docker-config-file
        mountPath: /kaniko/.docker/
  restartPolicy: Never
  volumes:
    - name: dockerfile
      configMap:
        name: dockerfile-example
    - name: docker-config-file
      configMap:
        name: docker-config

다음과 같은 로그가 출력되어지며 이미지가 push 되게 된다.

INFO[0005] Retrieving image manifest ubuntu
INFO[0005] Retrieving image ubuntu from registry index.docker.io
INFO[0007] Built cross stage deps: map[]
INFO[0007] Retrieving image manifest ubuntu
INFO[0007] Returning cached image manifest
INFO[0007] Executing 0 build triggers
INFO[0007] Building stage 'ubuntu' [idx: '0', base-idx: '-1']
INFO[0007] Skipping unpacking as no commands require it.
INFO[0007] ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
INFO[0007] Pushing image to jacobk8sacreg.azurecr.io/helloworld:v1.0
INFO[0009] Pushed jacobk8sacreg.azurecr.io/helloworld@sha256:4d0xxx22dxxxxxxxx07f0xxxxxxxxxxxxcd8c8xxxxxxxxx8b15cxxxxxxxxx774

업로드된 이미지를 az command로 확인할 수 있다.

jacob@desktop:~/workspace $ az acr repository list -n jacobk8sacreg -o table
Result
-----------------
helloworld

References

Disclaim
This document is a collection of personal experiences and is not an official document by Microsoft.
해당 문서는 개인의 경험을 정리한것으로 Microsoft의 공식 문서가 아닙니다.

'Cloud > Kubernetes' 카테고리의 다른 글

cert-manager with ingress-nginx  (0) 2023.02.13
fluent-bit with azure service  (0) 2023.01.02
kubectl debug and koolkit  (0) 2022.04.14
Kubernetes Sealed Secrets  (0) 2022.03.21
log transfer into Loki which is in external cluster  (0) 2022.03.17
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함