1、 实验环境
192.168.0.18 git /harbor
192.168.0.19 jenkins
192.168.0.20 docker
2、harbor 环境搭建
2.1 安装docker-ce(三台服务器都安装上)
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine rm -rf /var/lib/docker/* yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo yum install docker-ce docker-ce-cli containerd.io systemctl start docker
2.2 安装docker-compose
curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose docker-compose --version
2.3 安装harbor
wget https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-offline-installer-v1.2.0.tgz # cd /usr/local/src/ # tar zxf harbor-online-installer-v1.2.0.tgz -C /usr/local/ # cd /usr/local/harbor/
2.4 修改配置文件
# vim /usr/local/harbor/harbor.cfg hostname = 192.168.0.131 #可以是自己的harbor域名,也可以是本机的IP harbor_admin_password = Harbor12345 #harbor登陆密码
2.5 安装harbor
# ./preper.sh # ./install.sh
2.6 注意事项
需要注意的是关于adminserver容器一直重启的问题:
harbor.cfg 配置中secretkey_path = /data 不要修改目录,如果修改了目录,需要chown 10000:10000 /mydir/secretkey
另外需要添加daemon.json
{"insecure-registries": ["http://192.168.0.18"]}
3 git部署
3.1 部署
yum install -y git useradd git passwd git su - git mkdir solo.git #创建一个solo项目目录 cd solo.git git --bare init
3.2 模拟提交代码
(因为无代码,用git客户端从github上拉取solo代码,git客户端安装在其他服务器,实验在jenkins服务器上)
git clone https://github.com/b3log/solo.git
#在/mdata目录下拉取git服务的空代码,让git客户端连上git服务器 [root@localhost mdata]# git clone [email protected]:/home/git/solo.git warning: 您似乎克隆了一个空版本库。 #将网上下载的solo源码拷贝到空版本库 [root@localhost mdata]# cp -r /root/solo/* ./solo/ [root@localhost solo]# ls CHANGE_LOGS.html Dockerfile gulpfile.js LICENSE package.json package-lock.json pom.xml README.md scripts src #推送代码到git服务器 [root@localhost mdata]# cd solo/ [root@localhost solo]# git add . [root@localhost solo]# git commit -m "all" //提交到本地仓库 [root@localhost solo]# git push origin master //推送到代码仓库 [email protected]'s password: Counting objects: 668, done. Delta compression using up to 2 threads. Compressing objects: 100% (653/653), done. Writing objects: 100% (668/668), 5.17 MiB | 8.78 MiB/s, done. Total 668 (delta 236), reused 0 (delta 0) To [email protected]:/home/git/solo.git * [new branch] master -> master
4 jenkins部署
JDK环境(jenkins,docker,git都配下jdk,JDK配置略)
4.1 jenkins Dockerfile
FROM jenkins/jenkins:2.134 USER root RUN echo "" > /etc/apt/sources.list.d/jessie-backports.list && \ echo "deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib" > /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian-security stretch/updates main " >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian-security stretch/updates main " >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list RUN apt-get update && apt-get install -y git libltdl-dev
4.2 运行jenkins
[root@localhost jenkins]# cat docker-compose.yml version: "3" services: jenkins: image: jenkins:v1 restart: always container_name: jenkins ports: - 8080:8080 volumes: - ./jenkins_home:/var/jenkins_home - /usr/local/apache-maven-3.6.1:/usr/local/maven - /usr/local/jdk8:/usr/local/jdk - /var/run/docker.sock:/var/run/docker.sock - /usr/bin/docker:/usr/bin/docker - /usr/local/bin/docker-compose:/usr/bin/docker-compose - ~/.ssh:/root/.ssh [root@localhost jenkins]# docker-compose up -d
4.3 jenkins需要从git拉取代码,实现免交互拉取代码
[root@localhost jenkins]# ssh-keygen [root@localhost jenkins]# ssh-copy-id [email protected] #测试免交互 [root@localhost jenkins]# git clone [email protected]:/home/git/solo.git 正克隆到 'solo'... remote: Counting objects: 668, done. remote: Compressing objects: 100% (417/417), done. remote: Total 668 (delta 236), reused 668 (delta 236) 接收对象中: 100% (668/668), 5.17 MiB | 0 bytes/s, done. 处理 delta 中: 100% (236/236), done
4.4 安装jenkins
http://192.168.0.19:8080/
[root@localhost jenkins]# cat jenkins_home/secrets/initialAdminPassword c183ca7d1efc46c29a5ee80a5253ef99
jenkins插件安装不了的解决方法:
拷贝已经安装好的jenkins环境中plugins里的所有插件(打包整个目录),到jenkins_home/plugins/下,重启jenkins
https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/ 插件下载地址,也可以手动上传插件(插件管理配置=》高级=》上传文件)
4.5 配置jenkins基础配置
配置jdk的位置
配置maven的位置
配置git位置
jenkins系统配置配置ssh
为jenkins添加一个全局凭证
系统配置
安装maven插件
5 构建项目
5.1 创建一个maven构建的solo项目
构建触发器,采用沦陷方式,格式类似linux的计划任务,分时日月周
clean package -Dmaven.test.skip=true
构建后的shell,制作dockerfile,上传镜像
cd $WORKSPACE cat > Dockerfile <dockerbuild.sh << EOF VERSION=\$(date +%F) docker build -t 192.168.0.18/test/solo:\$VERSION . docker login -u cmn -p Harbor12345 192.168.0.18 docker push 192.168.0.18/test/solo:\$VERSION EOF sh dockerbuild.sh
ssh 远端(docker服务器上)执行脚本
cat >solo.sh << EOF docker rm -f solo VER=`date +%F` docker rmi -f 192.168.0.18/test/solo:\$VER docker login -u cmn -p Harbor12345 192.168.0.18 docker run -d --name solo -p 88:8080 192.168.0.18/test/solo:\$VER EOF sh solo.sh
需要注意现在版本的solo源码里需要添加皮肤到skins目录下,并且需要设置mysql配置文件,皮肤在github的solo项目里下载,Mysql配置在源码的local.properties中
5.2 构建cid项目
项目说明,项目为公司自己的项目,虽然为java项目,由于公司给到的是一个war包,可以当作一个普通的php项目,不需要再次maven构建
# 1)在git 仓库服务器创建cid.git目录,并初始化 [root@localhost solo.git]# su - git [git@localhost ~]$ mkdir cid.git [git@localhost ~]$ cd cid.git/ [git@localhost cid.git]$ git --bare init # 2)在git客户端创建一个cid项目目录,并初始化,指定git仓库地址 [root@localhost mdata]# mkdir cid [root@localhost mdata]# cd cid/ [root@localhost cid]# git init [root@localhost cid]# git remote add origin [email protected]:/home/git/cid.git 3)上传项目包,并提交,打tag [root@localhost cid]# ls cid.war [root@localhost cid]# git add . [root@localhost cid]# git commit -m "v1" [root@localhost cid]# git tag v1.0 [root@localhost cid]# git push origin v1.0
pipeline构建CID项目
需要注意的是,这种方式构建的镜像在jenkins服务器上,适用于jenkins的master-slave模式,
在node 后指定需要构建的主机
其中git_checkout错误,不能拉取代码版本,可以先重新打一个tag提交代码
项目中设置了参数化构建
流水线脚本
node { // 拉取代码 stage('git_checkout') { checkout([$class: 'GitSCM', branches: [[name: '$Tag']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:/home/git/cid.git']]]) } stage('Build and push image') { sh ''' REPOSITORY=192.168.0.18/test/cid:${Tag} cat >config.txt<< EOF 192.168.0.20:2181 EOF cat >cid <Dockerfile << EOF FROM 192.168.0.18/test/tomcat:v1 RUN rm -rf /usr/local/tomcat/webapps/* &&\ mkdir /usr/share/JFConfig/ COPY *.war /usr/local/tomcat/webapps/ COPY cid /usr/share/JFConfig/cid COPY config.txt /usr/share/JFConfig EOF docker build -t $REPOSITORY . docker login -u cmn -p Harbor12345 192.168.0.18 docker push $REPOSITORY ''' } stage('Deploy to docker') { sh ''' REPOSITORY=192.168.0.18/test/cid:${Tag} docker rm -f jf-cid |true docker rmi -f $REPOSITORY |true docker login -u cmn -p Harbor12345 192.168.0.18 docker pull $REPOSITORY cat > docker-compose.yml <
自由风格构建cid项目
cd $WORKSPACE cat > Dockerfile <
ssh 远端(docker服务器上)执行脚本
docker rm -f jf-cid docker rmi -f 192.168.0.18/test/cid:${Tag} docker login -u cmn -p Harbor12345 192.168.0.18 REPOSITORY=192.168.0.18/test/cid:${Tag} cat > docker-compose.yml <config.txt echo "123456" > cid docker-compose up -d 构建
构建过程中出现的构建失败情况参考控制台输出做出相应的调整
6 实验结果