haproxy+keepalived部署

Posted sailq21

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxy+keepalived部署相关的知识,希望对你有一定的参考价值。

 

VIP1:10.90.11.196

VIP2:10.90.11.195

DIP1:10.90.11.96

DIP2:10.90.11.95

 

keepalived部署

Director1、Director2:

yum install keepalived -y

vim /etc/keepalived/keepalived.conf

10.90.11.96
10.90.11.95

global_defs {
#notification_email {
[email protected]
#}
#notification_email_from [email protected]
#smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id HAPROXY_RECOM_MASTER
}


vrrp_instance VI_1 {
state MASTER
interface bond0
virtual_router_id 71
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.90.11.196
}
}

vrrp_instance VI_2 {
state BACKUP
interface bond0
virtual_router_id 72
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.90.11.195
}
}

global_defs {
#notification_email {
[email protected]
#}
#notification_email_from [email protected]
#smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id HAPROXY_RECOM_BACKUP
}


vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 71
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
10.90.11.196
}
}

vrrp_instance VI_2 {
state MASTER
interface bond0
virtual_router_id 72
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.90.11.195
}
}

启动keepalived

/etc/init.d/keepalived start

部署haproxy

director1、director2:

安装haproxy:

wget http://pkgs.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.6.9.tar.gz/c52eee40eb66f290d6f089c339b9d2b3/haproxy-1.6.9.tar.gz
tar zxf haproxy-1.6.9.tar.gz -C /usr/local/
cd /usr/local/haproxy-1.6.9
make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

修改配置文件:
vim /usr/local/haproxy/haproxy.cfg

global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
ulimit-n 40960
maxconn 4000
pidfile /usr/local/haproxy/haproxy.pid
#user haporxy
nbproc 2
daemon
defaults
mode tcp
option redispatch
option abortonclose
retries 3
timeout http-request 300s
timeout queue 1m
timeout connect 300s
timeout client 1m
timeout server 1m
timeout http-keep-alive 300s
timeout check 300s
maxconn 4096

listen tcptest
bind *:8180
balance roundrobin
mode tcp
option tcplog
log 127.0.0.1 local0 debug
server s1 10.80.29.142:8180 weight 1
server s2 10.80.30.142:8180 weight 1
server s3 10.80.31.142:8180 weight 1

 

启动
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
重启
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/haproxy.pid`
停止
killall haproxy

将haproxy加入到系统服务中。脚本内容如下

vim /etc/init.d/haproxy

脚本内容如下:

#! /bin/sh
set -e

#PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/$PROGNAME.cfg
PIDFILE=$PROGDIR/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

start()
{
echo -e "Starting $DESC: $PROGNAME"
$DAEMON -f $CONFIG
echo "."
}

stop()
{
echo -e "Stopping $DESC: $PROGNAME"
haproxy_pid="$(cat $PIDFILE)"
kill $haproxy_pid
echo "."
}

restart()
{
echo -e "Restarting $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac

exit 0





















































































































































以上是关于haproxy+keepalived部署的主要内容,如果未能解决你的问题,请参考以下文章

haproxy+keepalived部署

部署haproxy+keepalived高可用负载均衡器

k8s高可用部署:keepalived + haproxy

HAProxy+Keepalived高可用负载均衡

Linux集群(keepalived介绍,Keepalived配置高可用集群,Keepa+mysql

Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)