码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-10-27 21:45:40      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:interval   用户   emctl   注意   切换   cat   host   监听   节点   

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 { br/>linuxedu@foxmail.com
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 {
br/>linuxedu@foxmail.com
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 {
br/>linuxedu@foxmail.com
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 {
br/>linuxedu@foxmail.com
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

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

标签:interval   用户   emctl   注意   切换   cat   host   监听   节点   

原文地址:http://blog.51cto.com/14044882/2309786

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!