需要安裝的插件
Kubernetes Cli Plugin:該插件可直接在Jenkins中使用kubernetes命令行進行操作。
用此插件可以使用kubectl命令來管理k8s,首先需要在jenkins憑據中添加一個類型為"secret file" 類型的憑據,上傳k8s的config文件
設置完后可以用以下類似腳本來管理k8s
// Example when used in a pipeline node { stage('Apply Kubernetes files') { withKubeConfig([credentialsId: 'user1', serverUrl: 'https://api.k8s.my-company.com']) { sh 'kubectl apply -f my-kubernetes-directory' } } }
前提條件:
需要提前安裝kuberctl工具,不然會報錯找不到執行插件,這里我直接將宿主機的kubectl掛載到容器中,直接使用即可
Kubernetes plugin: 使用kubernetes則需要安裝該插件
Kubernetes Continuous Deploy Plugin:kubernetes部署插件,可根據需要使用
deploy 插件部署時需要在jenkins中添加憑據,憑據類型選擇“Kubernetes configuration(kubeconfig)”,kubeconfig將k8s集群config文件內容貼到content中即可
添加完成后,pipeline中deploy時配置kubeconfigID即可
eg://調用Kubernetes Continuous Deploy Plugin 插件 stage('Deploy ACK') { kubernetesDeploy configs: 'doc/yaml/sys-user.yaml', kubeconfigId: "ack" }
kubernetes Credentials Plugin:在jenkins中增加k8s cloud
pipeline部署腳本:
def git_auth = "0427bb2b-2ae3-4e1f-a48e-xxxx" //git的url地址 def git_url = "git@ssh.dev.azure.com:v3xxxxm" //發布分支 def branch_pipeline="${branch}" //鏡像的版本號 def tag = "$BUILD_NUMBER" //發布模塊 def deploy_module="sys-user" //yaml文件文件文件 def yaml_path="doc/yaml/sys-user.yaml" node { // 從gitlab拉取代碼 stage('CheckOutCode') { checkout([$class: 'GitSCM', branches: [[name: "*/${branch_pipeline}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]]) } //配置已安裝的maven插件及環境變量 def mvnHome = tool 'maven' env.PATH = "${mvnHome}/bin:${env.PATH}" stage('Maven Build') { //單獨構建xxx模塊,同時會構建xxx模塊依賴的其他模塊 sh "mvn clean install -Dmaven.test.skip=true -pl ${deploy_module} -am" } stage('Image Build&Push') { sh """ docker build -t registry-vpc.cn-beijing.aliyuncs.com/xxxx/sys-user:$BUILD_NUMBER sys-user/ docker push registry-vpc.cn-beijing.aliyuncs.com/xxxx/sys-user:$BUILD_NUMBER """ } //調用Kubernetes Continuous Deploy Plugin 插件 stage('Deploy ACK') { kubernetesDeploy configs: '${yaml_path}', kubeconfigId: "ack" } }