nginx学习(4)Nginx 配置高可用集群(主从配置)

Nginx 配置高可用集群

Nginx的高可用集群是指由两台或多台Nginx服务器组成的集群系统,通过负载均衡和故障转移等技术,实现高可用性和可伸缩性的目标。

在这种集群系统中,每个Nginx服务器都配置为主节点(master)或从节点(slave)。主节点负责处理业务逻辑,而从节点则通过心跳检查等方式与主节点保持通信,并随时准备接管业务逻辑。

当主节点出现故障时,从节点会通过心跳检查机制检测到,并立即接管主节点的业务逻辑,从而保证服务的连续性和可用性。这种集群系统可以有效解决单点故障的问题,提高系统的可用性和稳定性。

在配置Nginx的高可用集群时,需要考虑到负载均衡、故障转移、状态监控等多个方面,以保证系统的正常运行和可靠性。同时,也需要对每个服务器的硬件配置、网络环境、负载情况等进行合理的规划和调整,以保证整个集群的性能和效率。

1、准备

需要两台服务器

    server 172.31.0.99;
    server 172.31.0.100;

1.1在两台服务器安装 nginx

安装详情见 nginx学习(1): 链接: link
1.2第一台用默认的配置80端口,第二台用81端口


    server {
        listen       80;        
        #server_name  localhost;
       server_name  172.31.0.99 ;
        
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
           #proxy_pass http://myserver;
            index  index.html index.htm;
        }

nginx学习(4)Nginx 配置高可用集群(主从配置)_第1张图片


    server {
        listen       81;        
        #server_name  localhost;
       server_name  172.31.0.100 ;
        
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
           #proxy_pass http://myserver;
            index  index.html index.htm;
        }

nginx学习(4)Nginx 配置高可用集群(主从配置)_第2张图片

测试:

http://172.31.0.99/
nginx学习(4)Nginx 配置高可用集群(主从配置)_第3张图片

http://172.31.0.100:81/

nginx学习(4)Nginx 配置高可用集群(主从配置)_第4张图片

2、安装keepalived

在两台服务器安装keepalived

yum install keepalived -y
# 查看版本:
rpm -q -a keepalived

在这里插入图片描述
nginx学习(4)Nginx 配置高可用集群(主从配置)_第5张图片
主服务器配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_http_port {
	 script "/usr/local/src/nginx_check.sh"
	 interval 2 #(检测脚本执行的间隔)
	 weight 2
}


vrrp_instance VI_1 {
	 state MASTER # 备份服务器上将 MASTER 改为 BACKUP 
	 interface ens33 //网卡
	 virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
	 priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
	 advert_int 1
	 authentication {
		 auth_type PASS
		 auth_pass 1111
	 }
	 virtual_ipaddress {
		192.168.19.50 # VRRP H 虚拟地址(非真实物理地址)
	 } 

#vrrp_instance VI_1 {
#    state MASTER
#    interface eth0
#    virtual_router_id 51
#    priority 100
#    advert_int 1
#    authentication {
#        auth_type PASS
#        auth_pass 1111
#    }
#    virtual_ipaddress {
#        192.168.200.16
#        192.168.200.17
#        192.168.200.18
#    }
#}

virtual_server 192.168.200.100 443 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.201.100 443 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.2 1358 {
    delay_loop 6
    lb_algo rr 
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    sorry_server 192.168.200.200 1358

    real_server 192.168.200.2 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.3 1358 {
    delay_loop 3
    lb_algo rr 
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.200.4 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.5 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

从服务器配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}


vrrp_script chk_http_port {
	 script "/usr/local/src/nginx_check.sh"
	 interval 2 #(检测脚本执行的间隔)
	 weight 2

}

vrrp_instance VI_1 {
	 state BACKUP # 备份服务器上将 MASTER 改为 BACKUP 
	 interface ens33 //网卡
	 virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
	 priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
	 advert_int 1
	 authentication {
		 auth_type PASS
		 auth_pass 1111
	 }
	 virtual_ipaddress {
		192.168.19.50 # VRRP H 虚拟地址(非真实物理地址)
	 } 
}

#vrrp_instance VI_1 {
#    state MASTER
#    interface eth0
#    virtual_router_id 51
#    priority 100
#    advert_int 1
#    authentication {
#        auth_type PASS
#        auth_pass 1111
#    }
#    virtual_ipaddress {
#        192.168.200.16
#        192.168.200.17
#        192.168.200.18
#    }
#}

virtual_server 192.168.200.100 443 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.201.100 443 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.2 1358 {
    delay_loop 6
    lb_algo rr 
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    sorry_server 192.168.200.200 1358

    real_server 192.168.200.2 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.3 1358 {
    delay_loop 3
    lb_algo rr 
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.200.4 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.5 1358 {
        weight 1
        HTTP_GET {
            url { 
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url { 
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

接着重启nginx,启动keepalived
(主服务器:)
nginx学习(4)Nginx 配置高可用集群(主从配置)_第6张图片
接着重启nginx,启动keepalived
(从服务器:)

nginx学习(4)Nginx 配置高可用集群(主从配置)_第7张图片

3、访问测试:

1、使用 ip a 查看主服务器的虚拟IP,进行访问
nginx学习(4)Nginx 配置高可用集群(主从配置)_第8张图片
http://172.31.0.152/,成功访问!
nginx学习(4)Nginx 配置高可用集群(主从配置)_第9张图片
成功访问!

1、使用 ip a 查看从服务器的虚拟IP,进行访问
nginx学习(4)Nginx 配置高可用集群(主从配置)_第10张图片
http://172.31.0.151:81/

nginx学习(4)Nginx 配置高可用集群(主从配置)_第11张图片
成功访问!

你可能感兴趣的:(nginx,学习,运维)