LNMP+Tomcat+Keepalived+LVS 多端口负载均衡

摘要:应用场景:Keepalived+LVS高可+负载均衡集群架构中rs同时跑httpd(80端口)和Java(8080端口)服务,使用Keepalived负载均衡多个端口。 如果想全部由80端口提供服务,则Keepalived+LVS负载均衡层就只开启80端口,内部节点使用nginx代理8080端口。 多端口负载均衡

应用场景:Keepalived+LVS高可+负载均衡集群架构中rs同时跑httpd(80端口)和Java(8080端口)服务,使用Keepalived负载均衡多个端口。 如果想全部由80端口提供服务,则Keepalived+LVS负载均衡层就只开启80端口,内部节点使用nginx代理8080端口。

多端口负载均衡

timg.jpg

[root@director sbin]# yum install -y keepalived [root@Centos-1 sbin]# 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 } } }

在dir2上执行同样的操作,lvs多端口负载均衡,需要注意的是dir2机器上state改为BACKUP,priority设为90。

nginx反向代理Tomcat

在rs上创建nginx代理虚拟主机,名为zrlog.conf,然后配置内容如下:

[root@128rs2 ~]# vim /usr/local/nginx/conf/vhosts/zrlog.conf server { listen 80; server_name www.3zuzrlog.com; location / { proxy_pass http://localhost:8080; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

此时Keepalived+LVS只需监听80端口即可:

[root@Centos-1 sbin]# 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 } } }