Keepaliev+Nginx+http
As a powerful open source software, Ngxin can be used as a high availability cluster service first. This blog article introduces how nginx+Keepalived implements high performance + high availability cluster services.
Environment introduction:
Hardware: 4 virtual servers
System: CentOS 7.3
Software: Keepalived, Apache, Nginx
IP and hostname
Master
Hostname: shiyan1
IP address: 172.18.17.31
Backup
Hostname: shiyan2
IP address: 172.18.17.32
Httpd1
Hostname: shiyan3
IP address: 172.18.17.33
Httpd2
Hostname: shiyan4
IP address: 172.18.17.34
Four servers initialize the configuration (the same configuration for four servers)
Turn off the firewall
[root@shiyan~] # systemctl disable firewalld [root@shiyan~] # systemctl stop firewalld [root@shiyan~] # iptables-F
Close Selinux
[root@shiyan~] # vim / etc/selinux/config SELINUX=disabled # Save and restart the system takes effect
Install softwar
Master/Backup
[root@shiyan~] # yum install keepalived httpd nginx # (EPEL source needs to be configured separately for Nginx)
Httpd1/Httpd2
[root@shiyan~] # yum install httpd
Httpd1 configuration
[root@shiyan3 ~] # mkdir-p / app/apache/html/ [root@shiyan3 ~] # chown-R apache.apache / app/apache/html/ [root@shiyan3 ~] # echo "Apache Server 1" > / app/apache/html/index.html [root@shiyan3 ~] # vim / etc/httpd/conf/httpd.conf # here is to change the content in httpd.conf Do not add content DocumentRoot "/ app/apache/html" # change to custom path # # Relax access to content within / var/www. # # change to custom path AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root: # change to custom path. [root@shiyan3 ~] # systemctl restart httpd # Test whether the website is running properly [root@yum ~] # curl http://172.18.17.33 Apache Server 1 # test is successful
Httpd2 configuration
[root@shiyan4 ~] # mkdir-p / app/apache/html/ [root@shiyan4 ~] # chown-R apache.apache / app/apache/html/ [root@shiyan4 ~] # echo "Apache Server 2" > / app/apache/html/index.html [root@shiyan4 ~] # vim / etc/httpd/conf/httpd.conf # here is to change the content in httpd.conf Do not add content DocumentRoot "/ app/apache/html" # change to custom path # # Relax access to content within / var/www. # # change to custom path AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root: # change to custom path. [root@shiyan4 ~] # systemctl restart httpd # Test whether the website is running properly [root@yum ~] # curl http://172.18.17.34 Apache Server 2 # test is successful
Master configuration
Configure Sorry-Server [root@shiyan1 ~] # mkdir-p / app/apache/html/ [root@shiyan1 ~] # chown-R apache.apache / app/apache/html/ [root@shiyan1 ~] # echo "Sorry Server 1" > / app/apache/html/index.html [root@shiyan1 ~] # vim / etc/httpd/conf/httpd.conf # this is to change the content in httpd.conf Do not add content Listen 8080 DocumentRoot "/ app/apache/html" # change to custom path # # Relax access to content within / var/www. # # change to custom path AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root: # change to custom path. [root@shiyan1 ~] # systemctl restart http # Test whether the website is running properly [root@yum ~] # curl http://172.18.17.31:8080 Sorry Server 1 # Test successfully configured Keepalived [root@shiyan1 ~] # cp / etc/keepalived/keepalived.conf { .bak} # backup file [root@shiyan1 ~] # vim / etc/keepalived/keepalived.conf global_defs {notification_email {root # define email user} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 172.18.17.31 # Define email address smtp_connect_timeout 30 router_id node1 # define Node name} vrrp_instance VI_1 {state MASTER # define Node Master Node Mode interface ens33 # Definitions use ens33 for VIP Nic virtual_router_id 51 # defines node number priority 150 # defines priority advert_int 1 authentication {auth_type PASS auth_pass 1111} virtual_ipaddress { 172.18.17.30 # definition VIP}} ~ configure Nginx service [root@shiyan1 ~] # vim / etc/nginx/nginx.conf # add nginx cluster upstream websrvs {server 172.18.17.33 vim 80 Server 172.18.17.34 backup; server 127.0.0.1 backup } # server part needs to be completely commented out [root@shiyan1 ~] # vim / etc/nginx/conf.d/default.conf server {listen 80; location / {root html Proxy_pass http://websrvs; index index.html index.htm;}} [root@shiyan1 ~] # systemctl restart nginx [root@shiyan1 ~] # systemctl restart keepalived [root@shiyan1 ~] # systemctl restart httpd
Backup configuration
Configure Sorry-Server [root@shiyan2 ~] # mkdir-p / app/apache/html/ [root@shiyan2 ~] # chown-R apache.apache / app/apache/html/ [root@shiyan2 ~] # echo "Sorry Server 2" > / app/apache/html/index.html [root@shiyan2 ~] # vim / etc/httpd/conf/httpd.conf # this is to change the content in httpd.conf Do not add content Listen 8080 DocumentRoot "/ app/apache/html" # change to custom path # # Relax access to content within / var/www. # # change to custom path AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root: # change to custom path. [root@shiyan2 ~] # systemctl restart http # Test whether the website is running properly [root@yum ~] # curl http://172.18.17.31:8080 Sorry Server 2 # Test successfully configured Keepalived [root@shiyan2 ~] # cp / etc/keepalived/keepalived.conf { .bak} # backup file [root@shiyan2 ~] # vim / etc/keepalived/keepalived.conf global_defs {notification_email {root # define incoming mail user} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 172.18.17.31 # define mail Part address smtp_connect_timeout 30 router_id node1 # define Node name} vrrp_instance VI_1 {state MASTER # define Node Master Node Mode interface ens33 # Definitions use ens33 for VIP network card Virtual_router_id 51 # define node number priority 150 # define priority advert_int 1 authentication {auth_type PASS auth_pass 1111} virtual_ipaddress { 172.18.17.30 # definition VIP}} ~ configure Nginx service [root@shiyan2 ~] # vim / etc/nginx/nginx.conf # add nginx cluster upstream websrvs {server 172.18.17.33 vim 80 Server 172.18.17.34 backup; 80; server 127.0.0.1 backup;} # server part needs to be commented out [root@shiyan2 ~] # vim / etc/nginx/conf.d/default.conf server {listen 80; location / {root html Proxy_pass http://websrvs; index index.html index.htm;}} [root@shiyan2 ~] # systemctl restart keepalived [root@shiyan2 ~] # systemctl restart nginx [root@shiyan2 ~] # systemctl restart httpd
Test environment
# by default, use rr algorithm to poll to access the back-end httpd server [root@yum ~] # curl http://172.18.17.30 Apache Server 1 [root@yum ~] # curl http://172.18.17.30 Apache Server 2 # disable the back-end http1 service In this way, you can only access the httpd2 service [root@yum ~] # curl http://172.18.17.30 Apache Server 2 [root@yum ~] # curl http://172.18.17.30 Apache Server 2 # disable the httpd service of two backend hosts In this way, because there is no back-end server, Master's sorry-server provides services [root@yum ~] # curl http://172.18.17.30 Sorry Server turns off Master testing [root@yum ~] # curl http://172.18.17.30 Sorry Server 2