Nginx Keepalived

1、整体架构图如下

2021-05-25-13-50-07-713546.png

2、环境准备

配置keepalived+nginx 的负载均衡
下载keepalived软件

  1. [root@LB01 tools]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz

注意安装前检查内核的link文件

  1. root@LB02 tools]# ll /usr/src/
  2. total 8
  3. drwxr-xr-x. 2 root root 4096 Sep 23 2011 debug
  4. drwxr-xr-x. 3 root root 4096 Oct 19 02:03 kernels
  5. lrwxrwxrwx. 1 root root 43 Oct 19 02:05 linux -> /usr/src/kernels/2.6.32-642.6.1.el6.x86_64/

安装keepalived之前,安装几个依赖包

  1. yum install openssl-devel -y
  2. yum install popt* -y

然后进行编译安装keepalived
./configure得出下面的结果

  1. Keepalived configuration
  2. ------------------------
  3. Keepalived version : 1.1.17
  4. Compiler : gcc
  5. Compiler : -g -O2
  6. Extra Lib : -lpopt -lssl -lcrypto
  7. Use IPVS Framework : Yes
  8. IPVS sync daemon support : Yes
  9. Use VRRP Framework : Yes
  10. Use LinkWatch : No
  11. Use Debug flags : No

注意./configure之后的结果,没有错误就可以了

  1. make && make install

之后规范配置、启动文件路径

  1. /bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
  2. /bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  3. mkdir /etc/keepalived -p
  4. /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
  5. /bin/cp /usr/local/sbin/keepalived /usr/sbin/
  6. /etc/init.d/keepalived start

3、配置keepalived

  1. [root@LB01 keepalived]# vi keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. abc@qq.com
  6. }
  7. notification_email_from Alexandre.Cassen@firewall.loc
  8. smtp_server 1.1.1.1
  9. smtp_connect_timeout 30
  10. router_id LVS_3
  11. }
  12. vrrp_instance VI_1 {
  13. state MASTER
  14. interface eth0
  15. virtual_router_id 19
  16. priority 150
  17. advert_int 1
  18. authentication {
  19. auth_type PASS
  20. auth_pass 1111
  21. }
  22. virtual_ipaddress {
  23. 192.168.1.254/24
  24. }
  25. }
  26. [root@LB02 keepalived]# vi keepalived.conf
  27. ! Configuration File for keepalived
  28. global_defs {
  29. notification_email {
  30. abc@qq.com
  31. }
  32. notification_email_from Alexandre.Cassen@firewall.loc
  33. smtp_server 1.1.1.1
  34. smtp_connect_timeout 30
  35. router_id LVS_6
  36. }
  37. vrrp_instance VI_1 {
  38. state BACKUP
  39. interface eth0
  40. virtual_router_id 19
  41. priority 100
  42. advert_int 1
  43. authentication {
  44. auth_type PASS
  45. auth_pass 1111
  46. }
  47. virtual_ipaddress {
  48. 192.168.1.254/24
  49. }
  50. }
  51. [root@LB01 keepalived]# /etc/init.d/keepalived start
  52. Starting keepalived: [ OK ]
  53. [root@LB02 keepalived]# /etc/init.d/keepalived start
  54. Starting keepalived [ OK ]
  55. [root@LB01 keepalived]# ip add|grep 192.168.1.254
  56. inet 192.168.1.254/24 scope global secondary eth0
  57. [root@LB02 keepalived]# ip add|grep 192.168.1.254

测试访问发现可以正常切换
现在模拟keepalived主宕机,再测试

  1. [root@LB01 conf]# /etc/init.d/keepalived stop
  2. Stopping keepalived: [ OK ]
  3. [root@LB02 ~]# ip add|grep 254
  4. inet 192.168.1.254/24 scope global secondary eth0

4、反向代理服务故障自动切换

如果实际生产环境中当keeplived主的服务器Nginx服务宕机,但是主又有VIP,这时就出现无法访问的现象,因此可以做如下的配置,使得这种情况可自已切换

  1. vi check_nginx.sh
  2. #!/bin/sh
  3. white true
  4. do
  5. PNUM=`ps -ef|grep nginx|wc -l`
  6. # 这里也可使用nmap 192.168.1.3 -p 80|grep open|wc -l来判断个数
  7. if [ $PNUM -lt 3 ];then
  8. /etc/init.d/keepalived stop >/dec/null 2>&1
  9. kill -9 keealived >/dec/null 2>&1
  10. kill -9 keealived >/dec/null 2>&1
  11. fi
  12. sleep 5
  13. done
  14. sh check_nginx.sh &

启动个守护进程进行检查(或者加入定时任务定时执行检查),如果Nginx服务出现故障,就立马停掉keepalived的服务,让它自动切换到备节点上去,这样就实现了自动切换的工作。