• Keepalived 配置说明


    1 环境准备
    系统需要的 LINUX 版本为:2.6.9‐5.ELsmp 需要的keepalived 版本为:1.1.20
    ? 检查 LINUX版本
    [root@test01 etc]# uname -r
    2.6.9-5.ELsmp
    ? 检查 keepalived 版本
    [root@test01 keepalived]# pwd
    /root/disk/keepalived
    [root@test01 keepalived]# cat VERSION
    1.1.20

    2目标
    两台服务器,一主一备。提供两个虚拟 IP。
    Server A: 10.10.0.41 (主服务器)
    Server B: 10.10.0.118 (备服务器)
    Virtual IP: 10.10.0.44/45
    要求正常情况,主服务器提供服务,主服务器失效时,备服务器接管。

    3 安装 keepalive

    yum install openssl-devel

    [root@test01 keepalived]# ./configure
    ……
    [root@test01 keepalived]# make
    ……
    [root@test01 keepalived]# make install
    ……

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

    cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

    mkdir /etc/keepalived

    cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

    cp /usr/local/sbin/keepalived /usr/sbin/

    做成系统启动服务方便管理.
    [root@slave init.d]# chkconfig keepalived on
    [root@slave init.d]# chkconfig –list | grep keepalivd
    [root@slave init.d]# chkconfig –list | grep keep
    keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off

    负载均衡是一个服务的虚拟化是一个很好的解决方案,当你设计一个负载均衡的拓扑

    必须关注:

    1.使用健康检查 检查server真的可用

    1. 使用failover协议来实现负载均衡可用性

    负载平衡服务,提供了一个全局的高度可用的虚拟服务。为了增加负载均衡服务的可用性,我们需要

    监控每个正则的server节点,这个问题是主要处理 使用健康检查框架操作一个真正的server pool.

    换句话说,当使用负载director,我们引入一个单点故障
    对于虚拟服务。因此负载均衡高可用必须被解决,

    使用专用的路由
    协议切换/虚拟化协议。

    简单的试图解决这两个问题,通过添加,一方面,一个强大的
    健康检查框架,另一方面,

    实现热备份协议.这两个框架可以处理Linux Vritual Server(LVS)框架来操纵LVS real server pools

    通过增加和去除真正的real server 基于健康检查的决定。

    LVS 代表 “Linux Virtual Server”.LVS是一个
    patched Linux内核,添加了一个负载均衡的功能。

    LVS表现为一个网桥(使用NAT)来负载均衡TCP/UDP LVS 路由器组件是:

    1.WAN口, 以太网接口控制了所有访问的客户端

    2.LAN口,以太网控制所有的负载均衡server

    在这篇文章中,我们使用下面的关键字:

    LVS组件:

    1.VIP Virtual IP是 被客户端访问的地址,客户端只会访问这个地址

    2.Real server.一个real server 主机被客户端访问,

    在我们的拓扑里 是WEB SERVER 1和WEB SERVER 2

    3.Server pool: 一个真正的服务器农场

    4.Virtual server:一个服务器池的接入点

    5.和VIP相关的TCP/UDP 服务

    VRRP 组件:(虚拟路由冗余协议(Virtual Router Redundancy Protocol,简称VRRP)

    1.VRRP 主管failover/virtualization 实现的协议

    1. VRRP Instance: 一个thread 控制 VRRPv2 IP地址的具体设置,一个VRRP 实例

    可能备份一个或者多个VRRP 实例。在我们的 “Case study:Failover”, 我们处理4个

    VRRP 实例。 一个有(VIP1,VIP2),一个拥有(VIP3,VIP4),一个拥有(DIP1) 另一个拥有(DIP2)

    可以参与一个或多个虚拟路由器.

    3.IP Address owner. VRRP 实例 的IP地址 作为一个真正的接口地址。

    这就是VRRP 实例, 当起来的时候
    将响应数据包处理
    一个IP地址(ES)对ICMP,TCP连接

    4.MASTER state: VRRP 实例状态,它是假设代理了转发数据包到VRRP实例相关的地址。

    5.BACKUP state: VRRP 实例状态 它可以转发数据库 由于当前的VRRP实例MASTER 失败

    6.real load balance :一个LVS director 运行一个或者多个VRRP 实例

    我们的软件结构 处理4个Linux 内核的请求:

    下面的图说明了keepalived 内部的实现组件。keepalived 使用一个完全的多线程框架 基于

    核心的I/O多路复用。 2个主要的组件是:

    1.Health-checker worker threads.每个 health-check 是注册到我们的全局框架。

    这些workers 执行监控检查使用keepalived health-check 框架。

    health-check 框架当前实现3个检查:

    1. TCP CHECK:执行一个3层的检查

    2.HTTP GET: 检查HTTP server html 内存的完整性

    3.MISC CHECK: 执行用户定义的完整性检查

    2.VRRP packet 调度器: 多路复用I/O处理VRRP实例
    相应的

    2个主要的部件使用下面的低级别的原始的

    SMTP 通知; 一个SMTP 包装 使用异步的stream 进程。这种原始的
    使简单的发送邮件通知。

    IPVS 框架: LVS 内核接口,真正的服务器池的操作

    Netlink: 内核路由VRRP部分接口,提供VRRP VIP 操纵。

    Multicast(组播): 用于发送VRRP广告,我们使用组播

    当前的多层 health-check 框架实现了下面的检查模块:

    1. TCP_CHECK:工作在4层。为了确保这个check,我们使用TCP Vanilla check 使用非阻塞的/超时的 TCP连接。

    如果远程的server 不回复请求,那么测试是错误的,服务器被从sereer pool上移除。

    2.HTTP_GET 工作在第5层, 执行一个URL的 GET HTTP。 get的结果是用MD5计算的

    如果sum值和预期的值不匹配, 测试是失败的 server 会从server pool移除。

    这个模块实现一个multi-URL的get check 在相同的service上。

    SSL_GET: 和HTTP_GET 类似但是使用了SSL连接到远程的webservers.

    MISC_CHECK: 这个检查允许一个用户定义脚本来运行作为健康检查,结果必须是0或者1.

    keepalived 的目的是定义一个通用的框架容易扩展来增加新的checks modules.

    如果你对checkers 开发感兴趣,你可以读一下HackingGuide

    导演切换简单实现VRRP协议。快速描述这个协议:

    1.”VRRP 指定选举协议动态分配reponsibility 为一个虚拟的路由器

    VRRP 路由器 控制了和虚拟路由器叫做master的IP address,转发的包发送到这些IP地址。

    选择的进程提供了动态的fail over 在转发reposibility 可能是Master 变得不可用的时候

    这允许任何的虚拟路由器IP地址
    局域网作为默认的第一跳路由器的主机

    安装keepalived

    在安装keepalived 前,任何的先前安装的版本必须要删除

    1.下载最新的keepalived 源代码

    下载OpenSSL

    你也需要使用Linux kernel 源代码

    2.简单的编译

    [root@lvs keepalived]# ./configure
    [root@lvs keepalived]# make
    [root@lvs keepalived]# make install

    3.所有的2进制和临时的配置文件被安装, 你需要创建一个call到keepalived daemon在你的rc 文件里

    [root@lvs keepalived]# ln -s /etc/rc.d/init.d/keepalived.init /etc/rc.d/rc3.d/S99keepalived

    keepalivd 配置简介:

    keepalivd 配置使用下面的简介:

    7.1 全局定义摘要:

    global_defs {
    notification_email {
    email
    email
    }
    notification_email_from email
    smtp_server host
    smtp_connect_timeout num
    lvs_id string
    }

    关键字 定义 类型
    global_defs identify the global def configuration block
    确定全局定义的配置块

    notification_email email accounts that will receive the notification mail List
    电子邮件帐户,将收到通知邮件

    notification_email_from email to use when processing “MAIL FROM:” SMTP command List
    电子邮件使用时处理邮件:“SMTP命令

    smtp_server remote SMTP server to use for sending mail notifications alphanum

                           服务器用于发送邮件通知
    

    smtp_connection_timeout specify a timeout for SMTP stream processing numerical

                           指定SMTP流处理超时
    

    lvs_id specify the name of the LVS director alphanum
    指定LVS director的姓名

    7.2 Virtual server definitions synopsis(定义虚拟服务器简介)

    virtual_server (@IP PORT)|(fwmark num) {
    delay_loop num
    lb_algo rr|wrr|lc|wlc|sh|dh|lblc
    lb_kind NAT|DR|TUN
    (nat_mask @IP)
    persistence_timeout num
    persistence_granularity @IP
    virtualhost string
    protocol TCP|UDP
    sorry_server @IP PORT
    real_server @IP PORT {
    weight num
    TCP_CHECK {
    connect_port num
    connect_timeout num
    }
    }
    real_server @IP PORT {
    weight num
    MISC_CHECK {
    misc_path /path_to_script/script.sh
    (or misc_path “/path_to_script/script.sh ”)
    }
    }
    }
    real_server @IP PORT {
    weight num
    HTTP_GET|SSL_GET {
    url { # You can add multiple url block
    path alphanum
    digest alphanum
    }
    connect_port num
    connect_timeout num
    nb_get_retry num
    delay_before_retry num
    }
    }
    }
    keepalived主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。
    keepalived主要目的在于,其自身启动一个服务,能够实现工作在双节点或多个节点上,并且可以在内核生效的ipvs规


    其中当前持有资源的节点被称为活跃节点,另外的节点被称为备节点
    被称为 Master/Backup 传承了vrrp的特*

    备节点如果收不到主节点传递过来的心跳信息,之后于是将资源抢回并使用,并在本地生效规则

    virtual_server identify a virtual server definition block
    确定虚拟服务器定义块

    fwmark specify that virtual server is a FWMARK

                           指定虚拟服务器是一个fwmark
    

    delay_loop specify in seconds the interval between checks numerical

                           指定检查间隔(单位秒)
    

    lb_algo select a specific scheduler (rr|wrr|lc|wlc…) string
    选择一个特定的调度算法(RR | WRR | LC | WLC……)

    lb_kind select a specific forwarding method (NAT|DR|TUN) string

                           选择一个特定的转发方法(NAT |DR|TUN)
    

    persistence_timeout specify a timeout value for persistent connections numerical

                           指定持久连接超时值
    

    persistence_granularity specify a granularity mask for persistent connections
    指定一个持久连接的粒度

    Virtualhost specify a HTTP virtualhost to use for HTTP|SSL_GET alphanum

                           指定一个HTTP 虚拟主机用于HTTP|SSL_GET
    

    protocol specify the protocol kind (TCP|UDP) numerical
    这种协议类型(TCP|UDP)

    sorry_server server to be added to the pool if all real servers are down

                          如果所有的real servers down了 server 被增加到pool
    

    real_server specify a real server member

                           指定一个real server 
    

    Weight specify the real server weight for load balancing decisions numerical
    指定real server 衡量load balancing决定

    TCP_CHECK check real server availability using TCP connect

                            检查real server 可用性 使用TCP连接
    

    MISC_CHECK check real server availability using user defined script

                           检查real server 可用性使用定义的脚本
    

    misc_path identify the script to run with full path path

                            定义脚本运行的路径
    

    HTTP_GET check real server availability using HTTP GET request

                            检查real server 可用性 使用HTTP GET请求
    

    SSL_GET check real server availability using SSL GET request

                            检查real server 可用性使用SSL GET 请求
    

    url identify a url definition block
    确定一个URL定义块

    Path specify the url path alphanum

                           定义一个url 路径
    

    Digest specify the digest for a specific url path alphanum
    指定一个摘要用于特定的路径

    connect_port connect remote server on specified TCP port numerical

                           连接到远程server 在指定的TCP端口上
    

    connect_timeout connect remote server using timeout numerical

                            连接到远程server 使用超时
    

    Nb_get_retry maximum number of retries numerical
    最大重试次数

    delay_before_retry delay between two successive retries numerica
    连续两次重试之间的延迟

    NB: nat_mask 关键字是过时的 如果你不使用LVS 这个标志给你定义的反向NAT粒度的能力。

    NB:Currrently,Healthcheck 框架,只实现TCP 协议用于服务监控

    NB:Type “path” 指向一个完整的脚本执行的路径。

    7.3 VRRP Instance definitions synopsis

    vrrp_sync_group string {
    group {
    string
    string
    }
    notify_master /path_to_script/script_master.sh
    (or notify_master “/path_to_script/script_master.sh ”)
    notify_backup /path_to_script/script_backup.sh
    (or notify_backup “/path_to_script/script_backup.sh ”)
    notify_fault /path_to_script/script_fault.sh
    (or notify_fault “/path_to_script/script_fault.sh ”)
    }
    vrrp_instance string {
    state MASTER|BACKUP
    interface string
    mcast_src_ip @IP
    lvs_sync_daemon_interface string
    virtual_router_id num
    priority num
    advert_int num
    smtp_alert
    authentication {
    auth_type PASS|AH
    auth_pass string
    }
    virtual_ipaddress { # Block limited to 20 IP addresses
    @IP
    @IP
    @IP
    }
    virtual_ipaddress_excluded { # Unlimited IP addresses number
    @IP
    @IP
    @IP
    }
    notify_master /path_to_script/script_master.sh
    (or notify_master “/path_to_script/script_master.sh ”)
    notify_backup /path_to_script/script_backup.sh
    (or notify_backup “/path_to_script/script_backup.sh ”)
    notify_fault /path_to_script/script_fault.sh
    (or notify_fault “/path_to_script/script_fault.sh ”)
    }

    vrrp_instance identify a VRRP instance definition block
    确定一个VRRP实例定义块
    State specify the instance state in standard use
    在标准使用指定实例的状态

    Interface specify the network interface for the instance to run on string
    指定的实例运行在字符串的网络接口

    mcast_src_ip specify the src IP address value for VRRP adverts IP header

                             指定源IP地址值用于VRRP IP 报头
    

    lvs_sync_daemon_inteface specify the network interface for the LVS sync_daemon to run on

    string

                             指定网络接口用于LVS sync_daemon 运行
    

    Virtual_router_id specify to which VRRP router id the instance belongs numerical

                             指定实例属于 哪个VRRP 路由器 ID 
    

    Priority specify the instance priority in the VRRP router numerical

                             指定VRRP 路由器的 实例优先级
    

    advert_int specify the advertisement interval in seconds (set to 1) numerical
    指定公告时间间隔

    smtp_alert Activate the SMTP notification for MASTER state transition
    激活SMTP 通知用于MASTER 状态转换

    authentication identify a VRRP authentication definition block

                             定义一个VRRP 认证定义模块
    

    auth_type specify which kind of authentication to use (PASS|AH)

                             指定哪种类型认证使用(PASS|AH)
    

    auth_pass specify the password string to use string

                             指定 密码字符窜来使用
    

    virtual_ipaddress identify a VRRP VIP definition block

                             定义一个VRRP  VIP 定义块
    

    virtual_ipaddress_excluded identify a VRRP VIP excluded definition block (not protocol VIPs)

                             定义一个VRRP VIP 排除定义块(不是VIPs协议)
    

    notify_master specify a shell script to be executed during transition to master state

    path

                             指定一个shell脚本用于执行在转换为master状态时执行
    

    notify_backup specify a shell script to be executed during transition to backup state

    path

    notify_fault specify a shell script to be executed during transition to fault state

    path

    vrrp_sync_group Identify the VRRP synchronization instances group string

    Keepalived 程序简介:

    8.1 keepalived daemon

    相关的命令行参数:

    首先你需要一个配置好的LVS的拓扑结构,在本文档的其余部分,我们假设所有的系统配置都已经做好。

    这种拓扑结构一般是DMZ中的实现
    的结构。

    9.1 主要部件

    1. LVS 路由器,有负载均衡IP地址(192.168.100.0/24)

    2.Network Router.默认的路由器对于整个内部网络,所有局域网中的工作站
    通过这个IP地址的处理

    3.Network DNS Server :引用内部网络IP拓扑

    4.SMTP Server
    5.SERVER POOL

    9.2 Server pool specifications

    在这个简单配置里,我们有2个server pools:

    1. Server pool 1:托管的HTTP和SSL服务。每个服务器拥有两个应用服务器

    2.Server pool 2:托管SMTP service

    9.3 Keepalived configuration

    你现在准备配置 keepalived daemon 根据你的LVS 拓扑。整个配置是在 /etc/keepalived/keepalived.conf file。

    Configuration File for keepalived

    global_defs {
    notification_email {
    admin@domain.com
    0633225522@domain.com
    }
    notification_email_from keepalived@domain.com
    smtp_server 192.168.200.20
    smtp_connect_timeout 30
    lvs_id LVS_MAIN
    }
    virtual_server 192.168.200.15 80 {
    delay_loop 30
    lb_algo wrr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    sorry_server 192.168.100.100 80
    real_server 192.168.100.2 80 {
    weight 2
    HTTP_GET {
    url {
    path /testurl/test.jsp
    digest ec90a42b99ea9a2f5ecbe213ac9eba03
    }
    url {
    path /testurl2/test.jsp
    digest 640205b7b0fc66c1ea91c463fac6334c
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 2
    }
    }
    real_server 192.168.100.3 80 {
    weight 1
    HTTP_GET {
    url {
    path /testurl/test.jsp
    digest 640205b7b0fc66c1ea91c463fac6334c
    }
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 2
    }
    }
    }
    virtual_server 192.168.200.15 443 {
    delay_loop 20
    lb_algo rr
    lb_kind NAT
    persistence_timeout 360
    protocol TCP
    real_server 192.168.100.2 443 {
    weight 1
    TCP_CHECK {
    connect_timeout 3
    }
    }

    real_server 192.168.100.3 443 {
    weight 1
    TCP_CHECK {
    connect_timeout 3
    }
    }
    }
    virtual_server 192.168.200.15 25 {
    delay_loop 15
    lb_algo wlc
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    real_server 192.168.100.4 25 {
    weight 1
    TCP_CHECK {
    connect_timeout 3
    }
    }
    real_server 192.168.100.5 25 {
    weight 2
    TCP_CHECK {
    connect_timeout 3
    }
    }
    }

    根据这种配置的例子,简单的守护进程将驱动内核使用
    以下信息:

    1.LVS server 拥有的名字:LVS_MAIN lvs_id LVS_MAIN

    2.通知:

    1. SMTP server 是:192.168.200.20

    2. SMTP connection timeout 设置为30秒

    3.通知邮件: admin@domain.com & 0633225522@domain.com

    Load balanced services:

    HTTP: VIP 192.168.200.15 port 80

    1.Load balancing:使用轮询调度算法使用NAT 转发,持久性连接是设置为50秒在每个TCP 服务上。

    如果你使用Linux Kernel 2.2 你需要制定NAT mask 来定义IPFW 伪装的粒度。延迟循环设置为30秒。

    2.Sorry Server.如果所有的servers 从VS的server pool里移除, 我们增加192.168.100.100 端口80

    来服务客户端的请求。

    3.Real server 192.168.100.2 端口80 被加权到2, Failure 检测会基于HTTP_GET 通过2 URLS.

    服务连接超时时间设置为3秒, real server 会被认为down了在3次尝试后。daemon 会等待2秒在重试前

    4.Real server 192.168.100.3 端口80 被加权为1.Failure 检测会基于HTTP_GET 通过1 URL。

    SSL VIP 192.168.200.15 端口443

  • 相关阅读:
    C#博客随笔之四:使用C#模拟办公网登录HttpClient的使用
    C#博客随笔之三:Linq in C#
    C#博客随笔之二:wp开发之弹出对话框
    C#博客随笔之一:使用C#的第一个WP程序
    Fedora15命令速查手册
    乐观是一种智慧
    完全教程 Aircrackng破解WEP、WPAPSK加密利器
    FreeBSD常用命令大全
    Linux 网络管理员指南——前言
    API
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351599.html
Copyright © 2020-2023  润新知