docker镜像仓库:https://hub.docker.com/r/jenkins/inbound-agent
github文档:https://github.com/jenkinsci/docker-agent/tree/master
Dockerfile文档:https://hub.docker.com/r/jenkins/inbound-agent/dockerfile
一、背景
这里考虑自己封装jenkins-agent,所以会使用官方的dockerfile,也同时会做一些调整(因为目前公司使用的jdk版本还是8,jenkins最新版本使用的是jdk17版本)
二、部署jenkins-master服务
2.1 jenkins配置文件
root@iZj6cizrl9q8cw0193em9oZ:/opt# mkdir jenkins
root@iZj6cizrl9q8cw0193em9oZ:/opt# groupadd -g 1000 jenkins
root@iZj6cizrl9q8cw0193em9oZ:/opt# useradd -u 1000 -g jenkins jenkins
root@iZj6cizrl9q8cw0193em9oZ:/opt# chown -R jenkins. jenkins
root@iZj6cizrl9q8cw0193em9oZ:/opt# vi docker-compose.yaml
version: '3.8'
services:
jenkins:
#image: jenkins:latest
image: jenkins/jenkins:2.460-jdk17
container_name: jenkins
restart: always
ports:
- 8080:8080
- 50000:50000
volumes:
- ./jenkins:/var/jenkins_home
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
environment:
- JAVA_OPTS=-Djava.awt.headless=true -Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Shanghai
networks:
- ops
networks:
ops:
root@iZj6cizrl9q8cw0193em9oZ:/opt# docker compose up -d
root@iZj6cizrl9q8cw0193em9oZ:/opt# docker compose ps
2.2 配置websocket代理
指定对应的端口号
2.2 创建节点
创建节点:jenkins-slave-1
# slave执行的java命令
curl -sO http://47.238.183.86:8080/jnlpJars/agent.jar
java -jar agent.jar -url http://47.238.183.86:8080/ -secret 0a759afbc7409576710b813ac546dae9e95a8f3147c75e2be9d7c9d9881be6f2 -name "jenkins-slave-1" -workDir "/home/jenkins"
二、基础Dockerfile
2.1 基础配置文件整理
root@iZj6cizrl9q8cw0193em9oZ:/data/slave# curl -sO http://47.238.183.86:8080/jnlpJars/agent.jar
root@iZj6cizrl9q8cw0193em9oZ:/data/slave# curl -sO https://raw.githubusercontent.com/jenkinsci/docker-agent/master/jenkins-agent
root@iZj6cizrl9q8cw0193em9oZ:/data/slave# tree ./
2.2 构建镜像
root@iZj6cizrl9q8cw0193em9oZ:/data/slave# vi Dockerfile
FROM dokken/centos-stream-8:main
ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000
RUN groupadd -g "{gid}" "{group}" \
&& useradd -l -c "Jenkins user" -d /home/"{user}" -u "{uid}" -g "{gid}" -m "{user}" || echo "user {user} already exists."
ARG AGENT_WORKDIR=/home/"{user}"/agent
ENV TIME_ZONE=Asia/Shanghai \
JAVA_HOME=/usr/lib/jvm/java-1.8.0 \
AGENT_WORKDIR={AGENT_WORKDIR}
VOLUME /home/"{user}"/.jenkins
VOLUME "{AGENT_WORKDIR}"
COPY jenkins-agent /usr/local/bin/jenkins-agent
COPY agent.jar /usr/share/jenkins/agent.jar
COPY conf/settings.xml /usr/share/maven/conf/settings.xml
USER root
ARG MAVEN_VERSION=3.9.7
ARG SHA=f64913f89756264f2686e241f3f4486eca5d0dfdbb97077b0efc389cad376053824d58caa35c39648453ca58639f85335f9be9c8f217bfdb0c2d5ff2a9428fac
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/{MAVEN_VERSION}/binaries
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& yum install -y curl \
&& curl -fsSL -o /tmp/apache-maven.tar.gz {BASE_URL}/apache-maven-{MAVEN_VERSION}-bin.tar.gz \
&& echo "{SHA} /tmp/apache-maven.tar.gz" | sha512sum -c - \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn \
&& yum install -y yum-utils epel-release wget bash-completion lrzsz git java-17-openjdk-devel openssl-devel \
rpm-build.x86_64 wget sudo vim gcc gcc-c++ gdb make automake autoconf libtool gettext-devel libcurl-devel unixODBC-devel \
&& yum install -y java-1.8.0-openjdk-devel \
&& yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo \
&& yum install -y docker-ce-20.10.22 \
&& curl -fsSL https://rpm.nodesource.com/setup_22.x -o nodesource_setup.sh|bash - \
&& yum install nodejs -y \
&& curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo \
&& yum install -y yarn \
&& npm install -g cnpm --registry=https://registry.npmmirror.com \
&& curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
&& unzip awscliv2.zip \
&& sudo ./aws/install \
&& rm -rf awscliv2* \
&& yum clean all \
&& echo " StrictHostKeyChecking no" >> /etc/ssh/ssh_config
RUN chmod 0644 /usr/share/jenkins/agent.jar \
&& ln -sf /usr/share/jenkins/agent.jar /usr/share/jenkins/slave.jar \
&& chmod +x /usr/local/bin/jenkins-agent \
&& ln -s /usr/local/bin/jenkins-agent /usr/local/bin/jenkins-slave
USER{user}
WORKDIR /home/"${user}"
ENTRYPOINT ["/usr/local/bin/jenkins-agent"]
root@iZj6cizrl9q8cw0193em9oZ:/data/slave# docker build -t xiangys0134/jenkins:slave-v1.0.1 .
2.3 compose启动服务
root@iZj6cizrl9q8cw0193em9oZ:/opt# vi docker-compose.yaml
version: '3.8'
services:
jenkins:
#image: jenkins:latest
image: jenkins/jenkins:2.460-jdk17
container_name: jenkins
restart: always
ports:
- 8080:8080
- 50000:50000
volumes:
- ./jenkins:/var/jenkins_home
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
environment:
- JAVA_OPTS=-Djava.awt.headless=true -Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Shanghai
networks:
- ops
jenkins-slave-1:
image: xiangys0134/jenkins:slave-v1.0.1
container_name: jenkins-slave-1
restart: always
privileged: true
volumes:
- '/var/run/docker.sock:/var/run/docker.sock:ro'
environment:
- JENKINS_JAVA_BIN=/usr/lib/jvm/java-17/bin/java
- JENKINS_URL=http://jenkins:8080/
- JENKINS_SECRET=0a759afbc7409576710b813ac546dae9e95a8f3147c75e2be9d7c9d9881be6f2
- JENKINS_AGENT_WORKDIR=/home/jenkins
- JENKINS_AGENT_NAME=jenkins-slave-1
networks:
- ops
networks:
ops:
#备注:标红为新增内容
root@iZj6cizrl9q8cw0193em9oZ:/opt# docker compose up -d jenkins-slave-1
root@iZj6cizrl9q8cw0193em9oZ:/opt# docker compose ps
三、查看节点
备注:节点已经上线了
四、总结
这里会面临以下几个问题:
-
jenkins版本为2.460,未来如果直接使用docker-compose的话,可能会面临jenkins版本的升级
-
如果想重新build容器的话那么需要修改的地方有:1、maven版本(官方仅保存最新的版本文件)2、agent.jar也需要跟着调整(大多时候应该兼容)
-
这里可以将xiangys0134/jenkins:slave-vx.x.x镜像作为基础镜像封装自己的功能。
留言