一、高可用介绍

用户通过虚拟IP访问Web服务,keepalived使用脚本自动检测Nginx 01的测存活状态,如果Nginx 01挂掉,自动切换至Nginx 02提供访问服务

image.png

二、高可用配置示例

环境准备:
Nginx01:192.168.10.103
Nginx02:192.168.10.104

Tomcat01:192.168.10.103
Tomcat02:192.168.10.104

192.168.10.103和192.168.10.104两台服务器都安装keepalived软件、
[root@hadoop103 ~]# yum -y install keepalived
[root@hadoop104 ~]# yum -y install keepalived
(配置文件所在位置:/etc/keepalived/keepalived.conf)

预设虚拟IP为:192.168.10.100

三、Keepalived+Nginx(主从模式)

在103/104f服务器上分别修改keepalived主配置文件:
[root@hadoop103 ~]# cd /etc/keepalived/
[root@hadoop103 keepalived]# vim /etc/keepalived/keepalived.conf

  1. global_defs {
  2. notification_email {
  3. acassen@firewall.loc
  4. failover@firewall.loc
  5. sysadmin@firewall.loc
  6. }
  7. notification_email_from Alexandre.Cassen@firewall.loc
  8. smtp_server 192.168.10.103
  9. smtp_connect_timeout 30
  10. router_id LVS_DEVEL # 访问到的主机(/etc/hosts文件里添加“127.0.0.1 LVS_DEVEL”配置行)
  11. }
  12. vrrp_script chk_http_port {
  13. script "/usr/local/src/nginx_check.sh" # 检测Nginx存活脚本文件
  14. interval 2 # 检测脚本执行的间隔
  15. weight 2
  16. }
  17. vrrp_instance VI_1 {
  18. state MASTER # 备份服务器上将 MASTER 改为 BACKUP
  19. interface ens33 # 网卡
  20. virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
  21. priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
  22. advert_int 1 # 服务器存活心跳检测,间隔1S
  23. authentication { # 权限校验方式,PASS类型/密码:PASS/1111
  24. auth_type PASS
  25. auth_pass 1111
  26. }
  27. virtual_ipaddress {
  28. 192.168.10.100 # VRRP H 虚拟地址 (虚拟IP地址)
  29. }
  30. }

在104服务器上:
[root@hadoop104 ~]# cd /etc/keepalived/
[root@hadoop104 keepalived]# vim /etc/keepalived/keepalived.conf

  1. global_defs {
  2. notification_email {
  3. acassen@firewall.loc
  4. failover@firewall.loc
  5. sysadmin@firewall.loc
  6. }
  7. notification_email_from Alexandre.Cassen@firewall.loc
  8. smtp_server 192.168.10.104
  9. smtp_connect_timeout 30
  10. router_id LVS_DEVEL # 访问到的主机(/etc/hosts文件里添加“127.0.0.1 LVS_DEVEL”配置行)
  11. }
  12. vrrp_script chk_http_port {
  13. script "/usr/local/src/nginx_check.sh" # 检测Nginx存活脚本文件
  14. interval 2 # 检测脚本执行的间隔,2s
  15. weight 2
  16. }
  17. vrrp_instance VI_1 {
  18. state MASTER # 备份服务器上将 MASTER 改为 BACKUP
  19. interface ens33 # 网卡
  20. virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
  21. priority 99 # 主、备机取不同的优先级,主机值较大,备份机值较小
  22. advert_int 1
  23. authentication {
  24. auth_type PASS
  25. auth_pass 1111
  26. }
  27. virtual_ipaddress {
  28. 192.168.10.100 # VRRP H 虚拟地址 (虚拟IP地址)
  29. }
  30. }

编写Nginx存活检测脚本:(在103、104服务器上)
[root@hadoop103 keepalived]# vim /usr/local/src/nginx_check.sh

  1. #!/bin/bash
  2. A=`ps -C nginx –no-header |wc -l`
  3. if [ $A -eq 0 ];then
  4. /usr/local/nginx/sbin/nginx
  5. sleep 2
  6. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  7. killall keepalived
  8. fi
  9. fi

104服务器上:
[root@hadoop104 keepalived]# vim /usr/local/src/nginx_check.sh

  1. #!/bin/bash
  2. A=`ps -C nginx –no-header |wc -l`
  3. if [ $A -eq 0 ];then
  4. /usr/local/nginx/sbin/nginx
  5. sleep 2
  6. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  7. killall keepalived
  8. fi
  9. fi

分别启动nginx、keepalived
[root@hadoop103 keepalived]# systemctl start keepalived.service
[root@hadoop103 keepalived]# systemctl status keepalived.service

[root@hadoop104 keepalived]# systemctl start keepalived.service
[root@hadoop104 keepalived]# systemctl status keepalived.service

浏览器访问nginx测试高可用:
在103服务器上:(将主机名和 IP 写入/usr/local/nginx/html/index.html文件)
[root@hadoop103 ~]# echo “hostname ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'“ >/usr/local/nginx/html/index.html
[root@hadoop103 ~]# cd /usr/local/nginx/sbin/
[root@hadoop103 sbin]# ./nginx -s reload

在104服务器上:(将主机名和 IP 写入/usr/local/nginx/html/index.html文件)
[root@hadoop104 ~]# echo “hostname ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'“ >/usr/local/nginx/html/index.html
[root@hadoop104 ~]# cd /usr/local/nginx/sbin/
[root@hadoop104 sbin]# ./nginx -s reload

浏览器访问虚拟IP:http://192.168.10.100/
image.png
[root@hadoop103 sbin]# ip address
image.png
将103主服务器上的 keepalived停掉,浏览器继续访问虚拟IP:http://192.168.10.100/
[root@hadoop103 sbin]# systemctl stop keepalived
[root@hadoop103 sbin]# systemctl start keepalived
image.png
[root@hadoop104 sbin]# ip address
image.png