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節點上。
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沒有語法錯誤。
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 { [email protected]
br/>[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 { [email protected]
br/>[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=‘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 { [email protected]
br/>[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=‘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 { [email protected]
br/>[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=‘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高可用負載均衡集群實例架設