• 欢迎访问VPS岛网站,国外VPS,国内VPS,国外服务器,国内服务器,服务器主机,测评及优惠码,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群

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

Nginx相关 乔峰磊 172次浏览 已收录 0个评论

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服务。
root@node1 ~] yum install nginx
root@node2 ~] yum install nginx

2、 配置nginx成为反向代理,并且能够让用户的请求RS代理至node节点上。
在node节点上,编辑node节点的配置文件。
root@node1 ~] cd /etc/nginx
root@node1 ~] 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配置文件有没有语法错误。
root@node1 ~] nginx -t

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

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

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

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

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

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

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

global_defs {
notification_email { linuxedu@foxmail.com
br/>linuxedu@foxmail.com<br/
mageedu@126.com
}
notification_email_from kanotify@magedu.com
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 { linuxedu@foxmail.com
br/>linuxedu@foxmail.com<br/
mageedu@126.com
}
notification_email_from kanotify@magedu.com
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=’root@localhost’

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服务。
root@node1 ~] systemctl start keepalived.service; ssh node2 ‘systemctl start keepalived.service’

11、 利用systemctl status keepalived.service查看keepalived是否正常。
root@node1 ~] 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 { linuxedu@foxmail.com
br/>linuxedu@foxmail.com<br/
mageedu@126.com
}
notification_email_from kanotify@magedu.com
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=’root@localhost’

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 { linuxedu@foxmail.com
br/>linuxedu@foxmail.com<br/
mageedu@126.com
}
notification_email_from kanotify@magedu.com
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=’root@localhost’

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


VPS岛 的文章和资源来自互联网,仅作为参考资料,如果有侵犯版权的资源请尽快联系站长,我们会在24h内删除有争议的资源。丨 转载请注明Nginx高可用负载均衡集群实例架设
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址