使用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"按钮,即可自动打包,发布到业务节点服务器

相关推荐
dessler1 小时前
Elasticsearch(ES)Cerebro部署和使用
linux·运维·elasticsearch
an86950011 小时前
ubuntu 安装 JDK8
linux·运维·ubuntu
weixin_436525071 小时前
Docker 镜像导出与导入教程(Windows - Linux)
运维·docker·容器
小鹿学程序2 小时前
虚拟机之间配置免密登录(Centos)
大数据·linux·运维·centos
边疆.2 小时前
【Linux】编辑器vim的使用和理解gcc编译器
linux·运维·服务器·编辑器·vim
羑悻的小杀马特2 小时前
从零搭建群晖私有影音库:NasTool自动化追剧全流程拆解与远程访问协议优化实践
运维·数据库·自动化
雾江流2 小时前
快指点击器 1.1.9 | 支持多点操作、自动化脚本,操作记录等功能,快速解放双手
运维·自动化·软件工程
孙同学要努力3 小时前
《Linux篇》命令行参数与环境变量
linux·运维
WIN赢4 小时前
【UI自动化相关】
运维·自动化