Devops关键工具及技术(一)—Jenkins 容器化

在进行Devops思想与方法论落地的过程中,Jenkins这个开源的软件基本上会成为我们的首选,因为它的成熟度以及插件的丰富程度都无法让我们拒绝它。而随着Jenkins 2.0的发布,Pipeline As Code的理念,无疑给Jenkins使用者在Devops落地过程中更加得心应手。

Devops盛行之下,Docker无疑慢慢成为我们应用部署方式的首选,Docker在效率、隔离型、移植性、复用性相对传统的虚拟机部署优势都很明显。

Devops工具链的容器化在Devops整体体系的更快更简单地落地都起着至关重要的作用。下面就介绍Jenkins的容器化,包括Jenkins Master容器化、Jenkins Slave 容器化以及使用Jenkins的Docker Cloud 插件进行Slave的云部署。以及对于Jenkins容器化后内置构建环境安装以及软件环境,包括maven、ruby、python、golang、ruby、php、dotnet、git、docker、nodejs、ant等等。

以下所有的文件代码都可以在Github找到:
https://github.com/zbbkeepgoing/containerization

1、Jenkins Master

Jenkins的Master镜像构建。首先如果我们需要用我们的Jenkins来作为Devops持续构建和持续部署的工具,那对应的构建不同语言应用的环境那也是必须要构建到我们Master的镜像中。下面就是Jenkins Master的Dockerfile部分文件内容(由于文件内容过长,只黏贴一部分,所有内容可以查看上面笔者的Github地址)。

...
...
# Install NodeJs 10.x , if you don't need, you can remove it,you also can change version 
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get  update && apt-get install -y \
    nodejs

# Install Python3.7 , if you don't need, you can remove it ,you also can change version
RUN wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
RUN tar xvf Python-3.7.0.tgz
RUN rm -rf  Python-3.7.0.tgz
RUN cd Python-3.7.0 \
    ./configure --enable-optimizations \
    make -j8 \
    make altinstall

# Install Golang1.10.3 , if you don't need, you can remove it,you also can change version
ENV GOLANG_VERSION 1.10.3

RUN set -eux; \
        \
# this "case" statement is generated via "update.sh"
        dpkgArch="$(dpkg --print-architecture)"; \
        case "${dpkgArch##*-}" in \
                amd64) goRelArch='linux-amd64'; goRelSha256='fa1b0e45d3b647c252f51f5e1204aba049cde4af177ef9f2181f43004f901035' ;; \
                armhf) goRelArch='linux-armv6l'; goRelSha256='d3df3fa3d153e81041af24f31a82f86a21cb7b92c1b5552fb621bad0320f06b6' ;; \
                arm64) goRelArch='linux-arm64'; goRelSha256='355128a05b456c9e68792143801ad18e0431510a53857f640f7b30ba92624ed2' ;; \
                i386) goRelArch='linux-386'; goRelSha256='3d5fe1932c904a01acb13dae07a5835bffafef38bef9e5a05450c52948ebdeb4' ;; \
                ppc64el) goRelArch='linux-ppc64le'; goRelSha256='f3640b2f0990a9617c937775f669ee18f10a82e424e5f87a8ce794a6407b8347' ;; \
                s390x) goRelArch='linux-s390x'; goRelSha256='34385f64651f82fbc11dc43bdc410c2abda237bdef87f3a430d35a508ec3ce0d' ;; \
                *) goRelArch='src'; goRelSha256='567b1cc66c9704d1c019c50bef946272e911ec6baf244310f87f4e678be155f2'; \
                        echo >&2; echo >&2 "warning: current architecture ($dpkgArch) does not have a corresponding Go binary release; will be building from source"; echo >&2 ;; \
        esac; \
        \
        url="https://golang.org/dl/go${GOLANG_VERSION}.${goRelArch}.tar.gz"; \
        wget -O go.tgz "$url"; \
        echo "${goRelSha256} *go.tgz" | sha256sum -c -; \
        tar -C /usr/local -xzf go.tgz; \
        rm go.tgz; \
        \
        if [ "$goRelArch" = 'src' ]; then \
                echo >&2; \
                echo >&2 'error: UNIMPLEMENTED'; \
                echo >&2 'TODO install golang-any from jessie-backports for GOROOT_BOOTSTRAP (and uninstall after build)'; \
                echo >&2; \
                exit 1; \
        fi; \
        \
        export PATH="/usr/local/go/bin:$PATH"; \
        go version

ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
WORKDIR $GOPATH


# Install Dotnet-SDK 2.1.302, if you don't need, you can remove it,you also can change version

RUN wget https://download.microsoft.com/download/4/0/9/40920432-3302-47a8-b13c-bbc4848ad114/dotnet-sdk-2.1.302-linux-x64.tar.gz
RUN mkdir -p /home/dotnet && tar zxf dotnet-sdk-2.1.302-linux-x64.tar.gz -C /home/dotnet
ENV DOTNET_ROOT $PATH:/home/dotnet
ENV PATH $PATH:/home/dotnet
RUN rm -rf dotnet-sdk-2.1.302-linux-x64.tar.gz




