使用Nginx+Keepalived部署Python打包的API接口

Python文件打包

1)使用pyinstaller 打包 python代码文件

参数功能说明
-h,–help查看模块功能帮助信息
-F,-onefile生成单个可执行文件
-D,–onedir生成一个目录(包含多个文件)作为可执行程序
-a,–ascii不包含Unicode字符集支持
-d,–debug产生debug版本的可执行文件
-w,–windowed,–noconsolc指定程序运行时不显示命令窗口(仅支持windows)
-c,–nowindowed,–console指定使用命令行窗口运行程序(仅支持windows)
-o DIR, --out=DIR设置spec文件生成目录
-p DIR, --path=DIR设置Python导入模块的路径
-n NAME, --name=NAME指定项目(生成的spec)名字,此项目缺省默认第一个主文件名作为spec的文件名
# 安装pyinstaller打包软件
[root@3893e696a14d ~]# pyinstall -D <file1Name.py,fileName2.py,...> -
[root@3893e696a14d ~]# pyinstaller <fileName.py>
[root@3893e696a14d ~]# pyinstaller -D PosDriver_API.py config.py connect_db.py jwtToken.py -n it-pos

2)将打包好的程序上传到服务器运行

[root@3893e696a14d ~]# cd /<程序目录路径>
[root@3893e696a14d ~]# nohup <程序执行文件>
[root@3893e696a14d ~]#nohup ./it-pos	#使用nohup 保证会话断开后程序不被中止
使用Nginx实现程序的高可用性

1)配置Nginx负载均衡

[root@dev-nginx conf.d]# vim <nginxconf>.conf 
upstream <cluster name> {
	server	server_ip:port;
	server	server_ip:port;
}

server {
	listen		port;
	server_name	it-pos;

	location /<url_path01> {
		proxy_pass	http://<cluster name>;
	}
	
	location /<url_path02> {
		proxy_pass	http://<cluster name>;
	}
}
# 检查Nginx配置文件是正确
[root@3893e696a14d ~]# nginx -tc /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@3893e696a14d ~]# systemctl start nginx
[root@3893e696a14d ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-06-02 04:00:30 UTC; 4s ago
     Docs: http://nginx.org/en/docs/
  Process: 593 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 594 (nginx)
   CGroup: /docker/3893e696a14d912e0d30181626c56718d481c10f02a8da59a56135d6002576e2/system.slice/nginx.service
           ├─594 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           ├─595 nginx: worker process
           ├─596 nginx: worker process
           ├─597 nginx: worker process
           └─598 nginx: worker process
           ‣ 594 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

Jun 02 04:00:30 3893e696a14d systemd[1]: Starting nginx - high performance web server...
Jun 02 04:00:30 3893e696a14d systemd[1]: Started nginx - high performance web server.
验证

分别关闭两台服务器中的任意一个运行服务后测试访问是否正常

使用keepalived实现Nginx的高可用

Keepalived高可用故障切换转移原理
Keepalived高可用服务的故障切换转移,是通过VRRP来实现的。

在Keepalived服务正常运行时,主Master节点会不断向备用结点发送(多播方式)心跳信息,用以告诉Backup节点自己还活着。
当Master节点发生故障时,就无法发送心跳信息,Backup节点也就无法检测到来自Master的心跳信息,于是调用自身的接管程序,接管Master的IP资源和服务;
当Master恢复时,Backup又会释放Master故障时自身接管的IP资源和服务,恢复到原来的备用角色。

VRRP(Virtual Router Redundancy Protocol)虚拟路由协议,为了解决静态路由的单点故障问题,VRRP通过一种竞选机制来将路由的任务交给某台VRRP路由器。
VRRP早期是用来解决交换机、路由器等设备单点故障。交互、路由的Master和Backup的切换原理同样适用于Keepalived的工作原理。
在一组VRRP路由器集群中,有多台物理VRRP路由器,但并不是同时工作,而是由一台Master的机器负责路由工作,其他机器都是Backup。Master并不是一成不变,VRRP会让每个VRRP路由参与竞选,最终获胜的就是Master。获胜的Master有一些特权,比如拥有虚拟路由器的IP地址等。
拥有系统资源的Master负责转发发送给网关地址的包和响应ARP请求。

VRRP通过竞选机制来实现虚拟路由器的功能,所有的协议报文都是通过IP多播(Multicast)包形式来发送,默认多播地址224.0.0.18。在一组虚拟路由器中,不管谁是 Master,对外都是相同的MAC和IP,称之为VIP。客户端主机并不需要因Master的改变而修改自己的路由配置。对他们来说,这种切换时透明的。

在一组虚拟路由器中,只有作为Master的VRRP路由器会一直发送VRRP广播包,此时Backup不会抢占Master。当Master不可用时,Backup就收不到来自Master的广播包了,此时多台Backup中优先级最高的路由器会抢占为Master。
这种抢占非常快速,以保证服务的连续性。处于安全性考虑,VRRP数据报使用了加密协议进行加密。

keepalived工作原理:通过vrrp协议实现
VRRP:虚拟路由冗余协议
VRRP的工作工程:
		(1) 虚拟路由器中的路由器根据优先级选举出 Master。 Master 路由器通过发送免费 ARP 报文,将自己的虚拟 MAC 地址通知给与它连接的设备或者主机,从而承担报文转发任务;
		(2) Master 路由器周期性发送 VRRP 报文,以公布其配置信息(优先级等)和工作状况;
		(3) 如果 Master 路由器出现故障,虚拟路由器中的 Backup 路由器将根据优先级重新选举新的 Master;
		(4) 虚拟路由器状态切换时, Master 路由器由一台设备切换为另外一台设备,新的 Master 路由器只是简单地发送一个携带虚拟路由器的 MAC 地址和虚拟 IP地址信息的ARP 报文,这样就可以更新与它连接的主机或设备中的ARP 相关信息。网络中的主机感知不到 Master 路由器已经切换为另外一台设备。
		(5) Backup 路由器的优先级高于 Master 路由器时,由 Backup 路由器的工作方式(抢占方式和非抢占方式)决定是否重新选举 Master。
		VRRP优先级的取值范围为0到255(数值越大表明优先级越高)
Keepalived工作方法:当Master故障后,VIP和vmac地址自动漂移到Backup Server(Master Server和Backup Server会每隔1s向组播地址发送数据包,检测主或备是否存活),并通知下端主机刷新ARP表

在这里插入图片描述

## Nginx 进程检查脚本
ng=`ps -C nginx --no-header | wc -l`
if [ ng -eq 0 ];then
	systemctl start nginx
	sleep 2
	ng=`ps -C nginx --no-header| wc -l`
	if [ ng -eq 0 ];then
		killall -s QUIT keepalived
	fi
fi

使用yum安装keepalived
yum install keepalived -y
# 配置keepalive配置文件
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx_status {
  script '/usr/local/src/check_nginx.sh'   # 定义检查Nginx状态脚本的文件路径
  intervel    2
  weight      2
}

vrrp_instance VI_1 {
    state MASTER	# 配置当前服务器主从状态(MASTER:主;BACKUP:从)
    interface eth0	# 提供网络服务的网卡名称
    virtual_router_id 51	# 主从服务器的虚拟路由ID(两台必须保持一致)
    priority 100	# 主从服务器的优先级,主服务器高于从服务器
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress { # VRRP H虚拟IP地址
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值