`
AILIKES
  • 浏览: 187089 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

keepalived安装配置实现高可用

阅读更多
环境

角色

IP

用途

Master A

10.10.10.33

MySQL DB

Master B

10.10.10.36

VIP

10.10.10.199

KeepAlived VIP

安装 KeepAlived 
 
1.下载安装包:
# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

解压安装包到指定目录/opt/usr/

 # tar -zxvf keepalived-1.2.13.tar.gz -C /opt/usr/

# cd /opt/usr/keepalived-1.2.13/

创建安装目录:

# mkdir /opt/usr/keepalived

编译安装

# ./configure --prefix=/opt/usr/keepalived/

# make;make install

可能遇到问题:configure报错

configure: error:

  !!! OpenSSL is not properly installed on your system. !!!

  !!! Can not include OpenSSL headers files.            !!!

解决:

yum install -y openssl openssl-devel

 

配置KeepAlived
Create Control File

默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件

#A主机和B主机都操作

# mkdir /etc/keepalived

#cp安装生成的conf模板到/etc/keepalived

# cp /opt/usr/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

Master A配置
      Master A配置文件

#Master A配置文件

# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
 
global_defs {
   notification_email {
                15600499930@163.com
        }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MySQL_HA
}
 
vrrp_instance MySQL-HA{
    state MASTER  #state指定instance的初始状态,但这里指定的不算,还是得通过优先级竞选来确定。两台配置此处均是BACKUP。
    interface eth0 #实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
    virtual_router_id 51 #这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
     priority 100 #设置本节点的优先级,优先级高的为master,如另外一个节点配置为90,那此节点就是master
    advert_int 1  #检查间隔,默认为1秒
    nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {         #这里设置的就是VIP,也就是虚拟IP地址
        10.10.10.199
    }
}
 
virtual_server 10.10.10.199 3306{
    delay_loop 2    #每个2秒检查一次real_server状态
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50 #会话保持时间
    protocol TCP
 
    real_server 10.10.10.33 3306{
        weight 3
        notify_down /usr/local/MySQL/bin/MySQL.sh     #检测到服务down后执行的脚本
        TCP_CHECK {
            connect_timeout 3  #连接超时时间
            nb_get_retry 3        #重连次数
            delay_before_retry 3 #重连间隔时间
            connect_port 3306   #健康检查端口
        }
    }
}
 
}
b)     Master A编写notify_down脚本

# mkdir -p /usr/local/MySQL/bin/

# vi /usr/local/MySQL/bin/MySQL.sh

#!/bin/sh
pkill keepalived
sleep 10s
/etc/init.d/keepalived start
sleep 120s
/etc/init.d/mysql start

注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP

c)      Master A启动KeepAlived服务

~]# /opt/us/keepalived/sbin/keepalived –D

ps -ef|grep keep 

root      1567     1  0 17:47 ?        00:00:00 keepalived -D
root      1568  1567  0 17:47 ?        00:00:00 keepalived -D
root      1569  1567  0 17:47 ?        00:00:00 keepalived -D
root      3568  1693  0 18:14 pts/0    00:00:00 grep keep
# ip a|grep eth0 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 10.10.10.33/24 brd 10.10.10.255 scope global eth0
    inet 10.10.10.199/32 scope global eth0
d)     Master A测试keepalived服务

#停止MySQL服务

# /etc/init.d/mysql stop

Shutting down MySQL.....                                   [  OK  ]

#查看vip,发现VIP已经不存在

 ip a|grep eth0 

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    inet 192.168.56.101/24 brd 192.168.56.255 scope global eth1

#查看keepalive进程消失

# ps -ef|grep keep

root     29537  4894  0 20:34 pts/2    00:00:00 grep keep

#监控日志

# tail -f /var/log/messages

Dec  3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: TCP connection to [192.168.56.101]:3306 failed !!!

Dec  3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Removing service [192.168.56.101]:3306 from VS [192.168.56.111]:3306

Dec  3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Executing [/usr/local/MySQL/bin/MySQL.sh] for service [192.168.56.101]:3306 in VS [192.168.56.111]:3306

Dec  3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Lost quorum 1-0=1 > 0 for VS [192.168.56.111]:3306

Dec  3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Remote SMTP server [127.0.0.1]:25 connected.

Dec  3 20:34:09 MySQL1 Keepalived[29423]: Stopping Keepalived v1.2.13 (12/03,2014)

Dec  3 20:34:09 MySQL1 Keepalived_vrrp[29425]: VRRP_Instance(MySQL-HA{) sending 0 priority

Dec  3 20:34:09 MySQL1 Keepalived_vrrp[29425]: VRRP_Instance(MySQL-HA{) removing protocol VIPs.

Dec  3 20:34:09 MySQL1 Keepalived_healthcheckers[29424]: Netlink reflector reports IP 192.168.56.111 removed

Dec  3 20:34:09 MySQL1 avahi-daemon[1430]: Server startup complete. Host name is MySQL1-62.local. Local service cookie is 1197773774.

Dec  3 20:34:09 MySQL1 avahi-daemon[1430]: Withdrawing address record for 192.168.56.111 on eth1.

Dec  3 20:34:10 MySQL1 avahi-daemon[1430]: Service "MySQL1-62" (/services/ssh.service) successfully established.

Master B配置
a)    Master B配置文件

#跟DB1基本一致,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP

# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
 
global_defs {
   notification_email {
                15600499930@163.com
        }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MySQL_HA
}
 
vrrp_instance MySQL-HA{
    state MASTER  #state指定instance的初始状态,但这里指定的不算,还是得通过优先级竞选来确定。两台配置此处均是BACKUP。
    interface eth0 #实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
    virtual_router_id 51 #这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
     priority 90 #设置本节点的优先级,优先级高的为master,如另外一个节点配置为90,那此节点就是master
    advert_int 1  #检查间隔,默认为1秒
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {         #这里设置的就是VIP,也就是虚拟IP地址
        10.10.10.199
    }
}
 
virtual_server 10.10.10.199 3306{
    delay_loop 2    #每个2秒检查一次real_server状态
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50 #会话保持时间
    protocol TCP
 
    real_server 10.10.10.36 3306{
        weight 3
        notify_down /usr/local/MySQL/bin/MySQL.sh       #检测到服务down后执行的脚本
        TCP_CHECK {
            connect_timeout 3  #连接超时时间
            nb_get_retry 3        #重连次数
            delay_before_retry 3 #重连间隔时间
            connect_port 3306   #健康检查端口
        }
    }
}
 
 
}
b)   Master B编写notify_down脚本

# mkdir -p /usr/local/MySQL/bin/

# vi /usr/local/MySQL/bin/MySQL.sh

#!/bin/sh
pkill keepalived
sleep 10s
/etc/init.d/keepalived start
sleep 120s
/etc/init.d/mysql start
c)    Master B启动KeepAlived服务

# /opt/usr/keepalived/sbin/keepalived 

# ps -ef|grep keep 
root      1596     1  0 17:46 ?        00:00:00 keepalived -D
root      1597  1596  0 17:46 ?        00:00:00 keepalived -D
root      1599  1596  0 17:46 ?        00:00:00 keepalived -D
root      3817  2135  0 18:16 pts/0    00:00:00 grep keep

# ip a|grep eth0 

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 10.10.10.36/24 brd 10.10.10.255 scope global eth0
    inet 10.10.10.199/32 scope global eth0
d)   Master B测试keepalived服务

#停止MySQL服务

# /etc/init.d/mysql stop

Shutting down MySQL.....                                   [  OK  ]

#查看vip,发现VIP已经不存在

# ip a|grep eth0

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    inet 192.168.56.101/24 brd 192.168.56.255 scope global eth1

#查看keepalive进程消失

# ps -ef|grep keep

root     29537  4894  0 20:34 pts/2    00:00:00 grep keep

#监控日志

# tail -f /var/log/messages

Dec  4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: TCP connection to [192.168.56.102]:3306 failed !!!

Dec  4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Removing service [192.168.56.102]:3306 from VS [192.168.56.111]:3306

Dec  4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Executing [/usr/local/MySQL/bin/MySQL.sh] for service [192.168.56.102]:3306 in VS [192.168.56.111]:3306

Dec  4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Lost quorum 1-0=1 > 0 for VS [192.168.56.111]:3306

Dec  4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Remote SMTP server [127.0.0.1]:25 connected.

Dec  4 10:35:53 MySQL2 Keepalived[6732]: Stopping Keepalived v1.2.13 (11/24,2014)

Dec  4 10:35:53 MySQL2 Keepalived_vrrp[6734]: VRRP_Instance(MySQL-HA{) sending 0 priority

Dec  4 10:35:53 MySQL2 Keepalived_vrrp[6734]: VRRP_Instance(MySQL-HA{) removing protocol VIPs.

Dec  4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Netlink reflector reports IP 192.168.56.111 removed

Dec  4 10:35:54 MySQL2 avahi-daemon[1387]: Withdrawing address record for 192.168.56.111 on eth1.

4)    MySQL Client使用VIP连接数据库

~]# mysql -u root -h 10.10.10.199 -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2622

Server version: 5.6.21-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

5)    配置init.d/keepalived脚本

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

cp /opt/usr/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /opt/usr/keepalived/sbin/keepalived /sbin/

# /etc/init.d/keepalived -h

Usage: /etc/init.d/keepalived {start|stop|reload|restart|condrestart|status}

6)   配置开机启动
    chkconfig --list  keepalived
    chkconfig --add keepalived

    chkconfig --level 2345 keepalived on

 

 

分享到:
评论

相关推荐

    Keepalived+Nginx实现高可用Web负载均衡

    ### Keepalived+Nginx 实现高可用Web负载均衡 #### 场景需求 在现代互联网应用中,网站和应用程序需要处理大量的用户请求,并确保即使在服务器出现故障的情况下也能持续提供服务。为了满足这一需求,通常采用高可用...

    nginx+keepalived实现双机热备高可用

    二、keepalived配置 keepalived的配置文件是/etc/keepalived/keepalived.conf。该文件定义了keepalived的全局设置、VRRP脚本、VRRP实例等信息。在主服务器和从服务器上,keepalived的配置文件都需要进行相应的修改...

    Keepalived + Nginx 实现高可用 Web 负载均衡配置文件

    Keepalived + Nginx 实现高可用 Web 负载均衡配置文件,具体详情参见博文:http://blog.csdn.net/l1028386804/article/details/72801492

    高可用之Keepalived+Nginx实现高可用Web负载均衡

    "高可用之Keepalived+Nginx实现...4. 高可用之 Keepalived+Nginx 实现高可用 Web 负载均衡的步骤和配置。 5. VIPIP 主机名、Nginx 端口、默认主从关系等高可用之 Keepalived+Nginx 实现高可用 Web 负载均衡的关键技术。

    keepalived安装实现高可用的配置

    keepalived安装实现高可用的配置

    Keepalived+Nginx实现高可用Web负载均衡.docx

    在这个高可用架构中,我们使用 Keepalived 来实现 Nginx 的高可用,通过 Keepalived,我们可以将多个 Nginx 服务器组成一个高可用集群,使得我们的 web 服务具有高可用性。同时,我们还可以使用 Nginx 的负载均衡...

    Keepalived+Nginx+Tomcat 高可用集群搭建实战记录

    Keepalived 是一种高可用性解决方案,可以与 Nginx 和 Tomcat 配合使用来实现高可用集群。下面是 Keepalived+Nginx+Tomcat 高可用集群搭建的实战记录。 Keepalived 简介 Keepalived 是一种基于 Linux 的高可用性...

    分布式架构高可用架构-Keepalived+Nginx实现高可用Web负载均衡.pdf

    ### 分布式架构高可用架构-Keepalived+Nginx实现高可用Web负载均衡 #### 一、场景需求 在互联网应用中,随着用户数量的增长和技术的发展,单一服务器已难以满足日益增长的服务需求。分布式系统架构应运而生于解决...

    keepalived 配置redis高可用

    2. **配置vrrp_instance**:在Keepalived配置文件(通常为`/etc/keepalived/keepalived.conf`)中,定义一个vrrp_instance,指定虚拟IP地址、优先级、密码等信息。 3. **配置检查脚本**:编写一个用于检查Redis服务...

    LVS+KeepAlived+Nginx高可用实现方案.pdf

    在本文中,我们将详细介绍LVS、KeepAlived和Nginx的安装和配置过程,并提供一个完整的高可用实现方案。 一、LVS安装和配置 LVS可以通过源码安装或yum安装,安装完成后需要配置ipvsadm工具来管理LVS集群。 二、...

    基于ubuntu自动化安装Keepalived以实现高可用

    此外,这个脚本假设你正在安装Keepalived作为MASTER节点;如果你正在安装BACKUP节点,你需要更改state MASTER为state BACKUP,并相应地调整priority值。 此外,对于生产环境,你可能还需要考虑其他因素,如防火墙...

    源码安装nginx+keepalived实现HA(高可用)

    ### 源码安装Nginx与Keepalived实现高可用集群 #### 一、概述 在本篇文章中,我们将详细介绍如何通过源码安装的方式部署Nginx与Keepalived来构建一个高可用(High Availability,简称HA)的Web服务集群。这种方式...

    Keepalived+HAProxy实现MySQL高可用负载均衡的配置

    【MySQL高可用负载均衡配置】 在IT领域,确保数据库系统的高可用性和负载均衡是至关重要的,特别是对于大型企业或高流量网站。Keepalived和HAProxy是两个强大的工具,常用于实现这一目标,尤其是针对MySQL数据库。在...

    Keepalived安装配置.pdf

    Keepalived 安装配置详解 Keepalived 是一个基于 Linux 平台的高可用性解决方案,可以实现服务器...Keepalived 是一个功能强大且灵活的高可用性解决方案,通过正确的安装和配置,可以实现服务器的负载均衡和高可用性。

    采用Keepalived实现Redis双机高可用

    本文将详细介绍如何采用Keepalived来实现Redis的双机高可用配置。 Keepalived是一款开源软件,主要用于网络服务的负载均衡和故障检测。它通常与Nginx、HAProxy等配合使用,但在Redis场景下,Keepalived可以用来实现...

    Redis+keepalived搭建教程和高可用测试

    5. **配置Keepalived**:在主节点的Keepalived配置中,指定健康检查脚本并设置较高优先级;从节点配置较低优先级,等待故障转移。 6. **启动服务**:启动Redis和Keepalived服务,验证虚拟IP是否正常工作,主从复制...

    MyCat高可用负载均衡集群实现(HAProxy+Keepalived+MyCat)

    最后,部署Keepalived,配置VIP,确保在HAProxy节点发生故障时可以进行故障转移,以实现整个系统的高可用性。 在整个架构中,高可用性不仅仅体现在MyCat本身,还体现在负载均衡器HAProxy以及整个高可用集群的配置上...

Global site tag (gtag.js) - Google Analytics