keepalived

keepalived官网:http://www.keepalived.org

Keepalived安装

方法一:yum方式

1、yum安装

        安装依赖包

[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel

[root@localhost ~]# yum install -y keepalived

2、初始化及启动

[root@localhost ~]# systemctl start keepalived   //启动keepalived

[root@localhost ~]# systemctl enable keepalived  //加入开机启动keepalived

[root@localhost ~]# systemctl restart keepalived  //重新启动keepalived

[root@localhost ~]# systemctl status keepalived   //查看keepalived状态

方法二、源码包

1、官网下载keepalived的最新版本,解压并安装

[root@master src]# pwd

/usr/local/src

[root@master src]# wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz

[root@master src]# tar xvf keepalived-2.0.7.tar.gz

[root@master src]# cd keepalived-2.0.7

[root@master keepalived-2.0.7]# ./configure --prefix=/usr/local/keepalived

[root@master keepalived-2.0.7]# make && make install

完成后会在以下路径生成:

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

/usr/local/etc/sysconfig/keepalived

/usr/local/sbin/keepalived

2、初始化及启动

# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)

[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived


# 将keepalived主程序加入到环境变量(安装目录下)

[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived


# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用

[root@localhost /]# cp /usr/local/src/keepalived-2.0.7/keepalived/etc/init.d/keepalived /etc/init.d/keepalived


# 将配置文件放到默认路径下

[root@localhost /]# mkdir /etc/keepalived

[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
  • 加为系统服务:chkconfig –add keepalived
  • 开机启动:chkconfig keepalived on
  • 查看开机启动的服务:chkconfig –list
  • 启动、关闭、重启service keepalived start|stop|restart

keepalived.conf 参数说明

全局配置

#就是告警邮箱配置
global_defs {
   # 指定keepalived在发生故障切换时发送email告警,一行一个邮件地址
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from  [email protected]   # 指定发件人
   smtp_server 192.168.200.1        # 指定smtp服务器地址
   smtp_connect_timeout 30         # 指定smtp连接超时时间
   router_id LVS_DEVEL                # 运行keepalived机器的一个标识,必须唯一

   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

 VRRP示例----虚拟IP

#vrrp 实例部分定义,VI_1自定义名称
vrrp_instance VI_1 {

   # 指定 keepalived 的角色,必须大写 可选值:MASTER|BACKUP
    state MASTER
    # 网卡设置,lvs需要绑定在网卡上,RealServer 绑定在回环口。区别:lvs对访问为外,realserver为内不易暴露本机信息
    interface eth0
    # 虚拟路由标识,是一个数字,同一个vrrp 实例使用唯一的标识,MASTER和BACKUP 的 同一个 vrrp_instance 下 这个标识必须保持一致
    virtual_router_id 51
    # 定义优先级,数字越大,优先级越高,范围:0~255。
    priority 100
    # 设定 MASTER 与 BACKUP 负载均衡之间同步检查的时间间隔,单位为秒,两个节点设置必须一样,默认1s
    advert_int 1
    # 设置验证类型和密码,两个节点必须一致
    authentication {
        auth_type PASS
        auth_pass 1111 ##真实生产环境下对密码进行匹配
    }

    # 设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
    virtual_ipaddress {
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
     # 当前节点成为master时,通知脚本执行任务
    # notify_master "/etc/keepalived/start_haproxy.sh start" 
    # 当前节点成为backup时,通知脚本执行任务
    # notify_backup "/etc/keepalived/start_haproxy.sh stop"
    # 当当前节点出现故障,执行的任务;
    # notify_fault  "/etc/keepalived/start_haproxy.sh stop"
}

LVS相关配置

没有用到LVS这个配置就跳过

# 定义RealServer 对应的VIP及服务端口,IP和端口之间用空格隔开  ----LVS相关配置,不用不用配置。。。
virtual_server 10.10.10.2 1358 {
    # 健康检查时间间隔
    delay_loop 6
    # lvs调度算法rr wrr lc wlc lblc sh dh
    lb_algo rr
    # 负载均衡转发规则 NAT DR RUN
    lb_kind NAT
    # 同一IP的连接50秒内被分配到同一台realserver
    persistence_timeout 50
    # 使用的协议
    protocol TCP
    # 备用机,所有realserver失效后启用
    sorry_server 192.168.200.200 1358

    real_server 192.168.200.2 1358 {
        # 权重,最大越高,lvs就越优先访问
        weight 1
        # keepalived的健康检查方式HTTP_GET | SSL_GET | TCP_CHECK | SMTP_CHECK | MISC
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            # 要坚持的URL,可以有多个
            url {
              # 具体路径
              path /testurl3/test.jsp
              # 检测效验码
              digest 640205b7b0fc66c1ea91c463fac6334d
              # 检测返回http状态码
              status_code 200         
            }
            # 3秒无响应超时
            connect_timeout 3
            # 重连次数3次
            retry 3
            # 重连间隔时间
            delay_before_retry 3
        }
    }

    real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

以上只是keepalived的安装和部分配置参数说明。记录keepalived不是为了LVS,主要是了解keepalived对nginx、web、MySQL等服务器的高可用一种解决方案。而且这个keepalived大多是工程做的,研发主要是了解就行。

推荐两个博主的keepalived

https://www.cnblogs.com/clsn/p/8052649.html#auto-id-25

https://www.jianshu.com/p/a6b5ab36292a

你可能感兴趣的:(集群方案,keepalived)