先來感覺一下若是分開提供服務,不作反向代理的keepalived配置文件:(ps這樣配置多不要驚慌)nginx
vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state BACKUP interface ens192 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass password } virtual_ipaddress { 192.168.14.13 } } virtual_server 192.168.14.13 80 { delay_loop 10 lb_algo wlc lb_kind DR persistence_timeout 180 protocol TCP real_server 192.168.14.127 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.14.128 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.14.129 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } virtual_server 192.168.14.13 8080 { delay_loop 10 lb_algo wlc lb_kind DR persistence_timeout 180 protocol TCP real_server 192.168.14.127 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.14.128 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.14.129 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
好了解釋一下,這裏不止給80端口作了負載均衡,後面加入了8080端口的負載均衡,確定不提倡這麼作,可是若是作了別忘了機器2的state改成BACKUP,priority設爲90web
如今講負載均衡應用場景:vim
Keepalived+LVS高可+負載均衡集羣架構中rs同時跑httpd(80端口)和Java(8080端口)服務,使用Keepalived負載均衡多個端口。 若是想所有由80端口提供服務,則Keepalived+LVS負載均衡層就只開啓80端口,內部節點使用nginx代理8080端口,那麼先作反向代理8080。服務器
#在rs上建立nginx代理虛擬主機,名爲zrlog.conf,而後配置內容以下: vim /usr/local/nginx/conf/vhosts/zrlog.conf server { listen 80; server_name www.3zuzrlog.com; #定義域名(通常和被代理ip的域名保持一致) location / { proxy_pass http://localhost:8080; #指定被代理(被訪問)的IP(web服務器IP) proxy_redirect default; proxy_set_header Host $host; #$host指的是代理服務器的servername(也是被代理IP的域名) proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #說明: 由於該虛擬主機只用做代理服務器,不須要訪問本地文件,因此不須要設置根目錄。
好了,此時Keepalived+LVS只需監聽80端口便可:架構
vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state BACKUP interface ens192 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass password } virtual_ipaddress { 192.168.14.13 } } virtual_server 192.168.14.13 80 { delay_loop 10 lb_algo wlc lb_kind DR persistence_timeout 180 protocol TCP real_server 192.168.14.127 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.14.128 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.14.129 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }