iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 8
0
Microsoft Azure

Azure 的奇幻之旅系列 第 8

Azure 系列文(8) - Azure Kubernetes Service to Pipeline

上一篇跟大家介紹到了Azure Kubernetes Service,那今天就將AKS跟Pipeline結合起來,讓把Web App部署到AKS的人,也可以透過Pipeline持續部署、持續整合到AKS,廢話不多說,馬上開始吧!

準備

  • Azure 帳號
  • Azure Devops
  • Azure Kubernetes Service
  • 本篇的範例

建立Pipeline

首先第一步,到Azure Devops
https://ithelp.ithome.com.tw/upload/images/20200917/20127994jPGHCddgIO.png

然後選Pipeline -> Pipeline
https://ithelp.ithome.com.tw/upload/images/20200917/20127994OiJjB2IBDi.png

再來點New Pipeline
https://ithelp.ithome.com.tw/upload/images/20200917/20127994scrEXplS9r.png

再來選擇你Code存放的地方,這邊我選Azure的Repo
https://ithelp.ithome.com.tw/upload/images/20200917/20127994sLsbszHOrY.png

接下來選Repo
https://ithelp.ithome.com.tw/upload/images/20200917/201279945rYh8Fgl5Z.png

再來選Deploy to Azure Kubernetes Service
https://ithelp.ithome.com.tw/upload/images/20200917/20127994529bu8pxpp.png

填一些Pipeline的基本資料
https://ithelp.ithome.com.tw/upload/images/20200917/20127994QZvc4Fei1O.png

接下來他會自動幫你代入預設的YAML檔案,還會自動幫你加入部署的YAML檔案

azure-pipelines-1.yml
deployment.yml
service.yml

# Deploy to Azure Kubernetes Service
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger: -> 觸發的Branch
- master

resources:
- repo: self

variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: {dockerRegistryServiceConnection}
  imageRepository: {imageRepository}
  containerRegistry: {containerRegistry}
  dockerfilePath: '**/Dockerfile'
  tag: '$(Build.BuildId)'
  imagePullSecret: {imagePullSecret}

  # Agent VM image name
  vmImageName: 'ubuntu-latest'
  

stages:
- stage: Build
  displayName: Build stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
          
    - upload: manifests
      artifact: manifests

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build

  jobs:
  - deployment: Deploy
    displayName: Deploy
    pool:
      vmImage: $(vmImageName)
    environment: 'jesperit.flask'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              action: createSecret
              secretName: $(imagePullSecret)
              dockerRegistryEndpoint: $(dockerRegistryServiceConnection)
              
          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              action: deploy
              manifests: |
                $(Pipeline.Workspace)/manifests/deployment.yml
                $(Pipeline.Workspace)/manifests/service.yml
              imagePullSecrets: |
                $(imagePullSecret)
              containers: |
                $(containerRegistry)/$(imageRepository):$(tag)

上面這個YAML檔案主要做的事情就是先去讀DockerFile,再來是Docker Build,Docker push至ACR,接下來AKS部署就去拉ACR的Images,這些步驟就可以完成部署了!!

當然只有上面這些預設可能會不夠,那我們這時候可以視自身狀況去新增一些Task,這時候可以透過官方的文件去看有哪些Task可以使用,或者也可以透過旁邊的assistant選擇你需要的Task,像是我們可以搜尋Docker。

https://ithelp.ithome.com.tw/upload/images/20200917/201279944I4zzM2T4m.png

然後在填一些資訊就可以新增Docker了!

https://ithelp.ithome.com.tw/upload/images/20200917/20127994QjKHJipNf4.png

之後按Save And Run就可以跑Pipeline to AKS了!

https://ithelp.ithome.com.tw/upload/images/20200917/20127994XMOrRfwElU.png

然後這邊會跳出一個error是

no matches for kind "Deployment" in version "apps/v1beta

查了一下原因是Kubernetes更新之後YAML檔的apps/v1beta要改成apps/v1

apiVersion : apps/v1beta -> 改成apps/v1
kind: Deployment
metadata:
  name: jesperit 
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: jesperit 
    spec:
      containers:
        - name: jesperit 
          image: jesperaks.azurecr.io/jesperit
          ports:
          - containerPort: 5000

接下來Pipeline跑完後會輸出一個IP,接下來只要IP:Port就可以瀏覽你的專案了!

https://ithelp.ithome.com.tw/upload/images/20200917/20127994zs6ScBXvae.png

再來我們可以測試一下Pipeline就可以測試成功

https://ithelp.ithome.com.tw/upload/images/20200917/20127994sRrCpC3fix.png

我們修改一下Home.html裡面的資料

https://ithelp.ithome.com.tw/upload/images/20200917/20127994M0vbisPhLF.png

等待Pipeline跑完後再看網頁上有沒有改變

https://ithelp.ithome.com.tw/upload/images/20200917/201279945YnQCIJzoz.png

的確是改成功了!!!離成為人生勝利組又更近了一步


上一篇
Azure 系列文(7) - Azure Kubernetes Service
下一篇
Azure 系列文(9) - 幫你的Azure Kubernetes Service加上Https
系列文
Azure 的奇幻之旅30

尚未有邦友留言

立即登入留言