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"
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值