iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
DevOps

關於我幫新公司建立整套部屬流程那檔事系列 第 24

EP24 - 持續部署使用 Octopus Deploy 四部曲,整合 Jenkins 自動部署到 EKS

今天終於將實作做完了,
前幾天我們都在調整系統底層的設定,
為的就是在 UI 上面可以直接連接,
今天我們終於可以繼續在 UI 上,
直接設定專案,
並把整條部署流水線建起來。

設定 Octopus 專案 Process

添加 ECR Repository

登入 Octopus
https://ithelp.ithome.com.tw/upload/images/20211006/20141518IsJ3w1Fs7r.png

點選上方 Library
https://ithelp.ithome.com.tw/upload/images/20211006/20141518cOB6mEVJTL.png

點選左側的 External Feeds
https://ithelp.ithome.com.tw/upload/images/20211006/20141518JNVHNxnoJ1.png

按下 ADD Feed,添加 Repostiroy
https://ithelp.ithome.com.tw/upload/images/20211006/20141518qUBhVWA0wU.png

填寫所需資料,並按下 SAVE AND TEST
TOKEN 和 SECRETS 一樣使用 Jenkins 那組
https://ithelp.ithome.com.tw/upload/images/20211006/20141518y6g1Pe0bE3.png

輸入 repostiory 的名稱
https://ithelp.ithome.com.tw/upload/images/20211006/20141518vTtTwuPSRn.png

如果有找到,表示設定成功
https://ithelp.ithome.com.tw/upload/images/20211006/20141518RJabNO8pF3.png

為專案添加參數組

這步驟是為了把所有系統需要的參數做管理
並且可以根據不同環境 assign 不同變數
如果專案中有細項需要修改
則可以將共用的放在 Library Sets 中
並在專案內部設定自己需要的參數

進入專案頁
https://ithelp.ithome.com.tw/upload/images/20211006/20141518D62zdghpxW.png

點按左側 Variable
https://ithelp.ithome.com.tw/upload/images/20211006/20141518DcD411GZgQ.png

選擇 Library Sets
https://ithelp.ithome.com.tw/upload/images/20211006/20141518V90MXtIMyK.png

點按 INCLUDE LIBRARY VARIABLE SETS
https://ithelp.ithome.com.tw/upload/images/20211006/20141518oyVHx5OXqi.png

添加完成
https://ithelp.ithome.com.tw/upload/images/20211006/20141518duB9hk6OQq.png

設定部署流程

進入專案頁
https://ithelp.ithome.com.tw/upload/images/20211006/20141518D62zdghpxW.png

點按左側的 Process
https://ithelp.ithome.com.tw/upload/images/20211006/201415182BDblu3wqg.png

點按 ADD STEP,並選擇 Kubernetes
https://ithelp.ithome.com.tw/upload/images/20211006/201415182qsgrh0xle.png

選擇 Deploy Kubernetes RAW YAML
https://ithelp.ithome.com.tw/upload/images/20211006/201415181AcNdzW9hX.png

On Behalf Of 選擇昨天建立的 jenkins-stage
https://ithelp.ithome.com.tw/upload/images/20211006/20141518myX3fCNWGv.png

按下 ADD SOURCE CODE
https://ithelp.ithome.com.tw/upload/images/20211006/20141518HYUdkNU0pJ.png

將之前部署流程的 deploy.yaml 貼上
https://ithelp.ithome.com.tw/upload/images/20211006/20141518kl2eWwvRYi.png

填寫 ECR 資訊
https://ithelp.ithome.com.tw/upload/images/20211006/20141518apuAwyXSqb.png

填寫 namespace
https://ithelp.ithome.com.tw/upload/images/20211006/20141518xu2HbztZSM.png

當然也可以選擇 Deploy Kubernetes Container
不過經過測試
目前似乎 ingress 的部分
api version 卡在 v1beta1 和 v1 的部分
所以建議還是先使用
https://ithelp.ithome.com.tw/upload/images/20211006/201415183qrApy1NNT.png

調整 YAML source code

新增 configMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: portal-config
  namespace: ithome-ironman
data:
  config.json |
    {
      "RDS_HOST": "#{RDS_HOST}"
      "USERNAME": "#{USERNAME}"
      "PASSWORD": "#{PASSWORD}"
    }

Deployment 掛載 config map

apiVersion: apps/v1
kind: Deployment
metadata:
  name: portal
  namespace: ithome-ironman
