【配置keepalived实现Nginx负载均衡】

一、基于 CentOS 构建 nginx+Keepalived 高可用群集。

注:nginx本身提供健康检测(负载调度器),而LVS的健康检测由keepalived实现
准备环境:
【配置keepalived实现Nginx负载均衡】_第1张图片
步骤:
1.安装Nginx,配置nginx负载调度器,去配置upstream池用于绑定后台web服务器

Manager192.168.131.107和backup192.168.131.108都要配置:
[root@manager ~]# mv /etc/nginx/conf.d/default.conf{,.bak}
[root@manager ~]# vim /etc/nginx/conf.d/vhost.conf
upstream wwwpools {
	server 192.168.131.109 weight=1;
	server 192.168.131.207 weight=1;
}
server {
    listen       80;
    server_name  nginx.org;
	location / {
		index index.html index.htm;
		proxy_pass http://wwwpools;
		proxy_set_header X-Forwarded-For $remote_addr;
	}
}

2.keepalived主从上都安装keepalived,并且保证都能启动服务。编辑keepalived配置文件,配置vip

192.168.131.107MASTER:配置vip和nginx负载挂的重启脚本
[root@manager ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL1
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx {
	script "/etc/keepalived/check_nginx.sh"
	interval 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
	track_script {
		check_nginx
	}
    virtual_ipaddress {
        192.168.131.18
    }
}
[root@manager ~]# cat /etc/keepalived/check_nginx.sh 
#!/bin/bash
#*************************************************************
#Author: pyy
#Date:  2020-08-11
#FileName:      check_nginx.sh
#*************************************************************
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]
then
	systemctl start nginx
	sleep 3
	if [ `ps -C nginx --no-header | sc -l` -eq 0 ]
	then
		systemctl stop keepalived
	fi
fi
[root@manager ~]# chmod +x /etc/keepalived/check_nginx.sh

192.168.131.1081.108BACKUP:
[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL1
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_nginx {
	script "/etc/keepalived/check_nginx.sh"
	interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
	track_script {
		check_nginx
	}
    virtual_ipaddress {
        192.168.131.18
    }
}
192.168.131.108查看nginx服务状态的脚本和MASTER一致。

3.RS1和RS2安装httpd,配置后台web服务器

[root@RS1 ~]# yum install httpd -y     
[root@RS1 ~]# echo "`hostname -I` web" > /var/www/html/index.html

4.RS1和RS2启动httpd,keepalived主从启动keepalived,负载调度器nginx

[root@RS1 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl restart httpd
[root@manager ~]# systemctl restart nginx
[root@backup ~]# systemctl restart nginx
[root@manager ~]# systemctl restart keepalived
[root@backup ~]# systemctl restart keepalived

5.测试:在一台客户机上测试192.168.131.106,访问vip192.168.131.18

配置客户机的hosts文件:
[root@node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 
192.168.131.18 nginx.org
[root@node1 ~]# for((i=1;i<=4;i++));do curl nginx.org;done
192.168.131.207  web 
192.168.131.109  web 
192.168.131.207  web 
192.168.131.109  web

1)测试当keepalived中有一个挂了之后,观察主的资源vip是否转移到备

192.168.131.107主MASTER:
[root@manager ~]# systemctl stop keepalived
[root@manager ~]# ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d0:85:02 brd ff:ff:ff:ff:ff:ff
    inet 192.168.131.107/24 brd 192.168.131.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever

192.168.131.108从BACKUP:
[root@backup ~]# ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:6b:42:87 brd ff:ff:ff:ff:ff:ff
    inet 192.168.131.108/24 brd 192.168.131.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.131.18/32 scope global ens33
       valid_lft forever preferred_lft forever

web服务器正常访问:
[root@node1 ~]# for((i=1;i<=4;i++));do curl nginx.org;done
192.168.131.207  web 
192.168.131.109  web 
192.168.131.207  web 
192.168.131.109  web

2)测试当RS中有一个挂了之后,观察资源是否跳转到另一个web服务器(nginx负载调度器本身就有健康检测的功能)

RS1 192.168.131.109:
[root@RS1 ~]# systemctl stop httpd

客户机192.168.131.106再次访问vip 192.168.131.18

[root@node1 ~]# for((i=1;i<=4;i++));do curl nginx.org;done
192.168.131.207   web 
192.168.131.207   web 
192.168.131.207   web 
192.168.131.207   web

你可能感兴趣的:(【配置keepalived实现Nginx负载均衡】)