基于Docker+Jenkins+Git的发布环境

流程概括

1、通过Docker管理Jenkins资源

2、通过Jenkins创建Docker镜像

整体流程:

 

构建与发布:

Jenkins执行编译>Docker中运行服务>操作可跟踪

环境要求:

一台提供Git服务的服务器,也可以直接使用Github

一台运行Jenkins服务的服务器(Tomcat)

一台或若干Docker宿主机服务器

Jenkins软件包及组件的安装

总体实验环境

服务 IP 说明
Git 172.16.1.8  
Docker 172.19.10.201  
Jenkins 172.19.10.232 https://www.jenkins.io/  LTS2.32.3>2.73.3

创建tomcat的用户,并使用tomcat用户来进行操作;而对于docker则是使用root用户进行操作

apache-maven-3.3.3

jdk1.7.0_60

tomcat-7.0.34

将jenkins.war下载到 -P tomcat-7.0.34/webapps/

他这个目录下还有docs,examples,host-manager,manager,ROOT等目录

启动tocat-7.0.34/bin/catalina.sh start

查看日志输出tail -100f tomcat-7.0.34/logs/catalina.out  在日志中会生成一个admin的密码,同时,密码也保存到/home/tomcat/.jenkins/secrets/initialAdminPassword文件中

在浏览器中打开页面:172.19.10.232:8080/jenkins  进入后也会告诉密码的存放位置

选择要安装的插件:Git plugin/Git Parameter Plug-in/GitLab Plugin/Publish Over SSH/SSH plugin

创建管理员帐户

搭建Docker宿主机

修改主机名:hostnamectl set-hostname docker01

国内:get.daocloud.io

创建用户:useradd docker -d /home/docker -s /bin/bash

设置密码:echo "docker:docker" | chpasswd

创建目录:mkdir /data/dockerfiles/scripts -p

Docker脚本:vi /data/dockerfiles/scripts/deplay.sh

chmod +x /data/dockerfiles/scripts/deplay.sh

chown docker:docker /data/dockerfiles -R

用于在Jenkins中调用,通过脚本完成对工程的部署

service docker start

docker search tomcat  搜索镜像

docker pull tomcat

使用docker加速器,需要重启一下docker服务

Jenkins全局配置

系统管理>Global Tool Configuration

来自于more apache-maven-3.3.3/conf/settings.xml  记录本地仓库的路径/用户/密码等的配置信息

默认设置:

Maven Configuration:Setting file in filesystem

File Path:/home/tomcat/apache-maven-3.3.3/conf/setting.xml

全局设置:

Maven Configuration:Global setting file in filesystem

File Path:/home/tomcat/apache-maven-3.3.3/conf/setting.xml

JDK

别名:jdk1.7

JAVA_HOME:/home/tomcat/jdk1.7.0_60

Git

Name:Default

Path to Git executable:git

Maven

Name:maven

MAVEN_HOME:/home/tomcat/apache-maven-3.3.3

保存

系统管理>系统设置

Publish over SSH

SSH Servers:

Name:172.19.10.201_docker01

Hostname:172.19.10.201

Username:docker

Remote Directory:/data/dockerfiles

高级Password:docker

测试连接:Test Configuration

系统管理>管理插件

可选插件>Maven Integration plugin>安装

创建和配置发布任务

创建一个新的任务定义一个项目名称:Enter an item name>构建一个maven项目>OK

General:勾选丢弃旧的构建,保持构建的最大数设置为10个;勾选参数化构建过程,设置Git Parameter,Name:release_branck,Parameter Type:Branch;String Parameter:Name:project_name,默认值:first_docker_proj01

源码管理:选择Git,Repository URL:http://172.16.1.8/libingyang/hellp_docker.git,然后添加鉴权:用户名/密码方式,Branches to build:$release_branch

Build:Goals and options:clean package -DskipTests=true

构建后操作:Send build artifacts  over SSH,

