【Devops】 发布一个Python项目(Flask服务后端)到K8S环境

2021-03-04 19:28

阅读:582

标签:ase   新博   ldd   script   config   count   stage   keep   cdn   

前言:

有一段时间没有更新博客了,今天给大家分享一下如何将一个python项目成功部署并运行到K8S环境,特做一个记录

 

准备工作

1. 编写一个python项目,我这边提供的一个Flask服务,提供接口的mock能力。(项目里面编写如下文件)

  1. dockerfile
  2. jenkinsfile
  3. deploy文件夹(内含: deploy.yaml   service.yaml   ingress.yaml)

流程简释:

jenkinsFile 执行流水线语法---打包docker镜像---把镜像发布到K8s环境(这里用到了deploy的三个文件: deploy,servic,ingress)

 

必备文件介绍

1. jenkinsFile 

技术图片技术图片
  1 @Library(‘devops‘) _
  2  
  3 String BUILD_RESULT = ""
  4 String RELEASE_BUILD=""
  5 pipeline {
  6     agent {
  7         label ‘slave‘
  8     }
  9     options {
 10         buildDiscarder(logRotator(numToKeepStr: ‘10‘))
 11         disableConcurrentBuilds()
 12         skipDefaultCheckout()
 13         timeout(time: 30, unit: ‘MINUTES‘)
 14         gitLabConnection(‘gitlab‘)
 15     }
 16     environment {
 17         IMAGE_CREDENTIALS = "credential-harbor"
 18         NOTIFY_ACCOUNT= "123456"
 19         DEV_BRANCH="dev"
 20         QA_BRANCH="v.*"
 21         IMAGE_REPOSITORY = "harbor.123.cn/test/mock"
 22         BUILD_CONTEXT="build"
 23     }
 24  
 25     stages {
 26  
 27         stage(‘Checkout‘) {
 28              
 29             steps {
 30                 script {
 31                         container(‘tools‘) {
 32                             // checkout code
 33                             retry(2) { scmVars = checkout scm }
 34                             env.RELEASE_BUILD = scmVars.GIT_COMMIT
 35                             BUILD_RESULT = devops.updateBuildTasks(BUILD_RESULT,"Checkout OK...√")
 36                             echo ‘begin checkout... 37                             echo sh(returnStdout: true, script: "env")
 38  
 39                     }
 40                 }
 41             }
 42         }
 43         
 44         stage(‘build-cdnplus-mock-image‘) {
 45              
 46             steps {
 47                 script {
 48                         container(‘tools‘) {
 49                             retry(2) {
 50                                 sh """
 51                                         mkdir -p ${BUILD_CONTEXT};                                     
 52                                      """                                                     
 53                             }
 54                             devops.dockerBuild(
 55                                     "Dockfile", //Dockerfile
 56                                     ".", // build context
 57                                     "${IMAGE_REPOSITORY}", // repo address
 58                                     env.RELEASE_BUILD, // tag
 59                                     IMAGE_CREDENTIALS, // credentials for pushing
 60                                 ).start().push()
 61                         }
 62                 }
 63             }
 64         }
 65         stage(‘deploy-cdnplus-mock‘) {
 66             when {
 67                expression { BRANCH_NAME ==~ env.DEV_BRANCH || BRANCH_NAME ==~ env.QA_BRANCH }
 68            }
 69             steps {
 70                 script {
 71                     container(‘tools‘) {
 72                             //create configmap and ingress
 73                         devops.deploy("", "deploy/ingress.yaml","",false).start()   
 74                         devops.deploy(
 75                                 "deploy", //k8s files dir
 76                                 "deploy/deploy.yaml",
 77                                 RELEASE_BUILD,
 78                                 true
 79                         ).start()
 80                     }
 81                 }
 82             }
 83         }
 84     }   
 85     post {
 86         success {
 87                 script {
 88                     container(‘tools‘) {
 89                         devops.notificationSuccess("mock", "流水线完成了", RELEASE_BUILD, "dingTalk")
 90                     }
 91                 }
 92             }
 93         failure {
 94                 script {
 95                     container(‘tools‘) {
 96                         devops.notificationFailed("mock", "流水线失败了", RELEASE_BUILD, "dingTalk")
 97                     }
 98                 }
 99             }
100     }
101  
102 }
View Code

 

【Devops】 发布一个Python项目(Flask服务后端)到K8S环境

标签:ase   新博   ldd   script   config   count   stage   keep   cdn   

原文地址:https://www.cnblogs.com/Ronaldo-HD/p/14345904.html


评论


亲,登录后才可以留言!