• 关闭防火墙
systemctl stop firewalld.service #临时关闭,重启失效
systemctl disable firewalld.service  #禁止开机启动

详情参考:

keepalived+nginx实现高可用

安装keepalived命令 yum -y install keepalived

安装后配置文件所在位置/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 LVS_DEVEL
 #  vrrp_skip_check_adv_addr
 #  vrrp_strict
 #  vrrp_garp_interval 0
 #  vrrp_gna_interval 0
}
vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh"
        interval 2  ## 检测时间间隔
        weight -20  ## 如果条件成立,权重-20
}
vrrp_instance VI_1 {
    state MASTER # 标识为主服务
    interface ens192 #绑定虚拟机的IP,这个注意
    virtual_router_id 51 # 虚拟路由id,和从机保持一致
    #mcast_src_ip 192.168.1.93  #本机ip
    priority 100 #权重,需要高于从机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   track_script {
                chk_nginx ## 执行 Nginx 监控的服务
        }
    virtual_ipaddress {
        192.168.1.99 #/32 brd 255.255.255.0 dev ens33 label ens33:vip #虚拟IP地址
    }
}

从机配置

! 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 dreamer1
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}
vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
        interval 2 ## 检测时间间隔
        weight -20 ## 如果条件成立,权重-20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192  #这个注意
    virtual_router_id 51
    #mcast_src_ip 192.168.1.94 ## 本机 IP 地址 
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
        track_script {
                chk_nginx ## 执行 Nginx 监控的服务
        }
    virtual_ipaddress {
        192.168.1.99
    }
}
service keepalived start
service keepalived stop

/etc/keepalived/nginx_check.sh

增加执行权限chmod +x /etc/keepalived/nginx_check.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/local/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /usr/sbin/keepalived stop
    fi
fi

wget http://nginx.org/download/nginx-1.16.1.tar.gz

tar -zxvf nginx-1.16.1.tar.gz -C /root

yum install pcre

yum install pcre-devel

yum install zlib

yum install zlib-devel

yum -y install openssl openssl-devel

./configure --with-stream --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make && make install

安装后nginx目录/usr/local/nginx

nginx配置文件目录/usr/local/nginx/conf/nginx.conf

nginx热更新/usr/local/nginx/sbin/nginx -s reload

Copyright © TouchNet 2015 all right reserved,powered by Gitbook最后修订时间: 2022-06-21 09:21:06

results matching ""

    No results matching ""