Transfers:Source files:target/*.war,Remove prefix:target/  拿掉前缀,Remote directory:/war/$project_name,Exec command:/data/dockerfiles/scripts/deploy.sh $project_name hello_docker $release_branch 11000 8080 >保存

Build with Parameters:选择好参数后,开始构建

Jenkins实现工程的集群发布

配置刚创建的发布任务

Send build artifacts over SSH添加多台主机

附:脚本文件

#!/bin/bash
############################################
# this script function is :                
# deploy new docker container             
#                                          
# USER        YYYY-MM-DD - ACTION          
# junsansi    2016-01-25 - CREATED        
#                                          
############################################
 
parasnum=5
# function
help_msg()
{
cat << help
+----------------------------------------------------+
+ Error Cause:
+ you enter $# parameters
+ the total paramenter number must be $parasnum
+ 1st :DOCKER_NAME
+ 2nd :PROJECT_NAME
+ 3rd :PROJECT_VERSION
+ 4th :SOURCE_PORT
+ 5th :DESTINATION_PORT
+----------------------------------------------------+
help
}
 
# ----------------------------------------------------
# Check parameter number
# ----------------------------------------------------
if [ $# -ne ${parasnum} ]
then
        help_msg 
        exit
fi
 
# ----------------------------------------------------
# Initialize the parameter.
# ----------------------------------------------------
DOCKER_NAME=$1
PROJECT_NAME=$2
PROJ_VERSION=$3
SPORT=$4
DPORT=$5
 
PROJ_VERSION=${PROJ_VERSION/"origin/"/""}
 
DOCKER_FILE="/data/dockerfiles/${DOCKER_NAME}/Dockerfile"
DOCKER_FILE_DIR=/data/dockerfiles/${DOCKER_NAME}
if [ ! -d ${DOCKER_FILE_DIR} ]; then
        mkdir -p ${DOCKER_FILE_DIR}
fi
 
# ----------------------------------------------------
# check docker images
# ----------------------------------------------------
DOCKER_IMAGE=`/usr/bin/docker images | grep ${DOCKER_NAME} | awk -F ' ' '{print $3}'`
if [ -n "${DOCKER_IMAGE}" ]; then
        # check docker container
        for dc in `/usr/bin/docker ps -a | grep ${DOCKER_NAME} | awk -F " " '{print $1}'`
        do
                echo "Stop container: ${dc}"
                /usr/bin/docker stop ${dc}
                # delete while docker container was exists
                echo "##Delete exists Container_Id: "${dc}
                /usr/bin/docker rm ${dc}
        done
 
        # delete while docker image was exists
        echo "##Delete exists Image: "${DOCKER_IMAGE}
        /usr/bin/docker rmi ${DOCKER_IMAGE} 
fi
echo ""
 
# ----------------------------------------------------
# Init dockerfile
# ----------------------------------------------------
echo "**Init dockerfile start: "${DOCKER_FILE}
echo "FROM tomcat" > ${DOCKER_FILE}
echo 'MAINTAINER junsansi "junsansi@sina.com"' >> ${DOCKER_FILE}
echo "ADD *.war /usr/local/tomcat/webapps/${PROJECT_NAME}.war" >> ${DOCKER_FILE}
echo "EXPOSE 8080" >> ${DOCKER_FILE}
echo "CMD /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out" >> ${DOCKER_FILE}
cat ${DOCKER_FILE}
echo "**Init dockerfile end."
 
# ----------------------------------------------------
# Build dockerfile
# ----------------------------------------------------
cd ${DOCKER_FILE_DIR}
rm *.war -rf
mv /data/dockerfiles/war/${DOCKER_NAME}/*.war ./
echo ""
echo "##Build dockerfile for "${DOCKER_NAME}
/usr/bin/docker build -t ${DOCKER_NAME}:${PROJ_VERSION} . 
 
 
# ----------------------------------------------------
# Run docker container
# ----------------------------------------------------
echo ""
echo "##Running docker container: "${DOCKER_NAME}
/usr/bin/docker run --name ${DOCKER_NAME}_d1 -d -p ${SPORT}:${DPORT} ${DOCKER_NAME}:${PROJ_VERSION}
 
#/usr/bin/docker run ${cmd}
 
echo ""

参考资料:

视频:http://edu.51cto.com/course/8640.html

posted @ 2021-04-22 14:35  ~*一生所爱*~  阅读(151)  评论(0编辑  收藏  举报
在国际交往中,实力 永远是维护正义的基础;国防 才是外交真正的后盾;尊严 只在剑峰之上;真理 只在大炮射程之内。