Nginx高可用负载均衡集群实例架设

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx高可用负载均衡集群实例架设相关的知识,希望对你有一定的参考价值。

nginx高可用负载均衡集群实例架设
一、 Nginx高可用集群拓扑架构

node1:17216.100.67
node2:170.16.100.68
VIP:172.16.100.89
RS1:172.16.100.6
RS2:172.16.100.69

注意:nginx的高可用集群架构的所有RS的内核参数arp_ignore和arp_announce都为0。

二、 Nginx的高可用集群架设步骤
1、 在所有node节点(172.16.100.67,172.16.100.68)上安装nginx服务。
[email protected] ~] yum install nginx
[email protected] ~] yum install nginx

2、 配置nginx成为反向代理,并且能够让用户的请求RS代理至node节点上。
在node节点上,编辑node节点的配置文件。
[email protected] ~] cd /etc/nginx
[email protected] ~] vim nginx.conf
以下是新添加的upstream和location /代码。
upstream websrvs {
server 172.16.100.6:80 weight=1;
server 172.16.100.69:80 weight=1;
}

server {

location / {
Proxy_pass http://websrbs/;
}

}

3、 编辑完nginx配置文件后,检查nginx配置文件有没有语法错误。
[email protected] ~] nginx -t

此时检查出nginx没有语法错误。
4、 启动node1上的nginx服务。
[email protected] ~] systemctl start nginx.service

5、 查看node1(172.16.100.6)上的nginx是否监听在80端口上。
[email protected] ~] netstat –tnlp

注意:启动nginx时,需要停掉httpd服务。同时防止httpd服务开启自启动。可以使用systemctl disabled httpd.service
[email protected] ~] systemctl disabled httpd.service

6、 利用浏览器查看node1(172.16.100.67)看是否完成负载均衡。

此时刷新RS1和RS2就可以来回切换。

7、 配置node2节点的nginx配置文件。
重复上述1~6步骤。
配置文件可以直接从node1上复制到node2上。
[email protected] ~] scp nginx.conf node2:/etc/nginx/

8、 在node1节点上安装keepalived服务。
在node1(172.16.100.67)节点上修改keepalived.conf配置文件。
! Configuration File for keepalived

global_defs {
notification_email { br/>[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}

vrrp_script chk_nginx { #检查nginx服务是否在线
script “killall -0 nginx &> /dev/null”
interval 1
wetght -20
}

vrrp_instance VI_1 {
interface eno16777736
state MASTER # BACKUP for slave routers
priority 100 # 99 for BACKUP
virtual_router_id 51
garp_master_delay 1
advert_int 1
authentication {
auth_type PASS
auth_pass 2231da37af98 #openssl rand –hex 6命令生成随机数
}
virtual_ipaddress {
172.16.100.89/16 dev eno16777736 label eno16777736:1
}
track_script {
chk_nginx
}

notify_master "/etc/keepalived/notify.sh master"  
notify_backup "/etc/keepalived/notify.sh backup"  
notify_fault "/etc/keepalived/notify.sh fault"  

}

9、 在node2节点上安装keepalived服务。
在node2(172.16.100.68)节点上修改keepalived.conf配置文件。
! Configuration File for keepalived

global_defs {
notification_email {
br/>[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}

vrrp_script chk_nginx { #检查nginx服务是否在线
script “killall -0 nginx &> /dev/null”
interval 1
wetght -20
}

vrrp_instance VI_1 {
interface eno16777736
state MASTER # BACKUP for slave routers
priority 99 # 100 for MASTER
virtual_router_id 51
garp_master_delay 1
advert_int 1
authentication {
auth_type PASS
auth_pass 2231da37af98 #openssl rand –hex 6命令生成随机数
}
virtual_ipaddress {
172.16.100.89/16 dev eno16777736 label eno16777736:1
}
track_script {
chk_nginx
}

notify_master "/etc/keepalived/notify.sh master"  
notify_backup "/etc/keepalived/notify.sh backup"  
notify_fault "/etc/keepalived/notify.sh fault"  

}

10、 编辑两个node节点的notify脚本。
#!/bin/bash

vip=172.16.100.179
contact=‘[email protected]

notify() {
mailsubject="hostname to be $1: $vip floating"
mailbody="date ‘+%F %H:%M:%S‘: vrrp transition, hostname changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
master)
notify master
systemctl restart nginx.service
exit 0
;;
backup)
notify backup
systemctl restart nginx.service
exit 0
;;
fault)
notify fault
exit
;;
*)
echo ‘Usage: basename $0 {master|backup|fault}‘
exit 1
;;
esac

启动两个节点上的nginx服务。
[email protected] ~] systemctl start keepalived.service; ssh node2 ‘systemctl start keepalived.service’

11、 利用systemctl status keepalived.service查看keepalived是否正常。
[email protected] ~] systemctl status keepalived.service

12、 停掉一个node1(MASTER)的nginx服务查看高可用是否可行。
可以利用浏览器查看vip(172.16.100.179)看能否访问到RS1和RS2。

三、 Nginx的双主模型。
1、 拓扑图

node1(DIP):17216.100.67
node2(DIP):170.16.100.68
VIP1:172.16.100.179
VIP2:172.16.100.180
RS1:172.16.100.6
RS2:172.16.100.69

2、 步骤与二里面的步骤相同,不同的是keepalived.conf文件和notify.sh脚本。
在node1(172.16.100.67)节点上修改keepalived.conf配置文件。
! Configuration File for keepalived

global_defs {
notification_email {
br/>[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}

vrrp_script chk_nginx { #检查nginx服务是否在线
script “killall -0 nginx &> /dev/null”
interval 1
wetght -20
}

vrrp_instance VI_1 {
interface eno16777736
state MASTER # BACKUP for slave routers
priority 100 # 99 for BACKUP
virtual_router_id 51
garp_master_delay 1
advert_int 1
authentication {
auth_type PASS
auth_pass 2231da37af98 #openssl rand –hex 6命令生成随机数
}
virtual_ipaddress {
172.16.100.179/16 dev eno16777736 label eno16777736:1
}
track_script {
chk_nginx
}

vrrp_instance VI_2 {
interface eno16777736
state BACKUP
priority 99
virtual_router_id 61
garp_master_delay 1
advert_int 1
authentication {
auth_type PASS
auth_pass qaz1da37af98 #openssl rand –hex 6命令生成随机数
}
virtual_ipaddress {
172.16.100.180/16 dev eno16777736 label eno16777736:2
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}

node1的notify脚本
#!/bin/bash

vip=172.16.100.88
contact=‘[email protected]

notify() {
mailsubject="hostname to be $1: $vip floating"
mailbody="date ‘+%F %H:%M:%S‘: vrrp transition, hostname changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit
;;
*)
echo ‘Usage: basename $0 {master|backup|fault}‘
exit 1
;;
esac

3、 在node2上修改keepalived.conf配置文件和notify.sh脚本。
在node2(172.16.100.68)节点上修改keepalived.conf配置文件。
! Configuration File for keepalived

global_defs {
notification_email {
br/>[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}

vrrp_script chk_nginx { #检查nginx服务是否在线
script “killall -0 nginx &> /dev/null”
interval 1
wetght -20
}

vrrp_instance VI_1 {
interface eno16777736
state BACKUP
priority 99
virtual_router_id 51
garp_master_delay 1
advert_int 1
authentication {
auth_type PASS
auth_pass 2231da37af98 #openssl rand –hex 6命令生成随机数
}
virtual_ipaddress {
172.16.100.179/16 dev eno16777736 label eno16777736:1
}
track_script {
chk_nginx
}

vrrp_instance VI_2 {
interface eno16777736
state MASTER
priority 100
virtual_router_id 61
garp_master_delay 1
advert_int 1
authentication {
auth_type PASS
auth_pass qaz1da37af98 #openssl rand –hex 6命令生成随机数
}
virtual_ipaddress {
172.16.100.180/16 dev eno16777736 label eno16777736:2
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}

node1的notify脚本
#!/bin/bash

vip=172.16.100.88
contact=‘[email protected]

notify() {
mailsubject="hostname to be $1: $vip floating"
mailbody="date ‘+%F %H:%M:%S‘: vrrp transition, hostname changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit
;;
*)
echo ‘Usage: basename $0 {master|backup|fault}‘
exit 1
;;
esac





以上是关于Nginx高可用负载均衡集群实例架设的主要内容,如果未能解决你的问题,请参考以下文章

keepalived+nginx实现高可用and负载均衡集群

Eureka高可用与负载均衡

Keepalived+Nginx实现高可用负载均衡集群

nginx负载均衡nginx的高可用集群利用keepalive实现双vip

nginx+keepalived的高可用负载均衡集群构建

Keepalived+Nginx实现高可用负载均衡集群