1、实现haproxy+keepalived高可用集群
1.1 部署规划:
192.168.37.37:haproxy+keepalived 服务器1
192.168.37.47:haproxy+keepalived 服务器2
192.168.37.57:web 服务器1
192.168.37.67:web 服务器2
1.2 web服务器安装配置
1.2.1 web1
安装nginx:yum install nginx -y
修改页面:echo "web1-192.168.37.57" > /usr/share/nginx/html/index.html
启动:nginx
1.2.2 web2
安装nginx:yum install nginx -y
修改页面:echo "web2-192.168.37.67" > /usr/share/nginx/html/index.html
启动:nginx
1.3 keepalived安装配置
1.3.1 ka1(主服务器配置)
安装:yum install keepalived -y
配置:vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/32 dev eth0 label eth0:1
}
}
启动:systemctl start keepalived
1.3.2 ka2(从服务器配置)
keepalived配置文件:
state BACKUP
interface eth0
virtual_router_id 55
priority 80
其他与主服务器相同
1.4 haproxy安装配置
yum安装:yum install haproxy -y
配置(两台服务器配置相同):
vim /etc/haproxy/haproxy.cfg
listen web_prot_80
bind 192.168.37.100:80
mode http
balance static-rr
server web1 192.168.37.57:80 weight 1 check port 80 inter 3s fall 2 rise 5
server web2 192.168.37.67:80 weight 1 check port 80 inter 3s fall 2 rise 5
1.5 测试haproxy负载均衡
1.6 测试keepalived
主服务器:ka1
从服务器:ka2
ka1宕机后
VIP飘到ka2
2、实现LVS+keepalived高可用集群
2.1 部署规划:
192.168.0.104:客户端client
192.168.37.3及192.168.0.113:路由
192.168.37.37:LVS1+keepalived 服务器1
192.168.37.47:LVS2+keepalived 服务器2
192.168.37.57:web 服务器1
192.168.37.67:web 服务器2
2.2 web服务器安装配置
2.2.1 web1
安装nginx:yum install nginx -y
修改页面:echo "web1-192.168.37.57" > /usr/share/nginx/html/index.html
启动:nginx
修改网关:192.168.37.3 --重启网络:systemctl restart network
2.2.2 web2
安装nginx:yum install nginx -y
修改页面:echo "web2-192.168.37.67" > /usr/share/nginx/html/index.html
启动:nginx
修改网关:192.168.37.3--重启网络:systemctl restart network
2.2.3 绑定VIP
脚本:
执行脚本:bash lvs_dr_rs.sh start
2.3 LVS1 + keepalived安装配置
2.3.1 ka1(主服务器配置)
安装:yum install keepalived ipvsadm -y
配置keepalived: vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.37.100/32 dev eth0 label eth0:1
}
}
virtual_server 192.168.37.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.37.57 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.67 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
启动:systemctl start keepalived
查看虚拟服务列表:
安装nginx:yum install nginx -y
配置sorry server:echo sorry server1> /usr/share/nginx/html/index.html
2.3.2 ka2(从服务器配置)
keepalived配置文件:
state BACKUP
interface eth0
virtual_router_id 55
priority 80
安装nginx:yum install nginx -y
配置sorry server:echo sorry server2> /usr/share/nginx/html/index.html
其他与主服务器相同
2.4 其他配置
① 192.168.0.104:客户端client
网关:GATEWAY=192.168.0.113
② 192.168.37.3及192.168.0.113:路由
修改内核参数:vim /etc/sysctl.conf
net.ipv4.ip_forward=1 #启动路由转发
生效:sysctl -p