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 进行升级相似的内容:

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

Jenkins Pipeline 流水线 - K8S kubectl 升级 使用代理节点 Remote SSH 远程执行命令进行升级 Remote SSH 方式 安装插件 SSH Pipeline Steps Pipeline SSH 脚本 credentialsId: 'K8SMaster' pi

Jenkins Pipeline 流水线 - 添加节点 使用代理

Jenkins 安装在 Windows 上 Docker 在Linux 上 流程 将 Docker 在 Jenkins 节点中维护 Pipeline 中指定某些阶段使用哪个节点 添加节点 Checking Java version in the PATH openjdk version "1.8.0

Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本

Docker Jenkins 安装配置 Windows 2016 安装 Jenkins 前置条件可参考 Jenkins Pipeline 流水线 - 拉代码(SVN) + Maven 编译打包 Jenkins Pipeline 流水线 - 添加节点 使用代理 Jenkins Pipeline 流水线

Jenkins Pipeline 流水线 - withCredentials 使用

添加凭证 Pipeline script pipeline { agent any stages { stage('withCredentials 使用凭证') { steps { withCredentials([usernamePassword(credentialsId: 'DockerSer

Jenkins Pipeline 流水线 - 声明式 Demo

Jenkins Pipeline 流水线 流水线既能作为任务的本身,也能作为 Jenkinsfile 使用流水线可以让我们的任务从UI手动操作,转换为代码化,像dockerfile 一样。从shell 命令到配置文件,更适合大型项目,可以让团队其他开发者同时参与进来, 完整语法 tools:添加工具

Jenkins Pipeline 流水线 - 拉代码(SVN) + Maven 编译打包

Jenkins Pipeline 流水线 步骤 拉取SVN代码 -> Maven 构建 -> Docker 编译 -> 发布至阿里云仓库 -> K8S 更新 Jenkins插件安装 Localization: Chinese (Simplified) Subversion 【2.16.0】 --从S

Jenkins Pipeline 流水线 - 上传文件 Publish over SSH 执行命令

Jenkins Pipeline 流水线 - 上传文件 Publish over SSH 执行命令 Jenkins插件安装 Publish over SSH 系统配置 生成代码 sshPublisher: Send build artifacts over SSH 需要在 生成 sshPublish

Jenkins Pipeline 流水线 - 上传文件 Publish over SSH + Docker 编译 + 上传到阿里仓库

Jenkins Pipeline 流水线 Publish over SSH 将jar上传到远程 Docker 服务器、执行编译命令生成镜像、将镜像上传到阿里仓库 Publish over SSH Jar包上传、编译、上传 生成上传 jar 包脚本配置 生成上传 Dockerfile 脚本配置 注意在

Jenkins Pipeline 流水线 - Parameters 参数化构建

可以通过参数的方式,指定构建的版本 有两种方式 界面添加 Pipeline Script 脚本配置 (需要Build 一次,然后生效,不知道有没有其它办法) General 界面添加 Pipeline Script Declarative Directive Generator pipeline {

Jenkins 多分支流水线(SVN)

实际应用过程中,一般多分支流水线的方式用得比较多一些, master 对应 生成环境 develop 对应 测试环境, 将不同分支的代码构建到不同的环境中 添加 Jenkinsfile 文件 Jenkinsfile 内容见:Jenkins Pipeline 流水线 - 完整构建 Pipeline S