2:搭建web2 服务(1):安装httpd 服务
(2):echo "111111" > /var/www/html/index.html
3:安装 haproxy -1 和 haproxy-2 两台机器配置一致(1):安装httpd 服务
(2): echo "22222" > /var/www/html/index.html
(1): yum -y install gcc gcc-c++ make pcre-devel bzip2-devel 安装依赖包
(2): rz 上传
(3): tar xf haproxy-1.4.24.tar.gz -C /usr/src 解压
(4): cd /usr/src/haproxy-1.4.24 切换目录
(5): make TARGET=linux26 && make install 安装
(6):mkdir /etc/haproxy 创建目录
(7): cp examples/haproxy.cfg /etc/haproxy/ 复制
(8): vim /etc/haproxy/htproxy.cfg 编辑文件
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web-cluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.200.103:80 check inter 2000 fall 3
server inst2 192.168.200.104:80 check inter 2000 fall 3
(9): cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy 复制
(10):ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy 创建链接
(11): chmod +x /etc/init.d/haproxy 加权限
(12): /etc/init.d/haproxy start 启动服务
4:编译安装Keepalived 服务(13):访问。这时访问的是一个地址,一个测试页
(1):yum -y install keepalived 安装服务
(2): 配置Keepalived 的主配置文件
! Configuration File for keepalived
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.100.254
}
}
}
(3): 第二台haproxy配置Keepalived主配置文件
! Configuration File for keepalived
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.200.254
}
}
}
(4):两台机器上都配置haproxy 检测脚本
vim /etc/keepalived/check_haproxy.sh
#!/bin/bash num=`ps -C haproxy --no-header |wc -l` if [ $num -eq 0 ] then /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg sleep 3 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ] then /etc/init.d/keepalived stop fi fi
(5):chmod +x /etc/keepalived/check_haproxy.sh 加权限
(6): systemctl restart keepalived 启动服务
(7): ip a 查看地址
(8): 访问
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)