jenkins 使用 docker 作为动态代理节点

环境: centos7, docker 19.03.5, vm 192.168.116.6,Jenkins 2.249.3
jenkins 需要安装 docker 插件
1、配置 docker hosts
vm 192.168.116.6 设置 docker hosts

[root@k8s-noed02 ~]# cat /etc/docker/daemon.json 
{
  "bip": "192.167.1.1/24",
  "hosts": [
    "unix:///var/run/docker.sock",
    "tcp://0.0.0.0:2377"
  ]
}

重启 docker 报错

unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: hosts (from flag: [fd://], from file: [unix:///var/run/docker.sock tcp://0.0.0.0:2377])

需要修改 docker service 文件,一般在 /usr/lib/systemd/system/docker.service 或者 /etc/systemd/system/docker.service
查看 docker service 文件位置

[root@k8s-noed02 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Thu 2020-12-17 17:13:16 CST; 17h ago
     Docs: https://docs.docker.com
  Process: 22245 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=1/FAILURE)
 Main PID: 22245 (code=exited, status=1/FAILURE)

Dec 17 17:13:13 k8s-noed02 systemd[1]: Failed to start Docker Application Container Engine.
Dec 17 17:13:13 k8s-noed02 systemd[1]: Unit docker.service entered failed state.
Dec 17 17:13:13 k8s-noed02 systemd[1]: docker.service failed.
Dec 17 17:13:16 k8s-noed02 systemd[1]: docker.service holdoff time over, scheduling restart.
Dec 17 17:13:16 k8s-noed02 systemd[1]: Stopped Docker Application Container Engine.
Dec 17 17:13:16 k8s-noed02 systemd[1]: start request repeated too quickly for docker.service
Dec 17 17:13:16 k8s-noed02 systemd[1]: Failed to start Docker Application Container Engine.
Dec 17 17:13:16 k8s-noed02 systemd[1]: Unit docker.service entered failed state.
Dec 17 17:13:16 k8s-noed02 systemd[1]: docker.service failed.

删除flag -H 参考

[root@k8s-noed02 ~]# cat /usr/lib/systemd/system/docker.service
...
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd
...

重新加载 service 配置

systemctl daemon-reload

重启 docker

systemctl restart docker

现在通起其它主机,可以远程调用 vm 192.168.116.6 上的 docker。这里存在安全问题,需要自行设置可以访问192.168.116.6:2377的服务器,不然就相当于自行转让 root 权限了(慎重)。 可以参考官网添加 ssh 公钥认证
To use SSH connection, you need to set up ssh so that it can reach the remote host with public key authentication. Password authentication is not supported. If your key is protected with passphrase, you need to set up ssh-agent.

[root@map ~]# docker --host 192.168.116.6:2377 ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3、“系统管理”–“系统配置”–“cloud"–“docker”
注意这里的 Docker Image 选择“cnych/jenkins:jnlp“,也可以自己定制,参考文章
ps:使用官方的 jenkins/jnlp-slave 会有问题,会报下面这些错误。
自定义 jenkins jnlp-slave 参考文章 https://blog.csdn.net/Man_In_The_Night/article/details/111588717

Still waiting to schedule task
All nodes of label ‘jenkins-slave’ are offline
Cannot construct instance of `com.github.dockerjava.api.model.Volume` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('/home/jenkins/.jenkins')

在这里插入图片描述
在这里插入图片描述

4、构建流水线。
我这里已经构建好了 bbb,不再构建了
在这里插入图片描述在这里插入图片描述pipeline script node 选择 刚刚创建的 docker 的 label “jenkins-jnlp2”
pipeline script 内容如下:

node('jenkins-jnlp2') {
    stage('Clone') {
      echo "1.Clone Stage"
      sh "echo 'hello' >> /tmp/hello.txt"
    }
    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、立即构建,查看构建日志
在这里插入图片描述在这里插入图片描述6、这时我们在 vm 192.168.116.6 服务器上可以看到多了一个 jenkins-slave docker 容器

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值