• nginx+keepalived 主备搭建


    nginx1 ip:192.168.12.4 #MASTER
    nginx2 ip:192.168.12.10 #BACKUP
    nginx_vip :192.168.12.100

    原理可参考:
    http://www.keepalived.org/documentation.html
    系统为CentOS7

    1、配置一下yum源

    curl -L http://mirrors.aliyun.com/repo/Centos-7.repo > /etc/yum.repos.d/CentOS-Base.repo
    curl -L http://mirrors.aliyun.com/repo/epel-7.repo > /etc/yum.repos.d/epel.repo

    yum -y install keepalived nginx

    2、设置服务器基本环境

    关闭防火墙:iptables -F;service iptables save
    关闭seLinux:setenforce 0;sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

    3、配置一下nginx,好区分测试环境

    192.168.12.4:
    echo 192.168.12.4 > /usr/share/nginx/html/index.html

    192.168.12.10:
    echo 192.168.12.10 > /usr/share/nginx/html/index.html

    4、配置keepalived.conf

    [root@192.168.12.4]# cat keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    notification_email {
    notification@mail.com
    }
    notification_email_from fromnotification@mail.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    }

    vrrp_script check_nginx {
    script "sh /etc/keepalived/check_nginx.sh"
    interval 2
    }

    vrrp_instance VI_1 {
    state MASTER
    interface eth0
    mcast_src_ip 192.168.12.4
    virtual_router_id 51
    priority 100
    advert_int 2
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.12.100
    }
    track_script {
    check_nginx
    }
    }

    [root@192.168.12.10]# cat keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    notification_email {
    notification@mail.com
    }
    notification_email_from fromnotification@mail.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    }

    vrrp_script check_nginx {
    script "sh /etc/keepalived/check_nginx.sh"
    interval 2
    }

    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    mcast_src_ip 192.168.12.10
    virtual_router_id 51
    priority 99
    advert_int 2
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.12.100
    }
    track_script {
    check_nginx
    }
    }

    脚本check_nginx.sh内容如下

    #!/bin/bash

    A=`pgrep nginx|wc -l`
    if [ $A -eq 0 ];then
    /bin/systemctl start nginx.service
    if [ `pgrep nginx|wc -l` -eq 0 ];then
    /bin/systemctl stop keepalived.service
    fi
    fi


    5、启动
    在两个服务器执行启动命令
    service nginx start
    service keepalived start

    6、检查

    [root@192.168.12.4 keepalived]# ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:85:83:e8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.4/24 brd 192.168.12.255 scope global eth0
    valid_lft forever preferred_lft forever
    inet 192.168.12.100/32 scope global eth0
    valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe85:83e8/64 scope link
    valid_lft forever preferred_lft forever

    [root@192.168.12.10 keepalived]# ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:62:6a:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.10/24 brd 192.168.12.255 scope global eth0
    valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe62:6a50/64 scope link
    valid_lft forever preferred_lft forever


    仔细观察可以发现192.168.12.4的服务器多了一个IP,就是192.168.12.100,这个就是VIP
    再看日志发现主服务器日志有这下面一条:
    Jul 12 10:30:57 localhost Keepalived_vrrp[2510]: VRRP_Instance(VI_1) Entering MASTER STATE

    备服务器日志
    Jul 11 00:01:00 localhost Keepalived_vrrp[111937]: VRRP_Instance(VI_1) Entering BACKUP STATE

    综上主备已搭建成功

    7、模拟主nginx宕机:

  • 相关阅读:
    查看tomcat启动文件都干点啥---server对象
    Spring的AOP浅尝
    Spark1.3.0安装
    HMM的概率计算问题和预测问题的java实现
    C语言实现求字符串子集问题
    AdaBoost的java实现
    ID3决策树预测的java实现
    决策树ID3算法的java实现
    Naive Bayes在mapreduce上的实现
    Kmeans在MapReduce中的实现
  • 原文地址:https://www.cnblogs.com/zcx-python/p/7154566.html
Copyright © 2020-2023  润新知