spec:
  volumes:
    - name: portal-config-volume
      configMap:
        name: 'portal-config'
  replicas: 1
  selector:
    matchLabels:
      app: portal
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  minReadySeconds: 10
  template:
    metadata:
      name: portal
      labels:
        app: portal
    spec:
      containers:
      - name: portal
        image: [你的 AWS 10 碼 ID].dkr.ecr.ap-northeast-1.amazonaws.com/portal:latest
        ports:
        - name: portal
          containerPort: 31000
        volumeMounts:
          - name: portal-config-volume
            mountPath: /var/www/portal/config.json
            subPath: config.json
        resources:
          limits:
            memory: "512M"
            cpu: "300m"
          requests: 
            memory: "200M"
            cpu: "100m"

將專案調整成自動部署

網頁上方選擇 Library
https://ithelp.ithome.com.tw/upload/images/20211006/20141518PuJC4FxznY.png

選擇 Lifecycles
https://ithelp.ithome.com.tw/upload/images/20211006/20141518kNPTH5ZrBw.png

選擇 Default
https://ithelp.ithome.com.tw/upload/images/20211006/20141518RVTyB1bSas.png

按下 ADD PHASE
https://ithelp.ithome.com.tw/upload/images/20211006/20141518u5twTxoZ5r.png

Phase 名稱輸入 Stage,並點按中間的 ADD ENVIRONMET
https://ithelp.ithome.com.tw/upload/images/20211006/20141518RIcoOjrUQx.png

選擇 Stage 和 Automatically Deploy
https://ithelp.ithome.com.tw/upload/images/20211006/20141518SOuqdUFbF2.png

儲存後可看到剛剛新增的結果
https://ithelp.ithome.com.tw/upload/images/20211006/20141518mY4dcg5D3r.png


調整 Jenkins 部署

建立 FreeStyle 專案

Jenkins 首頁
https://ithelp.ithome.com.tw/upload/images/20211006/201415180pZjAZ1zYq.png

建立 FreeStyle 專案
https://ithelp.ithome.com.tw/upload/images/20211006/20141518iKbhUJfa7y.png

新增一個建置參數
等等會從原本的 pipeline 傳入
https://ithelp.ithome.com.tw/upload/images/20211006/201415181XlUYTv2r4.png

在建置後觸發流程,選擇 Create Release 到 Octopus
https://ithelp.ithome.com.tw/upload/images/20211006/20141518uV9UEmHl9Y.png

填寫相關資訊
https://ithelp.ithome.com.tw/upload/images/20211006/20141518erSavk8cOp.png

調整 Pipeline

新增 stage

Deploy to EKSDeploy 刪除起來
並新增 Trigger Octopus Deploy

    stage('Trigger Octopus Deploy') {
        steps {
            build job: 'portal-stage-deploy', parameters: [[$class: 'StringParameterValue', name: 'LATEST_BUILD_ID', value: "${env.BUILD_ID}"]]
        }
    }

新增 post action

在專案最下方
與 stages 同一層新增 post 區塊
這個的用意是 CI/CD 結束後
清理 Workspace
如果沒有清理 Workspace
會造成 CI 過程中打包的 .tar.gz 檔不會更新

  post {
    always {
      cleanWs(cleanWhenNotBuild: false,
              deleteDirs: true,
              disableDeferredWipeout: true,
              notFailBuild: true,
              pattern: [[pattern: '.gitignore', type: 'INCLUDE'],
                        [pattern: '.pyc', type: 'INCLUDE'],
                        [pattern: '.propsfile', type: 'EXCLUDE']]
      
      )
    }
  }

Check-in 程式碼

git add Jenkinsfile
git commit -m "adjust pipeline with integrating octopus deploy"

今天我們整合完 Octopus Deploy 和 Jenkins
讓我們在 Push Code 之後
可以自動部署到 EKS
Octopus Deploy 的功能真的蠻強大的
除了支援各種部署方式以外
將系統參數打包成 Library Sets 更是好用的一種方式
系統連線的明碼不進版控
而且有介面可以簡單編輯
部署方面也充分模組化
只需要更改 lifecyle
當你需要多個環境時
可以修改 library sets 和 lifecyle
就可以根據不同的環境套用不同的參數
但是在部署流程中 (Process) 又是使用同一套方式


上一篇
EP23 - 持續部署使用 Octopus Deploy 三部曲,新增 Octopus 專案及調整 Jenkins 環境
下一篇
EP25 - EKS 日誌蒐集使用 Loki 和 Grafana(一)
系列文
關於我幫新公司建立整套部屬流程那檔事30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言