Set up nginx high-availability server Load balancer cluster instances
I. nginx high-availability cluster topology architecture
Node1: 17216.100.67
Node2: 170.16.100.68
VIP: 172.16.100.89
RS1: 172.16.100.6
Rs2: 172.16.100.69
Note: The Kernel Parameters arp_ignore and arp_announce of all Rs in nginx high-availability cluster architecture are 0.
Ii. nginx high-availability cluster setup steps
1. Install the nginx service on all node nodes (172.16.100.67, 172.16.100.68.
[Email protected] ~] Yum install nginx
[Email protected] ~] Yum install nginx
2. Configure nginx as a reverse proxy, and send the RS request to the node.
On the node, edit the node configuration file.
[Email protected] ~] CD/etc/nginx
[Email protected] ~] Vim nginx. conf
The following is the newly added upstream and location/code.
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. After editing the nginx configuration file, check whether the nginx configuration file has a syntax error.
[Email protected] ~] Nginx-T
Check that nginx has no syntax error.
4. Start the nginx service on node1.
[Email protected] ~] Systemctl start nginx. Service
5. Check whether nginx listening on node1 (172.16.100.6) is on port 80.
[Email protected] ~] Netstat-tnlp
Note: When starting nginx, you need to stop the HTTPd service. It also prevents the HTTPd service from starting itself. You can use systemctl disabled httpd. Service
[Email protected] ~] Systemctl disabled httpd. Service
6. Check node1 (172.16.100.67) in the browser to see if Server Load balancer is complete.
At this time, you can refresh RS1 and rs2 to switch back and forth.
7. Configure the nginx configuration file for node2 nodes.
Repeat the above 1 ~ Step 6.
The configuration file can be copied directly from node1 to node2.
[Email protected] ~] SCP nginx. conf node2:/etc/nginx/
8. Install the keepalived service on node1.
Modify the keepalived. conf configuration file on node1 (172.16.100.67.
! Configuration file for keepalived
Global_defs {
Notification_email {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 {# Check whether the nginx service is online
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 command to generate a random number
}
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. Install the keepalived service on node2.
Modify the keepalived. conf configuration file on node2 (172.16.100.68.
! Configuration file for keepalived
Global_defs {
Notification_email {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 {# Check whether the nginx service is online
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 command to generate a random number
}
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. Edit the y script for the two node nodes.
#! /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)
Y backup
Systemctl restart nginx. Service
Exit 0
;;
Fault)
Notify fault
Exit
;;
*)
Echo 'usage:basename $0
{Master | Backup | fault }'
Exit 1
;;
Esac
Start the nginx service on two nodes.
[Email protected] ~] Systemctl start keepalived. Service; SSH node2 'systemctl start keepalived. Service'
11. Use systemctl status keepalived. Service to check whether keepalived is normal.
[Email protected] ~] Systemctl status keepalived. Service
12. Stop a node 1 (master) nginx service to check whether high availability is feasible.
You can use the browser to check the VIP (172.16.100.179) to check whether RS1 and rs2 can be accessed.
Iii. nginx dual-Master Model.
1. Topology
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. Steps are the same as those in step 2. The difference is the keepalived. conf file and the notify. Sh script.
Modify the keepalived. conf configuration file on node1 (172.16.100.67.
! Configuration file for keepalived
Global_defs {
Notification_email {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 {# Check whether the nginx service is online
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 command to generate a random number
}
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 command to generate a random number
}
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"
}
Slave y script of node1
#! /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)
Y backup
Exit 0
;;
Fault)
Notify fault
Exit
;;
*)
Echo 'usage:basename $0
{Master | Backup | fault }'
Exit 1
;;
Esac
3. Modify the keepalived. conf configuration file and the notify. Sh script on node2.
Modify the keepalived. conf configuration file on node2 (172.16.100.68.
! Configuration file for keepalived
Global_defs {
Notification_email {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 {# Check whether the nginx service is online
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 command to generate a random number
}
Virtual_ipaddress {
172.16.100.179/16 Dev eno16777736 label eno16777736: 1
}
Track_script {
Chk_nginx
}
Vrrp_instance vi_2 {
Interface eno16777736
State master
Priority100
Virtual_router_id 61
Garp_master_delay 1
Advert_int 1
Authentication {
Auth_type pass
Auth_pass qaz1da37af98 # OpenSSL rand-hex 6 command to generate a random number
}
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"
}
Slave y script of node1
#! /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)
Y backup
Exit 0
;;
Fault)
Notify fault
Exit
;;
*)
Echo 'usage:basename $0
{Master | Backup | fault }'
Exit 1
;;
Esac
Set up nginx high-availability server Load balancer cluster instances