实验拓扑图
(1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台nginx作前端调度服务器(一台为主机,另外一台为备机),另外两台做为真实的Web服务器(向外部提供http服务,这里仅仅使用了CentOS默认自带的http服务,没有安装其余的相似Tomcat、Jexus服务)。html
(2)本次实验设置了一个VIP(Virtual IP)为172.18.38.99,用户只须要访问这个IP地址便可得到网页服务。其中,nginx主机为172.18.38.100,备机为172.18.38.101。Web服务器A为172.18.38.200,Web服务器B为172.18.38.201。前端
1,在http语句块中定义调度规则 vim /etc/nginx/nginx.conf http { ..... upstream webser { server 172.18.38.200:80; server 172.18.38.201:80; } ..... } 2,然后在server中调用 vim /etc/nginx/conf.d/vhost.conf server { listen 172.18.38.99:80; server_name www.a.com; location / { proxy_pass http://webser; } }
2,重启nginx,使配置生效nginx
systemctl restart nginx
1,安装keepalivedweb
yum install keepalived
2,修改keepalived配置文件vim
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id proxy1 vrrp_mcast_group4 224.1.1.1 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -30 fall 2 rise 2 } vrrp_instance VI_1 { state MASTER interface ens37 virtual_router_id 66 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.18.38.99/16 } track_script { chk_nginx } } 关键配置: 1,定义nginx健康检查脚本 vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 #以秒触发一次 weight -30 #nginx宕机就立马减去有限级30 fall 2 #检查两次若是都是宕机就表示nginx挂掉了 rise 2 #宕机以后两次检查nginx是活着的就从新+30优先级 } 2,调用 track_script { chk_nginx
3,启动keepalived后端
systemctl start keepalived systemctl enable keepalived
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id proxy2 vrrp_mcast_group4 224.1.1.1 } vrrp_instance VI_1 { state BACKUP interface ens37 virtual_router_id 66 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.18.38.99/16 } }
7,启动keepalived服务器
systemctl start keepalived systemctl enable keepalived
1,安装httpd软件curl
1,安装httpd软件 yum install http 2,启动服务 systemctl start httpd systemctl enable httpd 2,生成web页面, A主机 echo web_server_A > /var/www/html/index.html b主机 echo web_server_B > /var/www/html/index.html
1,不当任何服务ide
[root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done web_server_B web_server_B web_server_A web_server_A web_server_B web_server_B web_server_A web_server_A web_server_B web_server_B
2,宕一台keepalived服务测试
[root@ke_nginx_S ~]# systemctl stop keepalived.service [root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done web_server_B web_server_B web_server_A web_server_A web_server_B web_server_B web_server_A web_server_A web_server_B web_server_A
3,宕掉nginx_master服务器
[root@ke_nginx-M ~]# systemctl stop nginx [root@testsrvr ~]# for i in {1..100};do sleep 0.5;curl 172.18.38.99;done web_server_A web_server_A web_server_B web_server_B web_server_A curl: (7) couldn't connect to host curl: (7) couldn't connect to host curl: (7) couldn't connect to host curl: (7) couldn't connect to host web_server_B web_server_B web_server_A web_server_A