jenkins 添加 vm 节点

环境: centos7,vm ip 192.168.116.6,docker-ce 18.09.6,Jenkins 2.263.1
1、部署 jenkins

mkdir jenkins_home
docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts

如果出现日志报错:

touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

请修改 jenkins_home 目录权限,参考
artible2

chown -R 1000:1000 jenkins_home

2、登录 jenkins,127.0.0.1:8080(本文是在本地实现的,所以用 127.0.0.1)。按照提示输入登录密码,安装推荐插件就行。
3、添加 vm 到节点。“系统管理”–“节点管理”–“新建节点”,选择固定节点
连接报错

No entry currently exists in the Known Hosts file for this host. Connections will be denied until this new host and its associated key is added to the Known Hosts file. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.",

解决方法:Host Key Verification Strategy 选择 None verifying Verification Strategy。参考

在这里插入图片描述报错:

[12/17/20 00:39:39] [SSH] Checking java version of /home/jenkins/jdk/bin/java
Couldn't figure out the Java version of /home/jenkins/jdk/bin/java
bash: /home/jenkins/jdk/bin/java: No such file or directory

解决方法:在节点 vm ip 192.168.116.6 上安装 java,如果不使用 java jdk 的情况下,可以直接使用 openjdk

yum install -y java-1.8.0-openjdk

4、对 node2 节点进行测试
新建一个流水线 aaa(我的已经创建好了,这里显示已存在)
在这里插入图片描述pipeline scritpt

node('node2') {
    stage('Clone') {
      echo "1.Clone Stage"
    }
    stage('Test') {
      echo "2.Test Stage"
      echo "Skip it this time"
    }
    stage('Build') {
      echo "3.Build Docker Image Stage"
    }
    stage('Push') {
      echo "4.Push Docker Image Stage"
    }
    stage('Deploy') {
      echo "5. Deploy Stage"
      def userInput = input(
          id: 'userInput',
          message: 'Choose a deploy environment',
          parameters: [
              [
                  $class: 'ChoiceParameterDefinition',
                  choices: "Dev\nQA\nProd",
                  name: 'Env'
              ]
          ]
      )
      echo "=========heheda==========="
      echo "This is a deploy step to ${userInput}"
      if (userInput == "Dev") {
          // deploy dev stuff
      } else if (userInput == "QA"){
          // deploy qa stuff
      } else {
          // deploy prod stuff
      }
      echo "pipeline script end"
    }
}

在这里插入图片描述立即构建
在这里插入图片描述查看结果日志
在这里插入图片描述
在这里插入图片描述
5、补充。一个完整的部署 java 项目到 k8s 可以是这样的 pipeline script,参考

node('haimaxy-jnlp') {
    stage('Clone') {
      echo "1.Clone Stage"
      git url: "
http://192.168.1.13/joker/jenkins-demo.git"
;
      script {
        build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
      }
      echo "$build_tag"
      echo "$env.BRANCH_NAME"
    }
    stage('Test') {
      echo "2.Test Stage"
      echo "Skip it this time"
    }
    stage('Build') {
      echo "3.Build Docker Image Stage"
      sh "docker build -t cnych/jenkins-demo:${build_tag} ."
    }
    stage('Push') {
      echo "4.Push Docker Image Stage"
      withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
        sh "docker tag cnych/jenkins-demo:${build_tag} 192.168.1.200:32023/cnych/jenkins-demo:${build_tag}"
        sh "docker login 
http://192.168.1.200:32023
 -u ${harborUser} -p ${harborPassword}"
        sh "docker push 192.168.1.200:32023/cnych/jenkins-demo:${build_tag}"
      }
    }
    stage('Deploy') {
      echo "5. Deploy Stage"
      def userInput = input(
          id: 'userInput',
          message: 'Choose a deploy environment',
          parameters: [
              [
                  $class: 'ChoiceParameterDefinition',
                  choices: "Dev\nQA\nProd",
                  name: 'Env'
              ]
          ]
      )
      echo "=========heheda==========="
      echo "This is a deploy step to ${userInput}"
      sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s.yaml"
      if (userInput == "Dev") {
          // deploy dev stuff
      } else if (userInput == "QA"){
          // deploy qa stuff
      } else {
          // deploy prod stuff
      }
      sh "kubectl apply -f k8s.yaml"
    }
}
### Jenkins 与 Azure 的集成方法及配置教程 #### 1. 使用 Azure Slave Plugin 动态扩展 Agent 节点 Azure Slave Plugin 是 Jenkins 提供的一种插件,允许在 Azure 公有云上动态创建和销毁 Agent 节点。这种机制非常适合处理不可预测的工作负载变化,从而提高资源利用率并降低成本。 ##### 插件功能概述 该插件通过调用 Azure Management API 实现虚拟机的自动化管理和资源配置[^1]。它可以按需创建 Windows 或 Linux 类型的 Agent 节点,并在其空闲时自动删除这些实例以释放计算资源。 ##### 配置步骤 - **安装插件**: 在 Jenkins 管理界面中导航至“Manage Plugins”,搜索并安装“Azure Slave Plugin”。 - **连接认证**: 创建服务主体 (Service Principal),并将相应的凭据导入到 Jenkins 凭证管理系统中。 - **定义模板**: 设置新的 Cloud Configuration,在其中指定所需的 VM 大小、操作系统映像以及其他必要属性。 - **启用动态伸缩**: 根据实际需求调整最小/最大并发数及其他高级选项。 #### 2. 配合 SonarQube 实现代码质量监控 为了进一步增强开发效率,通常会将静态代码分析工具如 SonarQube 整合进来形成闭环反馈链路。两者之间可通过 Webhook 方式建立联系,确保每次扫描完成后及时通知下游流水线继续推进。 ##### 关键设置项 - 添加一个新的 Job Parameter 名称为 `SONAR_HOST_URL`,指向目标 SonarQube Server 地址。 - 访问 Jenkins URL 结尾附加路径 `/sonarqube-webhook/` 来触发回调事件监听器[^2]。 #### 3. 应用 RESTful 接口驱动复杂业务逻辑 除了图形化 UI 操作外,还可以借助标准 HTTP 请求格式化的 JSON 数据包直接操控远程服务器上的各项活动。这种方法尤其适用于脚本批量作业或者跨平台协作场景下的灵活调度需求。 ##### 示例演示 假设我们需要利用 PowerShell 编写一段简单的命令行程序去查询某个特定项目的最新状态,则可能涉及以下片段: ```powershell $response = Invoke-RestMethod ` -Uri 'https://your-jenkins-instance/api/json' ` -Headers @{"Authorization"="Basic $(ConvertTo-SecureString $cred)"} Write-Output ($response.jobs | Where {$_.name -eq "MyProject"}).color ``` 此处 `$cred` 表示预先编码好的 Base64 字符串形式的身份令牌[^3]。 #### 4. Maven 测试框架定制化改造建议 针对某些特殊应用场景而言,默认行为未必完全契合预期效果。因此有必要深入研究相关文档资料,合理修改默认规则集达到最佳实践水平。 例如下面这段 XML 片段展示了如何精确筛选待测单元集合的同时排除干扰项的影响[^5]: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- Include all classes ending with Test --> <includes><include>**/*Test.java</include></includes> <!-- Exclude any class starting with Test --> <excludes><exclude>**/Test*.java</exclude></excludes> </configuration> </plugin> </plugins> </build> ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值