【 Linux 】Keepalived实现双主模型高可用集群

要求:
    1. 两台web服务器安装wordpress,数据库通过nfs共享
    2. 使用keepalived实现双主模型

环境:
    主机:
        系统:CentOS6.7 x64
        1. node1: 192.168.2.11  node2: 192.168.2.12 vip: 192.168.2.200
        service iptables stop
        selinux: disabled

一、两台主机分别配置lamp架构,并使用nfs实现数据库共享

[[email protected] ~]# yum install nfs-utils httpd php php-mysql mysql-server -y     # yum安装软件包
[[email protected] ~]# mkdir -pv /mydata/{web,data}     # 创建共享web程序和数据库目录
mkdir: 已创建目录 "/mydata"
mkdir: 已创建目录 "/mydata/web"
mkdir: 已创建目录 "/mydata/data"
[[email protected] ~]# chown -R mysql:mysql /mydata/data/
[[email protected] ~]# chown -R apache:apache /mydata/web/
[[email protected] ~]# vim /etc/exports
/mydata *(rw,no_root_squash)
[[email protected] ~]# service nfs start
启动 NFS 服务:                                            [确定]
关掉 NFS 配额:                                            [确定]
启动 NFS mountd:                                          [确定]
正在启动 RPC idmapd:                                      [确定]
正在启动 RPC idmapd:                                      [确定]
启动 NFS 守护进程:                                        [确定]
[[email protected] ~]# showmount -e 192.168.2.11
Export list for 192.168.2.11:
/mydata *
[[email protected] ~]# mkdir /mydata
[[email protected] ~]# mount -t nfs 192.168.2.11:/mydata/ /mydata/
[[email protected] ~]# vim /etc/fstab
# 插入如下规则
192.168.2.11:/mydata    /mydata                 nfs     defaults        0 0
[[email protected] ~]# vim /etc/my.cnf
datadir=/mydata/data     # 修改mysql数据存放目录

[[email protected] ~]# vim /etc/my.cnf
datadir=/mydata/data     # 修改mysql数据存放目录

[[email protected] ~]# service mysqld start    # 启动数据库
[[email protected] ~]# ls /mydata/data/     # 确认是否生成数据
ibdata1  ib_logfile0  ib_logfile1  mysql  test
[[email protected] ~]# service mysqld start     # 第一次读取共享可能会慢点

[[email protected] ~]# wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip     # 下载discuz web程序
[[email protected] ~]# unzip Discuz_X3.2_SC_UTF8.zip
[[email protected] ~]# cp -a upload/* /mydata/web/     # 将web程序复制到nfs共享目录
[[email protected] ~]# chown -R apache:apache /mydata/web/     # 给与apache的执行权限
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf     # 修改apache配置文件
# 修改如下参数
行号
 276 ServerName localhost:80

 292 DocumentRoot "/mydata/web/"
 317 <Directory "/mydata/web">
 [[email protected] ~]# service httpd start
[[email protected] ~]# scp /etc/httpd/conf/httpd.conf node2:/etc/httpd/conf/     # 将配置文件拷贝至node2服务器
[[email protected] ~]# service httpd start
[[email protected] ~]# mysql     # 创建web程序的数据库
mysql> CREATE DATABASE ultrax;
mysql> GRANT ALL ON ultrax.* TO ‘ultrax‘@127.0.0.1 IDENTIFIED BY ‘123456‘;
mysql> FLUSH PRIVILEGES;
mysql> Bye

# node1 创建了库,node2最好重启下mysql

[[email protected] ~]# service mysqld restart

通过浏览器安装web程序

node1: 192.168.2.11

node2: 192.168.2.12

二、通过keepalived实现主备模式

[[email protected] ~]# yum install keepalived -y
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
# 全部内容如下
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id nodeA
}
vrrp_script chk_httpd {
   script "/etc/keepalived/bash/chk_httpd.sh"
   interval 5
   weight -10
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_httpd
    }
    virtual_ipaddress {
        192.168.2.200/24
    }
}
[[email protected] ~]# mkdir /etc/keepalived/bash     # 创建检查脚本存放目录
[[email protected] ~]# vim /etc/keepalived/bash/chk_httpd.sh # 编写检查脚本
# 使用该脚本必须有wget命令,如没有请执行:yum install wget -y
#!/bin/bash

pidfile=/var/lock/subsys/`basename $0`.pid
if [ -f $pidfile ] && [ -e /proc/`cat $pidfile` ] ; then
    exit 1
