nginx+keepalived

下载包 下载地址 https://www.keepalived.org/software/keepalived-2.0.20.tar.gz

解压缩 tar -zxvf keepalived-2.0.20.tar.gz

进入到解压完的目录 cd keepalived-2.0.20

准备环境

安装openssl-devel

yum -y install openssl-devel

安装gcc

yum -y install gcc

安装

./configure --prefix=/usr/local/keepalived

make && make install

安装完成后系统会在/usr/local/keepalived目录下生成 bin etc sbin share 这 4 个文件夹。 配置文件(keepalived.conf)在 /usr/local/keepalived/etc/keepalived 这个路径下

拷贝配置文件

mkdir -p /etc/keepalived/ *keepalived启动时默认去这个路径加载配置文件*

cp keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

复制后就可以操作/etc/keepalived/keepalived.conf文件了

配置文件

主配置文件

global_defs {

  router_id NodeA

  vrrp_skip_check_adv_addr

  vrrp_garp_interval 0

  vrrp_gna_interval 0

}

vrrp_script check_script {

        script "/etc/keepalived/check_nginx.sh 80"

        interval 10

}

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 131

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.152.131

    }

}

从配置文件

global_defs {

  router_id NodeB

  vrrp_skip_check_adv_addr

  vrrp_garp_interval 0

  vrrp_gna_interval 0

}

vrrp_script check_script {

        script "/etc/keepalived/check_nginx.sh 80"

        interval 10

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 131

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.152.131

    }

}

按同样的方法配置节点B并修改配置文件,可将A节点的配置文件复制到B节点,并修改以下几项:

router_id NodeB

state BACKUP

priority 99

其它项不必修改。

启动keepalived

/usr/local/keepalived/sbin/keepalived -D

查看进程

ps aux | grep keepalived


Keepalived 正常运行时,共启动 3 个进程,其中一个进程是父进程,负责监控其子进程;一个是 vrrp 子进程;另外一个是 checkers 子进程

查看虚拟IP是否已经绑定到网卡上

ip a

常用命令

systemctl restart keepalived

配置nginx主从切换

将写好的脚本复制到/etc/keepalived/文件夹下*就是keepalived的配置文件内配置的 vrrp_script check_script 节点*

脚本内容

#!/bin/bash

#author:liudan

#description:check nginx service

port=$1

count=0

for (( k=0; k<2; k++ ))

do

    check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://127.0.0.1:$port -o /dev/null )

    if [ "$check_code" != "200" ]; then

        count=$(expr $count + 1)

        sleep 3

        continue

    else

        count=0

        break

    fi

done

if [ "$count" != "0" ]; then

    #/usr/local/nginx/sbin/nginx -s restart

    exit 1

else

    exit 0

fi

配置mysql主从切换

配置文件

主配置文件

! Configuration File for keepalived

global_defs {

    router_id HA_MySQL #标识,双主相同

}

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 51 #分组,主备相同

    priority 100 #优先级,这个高一点则先把它作为master

    advert_int 1

    nopreempt #不主动抢占资源,设置非抢占模式

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.152.131

    }

}

virtual_server 192.168.152.131 3306 {

    delay_loop 2

    lb_algo wrr #带有权重的轮询

    lb_kind DR

    persistence_timeout 60 #同一IP的连接60秒内被分配到同一台真实服务器

    protocol TCP

    real_server 192.168.152.132 3306 {

        weight 3 #权重为3

        notify_down /data/keepalived_shutdown.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换, 自杀脚本.

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

从配置文件

! Configuration File for keepalived

global_defs {

    router_id HA_MySQL

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 90 #优先级,这个低一点

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress { #虚拟IP (VIP)

        192.168.152.131

    }

}

virtual_server 192.168.152.131 3306 {

    delay_loop 2 #每2秒检查一次real_server存活

    lb_algo wrr

    lb_kind DR

    persistence_timeout 60

    protocol TCP

    real_server 192.168.152.130 3306 {

        weight 3

        notify_down /data/keepalived_shutdown.sh

        TCP_CHECK {

            connect_timeout 10 #连接超时时间

            nb_get_retry 3 #重连次数

            delay_before_retry 3 #重连间隔时间

            connect_port 3306 #健康检查端口,配置自己mysql服务端口

        }

    }

}

你可能感兴趣的:(nginx+keepalived)