Linux高可用之(HA)Keepalived+nginx实现web高可用
一.关于keepalived
1.介绍
keepalived起初是专为LVS设计的,专门用来监控LVS集群系统中各个服务节点的状态,后来又加入了VRRP的功能,因此除了配合LVS服务外,也可以作为其他服务(nginx,haproxy,mysql,httpd )的高可用软件,VRRP是Virtual Router Redundancy Protocol(虚拟路由器协议)的缩写,VRRP出现的目的就是为了解决静态路由出现的单点故障问题,它能够保证网络的不间断、稳定的运行。所以,keepalived一方面具有LVS cluster nodes healthchecks功能,另一方面也具有LVS directors failover功能。
keepalived服务的两大用途:healthcheck & failover
failover功能:实现LB Master主机和Backup主机之间故障转移和自动切换。
这是针对有两个负载均衡器Director同时工作而采取的故障转移措施。当主负载均衡器(Master)失效或出现故障时,备负载均衡器(Backup)将自动接管自负载均衡的所有工作(vip资源及相应服务);一旦主负载均衡(Master)故障修复,Mater又会接管回他原来处理的工作,而备份负载均衡器(Backup)会释放master失效时它接管的工作,此时两者将会恢复到最初各自的角色状态。
healthcheck功能:负载均衡定期检查RS的可用性决定是否给其分发请求。
当虚拟服务器中某一个甚至几个真实服务器同时发生故障无法提供服务时,负载均衡器会自动将失效的RS服务器从转发队列中清除出去,从而保证用户的访问不受影响;当故障的RS服务完全被修复以后,系统又会自动的把它们加入转发队列,分发请求提供正常服务。。
2.VRRP原理
keepalived高可用对之间的故障切换转移的原理是什么?是通过VRRP协议实现接管的
VRRP协议小结:keepalived工作原理
1、VRRP协议,全称Virtual Router Redundancy Protocol,中文虚拟路由器冗余协议,VRRP的出现是为了解决静态路由的单点故障,
2、VRRP是通过一种竞选协议机制来将任务交给某台VRRP路由器。。
3、VRRP通信是通过IP多播的方式实现通信
4、主发包,备接收包,备接不到包时,备接管主的资源。备可以有多个,通过优先级竞选。
5.VRRP使用了加密协议。
3.keepalived工作原理
keepalived通过vrrp通信,通过竞选确定主备,主优先级高于备。因此,工作时主会获得所有资源,备节点处于等待状态,当主节点宕机,那么备节点启用接管程序接管主节点资源,顶替主节点提供服务。
VRRP协议通过IP组播方式发送,可以保证业务的连续性,接管速度灰常快,小于1秒.
二.安装keepalived
0.机器准备
HOSTNAMEIP说明
lb02 10.20.23.171 keepalived备服务器(nginx备负载均衡)
web01 10.20.23.191 web01服务器
web02 10.20.23.192 web02服务器
1.关于负载均衡nginx
lb01和lb02nginx配置文件一样
[root@lb01 keepalived]# cat /application/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream static_pools { server 10.20.23.191:80 weight=1; server 10.20.23.192:80 weight=1; } server { listen 10.20.23.10:80; server_name blog.cool360.org; location / { proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } }
2.下载keepalived并安装
cd /usr/local/src wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz 安装keepalived: yum install openssl* -y yum install libnl* popt* -y ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux ##若没有/usr/src/kernels/2.6.32-431.el6.x86_64/路径,很可能是因为缺少kernel-2.6.32-431.el6.x86_64软件包,可通过yum install kernel-devel -y安装。。 tar zxf keepalived-1.1.19.tar.gz cd keepalived-1.1.19 ./configure ##结束后最好由3个Yes make make install
3.配置规范启动
/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ /bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived -p /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ /bin/cp /usr/local/sbin/keepalived /usr/sbin/ /etc/init.d/keepalived start或/etc/init.d/keepalived stop ps -ef | grep keepalived /etc/init.d/keepalived stop
4.在lb01和lb02上配置keepalived服务
master [root@lb01 keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 258818040@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb01 } vrrp_script chk_nginx_proxy { script "/server/scripts/check_nginx.sh" #检查nginx服务是否正常脚本,以便切换到备 interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 55 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.20.23.10/24 dev eth0 label eth0:1 } track_script { chk_nginx_proxy } } backup [root@lb02 keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 258818040@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb02 } vrrp_script chk_nginx_proxy { script "/server/scripts/check_nginx.sh" #检查nginx服务是否正常脚本,以便切换到备 interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 55 (主从要一致) priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.20.23.10/24 dev eth0 label eth0:1 } track_script { chk_nginx_proxy } }
5.监控nginx脚本
check_nginx.sh #!/bin/bash # check nginx server status NGINX=/usr/local/nginx/sbin/nginx PORT=80 nmap localhost -p $PORT | grep "$PORT/tcp open" #echo $? if [ $? -ne 0 ];then $NGINX -s stop $NGINX sleep 3 nmap localhost -p $PORT | grep "$PORT/tcp open" [ $? -ne 0 ] && /etc/init.d/keepalived stop fi
6.后端真实服务器配置web
web01 10.20.23.191 web01服务器
web02 10.20.23.192 web02服务器
7.模拟nginx故障,vip转移情况,及是否影响访问。
8.配置keepalived日志生成文件
vim /etc/sysconfig/keepalived 将#KEEPALIVED_OPTIONS="-D" 修改为 KEEPALIVED_OPTIONS="-D -d -S 0" 重启系统日志 /etc/init.d/rsyslog restart 编辑系统日志配置文件 vim /etc/rsyslog.conf 在最后一行添加 local0.* /var/log/keepalived.log 将第42行 修改为 *.info;mail.none;authpriv.none;cron.none;local0.none /var/log/message 重启keepalived /etc/init.d/keepalived restart
嗨、骚年、快来消灭0回复。