본문 바로가기
시스템/쿠버네티스

[쿠버네티스] 쿠버네티스 배포하기 위한 manifest 파일생성 & 깃랩전송 오류시 credential 설정

by cbwstar 2024. 1. 16.
728x90
반응형

1. manifest 레포지토리 생성

 

2. 로컬에 manifest 폴더 생성

 

- README.md , pipelineapp_deployment.yaml. pipelineapp_service.yaml 파일 생성

- 쿠버네티스에 디폴로이 하기 위한 deployment  파일 생성

pipelineapp_deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pipelineapp-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      type: pipeline
      version: v1
  template:
    metadata:
      labels:
        type: pipeline
        version: v1
    spec:
      containers:
      - name: pipelineapp-01
        image: invako.com/pipelinetest/pipe:latest
        ports:
        - containerPort: 8080

- 쿠버네티스에 외부에 서비스 하기 위한 service 파일 생성

pipelineapp_service.yaml

apiVersion: v1
kind: Service
metadata:
  name: pipelineapp-service
spec:
  selector:
    type: pipeline
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30050
  type: NodePort

- Jenkinsfile 수정

pipeline {
    agent any
    // any, none, label, node, docker, dockerfile, kubernetes
    tools {
      gradle 'gradle8.5'
    }
 environment {
      dockerHubRegistry = 'invako.com/pipelinetest/pipe' /* URL Harbor 저장소 push 테스트 */
      /* dockerHubRegistryCredential = '{Credential ID}'*/
  }
  stages {
    stage('Checkout Application Git Branch') {
        steps {
            git credentialsId: 'gitlab_token',
                url: 'https://invako.kro.kr:8090/cbw/piplinetest.git', /* URL변경에 따른 수정 필요 */
                branch: 'main'
        }
        post {
                failure {
                  echo 'Repository clone failure !'
                }
                success {
                  echo 'Repository clone success !'
                }
        }
    }


    stage('gardle Jar Build') {
            steps {
                sh 'chmod +x ./gradlew'
                sh './gradlew bootjar'
            }
            post {
                    failure {
                      echo 'Gradle jar build failure !'
                    }
                    success {
                      echo 'Gradle jar build success !'
                    }
            }

    }

    stage('Docker Image Build') {
            steps {
                sh "cp ./build/libs/piplinetest-0.0.1-SNAPSHOT.jar ./pipelinetest.jar"
                sh "docker build . -t ${dockerHubRegistry}:${currentBuild.number}"
                sh "docker build . -t ${dockerHubRegistry}:latest"
            }
            post {
                    failure {
                      echo 'Docker image build failure !'
                    }
                    success {
                      echo 'Docker image build success !'
                    }
            }
    }


    stage('Docker Image Push') {
            steps {
                      sh "echo 도커허브비밀번호 | docker login invako.com -u admin -p Invako12!@"
                      sh "docker push ${dockerHubRegistry}:${currentBuild.number}"
                      sh "docker push ${dockerHubRegistry}:latest"
                      sleep 10 /* Wait uploading */

            }
            post {
                    failure {
                      echo 'Docker Image Push failure !'
                      sh "docker rmi ${dockerHubRegistry}:${currentBuild.number}"
                      sh "docker rmi ${dockerHubRegistry}:latest"
                    }
                    success {
                      echo 'Docker image push success !'
                      sh "docker rmi ${dockerHubRegistry}:${currentBuild.number}"
                      sh "docker rmi ${dockerHubRegistry}:latest"
                    }
            }
    }
/* 쿠버네티스에 서비스 하기 위한 디폴로이 파일 수정후 커밋 */
    stage('K8S Manifest Update') {
        steps {
            git credentialsId: 'gitlab_token',
                url: 'https://invako.kro.kr:8090/cbw/manifest.git',
                branch: 'main'
            sh "git config --global user.email 'cbwstar@gmail.com'"
            sh "git config --global user.name 'manager'"
            sh "sed -i 's/pipe:.*\$/pipe:${currentBuild.number}/g' pipelineapp_deployment.yaml"
            sh "git add pipelineapp_deployment.yaml"
            sh "git commit -m '[UPDATE] pipelineapp ${currentBuild.number} image versioning'"
            sh "git remote set-url origin https://invako.kro.kr:8090/cbw/manifest.git"
            sh "git push -u origin main"
       /*
            sshagent (credentials: ['GitLab_SSH_Key']) {
                sh "git remote set-url origin git@git.kbotest.shop:kbo/manifest.git"
                sh "git push -u origin main"
            }
            */
        }
        post {
                failure {
                  echo 'K8S Manifest Update failure !@'
                }
                success {
                  echo 'K8S Manifest Update success !!'
                }
        }
    }

  }
}

 

 

3. 깃랩 전송 오류 발생시

 

/* Username 오류가 발생할 경우 */
+ git push --set-upstream origin main
fatal: could not read Username for 'https://invako.kro.kr:8090': No such device or address

vscode에서 push, pull, remote 등 명령어를 입력하면 Git 계정과 비밀번호를 묻는데, 

젠킨스 서버에서 비밀번호를 묻고 있어서 에러가 발생한 경우다.

젠킨스 서버에 접속하여 비밀번호 영구 저장을 하도록 git서버에 수동으로 push하여 비밀번호를 저장한다.

비밀번호 묻지 않게 옵션을 먼저 적용하고 push 해야 한다.

 

아래 명령어는 로그인 정보를 요구할 때 한 번 입력하게 되면 그 정보를 저장하고 이후로는 로그인 정보를 묻지 않고 자동으로 인증한다. (해당 repository만 적용하고자하면 global옵션을 제외하자)

git config --global credential.helper store

# 또는 로그인 정보를 시간단위로 저장
# git config --global credential.helper 'cache --timeout 600'

아래 명렁어로 설정이 되어있는지 확인할 수 있다.

git config --list

4. 쿠버네티스 환경 설정 파일 버전이 변경 되어서 적용 되었는지 확인

쿠버네티스 기본 배포 설정 끝

다음은 ArgoCD를 이용한 CD 구축

728x90
반응형

댓글



"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

loading