一般情况下,如果我们做小型项目,前端用一个nginx做反向代理即可,大概是这样的
keepalived - 图1

但是,作为互联网项目,纯2C的话必然需要做高可用,不仅后端的Server有N个,Nginx同样需要有N个,一主N备,当有一个服务器挂掉的时候,服务能瞬间切换到其他服务器,大概是这样的
keepalived - 图2

下面就以上图为例,说明一下如何实现server的高可用。

1.环境准备

名称 IP 虚拟IP 操作系统
虚拟机1(VM1) 192.168.127.134 192.168.127.200 centos7.6
虚拟机2(VM2) 192.168.127.135 192.168.127.200 centos7.6
  1. systemctl stop firewalld && systemctl disable firewalld
  2. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

2.修改欢迎页面

192.168.136.134:192.168.136.134
192.168.136.135:192.168.136.135

3.安装keepalived

yum -y install keepalived
如果找不到安装到哪儿了,可以使用whereis nginx查看,这里不再赘述

  1. yum install -y keepalived
  2. (截至本篇日志,keepalived已经更新至1.3.5版本)
  3. 指定版本下载:
  4. yum install -y openssl*
  5. yum install -y openssl-devel
  6. wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
  7. tar -zxvf keepalived-1.3.5.tar.gz
  8. ./configure --prefix=/home/wangpl/mine/soft/keepalived-1.3.5
  9. make && make install
  10. 查看keepalived版本信息:
  11. keepalived -version


4.修改keepalived的配置文件

主nginx

  1. 默认配置文件:/etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. # notification_email {
  5. # acassen@firewall.loc
  6. # failover@firewall.loc
  7. # sysadmin@firewall.loc
  8. # }
  9. # notification_email_from Alexandre.Cassen@firewall.loc
  10. # smtp_server 192.168.200.1
  11. # smtp_connect_timeout 30
  12. router_id LVS_DEVEL
  13. # vrrp_skip_check_adv_addr
  14. # vrrp_strict
  15. # vrrp_garp_interval 0
  16. # vrrp_gna_interval 0
  17. }
  18. vrrp_script chk_nginx {
  19. script "/etc/keepalived/nginx_check.sh"
  20. interval 2
  21. weight -20
  22. }
  23. vrrp_instance VI_1 {
  24. state MASTER # 标识为主服务
  25. interface ens33 #绑定虚拟机的IP
  26. virtual_router_id 51 # 虚拟路由id,和从机保持一致
  27. priority 100 #权重,需要高于从机
  28. mcast_src_ip 192.168.127.134 ## 本机 IP 地址
  29. advert_int 1
  30. authentication {
  31. auth_type PASS
  32. auth_pass 1111
  33. }
  34. track_script {
  35. chk_nginx ## 执行 Nginx 监控的服务
  36. }
  37. virtual_ipaddress {
  38. 192.168.127.200 #/32 brd 255.255.255.0 dev ens33 label ens33:vip #虚拟IP地址
  39. # 192.168.200.17
  40. # 192.168.200.18
  41. }
  42. }

从nginx

  1. ! Configuration File for keepalived
  2. global_defs {
  3. # notification_email {
  4. # acassen@firewall.loc
  5. # failover@firewall.loc
  6. # sysadmin@firewall.loc
  7. # }
  8. # notification_email_from Alexandre.Cassen@firewall.loc
  9. # smtp_server 192.168.200.1
  10. # smtp_connect_timeout 30
  11. router_id dreamer1
  12. # vrrp_skip_check_adv_addr
  13. # vrrp_strict
  14. # vrrp_garp_interval 0
  15. # vrrp_gna_interval 0
  16. }
  17. vrrp_script chk_nginx {
  18. script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
  19. interval 2 ## 检测时间间隔
  20. weight -20 ## 如果条件成立,权重-20
  21. }
  22. vrrp_instance VI_1 {
  23. state BACKUP
  24. interface ens33
  25. virtual_router_id 51
  26. priority 90
  27. mcast_src_ip 192.168.127.135
  28. advert_int 1
  29. authentication {
  30. auth_type PASS
  31. auth_pass 1111
  32. }
  33. track_script {
  34. chk_nginx ## 执行 Nginx 监控的服务
  35. }
  36. virtual_ipaddress {
  37. 192.168.127.200
  38. #192.168.200.17
  39. #192.168.200.18
  40. }
  41. }

5.编写监测心跳脚本

上面配置中可以看到有一个脚本文件:/etc/keepalived/nginx_check.sh
查看nginx是否启动,如果没启动则启动,如果启动不起来,停掉keepalived服务,此时心跳断掉,服务转向另一个nginx。

  1. #!/bin/bash
  2. counter=$(ps -C nginx --no-heading|wc -l)
  3. if [ "${counter}" = "0" ]; then
  4. /home/jw/nginx/sbin/nginx
  5. sleep 2
  6. counter=$(ps -C nginx --no-heading|wc -l)
  7. if [ "${counter}" = "0" ]; then
  8. service keepalived stop
  9. fi
  10. fi

6.开启keepalived

启动 :service keepalived start
停止 :service keepalived stop
重启 :service keepalived restart
开机自启 :service enable keepalived
1.访问
image.png
查看134的网络
image.png
2.此时停掉134上的nginx,访问
image.png
3.开启134上的nginx,访问
image.png