实验环境:
两台apache:
1)192.168.2.130
2)192.168.2.132
两台nginx+keepalived:
1)192.168.1.101(连接外网) + 192.168.2.133(内网,与apache通信)
2)192.168.1.102(连接外网) + 192.168.2.128(内网,与apache通信)
从后往前配置,由内而外配置
1. 配置httpd
随意,yum、rpm、编译安装都可
直接修改/var/www/html/index.html
2. nginx反向代理apache
两台主机配置方法相同,参数完全一致
# vim /etc/nginx/nginx.conf http{ upstream websrvs { #设置upstream模块 server 192.168.2.132:80 weight=2; server 192.168.2.130:80 weight=1; } } # vim /etc/nginx/conf.d/web.conf server { listen 99; 设置nginx监听端口 server_name www.go.com; add_header X-Via $server_addr; proxy_set_header X-Real-IP $remote_addr; location / { proxy_pass 将代理发往websrvs的请求 index index.html; #health_check interval=2 fails=3 uri=/.health.html; 健康监测 }
3. 配置keepalived
yum 安装
! Configuration File for keepalived!全局配置段global_defs { notification_email { root@localhost } notification_email_from kaadmin@lh.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 路由器 1 vrrp_mcast_group4 224.0.100.5}vrrp_script chk_down { 通过创建down文件的方式用脚本控制keepalived启动 script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -5} vrrp_script chk_nginx { 通过检测nginx的启动情况来分配任务 script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -5 优先级降低,切换成另一台keepaliced主机工作 }##!虚拟路由器vrrp_instance VI_1 { 路由器1 state MASTER interface eth2 绑定为当前虚拟路由器使用的物理接口 virtual_router_id 110 当前虚拟路由器的惟一标识,范围是0-255; priority 100 #通告时间间隔 advert_int 1 vrrp通告的时间间隔; authentication { auth_type PASS auth_pass A3vJivTm } virtual_ipaddress { VIP要设置成可与外网通信,下面同理 192.168.1.120 dev eth2 label eth2:0 }#调用上文脚本 track_script { 上面定义的脚本要在此处调用,不然无效 chk_down chk_nginx } 通知脚本 notufy_master "/etc/keepalived/notify.sh master" 当前节点成为主节点时触发的脚本; notufy_backup "/etc/keepalived/notify.sh backup" 当前节点成为备节点时触发的脚本; notufy_fault "/etc/keepalived/notify.sh fault" 当前节点转为失败时触发的脚本;}vrrp_instance VI_2{ 路由器2 state BACKUP interface eth2 VIP绑定的网卡 virtual_router_id 120 priority 98 #通告时间间隔 advert_int 1 authentication { auth_type PASS auth_pass A3vJivTm } virtual_ipaddress { 192.168.1.180 dev eth2 label eth2:1 }#调用上文脚本 track_script { chk_down chk_nginx } notufy_master "/etc/keepalived/notify.sh master" notufy_backup "/etc/keepalived/notify.sh backup" notufy_fault "/etc/keepalived/notify.sh fault"}
另一台keepalived需要修改:
1.将优先级和主备调换
2.虚拟路由器绑定的网卡根据本机情况更改
3.VIP自定随意
顺序和安装顺序最好一致,省的麻烦
apache--->nginx--->keepalived