fi
trap "rm -rf $pidfile ; exit 0" 1 2 3 15
echo $$ > $pidfile
maxfails=3
fails=0
success=0

while [ 1 ]
do

    /usr/bin/wget --timeout=3 --tries=1 http://192.168.2.11/ -q -O /dev/null && ping -c1 192.168.2.1 &> /dev/null
    if [ $? -ne 0 ] ; then
        let fails=$[$fails+1]
        success=0
    else
        fails=0
        let success=$[$success+1]
    fi

    if [ $fails -ge $maxfails ] ; then
        fails=0
        success=0
        #check keepalived is running ? try to stop it
        /etc/init.d/keepalived status | grep 正在运行
        if [ $? -eq 0 ] ; then
            /usr/bin/logger -is "local service fails $maxfails times ... try to stop keepalived."
            /etc/init.d/keepalived stop 2>&1 | /usr/bin/logger
        fi

    fi

    if [ $success -gt $maxfails ] ; then
        #check keepalived is stopped ? try to start it
        /etc/init.d/keepalived status | grep 已停    # 脚本中这里要注意,如果系统是英文安装的为 grep stopped 如果是中文为 grep 已停
        if [ $? -eq 0 ] ; then
            /usr/bin/logger -is "service changes normal, try to start keepalived ."
            /etc/init.d/keepalived start
        fi
        success=0
    fi
    sleep 3

done

[[email protected] ~]# chmod +x /etc/keepalived/bash/chk_httpd.sh
[[email protected] ~]# cd /etc/keepalived/bash/
[[email protected] bash]# sh -x chk_httpd.sh     # 调试脚本,让脚本来启动keepalived 这样就做到了检查脚本的目的

[[email protected] bash]# ps uaxfww | grep keepalived | grep -v grep
root      26552  0.0  0.1 109656  1112 ?        Ss   22:35   0:00 /usr/sbin/keepalived -D
root      26555  0.0  0.2 111760  2776 ?        S    22:35   0:00  \_ /usr/sbin/keepalived -D
root      26556  0.0  0.2 111760  2128 ?        S    22:35   0:00  \_ /usr/sbin/keepalived -D
root      26569  0.0  0.1 106092  1284 ?        S    22:35   0:00 /bin/bash /etc/keepalived/bash/chk_httpd.sh

node2 安装keepalived及配置

[[email protected] ~]# yum install keepalived -y
[[email protected] ~]# scp /etc/keepalived/keepalived.conf node2:/etc/keepalived/    #直接使用node1的配置文件覆盖并修改
[[email protected] ~]# scp -r /etc/keepalived/bash node2:/etc/keepalived/     # 将检查脚本也直接复制过去,不过需要修改,切记!
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id nodeB
}
vrrp_script chk_httpd {
   script "/etc/keepalived/bash/chk_httpd.sh"
   interval 5
   weight -10
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_httpd
    }
    virtual_ipaddress {
        192.168.2.200/24
    }
}

[[email protected] bash]# vim chk_httpd.sh
# 修改如下一行代码
16     /usr/bin/wget --timeout=3 --tries=1 http://192.168.2.12/ -q -O /dev/null && ping -c1 192.168.2.1 &> /dev/null

[[email protected] bash]# ps auxfww | grep keepalived | grep -v grep
root       3442  0.0  0.2 110276  1380 ?        Ss   02:18   0:00 /usr/sbin/keepalived -D
root       3444  0.0  0.6 112380  2980 ?        S    02:18   0:00  \_ /usr/sbin/keepalived -D
root       3446  0.0  0.4 112380  2192 ?        S    02:18   0:00  \_ /usr/sbin/keepalived -D
root       3454  0.0  0.2 106064  1420 ?        S    02:18   0:00 /bin/bash /etc/keepalived/bash/chk_httpd.sh

三、测试

node1:
[[email protected] ~]# ip a
    inet 192.168.2.11/24 brd 192.168.2.255 scope global eth0
    inet 192.168.2.200/24 scope global secondary eth0

[[email protected] ~]# service httpd stop     # 停止node1的web服务

node2:
[[email protected] bash]# ip a
    inet 192.168.2.12/24 brd 192.168.2.255 scope global eth0
    inet 192.168.2.200/24 scope global secondary eth0

测试成功。

四、练习小结:
    该服务器架构仅仅只是用来测试,这里存在很多问题:
        1. 如果node1节点down掉了,nfs共享也就断掉了
        2. keepalived也只是仅仅对http做了检测,如果mysql服务down掉,是无法进行主备切换的。