# Install dockerce for build docker images  in docker for jenkins, if you don't need, you can remove it,you also can change version
RUN apt-get update && \
    apt-get -y install apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
   add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable" && \
  apt-get update && \
  apt-get -y install docker-ce

RUN apt-get clean && apt-get autoclean

# Install Jenkins master
ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000
ARG http_port=8080
ARG agent_port=50000
ARG JENKINS_HOME=/var/jenkins_home

ARG bbdocker=12345


ENV JENKINS_HOME $JENKINS_HOME
ENV JENKINS_SLAVE_AGENT_PORT ${agent_port}

# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN mkdir -p $JENKINS_HOME \
  && chown ${uid}:${gid} $JENKINS_HOME \
  && groupadd -g ${gid} ${group} \
  && useradd -d "$JENKINS_HOME" -u ${uid} -g ${gid} -m -s /bin/bash ${user}


RUN groupadd -g ${bbdocker} bbdocker  && gpasswd -a ${user} bbdocker


# Jenkins home directory is a volume, so configuration and build history
# can be persisted and survive image upgrades
VOLUME $JENKINS_HOME

# `/usr/share/jenkins/ref/` contains all reference configuration we want
# to set on a fresh new installation. Use it to bundle additional plugins
# or config file with your custom jenkins Docker image.
RUN mkdir -p /usr/share/jenkins/ref/init.groovy.d
...
...

Dockerfile 中其他需要包含应用的文件可以直接在github中找到,如果有特殊的一些运行环境以及构建环境直接在Dockerfile里面新增即可。

之后可按照以下步骤进行Jenkins Master的构建与部署。

  • 镜像构建
docker build -t jenkins:master .
  • 运行配置化文件
./jenkins_dockerconfig.sh
  • 启动Jenkins的Master
./jenkins_master_run.sh
  • 访问vm ip+10000。开始配置Jenkins Master

    密码我们可以通过三种方式获取
1、访问Jenkins Master宿主机上的目录文件
/opt/jenkins_home/secrets/initialAdminPassword
2、直接查看容器日志
docker logs -f dockerid
3、到容器内部查看。推荐以上两种
  • 等待自动Jenkins插件安装
    这里写图片描述

  • 创建一个管理员
    这里写图片描述

  • 创建Master Jenkins中软件版本的pipeline并运行

pipeline code可在github中查看
这里写图片描述

2、Jenkins Slave

Jenkins的Slave镜像构建。Master镜像对应构建不同语言应用的环境那也是必须要构建到我们Slave的镜像中,因为一旦构建进程增多以后,构建的将会由各个Slave Jenkins去承担,所有Slave节点必须要有构建不同语言的能力。下面就是Jenkins Slave的Dockerfile部分文件内容(由于文件内容过长,只黏贴一部分,所有内容可以查看上面笔者的Github地址)。

...
...
# Install NodeJs 10.x , if you don't need, you can remove it,you also can change version
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install nodejs

# Install Python3.7 , if you don't need, you can remove it ,you also can change version
RUN wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
RUN tar xvf Python-3.7.0.tgz
RUN rm -rf  Python-3.7.0.tgz
RUN cd Python-3.7.0 \
    ./configure --enable-optimizations \
    make -j8 \
    make altinstall

# Install Golang1.10.3 , if you don't need, you can remove it,you also can change version
ENV GOLANG_VERSION 1.10.3

RUN set -eux; \
        \
# this "case" statement is generated via "update.sh"
        dpkgArch="$(dpkg --print-architecture)"; \
        case "${dpkgArch##*-}" in \
                amd64) goRelArch='linux-amd64'; goRelSha256='fa1b0e45d3b647c252f51f5e1204aba049cde4af177ef9f2181f43004f901035' ;; \
                armhf) goRelArch='linux-armv6l'; goRelSha256='d3df3fa3d153e81041af24f31a82f86a21cb7b92c1b5552fb621bad0320f06b6' ;; \
                arm64) goRelArch='linux-arm64'; goRelSha256='355128a05b456c9e68792143801ad18e0431510a53857f640f7b30ba92624ed2' ;; \
                i386) goRelArch='linux-386'; goRelSha256='3d5fe1932c904a01acb13dae07a5835bffafef38bef9e5a05450c52948ebdeb4' ;; \
                ppc64el) goRelArch='linux-ppc64le'; goRelSha256='f3640b2f0990a9617c937775f669ee18f10a82e424e5f87a8ce794a6407b8347' ;; \
                s390x) goRelArch='linux-s390x'; goRelSha256='34385f64651f82fbc11dc43bdc410c2abda237bdef87f3a430d35a508ec3ce0d' ;; \
                *) goRelArch='src'; goRelSha256='567b1cc66c9704d1c019c50bef946272e911ec6baf244310f87f4e678be155f2'; \
                        echo >&2; echo >&2 "warning: current architecture ($dpkgArch) does not have a corresponding Go binary release; will be building from source"; echo >&2 ;; \
        esac; \
        \
        url="https://golang.org/dl/go${GOLANG_VERSION}.${goRelArch}.tar.gz"; \
        wget -O go.tgz "$url"; \
        echo "${goRelSha256} *go.tgz" | sha256sum -c -; \
        tar -C /usr/local -xzf go.tgz; \
        rm go.tgz; \
        \
        if [ "$goRelArch" = 'src' ]; then \
                echo >&2; \
                echo >&2 'error: UNIMPLEMENTED'; \
                echo >&2 'TODO install golang-any from jessie-backports for GOROOT_BOOTSTRAP (and uninstall after build)'; \
                echo >&2; \
                exit 1; \
        fi; \
        \
        export PATH="/usr/local/go/bin:$PATH"; \
        go version

ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
WORKDIR $GOPATH


