部署 CODEARTS DEPLOY-基于Nginx实现应用的灰度发布:附录

时间:2024-10-23 16:31:51

附录

  • A边节点下线-代码示例
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format main  '$time_local|$remote_addr[$remote_port]|$request|$request_method|$content_length|'
            '$content_type|$http_referer|$host|$http_x_forwarded_for|'
            '$http_true_client_ip|$server_name|$request_uri|$server_addr|$server_port|'
            '$status|$request_time|$upstream_addr|$upstream_response_time|$cookie_domain_tag';
        access_log logs/access.log main; #访问日志:存放路径,日志级别。
        error_log logs/error.log; #错误日志:存放路径。
        sendfile        on;
        keepalive_timeout  65;
        upstream portal {
            # 下面填入A主机IP及应用服务端口
            #server X.X.X.X:X;      #A节点下线
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;
        }
        upstream portal_test {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;
        }
        
        server {
            listen       XXX;#填入Nginx端口
            server_name  localhost;
            
            location / {
                set $backend portal;
                set $test portal_test;
                #下面填入灰度验证机器IP
                #if ( $remote_addr ~* "X.X.X.X") {
                #    set $backend $test;
                #}
                proxy_pass https://$backend;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
       }
    }
  • 部署节点
    # 获取应用进程id
    pid=`ps -ef | grep app_name | grep -v grep | awk '{print $2}'`
    if [ -z "$pid" ];
    then
      echo "[app_name pid is not exist.]"
    else
      echo "app_name pid: $pid "
      # 停止该进程
      kill -15 $pid
    fi
    # 重新启动应用,可通过执行部署脚本和部署命令两种方式启动应用,如下:
    # 方式一:通过执行部署脚本启动应用
    # sh startup.sh
    # 方式二:通过执行命令启动应用,建议采用nohup后台启动方式
    # nohup java -jar /usr/local/app/SpringbootDemo.jar &
  • A边节点灰度上线-代码示例
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format main  '$time_local|$remote_addr[$remote_port]|$request|$request_method|$content_length|'
            '$content_type|$http_referer|$host|$http_x_forwarded_for|'
            '$http_true_client_ip|$server_name|$request_uri|$server_addr|$server_port|'
            '$status|$request_time|$upstream_addr|$upstream_response_time|$cookie_domain_tag';
        access_log logs/access.log main; #访问日志:存放路径,日志级别。
        error_log logs/error.log; #错误日志:存放路径。
        sendfile        on;
        keepalive_timeout  65;
        upstream portal {
            # 下面填入A主机IP及应用服务端口
            #server X.X.X.X:X;      #A节点下线
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;
        }
        upstream portal_test {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;       #A节点灰度上线
            # 下面填入B主机IP及应用服务端口
            #server X.X.X.X:X;
        }
        
        server {
            listen       XXX;#填入Nginx端口
            server_name  localhost;
            
            location / {
                set $backend portal;
                set $test portal_test;
                #下面填入灰度验证机器IP
                if ( $remote_addr ~* "X.X.X.X") {
                    set $backend $test;
                }
                proxy_pass https://$backend;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
       }
    }
  • B边节点下线-代码示例
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format main  '$time_local|$remote_addr[$remote_port]|$request|$request_method|$content_length|'
            '$content_type|$http_referer|$host|$http_x_forwarded_for|'
            '$http_true_client_ip|$server_name|$request_uri|$server_addr|$server_port|'
            '$status|$request_time|$upstream_addr|$upstream_response_time|$cookie_domain_tag';
        access_log logs/access.log main; #访问日志:存放路径,日志级别。
        error_log logs/error.log; #错误日志:存放路径。
        sendfile        on;
        keepalive_timeout  65;
        upstream portal {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;      
            # 下面填入B主机IP及应用服务端口
            #server X.X.X.X:X;      #B节点下线
        }
        upstream portal_test {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;
        }
        
        server {
            listen       XXX;#填入Nginx端口
            server_name  localhost;
            
            location / {
                set $backend portal;
                set $test portal_test;
                #下面填入灰度验证机器IP
                #if ( $remote_addr ~* "X.X.X.X") {
                #    set $backend $test;
                #}
                proxy_pass https://$backend;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
       }
    }
  • B边节点灰度上线-代码示例
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format main  '$time_local|$remote_addr[$remote_port]|$request|$request_method|$content_length|'
            '$content_type|$http_referer|$host|$http_x_forwarded_for|'
            '$http_true_client_ip|$server_name|$request_uri|$server_addr|$server_port|'
            '$status|$request_time|$upstream_addr|$upstream_response_time|$cookie_domain_tag';
        access_log logs/access.log main; #访问日志:存放路径,日志级别。
        error_log logs/error.log; #错误日志:存放路径。
        sendfile        on;
        keepalive_timeout  65;
        upstream portal {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;      
            # 下面填入B主机IP及应用服务端口
            #server X.X.X.X:X;      #B节点下线
        }
        upstream portal_test {
            # 下面填入A主机IP及应用服务端口
            #server X.X.X.X:X;
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;       #B节点灰度上线
        }
        
        server {
            listen       XXX;#填入Nginx端口
            server_name  localhost;
            
            location / {
                set $backend portal;
                set $test portal_test;
                #下面填入灰度验证机器IP
                if ( $remote_addr ~* "X.X.X.X") {
                    set $backend $test;
                }
                proxy_pass https://$backend;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
       }
    }
  • 节点上线-代码示例
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format main  '$time_local|$remote_addr[$remote_port]|$request|$request_method|$content_length|'
            '$content_type|$http_referer|$host|$http_x_forwarded_for|'
            '$http_true_client_ip|$server_name|$request_uri|$server_addr|$server_port|'
            '$status|$request_time|$upstream_addr|$upstream_response_time|$cookie_domain_tag';
        access_log logs/access.log main; #访问日志:存放路径,日志级别。
        error_log logs/error.log; #错误日志:存放路径。
        sendfile        on;
        keepalive_timeout  65;
        upstream portal {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;      
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;
        }
        upstream portal_test {
            # 下面填入A主机IP及应用服务端口
            server X.X.X.X:X;
            # 下面填入B主机IP及应用服务端口
            server X.X.X.X:X;
        }
        
        server {
            listen       XXX;#填入Nginx端口
            server_name  localhost;
            
            location / {
                set $backend portal;
                set $test portal_test;
                #下面填入灰度验证机器IP
                #if ( $remote_addr ~* "X.X.X.X") {
                #    set $backend $test;
                #}
                proxy_pass https://$backend;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
       }
    }
support.huaweicloud.com/bestpractice-deployman/deployman_practice_1022.html