05. Week5 Jenkins on Kubernetes - chuirang/DevOps GitHub Wiki

Jenkins ๋ฅผ ์ด์šฉํ•ด sampleapp์„ kubernetes์— ๋ฐฐํฌํ•˜๊ธฐ

์ ˆ์ฐจ

  1. ์ฝ”๋“œ๋ฅผ GitHub ๋ฆฌํฌ์— ์ €์žฅ
  2. Jenkins ์—์„œ Credential ์ƒ์„ฑ
  3. Jenkins Job ์ƒ์„ฑ

1. ์ฝ”๋“œ๋ฅผ GitHub ๋ฆฌํฌ์— ์ €์žฅ

1.1 ๊ธฐ๋ณธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ

user@u2004-master:~/04_cicd$ tree .
.
โ”œโ”€โ”€ docker
โ”‚   โ”œโ”€โ”€ app.py
โ”‚   โ””โ”€โ”€ Dockerfile
โ”œโ”€โ”€ Jenkinsfile
โ””โ”€โ”€ k8s
    โ”œโ”€โ”€ deployment.yaml
    โ””โ”€โ”€ service.yaml

docker ํ•˜์œ„์˜ app.py, Dockerfile๊ณผ k8s ํ•˜์œ„์˜ yaml ํŒŒ์ผ์€ Week 2์˜ ์‹ค์Šต ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

1.2 Jenkinsfile ์ˆ˜์ •

์†Œ์Šค์ฝ”๋“œ ์œ„์น˜์— Jenkinsfile ๋ณต์‚ฌํ•ด์˜ค๊ณ (cp /tmp/Jenkinsfile .) ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

..
  // ๊ฐ Credential ID๋ฅผ ์•„๋ž˜ ๋„ค์ด๋ฐ๋ฃฐ์— ๋”ฐ๋ผ ์ˆ˜์ • _ line 46, 47
  // ๋„ค์ด๋ฐ๋ฃฐ : {username}_kubeconfig, {username}_kubeconfig
  environment {
    DOCKER_CREDENTIAL_ID = "wonkilee_dockerhub"
    K8S_CREDENTIAL_ID = "wonkilee_kubeconfig"
  }
..
      // ๊ฐ์ž์˜ github ID์™€ repository๋กœ ์ˆ˜์ • _ line 60
      steps {
        git branch: 'main', url: 'https://github.com/chuirang/DevOps.git'
      }
..

Jenkinsfile์„ git์— ์ €์žฅํ•˜๋Š” ์˜๋ฏธ:

Jenkins ์˜ pipeline ์„ Jenkins GUI์—์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, Jenkins pipeline์„ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ด€๋ฆฌํฌ์ธํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜๊ณ  pipeline์„ ๋ณ€๊ฒฝํ•ด๋„ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๊ธฐ๋ก์œผ๋กœ ๋‚จ๊ธธ ์ˆ˜ ์—†๋‹ค.

Jenkinsfile๋กœ pipeline์„ ์ฝ”๋“œ๋กœ ์ •์˜ํ•˜๋ฉด, Git์„ ํ†ตํ•˜์—ฌ ์ฝ”๋“œ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ฝ”๋“œ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ์ฝ”๋“œ์™€ ์„ค์ •์€ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ๋˜๊ณ  ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

1.3 GitHub ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ ์ƒ์„ฑ

์ดํ›„ Jenkins Pipeline์˜ Checkout ๋‹จ๊ณ„์—์„œ ๊ฐœ์ธ github ์ €์žฅ์†Œ๋กœ ๋ถ€ํ„ฐ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋Œ์–ด์˜ค๊ธฐ ์œ„ํ•ด ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  1. GitHub ์ ‘์† > sign in > ์ขŒ์ธก ์ƒ๋‹จ์˜ [New] ๋ฅผ ๋ˆŒ๋Ÿฌ Repository ์ƒ์„ฑ

  2. Repository name ์ž…๋ ฅ ํ›„ [Create repository] ์„ ๋ˆ„๋ฅธ๋‹ค

Github ์ •์ฑ… ๋ณ€๊ฒฝ์œผ๋กœ plain username/password ๋กœ๋Š” ๋”์ด์ƒ git push ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค. (2021/8/13์ผ ๋ถ€ํ„ฐ)

