目录
实验需求:
1.部署gitlab将上面这个项目的代码上传到自己的gitlab仓库中(第一台主机)
2.基于tomcat部署jenkins(第2台主机)
3.部署harbor仓库(第3台主机)
4.制作tomcat镜像并上传到harbor仓库(第4台主机)
5.用jenkins将以上项目打包成war包(第2台主机)
6.用自己制作的tomcat镜像上线项目的war包(用jenkins流水线完成)(第2台主机)
项目路径:
https://gitee.com/forgotten/tomcat-java-demo
下载zip包在本地,传进gitlab主机并解压缩,在gitlab主机中将项目目录上传至仓库
[root@git ~]# ls
anaconda-ks.cfg
tomcat-java-demo-master
[root@git ~]# yum -y install unzip
[root@git ~]# unzip tomcat-java-demo-master.zip
[root@git ~]# ls
[root@git ~]# ls
anaconda-ks.cfg
tomcat-java-demo-master
tomcat-java-demo-master.zip
[root@git ~]# ls tomcat-java-demo-master
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src
# 这就是项目目录里的全部内容
一.准备工作
主机 | 说明 | IP | 系统 |
gitlab.example.com | gitlab仓库 | 192.168.187.129 | centos8 |
jenkins.example.com | jenkins集成 | 192.168.187.131 | centos8 |
harbor.example.com | harbor私有仓库 | 192.168.187.140 | centos8 |
docker.example.com | 应用主机 | 192.168.187.150 | centos8 |
在gitlab,jenkins,harbor主机上各自部署出服务,看到web界面
二.gitlab主机操作
在web页面创建了一个名为beauty的项目
在命令行页面进行上传操作
# 1.全局设置
# 设置用户和邮箱,邮箱暂时用不到,随便填的
[root@git ~]# git config --global user.name "root"
[root@git ~]# git config --global user.email "1@2.com"
# 2.创建一个存储库
# 这里存储卡的名称前面必须是项目的名称
[root@git ~]# git clone http://192.168.187.129/root/beauty.git
Cloning into 'beauty'...
Username for 'http://192.168.187.129': root
Password for 'http://root@192.168.187.129':
warning: You appear to have cloned an empty repository.
[root@git ~]# ls
anaconda-ks.cfg
beauty
tomcat-java-demo-master
tomcat-java-demo-master.zip
# 出现了一个名为beauty的空目录
# 3.创建文件并上传
[root@git ~]# cd beauty
# 创建一个新分支名为beautiful,也可以使用默认的分支main
[root@git beauty]# git switch --create beautiful
Switched to a new branch 'beautifil'
# 将项目内的全部东西移动至本目录下
[root@git beauty]# mv ../tomcat-java-demo-master/* .
[root@git beauty]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src
# 添加目录下的全部内容
[root@git beauty]# git add *
# 说明
[root@git beauty]# git commit -m "add beautiful"
[beautiful (root-commit) b84f797] add beautiful
[beautiful ad000d1] add beautiful
151 files changed, 2190 insertions(+), 92 deletions(-)
create mode 100644 Dockerfile
create mode 100644 LICENSE
rewrite README.md (100%)
......省略
#上传
[root@git beauty]# git push --set-upstream origin beautiful
Username for 'http://192.168.187.129': root
Password for 'http://root@192.168.187.129':
Enumerating objects: 183, done.
Counting objects: 100% (183/183), done.
Delta compression using up to 8 threads
Compressing objects: 100% (168/168), done.
Writing objects: 100% (181/181), 1.12 MiB | 6.75 MiB/s, done.
Total 181 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), done.
remote:
remote: To create a merge request for beautiful, visit:
remote: http://192.168.187.129/root/beauty/-/merge_requests/new?merge_request%5Bsource_branch%5D=beautiful
remote:
To http://192.168.187.129/root/beauty.git
* [new branch] beautiful -> beautiful
Branch 'beautiful' set up to track remote branch 'beautiful' from 'origin'.
# 上传成功
# 可以在项目上看到刚上传的beautiful
# 在设置中设置默认的分支为beautiful,这样我们拉取项目代码时,就是直接拉取beautiful分支下的内容
三.harbor主机操作
# 1.在harbor主机上制作一个基于centos8系统的容器在其中部署tomcat服务,这里我们可以用到dockerfile来创建镜像。dockerfile内容如下:
[root@harbor ~]# vim docker/dockerfile
FROM centos:8
RUN rm -rf /etc/yum.repos.d/* && \
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
yum clean all && \
yum makecache && \
yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel && \
yum -y install wget && \
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.79/bin/apache-tomcat-9.0.79.tar.gz && \
tar -xf apache-tomcat-9.0.79.tar.gz && \
mv apache-tomcat-9.0.79 /usr/local/tomcat && \
/usr/local/tomcat/bin/startup.sh
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
# 制作镜像
# 这里的容器镜像名称要与harbor仓库的信息:ip/存储目录/自定义名称:标签 一致
docker build -t 192.168.187.140/library/tomcat:v50.0 /root/docker/
[root@harbor ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.187.140/library/tomcat v50.0 905023dca678 2 days ago 539MB
# 登录harbor仓库,并上传镜像
[root@harbor ~]# docker login 192.168.187.140
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@harbor ~]# docker push 192.168.187.140/library/tomcat:v50.0
# 在web页面查看
四.jenkins主机操作
准备工作:尝试拉取项目目录与镜像操作
# 因为要在jenkins主机上制作镜像,所以jenkins主机上也要部署docker服务
# 没有git命令先yum安装一个
# 本实验中多处需要用到登录验证,所以在jenkins主机上针对其它主机做一个免密登录
[root@jenks ~]# ssh-keygen -t rsa 然后一直回车,创建成功
# 将公钥发送至其它几台主机
[root@jenks ~]# ssh-copy-id root@192.168.187.129
[root@jenks ~]# ssh-copy-id root@192.168.187.140
[root@jenks ~]# ssh-copy-id root@192.168.187.150
# 1.尝试拉取项目目录
[root@jenks ~]# git clone git@192.168.187.129:root/beauty.git
Cloning into 'beauty'...
remote: Enumerating objects: 184, done.
remote: Counting objects: 100% (184/184), done.
remote: Compressing objects: 100% (166/166), done.
remote: Total 184 (delta 4), reused 181 (delta 4), pack-reused 0
Receiving objects: 100% (184/184), 1.12 MiB | 31.10 MiB/s, done.
Resolving deltas: 100% (4/4), done.
[root@jenks ~]# ls
anaconda-ks.cfg beauty docker files
[root@jenks ~]# cd beauty/
[root@jenks beauty]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src
# 拉取成功,内容也在
# 2.尝试拉取harbor主机上的tomcat镜像
# 在/etc/hosts文件内添加上harbor主机的ip与域名做解析
192.168.187.140 harbor.example.com
# 编辑/etc/docker/daemon.json文件
{
"insecure-registries": ["192.168.187.140"], # 加上这行表示允许从不安全的镜像仓库拉取镜像
"registry-mirrors" : [ # 中括号内的ip是harbor主机的ip
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com"
]
}
# 拉取镜像
[root@jenks ~]# docker pull 192.168.187.140/library/tomcat:v50.0
v50.0: Pulling from library/tomcat
a1d0c7532777: Already exists
a06464f817fa: Already exists
7231e5739c7c: Already exists
109b05d3272e: Pull complete
Digest: sha256:4ab5e8b0e514fcc8c4f1396e2237e9f376c4b7e08bd19f99b3b5895ee7eaaa81
Status: Downloaded newer image for 192.168.187.140/library/tomcat:v50.0
192.168.187.140/library/tomcat:v50.0
[root@jenks ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.187.140/library/tomcat v50.0 4d65b426ccad 2 days ago 621MB
# 拉取成功,没有问题
编写jenkins流水线脚本
创建任务,选择流水线,点击确定后在配置中勾选上不允许并发构建
然后开始编辑脚本内容
# 流水线脚本说明
# 1.首先拉取gitlab主机上的项目目录,拉取成功后默认保存在/root/.jenkins/workspace/tomcat/目录下,进入其中的beauty(项目名称)目录,用mvn命令(yum安装maven)编译为war包的状态,编译完成后的war包在同级目录下的target目录下,名为ly-simple-tomcat-0.0.1-SNAPSHOT.war。
# 2.拉取harbor主机上的tomcat镜像,然后用镜像运行一个名为tomcat的容器,将/usr/local/tomcat/webapps目录下的ROOT删除,将war包移动至webapps目录下并改名为ROOT.war,这样解压缩后就成了ROOT。这个war包只有名为ROOT时才能看到图片。
# 3.重启tomcat服务,war包自动解压缩,由于我们前面运行镜像生成容器时做了端口映射,此时我们在jenkins主机的9000端口上是可以看到war包的内容的。
# 4.将容器制作成镜像,并上传(前提是jenkins主机登录了harbor仓库)
pipeline {
agent any
stages {
stage("pull code and mvn"){
steps {
sh """
git clone git@192.168.187.129:root/beauty.git
cd /root/.jenkins/workspace/tomcat/beauty
mvn clean package -Dmaven.test.skip=true
"""
}
}
stage("pull images and run and deploy tomcat"){
steps {
sh """
docker pull 192.168.187.140/library/tomcat:latest
docker run -d -p 9000:8080 --name tomcat 192.168.187.140/library/tomcat
docker exec tomcat rm -rf /usr/local/tomcat/webapps/ROOT
docker cp /root/.jenkins/workspace/tomcat/beauty/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war tomcat:/usr/local/tomcat/webapps/
docker exec tomcat /usr/local/tomcat/bin/catalina.sh stop
docker exec tomcat /usr/local/tomcat/bin/catalina.sh start
docker exec tomcat mv /usr/local/tomcat/webapps/ly-simple-tomcat-0.0.1-SNAPSHOT /usr/local/tomcat/webapps/ROOT
"""
}
}
stage("build image for tomcat and push"){
steps {
sh """
docker commit -a "zmq" tomcat 192.168.187.140/library/tomcat:v100.0
docker push 192.168.187.140/library/tomcat:v100.0
"""
}
}
}
}
在web页面访问ip加上映射的端口号,就可以看到web页面
并且镜像已经上传至harbor