[toc]
nginx高可用 keepalive的作用 keepalive高可用软件
什么是高可用 
一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的。 
lb01负载均衡,lb02就是lb01的备机,lb01挂了,lb02会快速接管 
 
高可用软件 
keepalive工作原理 keepalived软件是基于VRRP协议实现的,VRRP虚拟路由冗余协议,主要用于解决单点故障问题
1 2 3 4 5 问题一:假设用户将指向都修改为backup路由器,那么master路由器修好了怎么办? 答:使用keepalive可以在用户无感知状态下进行切换 问题二:假设Master网关故障,我们将backup网关配置为master网关的ip是否可以? 答:不可以,因为ARP是将IP和MAC地址映射起来存放在文件中,修改IP后MAC地址仍不一致;可以使用keepalive创建一个相同的VIP和VMAC组合 
keepalive的核心概念 
如何确定谁是主节点谁是备节点(选举投票,优先级) 
如果Master故障,Backup自动接管,那么Master回复后会夺权吗(抢占式、非抢占式) 
如果两台服务器都认为自己是Master会出现什么问题(脑裂) 
 
实践部署keepalive 环境准备 
主机名 
WanIP 
LanIP 
角色 
应用 
 
 
lb01 
10.0.0.5 
172.16.1.5 
Master keepalived 
keepalive 
 
lb02 
10.0.0.6 
172.16.1.6 
Backup keepalived 
keepalive 
 
部署抢占式keepalive(lb01 lb02) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 yum install -y keepalived rpm -ql keepalived /etc/keepalived/keepalived.conf vim /etc/keepalived/keepalived.conf global_defs {				 	router_id lb01 			 } vrrp_instance VI_1 { 	state MASTER			 	interface eth0			 	virtual_router_id 50	 	priority 150			 	advert_int 1			 	authentication {		 		auth_type PASS		 		auth_pass 1111		 	} 	virtual_ipaddress { 		10.0.0.3			 	} } vim /etc/keepalived/keepaslived.conf global_defs { 	router_id lb02 } vrrp_instance VI_1 { 	state BACKUP 	interface eth0 	virtual_router_id 50 	priority 100 	advert_int 1 	authentication { 		auth_type PASS 		auth_pass 1111 	} 	virtual_ipaddress { 		10.0.0.3 	} } systemctl start keepalived systemctl enable  keepalived ip -a 
Keepalived中Master和Backup配置的区别 
Keepalived配置 
Master节点配置 
Backup节点配置 
 
 
route_id(唯一标识) 
router_id lb01 
router_id lb02 
 
state(角色状态) 
state MASTER 
state BACKUP 
 
priority(竞选优先级) 
priority 150 
priority 100 
 
非抢占式keepalive概念 1.两个节点的stste都是backup
配置非抢占式keepalive(lb01 lb02) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 vim /etc/keepalived/keepalived.conf global_defs {				 	router_id lb01			 } vrrp_instance VI_1 { 	state backup			 	interface eth0			 	nopreempt 	virtual_router_id 50	 	priority 150			 	advert_int 1			 	authentication {		 		auth_type PASS		 		auth_pass 1111		 	} 	virtual_ipaddress { 		10.0.0.3			 	} } vim /etc/keepalived/keepalived.conf global_defs { 	router_id lb02 } vrrp_instance VI_1 { 	state BACKUP 	interface eth0 	nopreempt 	virtual_router_id 50 	priority 100 	advert_int 1 	authentication { 		auth_type PASS 		auth_pass 1111 	} 	virtual_ipaddress { 		10.0.0.3 	} } 
脑裂 脑裂故障原因 1、服务器网线松动等网络故障
1 2 3 4 5 6 7 8 9 10 11 12 13 14 cat  check_split_brain.shvip=10.0.0.3 lb01_ip=10.0.0.5 while  true ;do 	ping -c 2 $lb01_ip  &>/dev/null 	if  [ $? -eq 0 -a `ip add|grep "$vip " |wc  -l` -eq 1 ];then  		echo  "ha is split brain.warning."  	else  		echo  "ha is ok"  	fi  sleep  5done 
keepalived结合nginx做高可用 环境准备 
主机名 
WanIP 
LanIP 
角色 
应用 
 
 
lb01 
10.0.0.5 
172.16.1.5 
master keepalive主节点,nginx负载均衡 
keepalived nignx 
 
