CI/CD——在jenkins中构建流程实现springboot项目的自动化构建与部署
CI/CD——在jenkins中使用pipeline方式自动化构建java项目jpresshttps://blog.csdn.net/xiaochenXIHUA/article/details/160957816CI/CD——在jenkins中自动化构建与部署java项目jpress的镜像且搭建一键部署gitlab与jenkins环境
https://coffeemilk.blog.csdn.net/article/details/160831483?spm=1001.2014.3001.5502
一、spring Cloud与K8s
| 内容 | 说明 |
|---|---|
| Spring Boot | Spring Boot是应用开发层:
|
| Spring Cloud | Spring Cloud是分布式治理层:
|
| Kubernetes | Kubernetes是容器编排层:
|
| Istio | Istio是服务网格:
|
| 总结 |
|
| 使用场景 | Spring Cloud与Kubernetes部分功能重合,Spring Cloud侧重于开发,Kubernetes侧重与运维。 轻量场景:Spring Boot + K8s(用 K8s 原生能力替代部分 Spring Cloud功能)。 复杂场景:Spring Cloud + K8s + Istio(开发治理 + 平台运维 + 流量治理)。 |
| 推荐组合 | ✅推荐使用Spring boot+Kubernetes,使用Spring boot构建微服务,然后通过k8s管理微服务 |
二、jenkins构建流程实现spring-boot-file-upload项目的自动化编译部署
2.1、在gitlab中创建项目仓库
先在gitlab中创建一个名为【spring-boot-file-upload】的项目(即:登录到gitlab的web管理界面,点击左上角的【项目】-->【新建项目】-->【创建空白项目】-->输入项目名称【spring-boot-file-upload】并选择组织(ops)-->【新建项目】),并获取到项目的ssh地址(即:打开项目,然后点击项目左侧的【代码】即可获取到项目链接)如下图所示:
#将dockercompose-springboot-mysql-nginx源码提交到仓库中 #1-克隆gitlab仓库中的项目 git clone git@gitlab2.ck.com:ops/dockercompose-springboot-mysql-nginx.git #2-解压当前的dockercompose-springboot-mysql-nginx源码 tar -zxvf dockercompose-springboot-mysql-nginx-master.tar.gz #3-将源码复制到从gitlab仓库克隆到本地的项目中 cp -r dockercompose-springboot-mysql-nginx-master/* dockercompose-springboot-mysql-nginx/ #4-进入到从gitlab仓库克隆到本地项目中将源码内容提交到仓库中 cd dockercompose-springboot-mysql-nginx git add . git commit -m "init docker-compose-springboot-mysql-nginx" git status git branch git remote -v git push origin main2.2、上传spring-boot-file-upload源码到gitlab仓库中
#上传spring-boot-file-upload源码到gitlab中 #1-将spring-boot-file-upload的源码放置到指定位置并解压 tar -zxvf spring-boot-file-upload-master.tar.gz #2-从远程仓库克隆项目到本地 git clone git@gitlab2.ck.com:ops/spring-boot-file-upload.git #3-将本地解压好的spring-boot-file-upload的源码文件内容都复制到刚克隆下来的仓库中 cp -r spring-boot-file-upload-master/* spring-boot-file-upload/ #4-将仓库内容提交到暂存区缓存并提交到仓库中提交,最后推送到远程仓库中 cd spring-boot-file-upload git add . git commit -m"init spring-boot-file-upload" git status git branch git remote -v git push origin main #5-到gitlab的该项目下查看内容是否上传了注意:若使用ssh方式克隆或推送项目到gitlab仓库中提示“WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:QjJSTZTFWXMqcc5q8Ps996BS24IAZ+PbbBwu9WtoriY.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:3
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
”(即:警告:远程主机标识已更改!
有人可能正在做坏事!
有人可能正在窃听你的通话(中间人攻击)!
也有可能是主机密钥刚刚被更改。
远程主机发送的ECDSA密钥的指纹是
SHA256:QjJSTZTFWXMqcc5q8Ps996BS24IAZ+PbbBwu9WtoriY。
请联系您的系统管理员。
在 /root/.ssh/known_hosts 中添加正确的主机密钥以消除此消息。
/root/.ssh/known_hosts:3中的违规ECDSA密钥
密码认证已禁用以避免中间人攻击。
键盘交互式认证已禁用,以避免中间人攻击。)这是因为当前服务器“
- 本地保存了旧的 GitLab 服务器密钥
- 现在服务器密钥变了
- SSH 认为可能是中间人攻击,所以阻止连接
”解决方法是删除旧密钥,重新生成新密钥
#解决ssh连接“WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! ”错误方法 #1-删除旧密钥 ssh-keygen -R gitlab2.ck.com #2-重新连接生成新密钥(结果显示Welcome to GitLab, @当前用户名!则表示成功) ssh -T git@gitlab2.ck.com2.3、jenkins中配置流水线部署
登录到jenkins的web后台,选择左上角的【+ 新建Item】-->输入任务名称(如:spring-boot-file-upload)并选择【Freestyle project】-->点击【确定】按钮后进入流水线编辑界面配置:
| 流水线配置项 | 说明 |
|---|---|
| General | 1、在【描述】下的输入框输入该流水线的介绍,方便知道该流水线是做什么的(如:spring-boot-file-upload); 2、勾选【使用自定义的工作空间】在【目录】下可以输入绝对路径与相对路径(相对jenkins所在的基础路径【如:/data/jenkins/data】); |
| 源码管理 | 1、在【Repository URL】下的输入框输入需操作仓库的git地址(如:git@gitlab2.ck.com:ops/spring-boot-file-upload.git); 2、选择【Credentials】下配置好的凭据; 3、指定分支(如:*/main) |
| 构建触发器 | 看是否需要根据自己项目情况配置触发器(如:一旦项目被推送内容就触发);这里没有 |
| 构建环境 | 勾选【With Ant】然后选择JDK下的【jdk17】 |
| 构建 | 1、选择【Invoke top-level Maven targets】 2、选择Maven的版本(如:maven3.8.8); 3、【目标】下输入命令 |
| 构建后操作 | 1、选择【Send build artifacts over SSH】(表示:通过ssh发送构建的成果到应用服务器); 2、设置传送的目标【Source files的值是target/*.jar】;【Remove prefix的值是target/】;【Exec command】的命令是 |
| 命令解析 | 说明 |
|---|---|
| set +e |
默认 |
| 强制关闭旧进程 |
|
| && | 前面命令执行完(无论成功失败),继续执行后面的命令。 |
| nohup |
|
| -jar | 使用java运行SpringBoot jar 包。 |
| > /usr/local/spring.log 2>&1 & | 把日志输出到文件“/usr/local/spring.log”中(覆盖输入);
|
| | at now |
|
三、jenkins构建流程实现spring-boot项目制作为docker镜像发布
3.1、在gitlab中创建spring-boot项目并将项目源码推动到仓库中
先在gitlab中创建一个名为【dockercompose-springboot-mysql-nginx】的项目(即:登录到gitlab的web管理界面,点击左上角的【项目】-->【新建项目】-->【创建空白项目】-->输入项目名称【dockercompose-springboot-mysql-nginx】并选择组织(ops)-->【新建项目】),并获取到项目的ssh地址(即:打开项目,然后点击项目左侧的【代码】即可获取到项目链接)如下图所示:
#上传dockercompose-springboot-mysql-nginx源码到gitlab中 #1-从gitlab远程仓库克隆dockercompose-springboot-mysql-nginx项目到本地 git clone git@gitlab2.ck.com:ops/dockercompose-springboot-mysql-nginx.git #2-将dockercompose-springboot-mysql-nginx的源码放置到指定位置并解压 tar -zxvf dockercompose-springboot-mysql-nginx-master.tar.gz #3-将本地解压好的dockercompose-springboot-mysql-nginx的源码文件内容都复制到刚克隆下来的仓库中 cp -r dockercompose-springboot-mysql-nginx-master/* dockercompose-springboot-mysql-nginx/ #4-将仓库内容提交到暂存区缓存并提交到仓库中提交,最后推送到远程仓库中 cd dockercompose-springboot-mysql-nginx git add . git commit -m "init docker-compose-springboot-mysql-nginx" git status git branch git remote -v git push origin main #5-到gitlab的dockercompose-springboot-mysql-nginx项目下查看内容是否上传了3.2、在gitlab中创建spring-boot的流水线项目
先在gitlab中创建一个名为【jenkinsfile-dockercompose-springboot-mysql-nginx】的项目(即:登录到gitlab的web管理界面,点击左上角的【项目】-->【新建项目】-->【创建空白项目】-->输入项目名称【jenkinsfile-dockercompose-springboot-mysql-nginx】并选择组织(ops)-->【新建项目】),并获取到项目的ssh地址(即:打开项目,然后点击项目左侧的【代码】即可获取到项目链接)如下图所示:
3.3、在jenkins中创建spring-boot制作docker镜像并发布的流水线
登录到jenkins的web后台,选择左侧的【打开Blue Ocean】-->【流水线-->创建流水线】-->进入创建流水线界面配置:
| 创建流水线配置内容 | 说明 |
|---|---|
| 选择代码仓库 | 由于我们都是使用git操作gitlab仓库,因此直接选择【Git】即可。 |
| 连接到Git仓库 | 1、在仓库URL下面输入需要创建该流水线的项目地址(如:git@gitlab2.ck.com:ops/jenkinsfile-dockercompose-springboot-mysql-ngin.git); 2、将显示的【ssh-rsa xxxx】内容复制一份(点击“Copy to clipboard”)到gitlab中 |
3.4、在jenkins中配置spring-boot制作docker镜像并发布的流水线
在创建好的流水线中,进行如下配置:
3.4.1、Start配置
点击Start节点,然后在右侧的【流水线设置】中配置全局变量:
| springversion | v1.0 |
|---|
3.4.2、创建git clone节点并配置
点击Start后的【+】号图标新增节点(名称是:git clone),然后点击右侧的【添加步骤】进行如下配置:
| git clone节点配置 | 说明 |
|---|---|
Allocate workspace (分配工作区) | Dir:【/data/jenkins/docker-springboot-mysqlnginx】 在工作区下添加步骤:
|
3.4.3、创建docker build节点并配置
点击【git clone】后的【+】号图标新增节点(名称是:docker build),然后点击右侧的【添加步骤】进行如下配置:
| docker build节点的配置 | 说明 |
|---|---|
Allocate workspace (分配工作区) | Dir:【/data/jenkins/docker-springboot-mysqlnginx】 在工作区下添加步骤: 【shell script】的命令: cd app export DOCKER_BUILDKIT=0 docker build -t gitlab2.ck.com:5050/ops/dockercompose-springboot-mysql-nginx:${springversion} . |
3.4.4、创建docker push节点并配置
点击【docker build】后的【+】号图标新增节点(名称是:docker build),然后点击右侧的【添加步骤】进行如下配置:添加【shell script】命令是:
docker login gitlab2.ck.com:5050 -u root -p coffeemilk.blog.csdn.net docker push gitlab2.ck.com:5050/ops/dockercompose-springboot-mysql-nginx:${springversion}3.5、在jenkins中将spring-boot制作为docker镜像发布
登录到jenkins的web后台,选择左上角的【+ 新建Item】-->在任务界面输入任务的名称【deploy-springboot--mysqlnginx -docker】并选择【Freestyle project】后点击【确定】按钮-->进入创建流水线界面配置:
| 流水线界面配置 | 说明 |
|---|---|
| General |
|
| 构建 | 选择 |
最后登录部署服务器查看mysql数据是否与浏览器上的数据一致:
mysql -uroot -p -h 192.168.1.30 -P 3308 show databases; use test; show tables; select * from visitor; exit