cicd 03--构建通用scale流程

1 介绍

在实际项目中, 如果没有专用的变更系统,那么可以使用jenkins来快速实现各类基础流程,而且能够达到操作溯源的效果。本文基于jenkins做了一个简单的k8s服务scale流程,用于用户scale服务,同时达到操作溯源的目的。

2 cicd构建过程

2.1 基本配置

  1. 参数
    Cluster 参数:

    名称: Cluster
    类型: Choice Parameter
    数值: 
    cloudsim
    data-pipeline
    dtp-quarantine
    

    namespace 参数:

    名称: namespace
    类型: Active Choices Reactive Parameter
    Groovy Script:
    
    if( Cluster == "cloudsim" ){
      return ['sre-test', 'pi-prod', 'pi-dev', 'simulation-prod', 'simulation-dev' ]
    }else if ( Cluster == "data-pipeline" ){
      return ['sre-test', 'pi-prod', 'pi-dev', 'debezium-prod']
    }else if ( Cluster == "dtp-quarantine" ){
      return ['sre-test', 'quarantine']
    }else{
    return[]
    }
    
    Choice Type: Single Select
    Referenced parameters: Cluster
    

    deployment参数:

    名称: deployment
    类型: String Parameter
    默认值: busybox
    

    replicas 参数:

    名称: replicas
    类型: String Parameter
    默认值: 1
    
  2. jenkinsfile
    实际中需要更改yourPath 为正确的用户目录,.kube下的文件最好为 config-${Cluster} 的形式, 节点标签 SRE_NODE 按需更改即可。

    k8s_cluster = "${params.Cluster}"
    namespace = "${params.namespace}"
    deployment = "${params.deployment}"
    replicas = "${params.replicas}"
    println("${k8s_cluster} namespace/deployment=${namespace}/${deployment} replicas=${replicas}")
    
    default_description = "${k8s_cluster} ${namespace}/${deployment} ${replicas}"
    currentBuild.description = "${default_description}"
    
    pipeline {
        agent any
     
        stages {
            stage('check deploy') {
            agent { 
                node { 
                    label 'SRE_NODE' 
                } 
            }
             steps {
                echo 'Hello, check deployment ${namespace}/${deployment} exists'
                sh "/usr/bin/kubectl --kubeconfig /home/yourPath/.kube/config-${k8s_cluster} -n ${namespace} get deploy ${deployment}"
            }
          }
    
          stage("scale deploy"){
              agent { 
                 node { 
                     label 'SRE_NODE' 
                 } 
             }
             steps {
                echo 'Hello, scale deployment ${namespace}/${deployment} exists'
                sh "/usr/bin/kubectl --kubeconfig /home/yourPath/.kube/config-${k8s_cluster} -n ${namespace}  scale deployment ${deployment} --replicas=${replicas}"
             }
          }   
        }
     post {
         always {
             echo 'I have finished'
         }
         success {
             echo "scale , succeed!"
             sh """
             curl -X POST -H "Content-Type: application/json" -d '{"msg_type":"text","content":{"text":"deploy_kubectl_scale notify: scale ${k8s_cluster} ${namespace}/${deployment} replicas=${replicas}, succeed!"}}' https://open.feishu.cn/open-apis/bot/v2/hook/6c**c8
             """
         }
         failure {
             echo "scale , failed!"
             sh """
             curl -X POST -H "Content-Type: application/json" -d '{"msg_type":"text","content":{"text":"deploy_kubectl_scale notify: scale ${k8s_cluster} ${namespace}/${deployment} replicas=${replicas}, failed!"}}' https://open.feishu.cn/open-apis/bot/v2/hook/6c**c8
             """
            }
        }
    }
    

2.2 测试结果

输入界面:
在这里插入图片描述
执行结果:
在这里插入图片描述

3 注意事项

  1. 当前使用jenkins无法对用户进行细微的权限设置,如果想对用户做进一步的权限控制,例如A用户只能操作ns1的服务,那么可以考虑在jenkins的执行流程中调用专用的操作 api,通过 api调用kubectl,然后在api层面对应用户进行权限检查即可。
  2. 参数联动时候,参数名称首字母需要是大写的,否则在grovy脚本中无法识别,导致参数联动时效。

4 说明

软件环境:
jenkins 版本:2.299
参考文档:
jenkins 官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值