一、ip分配
1.master:192.168.67.1
2.slave:192.168.67.3
3.httpd1:192.168.67.20
4.httpd2:192.168.67.30
5.vip:192.168.67.100
二、操作步骤
1.master配置
(1)关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
(2)配置haproxy
yum -y install haproxy
vim /etc/haproxy/haproxy.cfg
global
daemon #以 daemon 方式运行。
maxconn 256 #指定了 HAProxy 允许的最大连接数。
pidfile /var/run/haproxy/haproxy.pid #指定了 HAProxy 进程的 PID 文件存放位置。
defaults #定义了默认配置。
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in #绑定 IP 地址和端口,指定将请求转发到 default_backend 后端。
bind *:80
default_backend servers
backend servers #定义了后端
server server1 192.168.67.20:80 maxconn 32 #限制了每个服务的最大连接数 #WEB1 ip地址
server server2 192.168.67.30:80 maxconn 32 #限制了每个服务的最大连接数 #WEB2 ip地址
listen stats #定义监控页面
bind *:1080 #绑定端口1080
stats refresh 30s #每30秒更新监控数据
stats uri /stats #访问监控页面的uri
stats realm HAProxy\ Stats #监控页面的认证提示
stats auth admin:admin #监控页面的用户名和密码
配置完成后开启haproxy:
systemctl start haproxy
(3)配置keepalived
yum -y install keeplaived
vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
}
#HAProxy健康检查配置
vrrp_script chk_haproxy {
script "killall -0 haproxy" #使用killall -0检查haproxy实例是否存在,性能高于ps命令
interval 2 #脚本运行周期
weight 2 #每次检查的加权权重值
}
vrrp_instance HA_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.67.100
}
track_script {
chk_haproxy #对应的健康检查配置
}
}
配置完成后开启keepalived:
systemctl start keepalived
2、slave配置
(1)关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
(2)配置haproxy
yum -y install haproxy
vim /etc/haproxy/haproxy.cfg
global
daemon #以 daemon 方式运行。
maxconn 256 #指定了 HAProxy 允许的最大连接数。
pidfile /var/run/haproxy/haproxy.pid #指定了 HAProxy 进程的 PID 文件存放位置。
defaults #定义了默认配置。
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in #绑定 IP 地址和端口,指定将请求转发到 default_backend 后端。
bind *:80
default_backend servers
backend servers #定义了后端
server server1 192.168.67.20:80 maxconn 32 #限制了每个服务的最大连接数 #WEB1 ip地址
server server2 192.168.67.30:80 maxconn 32 #限制了每个服务的最大连接数 #WEB2 ip地址
listen stats #定义监控页面
bind *:1080 #绑定端口1080
stats refresh 30s #每30秒更新监控数据
stats uri /stats #访问监控页面的uri
stats realm HAProxy\ Stats #监控页面的认证提示
stats auth admin:admin #监控页面的用户名和密码
配置完成后开启haproxy:
systemctl start haproxy
(3)配置keepalived
yum -y install keeplaived
vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
}
#HAProxy健康检查配置
vrrp_script chk_haproxy {
script "killall -0 haproxy" #使用killall -0检查haproxy实例是否存在,性能高于ps命令
interval 2 #脚本运行周期
weight 2 #每次检查的加权权重值
}
vrrp_instance HA_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.67.100
}
track_script {
chk_haproxy #对应的健康检查配置
}
}
配置完成后开启keepalived:
systemctl start keepalived
3.httpd1配置
(1)关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
(2)配置httpd
安装:yum -y install httpd
创建测试页面:
touch /var/www/html/index.html
vi /var/www/html/index.html
输入web1 192.168.67.20
配置完成后开启httpd:systemctl start httpd
4. httpd2配置
(1)关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
(2)配置httpd
安装:yum -y install httpd
创建测试页面:
touch /var/www/html/index.html
vi /var/www/html/index.html
输入web2 192.168.67.30
配置完成后开启httpd:systemctl start httpd
5.测试
(1) 同时开启master和slave的keepalived(此时vip只存在master上),在mater上访问vip:
(2)关闭master的keepalived,slave输入ip addr发现vip已漂移,并访问vip,发现能正常访问,成功!