两台rs需要进行lvs相关配置我这里用脚本实现,对于后端RS无论tcpudp都是一样的。
将脚本放到/etc/init.d/ 下
赋予执行权限
chmod +x /etc/init.d/realserver
执行脚本
/etc/init.d/realserver restart
设置开机自启动
chkconfig realserver on
- #!/bin/bash
- # chkconfig: - 95 50
- # description: Config realserver lo and apply noarp
- #2019年12月23日 wendianfei upload
- #This VIPS can write more than two IP separated by spaces.for example:VIPS="10.10.10.10 10.10.10.11 192.168.1.10 192.168.1.11"
- #Then,You need to confirm that the program is configured to start automatically. "chkconfig realserver on"
- VIPS="10.21.17.97"
- OLD_VIPS=`/sbin/ip a show dev lo |grep 'global'|awk '{print $4}'`
-
- . /etc/rc.d/init.d/functions
- start() {
- for VIP in $VIPS
- do
- ifconfig lo:`echo $VIP|awk -F. '{print $3"-"$4}'` $VIP netmask 255.255.255.255 broadcast $VIP
- /sbin/route add -host $VIP dev lo:`echo $VIP|awk -F. '{print $3"-"$4}'`
- done
- echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
- echo "RealServer Start: [ OK ]"
- }
-
- stop() {
- for VIP in $OLD_VIPS
- do
- ifconfig lo:`echo $VIP|awk -F. '{print $3"-"$4}'` down
- route del $VIP >/dev/null 2>&1
- done
- echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
- echo "RealServer Stoped: [ OK ]"
- }
-
-
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- stop
- start
- ;;
- *)
- echo "Usage: $0 {start|stop|restart}"
- exit 1
- esac
-
- exit 0
配置完后端rs的lvs配置后,安装并配置keepalived服务端。
yum install -y keepalived.x86_64
yum install -y ipvsadm
接下来配置keepalive。
UDP和TCP的主要区别就是,keepalve本身支持TCP的检测,UDP的不行,所以UDP要用到
MISC_CHECK模块自己写脚本进行检测,keepalive服务认为,脚本返回值为0代表正常,返回值为1代表异常。我们的脚本可以只要能实现正常返回0,异常范围1即可,可以自由发挥。
###主keepavlie配置
- ### Config file keepalived LVS
- global_defs {
- router_id LVS_Server_1
- script_user root
- enable_script_security
- }
-
- vrrp_instance LVS_1 {
- state MASTER #备的为BACKUP
- interface ens192
- virtual_router_id 151
- priority 150 #主的大于备的
- advert_int 2
- authentication {
- auth_type PASS
- auth_pass wdf.com
- }
-
- virtual_ipaddress {
- 10.21.17.97
- }
- }
-
-
- virtual_server_group dns-udp-53 {
- 10.21.17.97 53
- }
-
- virtual_server group dns-udp-53 {
- delay_loop 5
- lb_algo wrr
- lb_kind DR
- protocol UDP
-
- real_server 10.21.17.60 53 {
- weight 10
- MISC_CHECK {
- misc_path "/root/check_dns.sh 10.21.17.60"
- misc_timeout 10
- misc_dynamic
- }
- }
- real_server 10.21.17.61 53 {
- weight 10
- MISC_CHECK {
- misc_path "/root/check_dns.sh 10.21.17.61"
- misc_timeout 10
- misc_dynamic
- }
- }
-
-
- }
备:keepalive配置
- ## Config file keepalived LVS
-
- global_defs {
- router_id LVS_Server_1
- script_user root
- enable_script_security
- }
-
- vrrp_instance LVS_1 {
- state BACKUP
- interface ens192
- virtual_router_id 151
- priority 80
- advert_int 2
- authentication {
- auth_type PASS
- auth_pass wdf.com
- }
-
- virtual_ipaddress {
- 10.21.17.97
- }
- }
-
-
- virtual_server_group dns-udp-53 {
- 10.21.17.97 53
- }
-
- virtual_server group dns-udp-53 {
- delay_loop 5
- lb_algo wrr
- lb_kind DR
- protocol UDP
-
- real_server 10.21.17.60 53 {
- weight 10
- MISC_CHECK {
- misc_path "/root/check_dns.sh 10.21.17.60"
- misc_timeout 10
- misc_dynamic
- }
- }
- real_server 10.21.17.61 53 {
- weight 10
- MISC_CHECK {
- misc_path "/root/check_dns.sh 10.21.17.61"
- misc_timeout 10
- misc_dynamic
- }
- }
-
-
- }
下面附上我的dns服务检测的脚本:
- #!/bin/bash
- # Program:
- # check health
- # History:
- # 2022/11/25 wendianfei version:0.0.1
- IP=$1
- count=$( /usr/bin/dig @$1 www.wdf.com| grep 192.168.66.1 | wc -l)
- echo $count
- if [ ${count} -gt 0 ]
- then
-
- exit 0
-
- else
-
- exit 1
-
- fi