7.0下通过CARP+PF+relayd+PFSYNC实现健康检查等功能群集
Submitted by sysv on 2008, August 19, 3:32 PM. Cluster
在<
1. 对服务器池的健康检查;
2. 实现3层和7层的load balance;
一、安装relayd
1. 安装前需要确认内核配置文档中加入以下几个参数:
- options INET6 # IPv6 communications protocols (relayd默认对ipv6进行了支持)
- device tun # Packet tunnel.(relayd在实现上需要利用到tunnel, 必须加上)
- ### pf support ###
- device carp
- device pf
- device pflog
- device pfsync
- options ALTQ
- options ALTQ_CBQ
- options ALTQ_RED
- options ALTQ_RIO
- options ALTQ_HFSC
- options ALTQ_PRIQ
- options ALTQ_NOPCC
如重新配置了以上内核参数请重新编译内核,并重新升效
2. 开始安装relayd. 除自己DIY源代码, Makefile基本不必要改了。
# cd /usr/ports/net/relayd
# make install clean
二、配置rc.conf(加入的相关项)
# vim /etc/rc.conf
- gateway_enable="YES"
- defaultrouter="192.168.1.1"
- hostname="master.cluster.org"
- cloned_interfaces="carp0 carp1"
- # External Public Interface (for the secondary firewall use a different public ip.)
- ifconfig_em0="inet 192.168.1.52 netmask 255.255.255.0"
- # External Public Carp Interface
- #ifconfig_carp0="vhid 1 pass 11111 192.168.1.51/24"
- ifconfig_carp0="vhid 1 pass 11111 192.168.1.51/24 advskew 10"
- # Internal Interface (for the secondary firewall change the ip address to 192.168.10.11)
- ifconfig_em1="inet 192.168.10.10 netmask 255.255.255.0"
- # Internal Carp Interface
# vhid应为唯一值,不可与carp0相同。否则会出问题
- ifconfig_carp1="vhid 10 pass 22222 192.168.10.100/24 advskew 10"
- # Heartbeat Interface (for the secondary firewall, change the ip address to 10.10.10.251)
- ifconfig_vr0="10.10.10.250 netmask 255.255.255.0"
- # PFSync Interface
- #ifconfig_pfsync0="up syncdev vr0"
- pf_enable="YES"
- pf_rules="/etc/pf.conf"
- pf_flags=""
- pflog_enable="YES"
- pflog_logfile="/var/log/pflog"
- pflog_flags=""
- pfsync_enable="YES" # Expose pf state to other hosts for syncing
- pfsync_syncdev="vr0" # Interface for pfsync to work through
- pfsync_syncpeer="" # IP address of pfsync peer host
- relayd_enable="YES"
- relayd_flags=""
三、配置sysctl.conf(加入的相关项)
# vim /etc/sysctl.conf
- net.inet.carp.preempt=1
- net.inet.tcp.blackhole=2
- net.inet.udp.blackhole=1
- net.inet.tcp.sendspace=65536
- net.inet.tcp.recvspace=65536
四、配置pf.conf
# vim /etc/pf.conf
- ################################################################################
- # Macro and lists
- ################################################################################
- lop_if = "lo0"
- ext_if = "em0"
- int_if = "em1"
- sync_if= "vr0"
- ext_carp = "carp0"
- icmp_types = "echoreq"
- web_ports = "{ 80, 443 }"
- table
persist { 222.222.222.0/24, 221.222.212.0/24 } - table
persist \ - { \
- 192.168.10.20, \
- 192.168.10.21, \
- 192.168.10.22, \
- 192.168.10.23, \
- 192.168.10.24, \
- 192.168.10.30, \
- 192.168.10.31, \
- 192.168.10.32, \
- 192.168.10.33, \
- 192.168.10.34 \
- }
- ################################################################################
- # Options, scrub
- ################################################################################
- set block-policy drop
- set limit { states 10000, src-nodes 100000, frags 10000 }
- set skip on $lop_if
- scrub in on $ext_if
- ################################################################################
- # NAT
- ################################################################################
- nat on $ext_if from $int_if:network to any -> $ext_if
- ################################################################################
- # Redirection
- ################################################################################
- #rdr on $ext_if proto tcp from any to $ext_carp port $web_ports ->
round-robin sticky-address - rdr-anchor "relayd/*"
- ################################################################################
- # Filtering Rules
- ################################################################################
- block in all
- block out all
- pass quick on $lop_if all
- #pass quick on $int_if all
- pass quick on { $sync_if } proto pfsync keep state (no-sync)
- pass on { $ext_if, $int_if } proto carp keep state
- pass in quick inet proto icmp all icmp-type $icmp_types keep state
- # 这里做了对服务器池的访问策略,如果需要全网开放就将
改成any即可 - pass in quick on { $ext_if } proto tcp from
to $ext_carp port $web_ports flags S/SA modulate state - pass out quick on { $int_if } proto tcp from
to port $web_ports flags S/SA modulate state - #还有这里
- pass in quick on $ext_if inet from
to any keep state - pass in quick on $int_if inet from any to $int_if:network keep state
- pass out quick on $int_if inet from $int_if:network to any keep state
- pass out quick on $ext_if proto tcp all flags S/SA modulate state
- pass out quick on $ext_if proto { udp, icmp } all keep state
五、配置relayd.conf
# vim /usr/etc/relayd.conf
- ##################
- # MACROS #
- ##################
- #www1="192.168.10.20, 192.168.10.21"
- #www2="192.168.10.30, 192.168.10.31"
- ##### Global Options #####
- interval 10
- prefork 10
- ##################
- # TABLES #
- ##################
- #这里就是你的服务器池的IP了。
- table
{ \ - 192.168.10.20, \
- 192.168.10.21, \
- 192.168.10.22, \
- 192.168.10.23, \
- 192.168.10.24, \
- 192.168.10.30, \
- 192.168.10.31, \
- 192.168.10.32, \
- 192.168.10.33, \
- 192.168.10.34 \
- }
- table
{ 127.0.0.1 } - ##### REDIRECTIONS #####
- redirect "www" {
- #注意这里的网口是ext_if。
- listen on 192.168.1.51 port 80 interface em0
- #forward to
mode roundrobin check http "/" code 200 - #forward to
port 80 mode roundrobin sticky-address check tcp - #因为服务器池的应用是动态的,在重定向时我用了sticky-address来保证同个源请求到原目标服务器。并利用tcp对服务器池进行健康检查。
- tag REDIRECTED
- sticky-address
- forward to
port 80 mode roundrobin timeout 300 check tcp - forward to
check icmp - }
六、启动服务
# /usr/local/etc/rc.d/relayd start
# pfctl -Fa -f /etc/pf.conf
测试, 通过relayctl可以看到服务器池的健康情况,不健康的就不会进行调度了。relayctl命令还有一些比较实用的用法,请自行man 了。
# relayctl show summary
PHP代码- Id Type Name Avlblty Status
- 0 redirect www active
- 2 table www_pool:80 active (5 hosts up)
- 20 host 192.168.10.20 99.95% up
- 19 host 192.168.10.21 99.95% up
- 18 host 192.168.10.22 99.94% up
- 17 host 192.168.10.23 99.95% up
- 16 host 192.168.10.24 99.94% up
- 15 host 192.168.10.30 74.62% down
- 14 host 192.168.10.31 74.62% down
- 13 host 192.168.10.32 74.62% down
- 12 host 192.168.10.33 74.61% down
- 11 host 192.168.10.34 74.61% down
- 3 table fallback:80 active (1 hosts up)
- 21 host 127.0.0.1 100.00% up
七、服务器池的配置在<
八、LoadBalance 的master和slave的配置以<
Tags: relayd, pf, carp, pfsync, loadbalance
出处 http://blog.sysv.cn/?action=showid=14