1 環境介紹
1.1 keepalived的主從
master: 10.50.85.26
slave: 10.50.85.56
1.2 keepavlied的VIP和端口
10.50.85.200:10051
注意:keepavlied的VIP必須是和mater和slave是同一網段的
1.3 代理的兩臺主機的10050端口
10.50.85.26:10050
10.50.85.56:10050
2 在兩臺主機上分別按照haproxy和keeepalived
yum -y install keepalived haproxy
2.1 在mster上配置keepalive的和haproxy
2.1.1 keepalived配置
keepalived的配置文件路徑:/etc/keepalived/
注意:不使用默認配置
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {br/>admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}redis
vrrp_script chk_http_port {
script "/etc/keepalived/check.sh"
interval 2
weight 2
}vim
vrrp_instance VI_1 {
state MASTER
interface br0
virtual_router_id 200
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}後端
track_script {
chk_http_port
}centos
virtual_ipaddress {
10.50.85.200 dev br0 label br0:0
}
}
2.1.1.1 vrrp_script chk_http_port 配置部分是檢測的腳本,用於判斷後端端口是否存活,若是不存活的話,殺掉haproxy,並停掉keepalived,使服務能切換到正常的slave上。/etc/keepalived/check.sh腳本內容以下
#! /usr/bin/env bashbash
/usr/bin/pkill -0 -f haproxydom
if [ $? -ne 0 ];then
service start haproxy
sleep 3
fitcp
/usr/bin/pkill -0 -f haproxyide
if [ $? -ne 0 ];then
service stop keepalived
fi
2.1.1.2 vrrp_instance VI_1配置部分
vrrp_instance VI_1 是在vrrp中的id
state MASTER 當前主機的是地位(是master或者slave)
interface br0 vip綁定的物理接口
priority 100 在主從之間的優先級(數字越大,優先級越高)
2.1.1.3 virtual_ipaddress 配置部分
10.50.85.200 dev br0 label br0:0 實際綁定的vip
2.1.2 haproxy的配置
haproxy配置文件的路徑:/etc/haproxy/
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
maxconn 65536
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon測試
defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 65536
timeout connect 70000
timeout client 70000
timeout server 70000spa
listen admin_stats 10.50.85.26:48800
stats uri /admin-status
stats auth admin:admin
mode http
option httplog
listen mtmycat 10.50.85.200:10051
mode tcp
balance roundrobin
server host85-56 10.50.85.56:10050 check port 9158 inter 5s rise 2 fall 3
server host85-26 10.50.85.26:10050 check port 9158 inter 5s rise 2 fall 3
2.1.2.1 global和default是全局配置部分
2.1.2.2 listen admin_stats 是監控界面,便於查看haproxy狀態
mode 是tcp層的負載
balance 是負載的模式,上面是平均負載
server 是後端服務的相關信息
2.2 在slave上配置keepalive的和haproxy
2.2.1 keepalived的配置
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {br/>admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check.sh"
interval 2
weight 2
}
vrrp_instance VI_2 {
state BACKUP
interface br0
virtual_router_id 200
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
10.50.85.200 dev br0 label br0:0
}
}
2.2.2 haprox的配置
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
maxconn 65536
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 65536
timeout connect 70000
timeout client 70000
timeout server 70000
listen admin_stats 10.50.85.56:48800
stats uri /admin-status
stats auth admin:admin
mode http
option httplog
listen mtmycat 10.50.85.200:10051mode tcpbalance roundrobinserver host85-56 10.50.85.56:10050 check port 10050 inter 5s rise 2 fall 3server host85-26 10.50.85.26:10050 check port 10050 inter 5s rise 2 fall 33 啓動兩個主機上的keepavlied和haproxy服務,進行測試ping 10.50.85.200是通的,telnet 10.50.85.200 10051端口也是通的