zjxhx 发表于 2017-4-17 22:41:52

haproxy+keepalived安装配置说明

安装Keepalived
 yum install -y wget make gcc openssl-devel popt-devel

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --prefix=/usr/local/keepalived
make
make install

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

mkdir -p /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
admin@123.com
}
notification_email_from admin@123.com
smtp_server mail.123.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
mcast_src_ip 172.16.33.124
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_haproxy    #监测haproxy进程状态
}
virtual_ipaddress {
172.16.33.123
}
}
}

vi /etc/keepalived/chk_haproxy.sh
status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then
/etc/init.d/haproxy start
status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)
if [ "${status2}" = "0"]; then
/etc/init.d/keepalived stop
fi
fi

 
service keepalived start
service keepalived stop
1、将Master上的keepalived停止
查看Master上的日志
tail -f /var/log/messages
Sep 23 01:24:34 181 Keepalived: Stopping Keepalived v1.2.8 (09/23,2013)
Sep 23 01:24:34 181 Keepalived_vrrp: VRRP_Instance(VI_1) sending 0 priority
Sep 23 01:24:34 181 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
发现Master释放了VIP
再查看Backup上的日志
Sep 23 01:20:26 182 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 23 01:20:27 182 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 23 01:20:27 182 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 23 01:20:27 182 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180
Sep 23 01:20:27 182 Keepalived_healthcheckers: Netlink reflector reports IP 10.10.3.180 added
发现Backup绑定了VIP
2、将Master上的keepalived启动
查看Master上的日志
tail -f /var/log/messages
Sep 23 01:27:56 181 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 23 01:27:56 181 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 23 01:27:56 181 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180
Sep 23 01:27:56 181 Keepalived_healthcheckers: Netlink reflector reports IP 10.10.3.180 added
发现Master绑定了VIP
再查看Backup上的日志
Sep 23 01:21:15 182 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Sep 23 01:21:15 182 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep 23 01:21:15 182 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Sep 23 01:21:15 182 Keepalived_healthcheckers: Netlink reflector reports IP 10.10.3.180 removed
发现Backup释放了VIP
tcpdump vrrp 检查vrrp是否已正常工作,抓包会看到对应的ip
ip a
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:74:b0:22 brd ff:ff:ff:ff:ff:ff
inet 172.16.33.124/24 brd 172.16.33.255 scope global eth0
inet 172.16.33.123/32 scope global eth0
inet6 fe80::20c:29ff:fe74:b022/64 scope link
valid_lft forever preferred_lft forever

安装haproxy
#tar -xvzf haproxy-1.4.24.tar.gz

#cd haproxy-1.4.24

#make TARGET=linux26 ARCH=x86_64

#TARGET是指定内核版本,ARCH指定CPU架构,我使用的是64bit系统

#make install

 

#mkdir /etc/haproxy

#cp examples/haproxy.cfg /etc/haproxy

#cp examples/haproxy.init /etc/init.d/haproxy

#chmod +x /etc/init.d/haproxy

#ln -s /usr/local/sbin/haproxy /usr/sbin/

#mkdir /usr/share/haproxy

vi /etc/haproxy/haproxy.cfg

 

global

    log 127.0.0.1   local0  #日志输出配置,所有日志都记录在本机,通过local0输出


    log 127.0.0.1   local1 notice


    #log loghost    local0 info


    maxconn 4096                #最大连接数


    chroot /usr/share/haproxy   #改变当前工作目录。


    uid 99                  #所属用户的uid


    gid 99                  #所属运行的gid


    daemon                  #以后台形式运行haproxy


defaults

    log global


    mode    http


  #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK


    option  httplog


    option  dontlognull


    option   redispatch


  #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器


    option  abortonclose


  #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接


    retries 3               #两次连接失败就认为是服务器不可用


    maxconn 2000            #默认的最大连接数


    contimeout  5000        #连接超时


    clitimeout  50000       #客户端超时


    srvtimeout  50000       #服务器超时


frontend www

 bind *:80


#这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。

 acl web hdr(host) -i www.test.com


 #acl后面是规则名称,-i是要访问的域名,如果访问www.test.com这个域名就分发到下面的webserver 的作用域。


 use_backend webserver if web


acl url_static       path_beg       -i /static /images /javascript /stylesheets


acl url_static       path_end       -i .jpg .gif .png .css .js


acl url_dynamic      path_end       -i .php


use_backend static          if url_static


use_backend dynamic          if url_dynamic




 

backend webserver             #webserver作用域


    mode http


    balance   roundrobin     


    #banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数


    option  httpchk /index.html


    #检测文件,如果分发到后台index.html访问不到就不再分发给它


    server     web01 172.16.33.126:80  inter 3000 rise 2 fall 3 check maxconn 5000


   server     web02 172.16.33.127:80  inter 3000 rise 2 fall 3 check maxconn 5000

#listen web

# bind 172.16.33.123:80

# mode http

# server web01 172.16.33.126:80 check inter 2000 rise 2 fall 5

# server web02 172.16.33.127:80 check inter 2000 rise 2 fall 5

 

listenmysql
bind 0.0.0.0:23306    #代理端口
mode tcp            #模式 TCP
option mysql-check user root   #mysql健康检查root为mysql登录用户名
balance round robin            #调度算法
server mysql1 10.0.31.40:4306 weight 1 checkinter 1s rise 2 fall 2 #健康检查加上check
server mysql2 10.0.31.41:4306 weight 1 checkinter 1s rise 2 fall 2


listen admin_stats

        bind *:8080              #监听端口

        mode http                       #http的7层模式

        option httplog                  #采用http日志格式

        log 127.0.0.1 local0 err

        maxconn 10

        stats refresh 30s               #统计页面自动刷新时间

        stats uri /stats                #统计页面url

        stats realm XingCloud\ Haproxy  #统计页面密码框上提示文本

        stats auth admin:admin          #统计页面用户名和密码设置

        stats hide-version              #隐藏统计页面上HAProxy的版本信息
页: [1]
查看完整版本: haproxy+keepalived安装配置说明