设为首页 收藏本站
查看: 851|回复: 0

[经验分享] LVS+Keepalived 文档整理

[复制链接]

尚未签到

发表于 2018-12-29 07:43:58 | 显示全部楼层 |阅读模式

一.所需软件,ipvsadm,keepalived.
Keepalived在这里主要用作RealServer的健康状态检查以及Master主机和Backup主机之间failover的实现.
二.物理拓扑图如下:

三.配置文件:
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//vrrp  HA虚拟地址 如果有多个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 {
    state  MASTER//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 add  192.168.10.99/32 brd 192.168.10.99 dev lo
# ip addr add  192.168.10.100/32 brd 192.168.10.100 dev lo
2.lo.arp_ignore  arp忽略        lo.arp_announce  arp宣告  0,1,2  0默认响应。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-cip  dip-cip  cip-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 [tcp/ircd] succeeded!
  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-657008-1-1.html 上篇帖子: LVS+Keepalived实现高可用 下篇帖子: Keepalived 高可用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表