# Install Dotnet-SDK 2.1.302, if you don't need, you can remove it,you also can change version
RUN wget https://download.microsoft.com/download/4/0/9/40920432-3302-47a8-b13c-bbc4848ad114/dotnet-sdk-2.1.302-linux-x64.tar.gz
RUN mkdir -p /home/dotnet && tar zxf dotnet-sdk-2.1.302-linux-x64.tar.gz -C /home/dotnet
ENV DOTNET_ROOT $PATH:/home/dotnet
ENV PATH $PATH:/home/dotnet
RUN rm -rf dotnet-sdk-2.1.302-linux-x64.tar.gz




# Install dockerce for build docker images  in docker for jenkins, if you don't need, you can remove it,you also can change version
RUN apt-get update && \
    apt-get -y install apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
   add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
   $(lsb_release -cs) \
   stable" && \
  apt-get update && \
  apt-get -y install docker-ce


RUN apt-get clean && apt-get autoclean


# Install Jenkins
ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000

ARG bbdocker=12345
USER root

ENV HOME /home/${user}
RUN groupadd -g ${gid} ${group}
RUN useradd -c "Jenkins user" -d $HOME -u ${uid} -g ${gid} -m ${user}
LABEL Description="This is a base image, which provides the Jenkins agent executable (slave.jar)" Vendor="Jenkins project" Version="3.20"

RUN groupadd -g ${bbdocker} bbdocker  && gpasswd -a ${user} bbdocker


ARG VERSION=3.20
ARG AGENT_WORKDIR=/home/${user}/agent

RUN curl --create-dirs -sSLo /usr/share/jenkins/slave.jar https://repo.jenkins-ci.org/public/org/jenkins-ci/main/remoting/${VERSION}/remoting-${VERSION}.jar \
  && chmod 755 /usr/share/jenkins \
  && chmod 644 /usr/share/jenkins/slave.jar

USER ${user}
ENV AGENT_WORKDIR=${AGENT_WORKDIR}
RUN mkdir /home/${user}/.jenkins && mkdir -p ${AGENT_WORKDIR}

VOLUME /home/${user}/.jenkins
VOLUME ${AGENT_WORKDIR}
WORKDIR /home/${user}
...
...

如果有特殊的一些运行环境以及构建环境直接在Dockerfile里面新增即可。

之后可按照以下步骤进行Jenkins Slave的构建与部署。

  • 镜像构建
docker build -t jenkins:slave .
  • 更新Slave节点Docker的配置

该配置根据不同的系统可能在/etc/init/docker.conf、/etc/sysconfig/docker、 ,/etc/default/docker 或 /etc/default/docker.io中。

这个配置修改由于Jenkins的Master在启动Slave进行构件任务的时候是需要开启Docker的TCP端口。参考官方:https://plugins.jenkins.io/docker-plugin

DOCKER_OPTS="-H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock"
  • 重启Docker
$ systemctl restart docker
  • 运行配置化文件
./jenkins_dockerconfig.sh
  • JenkinsMaster节点上配置Slave的信息

系统管理中的最下面,可以看到Cloud的配置即可以添加Docker,每一个配置为一个Slave的配置。
这里写图片描述
这里写图片描述

  • 在Master Jenkins中创建Slave Jenkins构建软件版本的pipeline并运行

pipeline code可在github中查看

  • 构建中可以看到Slave节点上启动Jenkins Slave进行构建的Docker进程

这里写图片描述

以上即是Jenkins Master和Slave容器化步骤。我们可以在一台Jenkins Master中配置0-n台Slave。而且这些Slave节点将会在需要执行构建任务时才会启动Jenkins进程进行任务执行,在任务执行完毕后将进程删除掉,实现了资源的合理利用。而且Slave配置可以进行一些自定义的配置,如Slave节点最高的容器启动数为多少。这样可以合理的利用每一台Slave节点。

相比于传统部署Jenkins和配置Slave的方案,容器化后的方案在Devops中运用,部署效率和运维效率上都会有很大程度的提示,而且也不依赖与任何基础环境,不管在Windows下还是Linux下,又或者公有云、私有云下都可以快速地搭建起来。
对于后续不论Jenkins版本升级还是构建环境版本升级只需要修改Dockerfile文件,重新进行Master和Slave镜像的打包即可,这样,所有事情都变得简单起来了。

本文为博主原创文章,未经博主允许不得转载。

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值