Docker Agent Pod 구현

위의 구성을 구현하기 위해서 도커 에이전트를 pipeline syntax 내부에 agent로 정의해주어야 한다
Docker Agent PV, PVC
apiVersion: v1
kind: PersistentVolume
metadata:
name: docker-agent-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
local:
path: /mnt/docker_new
volumeMode: Filesystem
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values: #your node
------------------------------------
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: docker-agent-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10G
Jenkins가 존재하는 namespace에 PV와 PVC를 선언하여 마운트해준다
파이프라인의 Docker Agent yaml
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: jenkins-agent
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent
tty: true
- name: docker
image: docker:20.10.7-dind
env:
- name: http_proxy
value: #your
- name: https_proxy
value: #your
securityContext:
privileged: true
volumeMounts:
- name: docker-cache-storage
mountPath: /var/lib/docker
volumes:
- name: docker-cache-storage
persistentVolumeClaim:
claimName: docker-agent-pvc
"""
}
stage('Push Docker Image') {
steps {
container('docker') {}
........
}
pod template으로 Jenkins 내부에 깔끔하게 정의할 수 있지만 나는 편의를 위해 pipeline 내부에 그냥 정의해주었다
파이프라인 스테이지 예시
stage('Push Docker Image') {
steps {
container('docker') {
# 명령어
}
해당 agent에 접근하는 방식은 위와 같이 명령어를 작성해주면 된다
실행 예시
Pipeline 실행 중 파드 상태

Docker Agent pod 로그 일부

Jenkins_Tunnel인 jnlp를 통해 도커 에이전트와 Jenkins가 연동된 것을 알 수 있다
파이프라인 Docker 로그 일부

빌드가 잘 된 것을 확인할 수 있다
Agent Pod Terminating

파이프라인 종료 후 Terminaitng 되는 것을 볼 수 있다
캐싱으로 속도 개선

Docker Agent와 pv가 마운트되어 캐싱으로 속도가 개선된 것을 확인할 수 있다
속도 21s → 7s (-70%)

데모 앱이라 속도가 몇초 차이나지 않지만 코드의 라이브러리가 커질 수록, 프로젝트 규모가 증가할 수록 이미지 빌드 속도는 증가한다
'DevOps(kubernetes)' 카테고리의 다른 글
[k8s] k8s 클러스터 CI/CD 파이프라인 아키텍처 (0) | 2024.08.13 |
---|---|
[Jenkins] Jenkins 파드에서 도커 이미지 빌드(1) Docker Agent 파드 채택한 이유 (0) | 2024.08.13 |
[Gitlab] Gitlab 파드 root 비밀번호 찾는법 / 비밀번호 재설정 (0) | 2024.08.13 |
[Gitlab] root(admin)에서 user register 승인하기 (0) | 2024.08.13 |
[ArgoCD] Gitlab repo 연동하는 법 (0) | 2024.08.13 |