快速搭建内网测试网络,简单模拟WEB集群
svn项目
模拟WEB集群
后端WEB集群192.168.1.231:8080和192.168.1.232:8080.WEB后端可以按照需要增加,不同目录启动不同端口,通过使用192.168.1.231:80作为负载均衡访问.
upstream www.front.com
{
server 192.168.1.232:8080;
server 192.168.1.231:8080;
}
server
{
listen 80;
server_name 192.168.1.231;
location / {
proxy_pass http://www.front.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
SVN自动发布
使用SVN提交时,注释中输入"auto_deploy",自动发布到231,232的项目测试服务器.svn钩子文件post-commit
#!/bin/bash
REPOS="$1"
REV="$2"
if ( svnlook log -r $REV /data/www/svn/gtl/ |grep "auto_deploy" )
then
echo "`date` auto_deploy">> /tmp/testsvn.log
#front1
ssh root@192.168.1.231 'svn revert -R /data/wwwroot/gtl --username "sylar" --password "gtl1023"'
ssh root@192.168.1.231 'svn update /data/wwwroot/gtl/ --username "sylar" --password "gtl1023"'
ssh root@192.168.1.231 'chown www.www -R /data/wwwroot/gtl'
#front2
ssh root@192.168.1.232 'svn revert -R /data/wwwroot/gtl --username "sylar" --password "gtl1023"'
ssh root@192.168.1.232 'svn update /data/wwwroot/gtl/ --username "sylar" --password "gtl1023"'
ssh root@192.168.1.232 'chown www.www -R /data/wwwroot/gtl'
#admin
ssh root@192.168.1.232 'svn revert -R /data/wwwroot/admin/ --username "sylar" --password "gtl1023"'
ssh root@192.168.1.232 'svn update /data/wwwroot/admin/ --username "sylar" --password "gtl1023"'
ssh root@192.168.1.232 'chown www.www -R /data/wwwroot/admin'
#API
ssh root@192.168.1.235 'svn revert -R /data/wwwroot/api/ --username "sylar" --password "gtl1023"'
ssh root@192.168.1.235 'svn update /data/wwwroot/api/ --username "sylar" --password "gtl1023"'
ssh root@192.168.1.235 'chown www.www -R /data/wwwroot/api'
fi
另外一个SVN钩子,强制SVN提交时输入注释
#!/bin/sh
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c)
if [ "$LOGMSG" -lt 10 ]; then
echo -e "\n 提交文件时必须添加注释,提交中止."1>&2
exit 1
fi
git项目
GIT钩子post-receive
代码服务器的git用户需要对目标机器做ssh密钥授权,并且保证能够以目标机器的sudo用户登录
#!/bin/sh
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi
unset GIT_DIR
echo "==============================================="
ssh gtl@xxx.xxx.xxx.xxx '/bin/bash /home/project/local.sh'
ssh gtl@xxx.xxx.xxx.xxx '/bin/bash /home/project/local.sh'
ssh gtl@xxx.xxx.xxx.xxx '/bin/bash /home/project/local.sh'
echo "====================done======================="
目标机local.sh
目标机器需要首先克隆仓库到本地/data/source_code/. /data/backup/settings.py为项目的配置文件,先备份再cp,然后重启项目.目标机器的git需要cp,chown权限
#!/bin/bash
cd /data/source_code/
sudo git pull
sudo cp /data/wwwroot/project/bcdemo/settings.py /data/backup/settings.py -f
sudo cp /data/source_code/* /data/wwwroot/project/ -a
sudo cp /data/backup/settings.py /data/wwwroot/project/bcdemo/settings.py -f
sudo chown www.www /data/wwwroot/project/ -R
sudo systemctl stop demo
sudo systemctl restart demo
sudo echo "update done,`date -d today +"%Y-%m-%d %T"`" >> /home/project/test.txt