使用rsync+jenkins实现服务自动部署全流程

项目背景:城市政务云服务器没有上k8s,所有后端服务都是原始方式部署启动 (java -jar xxx.jar),那么有没有方式简化部署难度,实现自动部署?当然是有的,下面详细介绍(以CentOS7环境为例):

一、服务器安装配置rsync

  1. 安装rsync

一般服务器上自带的有rsync,怎么查系统自带的有没有rsync以及是哪个版本

bash 复制代码
[root@he-vm-0000000589 ~]# rpm -qa|grep rsync
rsync-3.1.2-11.el7_9.x86_64

或者使用which命令也行,查看服务器上有没有 rsync命令

bash 复制代码
[root@he-vm-0000000589 ~]# which rsync
/usr/bin/rsync

若系统没有安装rsync(没有rsync命令),则安装命令如下:

bash 复制代码
yum -y install rsync

2 被控端(业务节点服务器)上配置rsync

2.1 创建 /etc/rsync.pass 文件

bash 复制代码
touch /etc/rsync.pass

2.2 编辑 /etc/rsync.pass

vim /etc/rsync.pass

更改 rsync.pass 用户权限为 600

bash 复制代码
chmod 600 /etc/rsync.pass

效果如下:

2.3 配置 /etc/rsyncd.conf 文件

PS:/etc/rsyncd.conf 文件内容默认是全部注释了的

vim /etc/rsyncd.conf

bash 复制代码
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
max connections = 0

[activity-annual]  # 自定义模块名,填要自动部署的服务名称
path = /data/spring/activity-annual
comment = activity-annual
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
timeout = 600
auth users = xx
hosts allow = 10.8.11.240
hosts deny = 0.0.0.0/32


[hello-world]  # 自定义模块名,填要自动部署的服务名称
path = /data/spring/hello-world
comment = hello-world
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
timeout = 600
auth users = xx
hosts allow = 10.8.11.240
hosts deny = 0.0.0.0/32

上面配置文件,表明允许主服务器 (假设ip为10.8.11.240)访问,rsync同步模块名为[activity-annual] 和 [hello-world],将同步过来的文件分别放入 path指定的目录 /data/spring/activity-annual、/data/spring/hello-world 。如果是多节点部署,则每一台节点服务器都需要进行类似的 rsync 配置,上面的 uid gid 更换成您服务器的相应用户,注意 rsync 要有对被同步目录的操作权限。配置好之后,使用如下命令,开启 rsync 守护进程:

bash 复制代码
rsync --daemon

3.主控端(jenkins服务器)上配置rsync

创建 /etc/passwd.txt , 文件内写入 认证密码, 注意:要和 从控端的 /etc/rsync.pass 里设置的相对应

bash 复制代码
touch /etc/passwd.txt && echo admin@789 > /etc/passwd.txt

修改 /etc/passwd.txt 文件权限为 600

bash 复制代码
chmod 600 /etc/passwd.txt

验证主控端(jenkins服务器)文件到被控端(业务节点服务器)文件同步功能:

在主控端(jenkins服务器)上执行如下命令:

bash 复制代码
rsync -avz activity-annual.jar xx@10.8.11.248::activity-annual --password-file=/etc/passwd.txt

执行结果如下

执行后,主控端上的activity-annual.jar 文件自动同步到 目标服务器 10.8.11.248 上

4.被控端(业务节点服务器)上创建核心脚本

4.1 在被控端(业务节点服务器)上创建 /etc/systemd/system/jenkins 目录

mkdir /etc/systemd/system/jenkins

4.2在用户根目录下创建并配置 system_add.sh 脚本

核心脚本抽空会放在评论区

核心脚本用途

--- 在 后面 配置jenkins自动部署要用到该核心脚本。

执行核心脚本后会生成两个文件

----- 下班了,晚点补充

二 配置jenkins自动部署

jenkins web端 配置自动部署job

  1. New Item 创建一个 Job

可自定义该Job中的参数变量

  1. 自动部署核心配置

Build Steps 选择 Execute shell

shell脚本如下:

bash 复制代码
echo ${WORKSPACE}
echo ${JOB_NAME}
context_name=hello-world

wget ${backend_package_url} -O ${context_name}.tar.gz
tar -zxvf ${context_name}.tar.gz
mv ${WORKSPACE}/target/*.jar ${context_name}.jar
chmod -R o+g ${context_name}.jar
jar=${context_name}.jar

TIMESTAMP=`date +%Y%m%d_%H%M`
ansible 10.8.11.248 -m shell -a "sh /root/spring_add.sh ${context_name}"
ansible 10.8.11.249 -m shell -a "sh /root/system_add.sh ${context_name}" 
rsync -avz ${jar} xx@10.8.11.248::${context_name} --password-file=/etc/passwd.txt
rsync -avz ${jar} xx@10.8.11.249::${context_name} --password-file=/etc/passwd.txt

sleep 10
ansible 10.8.11.248 -m systemd -a "name=${context_name} state=restarted"
ansible 10.8.11.249 -m systemd -a "name=${context_name} state=restarted"

点击Build with Parameters,输入后端包下载地址,然后点"Build"按钮,即可自动打包,发布到业务节点服务器

相关推荐
江湖有缘2 小时前
Linux系统之htop命令基本使用
linux·运维·服务器
B***y8852 小时前
配置nginx访问本地静态资源、本地图片、视频。
运维·nginx
w***Q3506 小时前
Git工作流自动化
运维·git·自动化
xu_yule8 小时前
Linux_12(进程信号)内核态和用户态+处理信号+不可重入函数+volatile
linux·运维·服务器
虾..8 小时前
Linux 环境变量&&进程优先级
linux·运维·服务器
数据库学啊10 小时前
团队小希望运维简单,时序数据库选型有什么推荐?
运维·数据库·时序数据库
霍格沃兹软件测试开发10 小时前
Playwright MCP浏览器自动化指南:让AI精准理解你的命令
运维·人工智能·自动化
郝学胜-神的一滴11 小时前
Linux命名管道:创建与原理详解
linux·运维·服务器·开发语言·c++·程序人生·个人开发
wanhengidc11 小时前
云手机性能如何?
运维·服务器·科技·智能手机·云计算
wan_da_ren12 小时前
Docker安装Elasticsearch9.2.1与Kibana9.2.1 保姆教程(带验证版)
运维·docker·jenkins