Jenkins / DinD 3-2. K8S + Docker Cloud Agent

Flexyz·2024년 2월 7일
0

Jenkins

목록 보기
7/11

다시 Docker Cloud Agent ?

앞선 포스팅에서는 쿠버네티스 클라우드 에이전트 및 dind 컨테이너 템플릿을 이용하여 도커를 사용하였습니다.
관리 측면에서는 좋은 방법인 듯하지만 제 관리용 클러스터는 자원이 충분하지 못해서 빌드를 하나씩 해야 합니다...

현재 진행 중인 프로젝트가 마이크로 서비스 아키텍처 기반이라 DB 등 제외하고 서비스만 7개인데...

복수의 빌드를 병렬로 실행하지 않으면 10년 전의 빌드 지옥을 다시 경험하게 될 거 같습니다.
또한 관리 클러스터는 argocd 등 다른 개발용 컨테이너도 함께 구동하기 때문에 여유가 필요하지요.

K8S 젠킨스 컨테이너에서 다른 호스트(개발용 서버)의 자원을 사용하여 빌드할 수 있도록
다시 Docker cloud agent를 사용해 보기로 합시다.


미리 이실직고 하자면 이번 편은 다음 편을 위한 몸풀기입니다.


에이전트용 도커이미지 준비

# 에이전트용 도커 파일 작성
# Dockerfile
FROM jenkins/inbound-agent:latest
USER root

RUN apt-get update && apt-get install -y lsb-release && \
   curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
   https://download.docker.com/linux/debian/gpg && \
   echo "deb [arch=$(dpkg --print-architecture) \
   signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
   https://download.docker.com/linux/debian \
   $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list && \
   apt-get update && apt-get install -y docker.io && \
   apt-get clean && rm -rf /var/lib/apt/lists/*  && \
   usermod -aG docker jenkins

USER jenkins

# 에이전트용 이미지 빌드
docker build -t codelabmaster/jenkins-agent-docker . 

# 이미지 푸시
docker push codelabmaster/jenkins-agent-docker

Cloud docker Configuration

Docker Cloud details

Docker Agent templates

도커 빌드 테스트

파이프라인 생성

pipeline {
    agent {
        docker {
            image 'codelabmaster/jenkins-agent-docker'
            args '-v /var/run/docker.sock:/var/run/docker.sock -u root'
        }
    }
    stages {
        stage('Test') {
            steps {
                sh 'docker run hello-world'
            }
        }
    }
}

기존 쿠버네티스 클라우드 삭제 후 지금 빌드

Started by user Jenkins Admin
[Pipeline] Start of Pipeline
[Pipeline] node
Still waiting to schedule task
Waiting for next available executor
Running on docker-0000hulec5bqo on docker in /home/jenkins/agent/workspace/docker
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . codelabmaster/jenkins-agent-docker
.
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] withDockerContainer
docker-0000hulec5bqo on docker seems to be running inside container b59c8a0b45d890088e89da1cadaf5142a14510087f5da6f4806f995d98a588d0
$ docker run -t -d -u 1000:1000 -v /var/run/docker.sock:/var/run/docker.sock -u root -w /home/jenkins/agent/workspace/docker --volumes-from b59c8a0b45d890088e89da1cadaf5142a14510087f5da6f4806f995d98a588d0 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** codelabmaster/jenkins-agent-docker cat
$ docker top a12fcb565bb418fff744bab805a6456f88a9fc5aee312cffa6024a0275312ee8 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 a12fcb565bb418fff744bab805a6456f88a9fc5aee312cffa6024a0275312ee8
$ docker rm -f --volumes a12fcb565bb418fff744bab805a6456f88a9fc5aee312cffa6024a0275312ee8
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
profile
Think about a better architecture

0개의 댓글