1、Nginx+keepalived 主从配置
这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠。
2、Nginx+keepalived 双主配置
这种方案,使用两个vip地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于当前架构环境。
1、Nginx+keepalived 主从配置
lb-01:192.168.96.130 nginx+keepalived-master
lb-02:192.168.96.131 nginx+keepalived-backup
VIP:192.168.75.135
nginx+tomcat负载均衡集群参照 nginx+tomcat负载均衡集群参照
两台机器都要安装nginx 配置文件相同
upstream myServer{
server 192.168.96.130:8080;
server 192.168.96.131:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://myServer;
}
# yum install keepalived -y
查看keepalived版本
# keepalived -v
Keepalived v1.2.13 (11/20,2015)
在130master keepalived配置文件内容如下
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface eno16777736 //网卡
virtual_router_id 51 # 主、备机的virtual_router_id必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.138 // VRRP H虚拟地址
}
}
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.138
}
}
[root@lb-01 ~]# systemctl start keepalived.service
[root@lb-02 ~]# systemctl start keepalived.service
130-master机器查看
从上面可以看到虚拟VIP
地址192.168.96.138
131-backup机器查看
从上面可以看到没有虚拟VIP
地址
此时虚拟VIP可以轮询访问了
把130-master nginx和keepalived停止查看是否还能正常提供服务
[root@lb-01 sbin]# ./nginx -s stop
[root@lb-01 sbin]# systemctl stop keepalived.service
此时虚拟VIP已经不再master上了
130nginx和keepalived挂了也不影响服务
192.168.96.139
,192.168.75.138
是130机器上主虚拟VIP,192.168.96.139
是131机器上主虚拟VIP130的keepalived配置文件内容如下
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.138
}
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.139
}
}
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.138
}
}
vrrp_instance VI_2 {
state MASTER
interface eno16777736
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.139
}
}
[root@lb-01 sbin]# ./nginx [root@lb-01 sbin]# systemctl start keepalived.service130查看虚拟ip
131查看虚拟ip
访问虚拟VIP:192.168.96.139结果如下
把130服务停止
查看130虚拟VIP是否存在结果如下没了
从上面结果可以看到,即使130机器发生了故障也不影响使用,这样也利用131资源了
此时查看一下131虚拟ip结果情况如下
注:此时虚拟VIP地址都已经在lb-02机器上了