์•„๋ž˜์˜ ์ ˆ์ฐจ๋กœ Personal access token์„ ์ƒ์„ฑํ•œ๋‹ค.

  1. ์šฐ์ธก ์ƒ๋‹จ ๊ฐœ์ธํ”„๋กœํ•„ > settings > Developer settings > Personal access tokens > Generate new token

  2. ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ • ํ›„ [Generate token] ์„ ๋ˆ„๋ฅธ๋‹ค.

  • Note: git_acess_token
  • Selet scopes: repo ์ฒดํฌ
  1. ํ™”๋ฉด์— ๋…ธ์ถœ๋œ Token์„ ๋ณต์‚ฌํ•ด ๋‘”๋‹ค. (์ด ํ™”๋ฉด์€ ๋‹ค์‹œ ํ™•์ธ๋ถˆ๊ฐ€ํ•˜๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ๋…ธํŠธํŒจ๋“œ์— ๋ณต์‚ฌํ•ด๋‘˜ ๊ฒƒ)

1.4 GitHub ๋กœ ์†Œ์Šค์ฝ”๋“œ ๋™๊ธฐํ™”

์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ์ƒ๋‹จ ์œ„์น˜์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ น์–ด ์ˆ˜ํ–‰ํ•œ๋‹ค.

git init
git add -A
git commit -m "first commit"
git config --global user.email "[email protected]"  // ๊ฐ์ž์˜ github mail
git config --global user.name "your name"   // ๊ฐ์ž์˜ ์ด๋ฆ„
git branch -M main
git remote add origin https://github.com/{Github_ID}/{Repo_Name}.git
git push -u origin main
  • git push ์—์„œ 1.3์—์„œ ์ƒ์„ฑํ•œ Personal access token์„ ํŒจ์Šค์›Œ๋“œ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

์˜ˆ์‹œ)

user@u2004-master:~/04_sample$ ls
docker  Jenkinsfile  k8s
user@u2004-master:~/04_sample$ git init
Initialized empty Git repository in /home/user/04_sample/.git/
user@u2004-master:~/04_sample$ git add -A
user@u2004-master:~/04_sample$ git commit -m "first commit"
[master (root-commit) 8106e8d] first commit
 5 files changed, 159 insertions(+)
 create mode 100644 Jenkinsfile
 create mode 100755 docker/Dockerfile
 create mode 100755 docker/app.py
 create mode 100755 k8s/deployment.yaml
 create mode 100755 k8s/service.yaml

# ์ด๋ฏธ ์„ค์ •๋˜์–ด ์žˆ์–ด git config --global ์€ ์ƒ๋žต

user@u2004-master:~/04_sample$ git config --global -l
[email protected]
user.name=wonki lee
user@u2004-master:~/04_sample$ git branch -M main
user@u2004-master:~/04_sample$ git remote add origin https://github.com/chuirang/Devops_Sample.git
user@u2004-master:~/04_sample$ git push -u origin main
Username for 'https://github.com': user
Password for 'https://[email protected]': (*Personal access token์„ ํŒจ์Šค์›Œ๋“œ๋กœ ์‚ฌ์šฉ)
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 2.06 KiB | 234.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/user/Devops_Sample.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
user@u2004-master:~/04_sample$

์ •์ƒ์ ์œผ๋กœ push๊ฐ€ ๋˜๋ฉด GitHub ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ๋ฅผ ์ƒˆ๋กœ๊ณ ์นจ ํ–ˆ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด์ธ๋‹ค.

2. Jenkins ์—์„œ Credential ์ƒ์„ฑ

2.1 GitHub credential ์„ค์ • (public repository ์ด๋ฏ€๋กœ ์ƒ๋žต)

2.2 Docker Hub credential ์„ค์ •

Docker ์ด๋ฏธ์ง€ ๋นŒ๋“œ๊ฐ€ ๋๋‚˜๋ฉด ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Docker Hub์— ์ €์žฅํ•ด์•ผํ•œ๋‹ค. Docker Hub์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ธ์ฆ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ๋ฐ, Jenkins์˜ Credential ๋กœ ์ €์žฅํ•˜์—ฌ Pipeline์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.