经过调整,可以使用如下架构:

时间: 2025-01-13 13:17:02

【 Linux 】Keepalived实现双主模型高可用集群的相关文章

KeepAlived双主模式高可用集群

一.keepalived简介 keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测:通过共用的虚拟IP地址对外提供服务:每个热备组内同一时刻只有一台主服务器提供服务,其他服务器处于冗余状态,若当前在线的服务器宕机,其虚拟IP地址将会被其他服务器接替(优先级决定接替顺序),实现高可用为后端主机提供服务.   二.keepalived组件 Keepalived组件介绍 core:ke

基于keepalived实现双主模型高可用lvs

实验环境,使用的操作系统CentOS6.5: Director: node1:IP 172.16.103.2 安装keepalived VIP:172.16.103.20 node2:IP 172.16.103.3 安装keepalived VIP:172.16.103.30 RS: RS1:IP 172.16.103.1 提供httpd服务 RS2:IP 172.16.103.4 提供httpd服务 实验效果: 前端的两台Director运行keepalived,自动生成各自的一组lvs规则,

MM(主主数据库)+keepalived主备高可用集群

博客分享的第一篇技术文章: 项目主要搭建:主主数据库高可用集群搭建. 数据库互为主备,应用技术:MM+keepalived 使用的是虚拟机搭建的实验向大家展示: 数据库1:192.168.4.7 数据库2:192.168.4.77 VIP:192.168.4.68 web1:192.168.4.69 web2:192.168.4.70 一.安装mysql,部署主主同步结构. 直接yum安装 配置主主同步: 由于主数据库192.168.4.7里面存放着数据,所以需要先导出数据,方法很多,我们采取m

双主模型高可用负载均衡集群的实现(keepalived+lvs-dr)

实现keepalived双主模型lvs高可用集群 一.拓扑图 二.环境准备 两台负载均衡调度器,两台web服务器. 调度器A环境: VS:一张网卡 DIP:192.168.0.7/24 VIP(主):192.168.0.200 VIP(备):192.168.0.201 软件包:yum install -y keepalived ipvsadm nginx(作用:sorry-server服务) 调度器B环境: VS:一张网卡 DIP:192.168.0.8/24 VIP(主):192.168.0.

Keepalived+LVS+nginx搭建nginx高可用集群

nginx是一款非常优秀的反向代理工具,支持请求分发,负载均衡,以及缓存等等非常实用的功能.在请求处理上,nginx采用的是epoll模型,这是一种基于事件监听的模型,因而其具备非常高效的请求处理效率,单机并发能力能够达到上百万.nginx接收到的请求可以通过负载均衡策略分发到其下一级的应用服务器,这些服务器一般是以集群方式部署的,因而在性能不足的情况下,应用服务器可以通过加机器的方式扩展流量.此时,对于一些特大型的网站,性能的瓶颈就来自于nginx了,因为单机的nginx的并发能力是有上限的,

keepalived+mysql双主复制高可用方案

MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换.而Keepalived通过虚拟IP,实现了双主对外的统一接口以及自动检查.失败切换机制.联合使用,可以实现MySQL数据库的高可用方案. 实验环境:OS:centos 6.x x86_64系统MySQL版本: :mysql 5.6.22   64 位A: master :192.168.79.3 3306B: slave :192.168.

activitmq+keepalived+nfs 非zk的高可用集群构建

nfs 192.168.10.32 maast 192.168.10.4 savel 192.168.10.31 应对这个需求既要高可用又要消息延迟,只能使用变态方式实现 nfs部署 #yum install nfs-utils rpcbind #vim /etc/exports /data/activemq 192.168.10.31(rw,sync,no_root_squash,no_all_squash) /data/activemq 192.168.10.4(rw,sync,no_roo

keepalived双主模型高可用+lvs-实例

拓扑图: 环境准备: Centos6.5x86_64 关闭防火墙和Selinux node5: eth0:192.168.1.190/24   VIP:192.168.1.121/24 node1:eth1:192.168.1.191/24   VIP:192.168.1.122/24 node2:RIP:eth0: 192.168.19.2/24 node3:RIP:eth0: 192.168.19.3/24   所有节点网关/DNS都为:192.168.1.1 每个服务器的hosts文件 #

keepalived+nginx 双主模型实现高可用服务

一.keepalived的工作原理 keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗协议. 虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个虚拟路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为mas