现在系统会均衡地分配用户访问app1与app2。 接下来我们进行平滑发布,我们先把app1停止,然后将新版本发布到app1中:
步骤1: 准备发布app1配置文件
新做一个配置文件 pub_app1_down.conf,内容中把app1停止掉:
upstream myapp{
server 127.0.0.1:801 down; //app1
server 127.0.0.1:802; //app2
}
将这个文件内容覆盖掉在原有的pub_app.conf
cp -f /vhost/pub/pub_app1_down.conf /vhost/pub_app.conf
步骤2:停止app1应用
平滑重新加载一下nginx:
service nginx reload
或者:
/usr/local/nginx/sbin/nginx -s reload
此时所有的请求都转到了app2了;
步骤3:更新app1
现在可以通过各种方式来更新应用了,例如:压缩包方式:
wget http://version.my.com/appudate/myapp/myapp-v3.2.32.tar
unzip -o -d /home/wwwroot/app1/ myapp-v3.2.32.tar
其中:-o:不提示的情况下覆盖文件;-d:指定解压目录
步骤3.5 内部测试
如果需要的话,可以在这一步对app1进行内部测试,以确保应用的正确性;
步骤4:准备发布app2配置文件;
此时app1已经是最新版本的文件了,可以切换到app1来对外,
创建一个新的nginx配置文件:pub_app2_down.conf,设置为app1对外,app2停止即可:
upstream myapp{
server 127.0.0.1:801; //app1
server 127.0.0.1:802 down; //app2
}
将这个文件内容覆盖掉在原有的pub_app.conf
cp -f /vhost/pub/pub_app2_down.conf /vhost/pub_app.conf
步骤5:切换到app1新版本应用
平滑重新一下nginx:
service nginx reload
或者:
/usr/local/nginx/sbin/nginx -s reload
此时所有的请求都转到了app1了,新版本开始运行;
步骤6:更新app2
与第3步一样,解压就可以了,这里可以省去下载过程
unzip -o -d /home/wwwroot/app2/ myapp-v3.2.32.tar
步骤7:恢复app1,app2同时对外:
cp -f /vhost/pub/pub_app.conf /vhost/pub_app.conf
平滑重新一下nginx:
service nginx reload
或者:
/usr/local/nginx/sbin/nginx -s reload
至此,整个应用都已经更新。
将各步骤中的脚本汇总一下:
[pub.sh]
#============ 平滑发布 v1.0 ===============
#step 1
cp -f /vhost/pub/pub_app1_down.conf /vhost/pub_app.conf
#step 2
service nginx reload
#step 3
wget http://version.my.com/appudate/myapp/myapp-v3.2.32.tar
unzip -o -d /home/wwwroot/app1/ myapp-v3.2.32.tar
#step 4
cp -f /vhost/pub/pub_app2_down.conf /vhost/pub_app.conf
#step 5
service nginx reload
#step 6
unzip -o -d /home/wwwroot/app2/ myapp-v3.2.32.tar
#step 7
cp -f /vhost/pub/pub_app.conf /vhost/pub_app.conf
service nginx reload
#============ 平滑发布 v1.0 ===============
备注:也可以充分利用nginx的宕机检测,省去步骤1,2,4,5,7;
简化后的脚本如下:
[pub_mini.sh]
#======== 简化版脚本 =============
wget http://version.my.com/appudate/myapp/myapp-v3.2.32.tar
unzip -o -d /home/wwwroot/app1/ myapp-v3.2.32.tar
unzip -o -d /home/wwwroot/app2/ myapp-v3.2.32.tar
#========= over ===========
# 实验 nginx 配置文件 (按要求创建网站根目录及修改 index.html 文件)
upstream myapp{
server 192.168.11.128:801;
server 192.168.11.128:802;
}
server {
listen 80;
server_name myapp;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myapp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 801;
server_name app2;
root /usr/share/nginx/app1-html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 802;
server_name app2;
root /usr/share/nginx/app2-html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。