lb02 
10.0.0.6 
172.16.1.6 
master keepalive备节点,nginx负载均衡 
keepalived nignx 
 
web01 
10.0.0.7 
172.16.1.7 
web网站 
nginx php 
 
web02 
10.0.0.8 
172.16.1.8 
web网站 
nginx php 
 
检查nginx存活脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 vim check_web.sh nginx_count=$(ps -ef|grep [n]ginx|wc  -l) if  [ $nginx_count  -eq 0 ];then 	systemctl start nginx 	sleep  3 	nginx_count=$(ps -ef|grep [n]ginx|wc  -l) 	if  [ $nginx_count  -eq 0 ];then  	systemctl stop keepalived 	fi  fi vim check_web.sh nginx_count=$(ps -ef|grep [n]ginx|wc  -l) if  [ $nginx_count  -eq 0 ];then 	systemctl stop keepalived fi 
wordpress加证书 1 2 3 4 5 6 7 8 9 10 11 12 openssl genrsa -idea -out 20230112_blog.xxx.com.key 2048 Generating RSA private key, 2048 bit long modulus ...................+++ ................................................................................ ...............+++ e is 65537 (0x10001) Enter pass phrase for  20230112_blog.xxx.com.key: 1234 Verifying - Enter pass phrase for  20230112_blog.xxx.com.key: 1234 openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout 20230112_blog.xxx.com.key -out 20230112_blog.xxx.com.pem 
配置两台负载均衡(lb01 lb02) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 vim /etc/nginx/conf.d/blog_proxy.conf upstream blog.xxx.com{ 	server 172.16.1.7; 	server 172.16.1.8; } server { 	listen 80; 	server_name blog.xxx.com; 	rewrite (.*) https://blog.xxx.com; } server { 	listen 443 ssl; 	server_name blog.xxx.com; 	ssl_certificate ssl/20230112_blog.xxx.com.pem; 	ssl_certificate_key ssl/20230112_blog.xxx.com.key; 	location / { 		proxy_pass http://blog.xxx.com; 		proxy_set_header HOST $host ; 	} } nginx -t systemctl restrat nginx 
keepalived和nginx做结合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 vim /etc/keepalived/keepalived.conf global_defs {			 	router_id lb01		 } vrrp_script check_web {		 	script "/root/check_web.sh"  	interval 5 } vrrp_instance VI_1 { 	state backup			 	interface eth0			 	nopreempt 	virtual_router_id 50	  	priority 150			 	advert_int 1			 	 	authentication {		 		auth_type PASS		 		auth_pass 1111		 	} 	virtual_ipaddress { 		10.0.0.3			 	} 	track_script{ 		check_web 	} } chmod  +x /root/check_web.sh10.0.0.3 blog.xxx.com 
解决php破图问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 vim /etc/nginx/conf.d/blog.conf server { 	listen 80; 	server_name blog.xxx.com; 	root /code/wordpress; 	index index.php index.html; 	location / { 		if  ( -f $request_filename /index.html ){ 			rewrite (.*) $1 /index.html break ; 		} 		if  ( -f $request_filename /index.php ){ 			rewrite (.*) $1 /index.php; 		} 		if  ( !-f $request_filename  ){ 			rewrite (.*) /index.php; 		} 	} 	 	location ~ \.php$ { 		fastcgi_pass 127.0.0.1:9000; 		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name ; 		fastcgi_param HTTPS on;				 		include /etc/nginx/fastcgi_params; 	} }