單主模型
環境四個虛擬機
upstream server:node1:172.16.100.6 ? ? ? ? CeotOS 6
upstream server:node3:172.16.100.69 ? ? ? ? CeotOS 7
節點1:172.16.100.67 ? ? ? ? ? ? ? ? ? ? ? ?CeotOS 7
節點2:node2:172.16.100.68 ? ? ? ? ? ? ? ? ? ? ??CeotOS 7
節1&2
# yum install nginx -y
# cd /etc/nginx
# vim nginx.conf
? ? ? ? ? ? ? ? ? ? ? include /etc/nginx/conf.d/*.conf;
? ? ? ? ? ? ? ? ? ? ? upstream webservers {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? server 172.16.100.6:80 weight=1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? server 172.16.100.69:80 weight=1;
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?location ? / ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?proxy_pass ? http://websrvers/;
? ? ? ? ? ? ? ? ? ? ?}
# nginx ? -t ?
# systemctl start nginx.service
# netstat -tnlp
用瀏覽器訪問172.16.100.67
# scp nginx.conf node2:/etc/nginx/
節2
# systemctl start nginx.service
# netstat -tnlp
用瀏覽器訪問172.16.100.68看能否負載均衡
- 用keepalived監控nginx服務
節1&節2
# vim keepalived.conf
vrrp_script chk_nginx {
script "killall -0 nginx &> /dev/null"
interval 1
weight -10
}
vrrp_instance VI_1 {
track_script {
chk_nginx
}
}
節1
# systemctl status nginx.service
# systemctl start keepalived.service ssh node2:systemctl start keepalived.service
# systemctl status keepalived.service
# ip addr list
如果沒啟動起來,可能是組播地址和虛擬地址的問題,修改一下即可
宕掉nginx,肯是否會轉到節點2上
#?systemctl stop nginx.service
#?systemctl status nginx
節2
# ip addr list
節1
#?systemctl start nginx.service
瀏覽器訪問172.16.100.88
節1(主節點)只要在線,節2(備節點)就搶不走,除非節1宕掉 # vim notify #!/bin/bash # vip=172.16.100.88 contact='root@localhost' notify() { mailsubject="`hostname` to be $1: $vip floating"??? mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"??? echo $mailbody | mail -s "$mailsubject" $contact } case "$1" in master) ? notify master systemctl restart nginx.service exit 0 ;; backup) notify backup systemctl restart nginx.service exit 0 ;; fault) notify fault exit 0 ;; *) echo 'Usage: `basename $0` {master|backup|fault}' exit 1 ;; esac
# scp notify.sh node2:/etc/keepalived/
# systemctl status keepalived
# ip addr list
主停掉,備也停掉,會自動啟動nginx
# systemctl stop httpd.server
節2
# systemctl stop nginx.service
# systemctl status keepalived
節1
# systemctl status nginx.service
# systemctl status keepalived
雙主模型
節1
# vim keepalived.conf
vrrp_instance VI_2 {
state BACKUP
interface ens33 virtual_router_id 61 priority 99 advert_int 1 authentication { auth_type PASS
auth_pass 1111
} virtual_ipaddress { 172.16.100.180/16 dev ens33 label ens33:2 }
track_script { chk_nginx } notify_master “/etc/keepalived/notify.sh master” (不手動啟動nginx,而讓監控系統確保nginx在線) notify_backup “/etc/keepalived/notify.sh backup” notify_fault “/etc/keepalived/notify.sh fault” } 節1&節2 (不手動啟動nginx,而讓監控系統確保nginx在線) # vim notify.sh 注釋掉systemctl restart nginx.service 節2 # vim keepalived.conf
vrrp_instance VI_2 { state MASTER
interface ens33 virtual_router_id 61 priority 100 advert_int 1
authentication { auth_type PASS
auth_pass 1111
} virtual_ipaddress { 172.16.100.180/16 dev ens33 label ens33:2 }
track_script { chk_nginx } notify_master “/etc/keepalived/notify.sh master” notify_backup “/etc/keepalived/notify.sh backup” notify_fault “/etc/keepalived/notify.sh fault” } 節1 # sytemctl restart keepalived.service; ssh node2'sytemctl restart keepalived.service' # systemctl status keepalived # ifconfig ens33:1 down # sytemctl restart keepalived.service; ssh node2'sytemctl restart keepalived.service' # systemctl status keepalived節2 # systemctl status keepalived.service # ip addr list 用瀏覽器訪問兩個地址,看調度是否有問題 節1 # sytemctl restart keepalived.service; sytemctl start httpd.service # systemctl status keepalived
節2 # ifconfig 節1 # sytemctl stop httpd.service; sytemctl restart nginx.service' # systemctl status nginx.service
# systemctl status keepalived# ifconfig
原創文章,作者:nene,如若轉載,請注明出處:http://www.www58058.com/88155