์•„๋ž˜์˜ ์ ˆ์ฐจ๋กœ Docker Hub ์ธ์ฆ์„ ์œ„ํ•œ credential์„ ์ƒ์„ฑํ•œ๋‹ค.

  • ๋„ค์ด๋ฐ๋ฃฐ : {username}_dockerhub
  1. Jenkins ์ ‘์† > ์ขŒ์ธก ์ฐฝ์—์„œ Manage Jenkins ํด๋ฆญ > Manage Credential ํด๋ฆญ > ํ•˜๋‹จ ํ‘œ์—์„œ Jenkins ํด๋ฆญ > Global credentials (unrestricted) ํด๋ฆญ

  2. ์ขŒ์ธก ์ฐฝ์—์„œ Add Credentials ํด๋ฆญ ํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ

  • Kind: Username with password
  • Scope: Global
  • Username: (DockerHub ID)
  • password: (DockerHub Password)
  • ID: ๋„ค์ด๋ฐ๋ฃฐ์— ๋”ฐ๋ผ ์ž…๋ ฅ (ex. wonkilee_dockerhub)
  • Description: (Optional)

์ƒ์„ฑํ•œ Credential ID๊ฐ€ Pipeline์—์„œ ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด๋‹ค.

2.3 Kubeconfig credential ์„ค์ •:

Jenkins Pipeline ์˜ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ Deploy๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ, ์ด ๋•Œ API Server ์™€ ์ธ์ฆํ•˜๊ธฐ ์œ„ํ•œ kubeconfig์„ Jenkins ์˜ credential๋กœ ๋“ฑ๋กํ•œ๋‹ค.

์•„๋ž˜์˜ ์ ˆ์ฐจ๋กœ pipeline์˜ Deploy stage์—์„œ kubectl ์„ ์‚ฌ์šฉํ•  ๋•Œ ์ฐธ์กฐํ•˜๋Š” credential์„ ์ƒ์„ฑํ•œ๋‹ค.

  • ๋„ค์ด๋ฐ๋ฃฐ : {username}_kubeconfig
  1. Jenkins ์ ‘์† > ใ…์ธก ์ฐฝ์—์„œ Manage Jenkins ํด๋ฆญ > Manage Credential ํด๋ฆญ > ํ•˜๋‹จ ํ‘œ์—์„œ Jenkins ํด๋ฆญ > Global credentials (unrestricted) ํด๋ฆญ

  2. ์ขŒ์ธก ์ฐฝ์—์„œ Add Credentials ํด๋ฆญ ํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ

  • kind: Kubernetes configuration (kubeconfig)
  • Scope: Global
  • ID: ๋„ค์ด๋ฐ๋ฃฐ์— ๋”ฐ๋ผ ์ž…๋ ฅ (ex. wonkilee_kubeconfig)
  • Description: (Optional)
  • kubeconfig: Enter directly ์„ ํƒ > ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ์˜ ์•„๋ž˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ Content์— ์ž…๋ ฅํ•œ๋‹ค.
    $ cat ~/.kube/config
    

์ƒ์„ฑํ•œ Credential ID๊ฐ€ Pipeline์—์„œ ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด๋‹ค.

3. Jenkins Job ์ƒ์„ฑ

GitHub ์˜ repository์—์„œ Jenkinsfile์„ ๊ฐ€์ ธ์˜ค๋„๋ก Job์„ ์ƒ์„ฑํ•œ๋‹ค.

  1. Jenkins > ์ขŒ์ธก์˜ New item

  2. item name์„ Week5_username ์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ  [Pipeline] ์„ ์„ ํƒํ•˜๊ณ  [OK] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค.

  3. ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅ ํ›„ [Save] ๋ฅผ ๋ˆ„๋ฅธ๋‹ค.

  • Pipeline

    Definition: Pipeline scriptPipeline script from SCM

    SCM: Git

    Repository URL: ์•ž์„œ ์ƒ์„ฑํ•œ github repository ๋ฅผ ์ž…๋ ฅ

    Branch Specifier: */main

  1. ์ƒ์„ฑ์ด ์™„๋ฃŒ๋œ Job์—์„œ Build Now ๋ฅผ ๋ˆ„๋ฅด๋ฉด, ํ•˜๋‹จ์— Build History๊ฐ€ ํ™•์ธ๋œ๋‹ค.

  1. ์ •์ƒ์ ์ธ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด Stage View๊ฐ€ ์šฐ์ธกํ™”๋ฉด ํ™•์ธ๋œ๋‹ค.

  1. Build History์˜ Build #1 ์œผ๋กœ ์ด๋™ํ•˜๊ณ , ์ขŒ์ธก์˜ Console Output ์„ ๋ˆŒ๋Ÿฌ๋ณด๋ฉด pipeline ์‹คํ–‰๊ณผ์ •์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.