ansible是作为自动化运维工具,非常简单且使用,本文为实际项目配置过程记录。
系统环境
本系统由1台Nginx服务器(负责反向代理及负载均衡)、3台应用服务器、2台数据库+redis服务器构成。程序升级在3台服务器完成,分别为192.168.1.10、192.168.1.11、192.168.1.12。升级时,先升级192.168.1.10服务器(便于描述后文称为“主服务器”),然后通过ansible完成192.168.1.11、192.168.1.12(便于后文称为“从服务器”)自动升级。
服务器的操作系统为centos7.9。
ansible安装配置
由于ansible使用ssh免密访问,因此需配置主到从服务器的免密访问。
ssh配置
1、主服务器产生密钥(root或运行用户):
ssh-keygen -t rsa -b 4096 -P ‘’
2.主服务器专用命令拷贝并设置生效
ssh-copy-id -i -p 端口 ~/.ssh/id_rsa.pub username@从服务器ip
ssh-copy-id 可以把本地主机的公钥复制到远程主机的 authorized_keys 文件上。ssh-copy-id 也会给远程主机的用户主目录(home)和 ~/.ssh 和 ~/.ssh/authorized_keys 设置合适的权限。
3、测试:主服务器执行如下命令:
ssh 从服务器ip date
正常显示日期时间就表示配好了,表示主服务器可以免密访问从服务器。
ansible安装配置
1、主服务器安装
yum install epel-release -y
yum install ansible –y
3、主服务器配置/etc/ansible/ansible.cfg (没有特殊要求,采用默认即可)
4、主服务器配置/etc/ansible/hosts
command_warnings = False
[app]
192.168.1.11
192.168.1.12
#灾备服务器,操作通从服务器
[backup-app]
10.10.14.110
想了解ansible详细配置见“”
其它注意事项
1、本文采用应用下文件的日期时间是否有更新来判断是否要批量升级,因此应用服务器的日志文件应该放在另外目录。
2、主服务器压缩用tar的zip格式,从服务器检查需安装unzip
yum install unzip
升级脚本
编写ansible的升级脚本如下:
#!/bin/bash
#思路:检查相关目录下有否24小时内修改过的文件,有则表示有更新
if [ "" != "app" ] && [ "" != "backup-app" ];
then
echo $"Usage: 升级
app|backup-app (在/etc/ansible/hosts中定义)"
exit 1
fi
hosts=
configChg=0
echo "`date` 检查更新微服务(jar包形式)..."
sPath=/usr/app/cloud
#找到所有有更新的微服务jar包,逐一处理
m_jar=`find $sPath -name '*.jar' -mtime 0`
for jf in $m_jar
do
IFS='/'
if [ "${hosts}" = "backup-app" ] && [ "${jfarr[4]}" == "config" ]; #config为配置参数包,不能同步到灾备
then
echo "请手工调整灾备环境config参数"
continue
fi
#基础配置文件如果有更新,所有服务都要重新启动
if [ "${jfarr[4]}" == "config" ];
then
configChg=1
fi
cpDir=${sPath}/${jfarr[4]}
echo $cpDir
IFS='.'
fName=(${jfarr[5]})
IFS=''
#停止从服务器服务(start-stop-one.sh是从服务器上的启动/停止脚本)
ansible $hosts -m shell -a "source /etc/profile && ${sPath}/start-stop-one.sh ${fName[0]} stop"
#复制
ansible $hosts -m copy -a "src=${jf} dest=${cpDir}"
#启动
ansible $hosts -m shell -a "source /etc/profile && ${sPath}/start-stop-one.sh ${fName[0]} start"
IFS=''
done
#应用更新
function ansibleTomcat()
{
echo "`date` 检查更新${serverName}..."
up_files=`find ${sPath}/webapps -mtime 0`
if [ "$up_files" != "" ];
then
#打包
cd ${sPath}/webapps && tar zcvf /tmp/${serverName}.tar.gz * --exclude=*.war --exclude=*.zip
#停止
ansible $hosts -m shell -a "ps -ef | grep ${serverName} | grep -v grep | awk '{print $2}' | xargs kill -9"
#复制
#ansible $hosts -m copy -a "src=${sPath}/webapps dest=${sPath}" #文件多,会很慢
ansible $hosts -m shell -a "rm -rf ${sPath}/webapps/*"
ansible $hosts -m unarchive -a "src=/tmp/${serverName}.tar.gz dest=${sPath}/webapps copy=yes"
#删掉war包等防止启动出错
ansible $hosts -m shell -a "rm -f ${sPath}/webapps/*.war"
#启动
if [ ${configChg} -eq 0 ];
then
ansible $hosts -m shell -a "nohup ${sPath}/bin/startup.sh &"
fi
echo "更新成功"
else
echo "不需要更新"
fi
}
serverName=manager-server
sPath=/usr/app/manager-server
ansibleTomcat $serverName $sPath $hosts
serverName=bz-server
sPath=/usr/app/bz-server
ansibleTomcat $serverName $sPath $hosts
#如果config变动,重启所有程序
if [ ${configChg} -eq 1 ];
then
ansible $hosts -m shell -a "source /etc/profile && /usr/app/cyberkey-all stop"
ansible $hosts -m shell -a "source /etc/profile && /usr/app/cyberkey-all start"
fi;
echo "`date` 检查更新完毕"
1、手工更新主服务器程序
2、批量自动部署更新到从服务器,运行如下命令即可:
/usr/app/ansible-upgrade.sh app