The Keepalived+nginx is available in both master and dual-active modes. Master and Standby mode to provide a VIP, while only one server to work, the other for the standby machine, the two-way external to provide two VIP, both machines for backup, the following detailed instructions to build the test steps.
Main Standby mode
Architecture diagram:
Configuration:
Host
|
Ip
|
Operating system
|
Software
|
Note
|
nginx01
|
172.27.9.91
|
Centos7
|
Nginx Port 82
Keepalived |
Shutting down firewalls and Selinu |
nginx02
|
172.27.9.92
|
Centos7 |
Nginx Port 82 Keepalived |
Shutting down firewalls and Selinu |
Web01
|
172.27.9.125
|
Centos7 |
Apache Port 1180
|
Shutting down firewalls and Selinu |
Web02
|
172.27.9.126
|
Centos7 |
Apache Port 1180
|
Shutting down firewalls and Selinu |
1.nginx Installation
NGINX01 and nginx02 installation nginx See CENTOS7 installation Nginx
2.nginx Configuration
The two Nginx servers are configured identically, as follows:
[[Email protected] ~]# more /usr/local/nginx/conf/nginx.conf#user nobody;worker _processes 1; #error_log logs/error.log; #error_log logs/error.log Notice, #error_log logs/error.log info; #pid logs/nginx.pid;events { worker_connections 1024;} http { include mime.types; default_type application/octet-stream; #log_format main ' $remote _addr - $remote _user [$time _local] "$request" " # ' $status $body _bytes_sent "$http _referer ' # "$http _user_agent" "$http _x_forwarded_for" '; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream webser{ server 172.27.9.125:1180; server 172.27.9.126:1180; } server { listen 82; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webser; #root html; #index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}[[email protected] ~]#
3.keepalived Installation
Install the keepalived on nginx01 and nginx02 respectively:
[Email protected] ~]# yum-y install keepalived
4.keepalived Configuration
NGINX01 on keepalived configuration:
[[email protected] keepalived]# more keepalived.conf! configuration file for keepalivedglobal_defs { notification_email { [Email protected] [email protected] [email protected] } notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id proxy1 }vrrp_script chk_nginx { script "/etc/keepalived/check_ Nginx.sh " interval 2 weight 20 fall 1 rise 10} Vrrp_instance vi_1 { state master interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 172.27.9.200 } track_script { chk_nginx }}
Keepalived configuration on nginx02:
[[email protected] keepalived]# more keepalived.conf! configuration file for keepalivedglobal_defs { notification_email { [Email protected] [email protected] [email protected] } notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id proxy2}vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh " interval 2 weight 20 fall 2 rise 1} Vrrp_instance vi_1 { state backup interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 172.27.9.200 } track_script { chk_nginx }}
check_nginx.sh script:
[Email protected] keepalived]# more check_nginx.sh #!/bin/basha= ' ps-c nginx--no-header |wc-l ' If [$A-eq 0];then /usr/local/nginx/sbin/nginx if [' Ps-c nginx--no-header |wc-l '-eq 0];then Pkill Keep Fifi
The script user detects if the Nginx process is present, restarts if it does not exist, and kills the keepalived process directly if the reboot fails, triggering the switchover. (If not pkill command please install first)
5.apache Installation
Install the Apache configuration on WEB01 and WEB02 separately:
[[email protected] ~]# yum-y install http[[email protected] ~]# systemctl start httpd[[email protected] ~]# systemctl ENA ble httpd[[email protected]/]# echo web01-172.27.9.125 >/var/www/html/index.html[[email protected]/]# Echo web02-172.27.9.126 >/var/www/html/index.html
Modify the Apache default port, respectively, with 80 change to 1180:
[Email protected]/]# View/etc/httpd/conf/httpd.conflisten 1180
6. Start the service
Launch two server nginx and keepalived services.
[[email protected] ~]# Nginx[[email protected] ~]# service keepalived startredirecting to/bin/systemctl start keepalived . Service
7. Highly Available tests
Page Access http://172.27.9.200:82/
VIP View:
The VIP is found on the nginx01, at which time the nginx01,nginx02 is used as a backup for the external service. Stop the keepalived service on the NGINX01 and trigger the switchover.
[[email protected] ~]# pkill keep[[email protected] ~]# ps-ef|grep keeproot 11389 2172 0 16:56 pts/0 00:00:00 grep--color=auto Keep
At this point VIP switch to nginx02, refresh page access http://172.27.9.200:82/
Discover access to the VIP or polling to access the backend Web server, at this time the external server is nginx02,nginx01 equivalent to a downtime state.
Dual Live Mode
Architecture diagram:
Configuration:
Host
|
Ip
|
Operating system
|
Software
|
Vip
|
nginx01
|
172.27.9.91
|
Centos7
|
Nginx Port 82
Keepalived |
172.27.9.200 |
nginx02
|
172.27.9.92
|
Centos7 |
Nginx Port 82 Keepalived |
172.27.9.210 |
Web01
|
172.27.9.125
|
Centos7 |
Apache Port 1180
|
/ |
Web02
|
172.27.9.126
|
Centos7 |
Apache Port 1180
|
/ |
1.keepalived Configuration
NGINX01 on keepalived configuration:
[[Email protected] ~]# more /etc/keepalived/keepalived.conf! configuration file for keepalivedglobal_defs { notification_email { [email protected] [email protected] [Email protected] } notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id proxy1 }vrrp_script chk_nginx { script "/etc/keepalived/ Check_nginx.sh " interval 2 weight 20 fall 1 rise 10}vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 172.27.9.200 } track_script { chk_nginx }}vrrp_instance VI_2 { state backup interface ens33 virtual_router_id 52 priority 90 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ ipaddress { 172.27.9.210 } track_script { chk_nginx }}
nginx02 on keepalived configuration:
[[Email protected] ~]# more /etc/keepalived/keepalived.conf! configuration file for keepalivedglobal_defs { notification_email { [email protected] [email protected] [Email protected] } notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id proxy2}vrrp_script chk_nginx { script "/etc/keepalived/check_ Nginx.sh " interval 2 weight 20 fall 2 rise 1} Vrrp_instance vi_1 { state backup interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 172.27.9.200 } track_script { chk_nginx }}vrrp_instance VI_2 { state master interface ens33 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ ipaddress { 172.27.9.210 } track_script { chk_nginx }}
After modifying the keepalived configuration, restart the keepalived service separately:
[Email protected] ~]# service keepalived restart
2.VIP View
NGINX01:
NGINX02:
3. Page access:
vip1:http://172.27.9.200:82/
vip2:http://172.27.9.210:82/
Discover Vip1 and VIP2 to access the Web server in polling mode, respectively
4. Highly Available tests
At this time vip1 on the nginx01, vip2 on the nginx02, the two servers each other to provide services for the first time. Now stop the keepalived service on the nginx01, simulate the outage and trigger the switchover.
[[email protected] ~]# pkill keep[[email protected] ~]# ps-ef|grep keeproot 13688 2172 0 17:22 pts/0 00:00:00 grep--color=auto Keep
Page access:
vip1:http://172.27.9.200:82/
vip2:http://172.27.9.210:82/
Vip1 and VIP2 are found to have access to the Web service properly. VIP View:
NGINX01:
NGINX02:
Found Vip1 drift to vip2,nginx02 take over nginx01 vip1, at this time nginx02 alone to provide services.
Summarize:
1. Primary and Standby mode to provide only one VIP, access convenient, but at the same time only one server external services;
2. Double Live mode provides two VIP, access is troublesome, but at the same time two servers provide services externally;
3. High-availability operation in both primary and Standby mode.
Keepalived+nginx+apache Main and double-live building test