Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级

jenkins,pipeline,流水线,使用,代理,节点,remote,ssh,k8s,进行,升级 · 浏览次数 : 366

小编点评

```yaml pipeline: stage: Remote SSH agent: any stages: - Remote SSH pipeline: agent: any stages: - Remote SSH steps: - script: def remote: [:] remote.name: 'Test' remote.host: '172.16.3.181' remote.allowAnyHosts: true withCredentials([usernamePassword(credentialsId: 'K8SMaster', passwordVariable: 'password', usernameVariable: 'username')]) { remote.user: "${username}" remote.password: "${password}" } sshCommand remote: remote, command: "kubectl version" - script: def remote: [:] remote.name: 'Test' remote.host: '172.16.3.181' remote.allowAnyHosts: true withCredentials([usernamePassword(credentialsId: 'K8SMaster', passwordVariable: 'password', usernameVariable: 'username')]) { remote.user: "${username}" remote.password: "${password}" } sshCommand remote: remote, command: "kubectl set image deployment/javademo1 vipsoft=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:4.0" - script: sh: Pipeline Scriptpipeline ``` **说明:** * `pipeline` 和 `pipeline` 是可选的名称,可以根据需要更改。 * `stage` 是可选的名称,可以根据需要更改。 * `steps` 是包含多个脚本的步骤。 * `script` 是包含命令的脚本。 * `agent` 是可选的名称,可以根据需要更改。 * `remote` 是一个包含所有远程节点信息的对象。 * `withCredentials` 用于获取远程节点的用户名和密码。 * `sshCommand` 用于执行远程 SSH 命令。

正文

Jenkins Pipeline 流水线 - K8S kubectl 升级

  • 使用代理节点
  • Remote SSH 远程执行命令进行升级

Remote SSH 方式

安装插件

  • SSH Pipeline Steps

Pipeline SSH 脚本

image
credentialsId: 'K8SMaster'

pipeline {
    agent any

    stages {
        stage('Remote SSH') {
            steps {
                script {                 
                    def remote = [:]
                    remote.name = 'Test'
                    remote.host = '172.16.3.181'
                    remote.allowAnyHosts = true
                    withCredentials([usernamePassword(credentialsId: 'K8SMaster', passwordVariable: 'password', usernameVariable: 'username')]) {
                        remote.user = "${username}"
                        remote.password = "${password}"
                    }
                    sshCommand remote: remote, command: "kubectl version" 
                }
            }
        }
    }
}

image

Pipeline Remote SSH K8S 升级

pipeline {
    agent any

    stages {
        stage('Remote SSH') {
            steps {
                script {                 
                    def remote = [:]
                    remote.name = 'Test'
                    remote.host = '172.16.3.181'
                    remote.allowAnyHosts = true
                    withCredentials([usernamePassword(credentialsId: 'K8SMaster', passwordVariable: 'password', usernameVariable: 'username')]) {
                        remote.user = "${username}"
                        remote.password = "${password}"
                    }
                    sshCommand remote: remote, command: "kubectl set image deployment/javademo1 vipsoft=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:4.0" 
                }
            }
        }
    }
}

image
image

代理方式

新增K8S节点

image
image
image
image
注意JDK

[root@k8smaster k8s]# tar -zxvf /opt/k8s/jdk-11.0.17_linux-x64_bin.tar.gz -C /opt/k8s 
[root@k8smaster k8s]# mv jdk-11.0.17 jdk
[root@k8smaster k8s]# ll
总用量 166600
drwxr-xr-x. 9 root root       126 11月  2 15:54 jdk
-rw-r--r--. 1 root root 169251172 11月  1 16:39 jdk-11.0.17_linux-x64_bin.tar.gz
-rw-r--r--. 1 root root   1339992 11月  2 15:50 remoting.jar
[root@k8smaster k8s]#

image

Pipeline Script

pipeline {
    agent none
 
    stages { 
        stage('代理节点运行') {
            agent {	label 'K8SAgent' }  //这边使用节点名称或节点标签,都可以运行
            steps {
                sh 'kubectl set image deployment/javademo1 vipsoft=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:4.0' 
                echo '构建完成'
            }
        }
    }
}

image
image

与Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级相似的内容: