worker321 发表于 2018-12-29 07:43:58

LVS+Keepalived 文档整理


一.所需软件,ipvsadm,keepalived.
Keepalived在这里主要用作RealServer的健康状态检查以及Master主机和Backup主机之间failover的实现.
二.物理拓扑图如下:
http://blog.运维网.com/attachment/201308/222403495.png
三.配置文件:
1.Director1
1>Keepalived.conf 文件 /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
   dg@gwbnsh.net.cn
   dg@gwbnsh.net.cn
   dg@gwbnsh.net.cn
   }
   notification_email_from dg@gwbnsh.net.cn
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL//LVS
}
vrrp_instance VI_1 {         //虚拟路由器冗余协议VI_1
    state MASTER//类型MASTER
    interface eth2//监测网络接口
    virtual_router_id 51//虚拟路由ip号
    priority 150   //vip1的优先级
    advert_int 1//VRRP Multicast广播周期秒数通告包的发送间隔时间,缺省为1秒
    authentication {//vip1的认证密码 另一台VI_Ipasswd不相同会导致群集分裂
      auth_type PASS   //vrrp 认证方式
      auth_pass 1111 //vrrp 认证口令
    }
    virtual_ipaddress {
      192.168.10.99//vrrpHA虚拟地址 如果有多个VIP,继续换行填写
    }
}

virtual_server 192.168.10.99 53 {    //vip1服务的端口
    delay_loop 6// 每隔6秒查询realserver状态
    lb_algo wrr //负载均衡的轮询算法,wrr加权轮询
    lb_kind DR //负载均衡的模式 DR 直接路由模式
    #persistence_timeout 1 //同一IP的连接1 秒内被分配到同一台realserver
    protocol UDP// UDP 的协议(DNS)
    include realserver_dns//指明包含的真实服务器
}
vrrp_instance VI_2 {
    stateMASTER//VI_2也是做主,启动后会根据优先级来选举。
    interface eth2
    virtual_router_id 52
    priority 50
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 2222
    }
    virtual_ipaddress {
      192.168.10.100
    }
}
virtual_server 192.168.10.100 53 {
    delay_loop 6// 每隔6秒查询realserver状态
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 1
    protocol UDP
    include realserver_dns
}
2.>Realserver_dns文件/etc/keepalived/realserver_dns
real_server 192.168.10.201 53 { //真实的服务器和端口
weight 4//真实服务器的权重
include only_tcp_53          //指明包含健康检测协议的文件
}
real_server 192.168.10.202 53 {
weight 2
include only_tcp_53         
}
3.>后端服务器健康检测的文件only_tcp_53
TCP_CHECK { //TCP协议检测
connect_timeout 5//5秒后没有响应,超时
connect_port 53 //端口
}
2.Director2   
1.Keepalived.conf 文件
/etc/keepalived/keepalived.conf
cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   dg@gwbnsh.net.cn
   dg@gwbnsh.net.cn
   dg@gwbnsh.net.cn
}
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state    MASTER
    interface enp2s2f1                      //网卡有点特别
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      192.168.10.99
    }
}

virtual_server 192.168.10.99 53 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
#   persistence_timeout 1
    protocol UDP
    include realserver_dns
}
vrrp_instance VI_2 {
    state MASTER
    interface enp2s2f1
virtual_router_id 52
    priority 150
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 2222
    }
    virtual_ipaddress {
      192.168.10.100
    }
}

virtual_server 192.168.10.100 53 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
#   persistence_timeout 1
    protocol UDP
    include realserver_dns
}
2. realserver_dns 的配置
real_server 192.168.10.201 53 {
            weight 2
      include only_tcp_53         
      }
real_server 192.168.10.202 53 {
            weight 4
            include only_tcp_53   
      }
3.only_tcp_53的配置
TCP_CHECK {
       connect_timeout 5
       connect_port 53
}
分别启动keepalived和ipvsadm
四.后端realserver真实服务器的配置,以dns服务器为例。配置zzdx.com域测试。
/chroot/etc/named.conf 配置要监听rip和vip
listen-on port 53 { 192.168.10.201; 192.168.10.202; 192.168.10.99; 192.168.10.100; 127.0.0.1; };
1.Lo接口上要配置vip
# ip addr add192.168.10.99/32 brd 192.168.10.99 dev lo
# ip addr add192.168.10.100/32 brd 192.168.10.100 dev lo
2.lo.arp_ignorearp忽略      lo.arp_announcearp宣告0,1,20默认响应。1是尽量避免,2是选择匹配。
sysctl -q -w net.ipv4.conf.lo.arp_ignore=1
sysctl -q -w net.ipv4.conf.lo.arp_announce=2
sysctl -q -w net.ipv4.conf.all.arp_ignore=1
sysctl -q -w net.ipv4.conf.all.arp_announce=2
启动dns服务器。
五.测试:
1.前端分发器高可用性,vip是否自动抢占/浮动。
2.前端分发器是否根据权重正常分发解析任务。
3.前端是否能够对后端的真实的服务器进行健康检测。
可以抓包
前端服务器之间的认证
在d1机器上做   tcpdump -nv -i eth2 host 192.168.10.200//检测
在d2机器上重启 keepalived 就可以看到验证信息
列出当前连接:watch -n1 'ipvsadm -Lc'
列出调度地址及端口:ipvsadm -L -n
六.思考
1.如果优先级相同呢?
答案:是两个keepalived都将成为MASTER,并且也都会配置VIP.这样会导致地址冲突.

2.virtual_router_id:代表组播ID.
事实上在一组MASTER/BACKUP实例中,virtual_router_id一定要相同,如果不同,则MASTER/BACKUP都会发送组播数据包.即vip在两台机器上都会生效.导致地址冲突。
3.DR模型中出现问题:1.vip 2.arp请求3.保证vip相应 做路由。
Vip-cipdip-cipcip-rip x
4. 后端真实服务器不能负载均衡
原因是 keepalived里面缺少ops 选项,添加上就就好了
  5.使用url检测后端服务器,使用md5生成工具
  使用测试页面生成MD5命令前端上有genhash命令
  /usr/bin/genhash-s 10.21.10.30 -p 6667 -u /keeptest/index.html

  6.检测ip的某一个端口,使用nc
  land@lenovo-g460:~$ nc -v -w 2 10.21.10.31 6667
  Connection to 10.21.10.31 6667 port succeeded!
  




页: [1]
查看完整版本: LVS+Keepalived 文档整理