美文网首页
nginx主备+keepalived实现高可用

nginx主备+keepalived实现高可用

作者: 彩色的炮灰 | 来源:发表于2021-08-19 16:11 被阅读0次

    1、上传nginx安装包、nginx_check.sh脚本到 /opt目录。

    [root@localhost opt]# ll
    -rw-r--r--. 1 root root 808876 Jun 8 15:36 nginx-1.20.1-1.el7.ngx.x86_64.rpm
    -rw-r--r--. 1 root root 190 Aug 19 14:31 nginx_check.sh

    2、nginx_check.sh脚本内容:

    #!/bin/bash
    A=`ps -C nginx – no-header |wc -l`
    if [ $A -eq 0 ];then
      systemctl start nginx
      sleep 2
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
      fi
    fi
    

    3、执行安装。脚本添加可执行权限

    chmod +x nginx_check.sh
    sudo yum install -y nginx-1.16.1-1.el7.ngx.x86_64.rpm

    4、启动Nginx并设置开机启动

    systemctl start nginx.service
    systemctl enable nginx.service

    5、查看Nginx版本

    nginx -v

    查看Nginx启动状态

    systemctl status nginx.service
    nginx监听的是:80端口

    注意:以上步骤需要在2台服务器做同样操作。

    keepalived实现高可用

    1、直接使用yum安装

    yum -y install keepalived

    2、编辑keepalived配置文件
    vim /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id zz01
       vrrp_skip_check_adv_addr
       vrrp_garp_interval 0
       vrrp_gna_interval 0
    }
    
    vrrp_script chk_http_port {
        script "/etc/nginx/nginx_check.sh"
        interval 2
        weight 2
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface ens192
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
    
        track_script {
            chk_http_port
    }
    
        virtual_ipaddress {
            172.16.40.220
        }
    }
    

    3、启动keepalived、添加开机自启动

    systemctl start keepalived
    systemctl enable keepalived

    4、查看keepalived状态、

    systemctl status keepalived

    5、查看vip状态:ip a

    2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:50:56:a6:b9:1c brd ff:ff:ff:ff:ff:ff
        inet 10.2.0.53/24 brd 10.2.0.255 scope global ens192
           valid_lft forever preferred_lft forever
        inet 10.2.0.49/32 scope global ens192
           valid_lft forever preferred_lft forever
        inet6 fe80::e142:af5a:8b12:2259/64 scope link 
           valid_lft forever preferred_lft forever
    

    此时,网卡已经有虚拟ip,nginx+keepalived高可用已建立。

    相关文章

      网友评论

          本文标题:nginx主备+keepalived实现高可用

          本文链接:https://www.haomeiwen.com/subject/mhxabltx.html