實驗拓撲:html
1.在兩臺realserver上配置web服務node
2.keepalived配置nginx
master的配置web
global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 } vrrp_script chk_down { #keepalived控制腳本 script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #腳本內容 interval 2 weight -6 } vrrp_script chk_nginx { #檢測nginx服務腳本 script "killall -0 nginx && exit 0 || exit 1" #腳本內容 interval 2 weight -6 } vrrp_instance VI_1 { #配置實例 state MASTER interface eno16777736 virtual_router_id 88 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.1.88.88/16 dev eno16777736 label eno16777736:0 } track_script { #調用腳本 chk_down chk_nginx } notify_master "/etc/keepalived/notify.sh master" #定義通告腳本 notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
backup的配置:
bash
global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 } vrrp_script chk_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 2 weight -6 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 2 weight -6 } vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 88 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.1.88.88/16 dev eno16777736 label eno16777736:0 } track_script { chk_down chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
3.nginx調度器配置app
兩臺nginx調度器的配置文件tcp
#http段中的配置 http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; upstream webserver { server 10.1.68.5 weight=1 max_fails=2; server 10.1.68.6 weight=1 max_fails=2; } server { listen 80; server_name localhost; location / { index index.html index.htm; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://webserver; } } }
在兩臺調度器上同時啓動nginx和keepalived,此時VIP在nginx1主機上ide
4.測試測試
經過VIP訪問,可正常調度
spa
中止調度器1上面的nginx服務,VIP漂移至nginx2主機上
依然能夠正常調度