keepalived+lvs+nat----负载均衡高可用

主服务器的配置

[root@lvs-master ~]# yum -y install keepalived ipvsadm
[root@lvs-master keepalived]# pwd
/etc/keepalived
[root@lvs-master keepalived]# cat keepalived.conf 
! configuration file for keepaslived 
global_defs {
	router_id keepalived-master
}
vrrp_instance VI_1 {
	state MASTER
	interface ens34     #桥接模式的网卡名   也就是外网的网卡
	virtual_router_id 80
	priority 100
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 111
	}
	virtual_ipaddress {
		192.168.1.120/24   
	}
}
virtual_server 192.168.1.120 80 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    protocol TCP
real_server 192.168.234.103 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
        connect_port 80
        connect_timeout 3
    }
}
real_server 192.168.234.104 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_timeout 3
    connect_port 80
    }
    }
}

重启keepalived

[root@lvs-master keepalived]# systemctl restart keepalived

备用服务器配置

[root@lvs-slave keepalived]# cat keepalived.conf 
! Configuration File for keepalived
global_defs {
    router_id lvs-keepalived-slave
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens34
    nopreempt
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.1.120/24
    }
    }

virtual_server 192.168.1.120 80 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    protocol TCP
real_server 192.168.234.103 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
        connect_port 80
        connect_timeout 3
    }
}
real_server 192.168.234.104 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_timeout 3
    connect_port 80
    }
    }
}

重启keepalived

[root@lvs-slave keepalived]# systemctl restart keepalived 

主服务器就会抢占虚拟ip 备用服务器就没有虚拟的ip
主服务器宕掉之后备用服务器会顶上去

[root@lvs-master keepalived]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens32:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:59:d0:61 brd ff:ff:ff:ff:ff:ff
    inet 192.168.234.101/24 brd 192.168.234.255 scope global ens32
       valid_lft forever preferred_lft forever
3: ens34:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:59:d0:6b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.113/24 brd 192.168.1.255 scope global dynamic ens34
       valid_lft 4191sec preferred_lft 4191sec
    inet 192.168.1.120/24 scope global secondary ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::1044:d7f2:85c8:14f8/64 scope link 
       valid_lft forever preferred_lft forever

备用服务器就没有虚拟的ip

[root@lvs-slave keepalived]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens32:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:bd:a8:fb brd ff:ff:ff:ff:ff:ff
    inet 192.168.234.102/24 brd 192.168.234.255 scope global ens32
       valid_lft forever preferred_lft forever
3: ens34:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:bd:a8:05 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.112/24 brd 192.168.1.255 scope global dynamic ens34
       valid_lft 4651sec preferred_lft 4651sec
    inet6 fe80::204a:b35:429e:9191/64 scope link 
       valid_lft forever preferred_lft forever

网页服务器配置好

[root@nginx-server1 ]# yum -y install nginx
[root@nginx-server1 ]# route add default gw 192.168.234.101 dev ens32
[root@nginx-server1 ]# ip r 
default via 192.168.234.101 dev ens32 
default via 192.168.234.2 dev ens32 proto static metric 100 
192.168.234.0/24 dev ens32 proto kernel scope link src 192.168.234.103 metric 100 
[root@nginx-server1 ~]# echo nginx-server1 >/usr/share/nginx/html/index.html 
[root@nginx-server1 ~]# systemctl restart nginx 

网页服务器2

[root@nginx-server2 nginx]# yum -y install nginx
[root@nginx-server2 html]# route add default gw 192.168.234.101 dev ens32
[root@nginx-server2 html]# ip r 
default via 192.168.234.101 dev ens32 
default via 192.168.234.2 dev ens32 proto static metric 100 
192.168.234.0/24 dev ens32 proto kernel scope link src 192.168.234.103 metric 100 
[root@nginx-server2 ~]# echo nginx-server1 >/usr/share/nginx/html/index.html 
[root@nginx-server2 ~]# systemctl restart nginx 

为了实验效果,nginx的长连接改为0

keepalived+lvs+nat----负载均衡高可用_第1张图片
keepalived+lvs+nat----负载均衡高可用_第2张图片

你可能感兴趣的:(负载均衡)