CenterOS7下安装 nginx1.8+keepalived2.10高可用

 

 

 

1.首先安装环境准备

 yum install popt-devel openssl openssl-devel libssl-dev libnl-devel popt-devel -y
 yum -y install kernel kernel-devel

2.上传nginx至服务器.解压已经上线好的安装包

tar -zxvf nginx-1.18.0.tar.gz 

3.进入解压目录进行编译


   ./configure --user=nginx  --group=nginx --prefix=/application/nginx-1.18.0  --with-http_stub_status_module --with-http_ssl_module

4.添加用户组 nginx

 useradd nginx -s /sbin/nologin -M

启动nginx

 /application/nginx-1.18.0/sbin/nginx #启动nginx

查看进程 ,nginx安装完成 keeplived 安装

  安装准备  keepalived-2.0.10.tar.gz

   


  解压 文件

tar -zxvf keepalived-2.0.10.tar.gz 
cd keepalived-2.0.10/
进行解压目录进行编译

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

创建keeplived根目录,拷贝文件到想到的目录

   mkdir /etc/keepalived/
   cp /root/soft/keepalived-2.0.10/keepalived/etc/init.d/keepalived /etc/init.d/ #执行命令
   /root/soft/keepalived-2.0.10/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
   cp /root/soft/keepalived-2.0.10/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ #配置文件路径 


 启动keepalived
  service keepalived start

设置开机启动
  chkconfig keepalived on

作相同的配置:整台主机

master slave
192.168.1.110 192.168.1.111

 

虚拟ip 192.168.1.188

master配置

[root@nginx02 soft]# vim /etc/keepalived/keepalived.conf 

global_defs {
   notification_email {
178916811--@wqq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   notification_email {
178916811--@wqq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_01   #serverid相当于
   #vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_garp_interval 0
   #vrrp_gna_interval 0
}


vrrp_script chk_nginx {
    script "/application/nginx-1.18.0/check_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100  #权重 差50主从
    advert_int 1
    authentication {
auth_type PASS
auth_pass 1111
    }
    virtual_ipaddress {
      192.168.1.188/24  #虚拟VIP
   }
   track_script {
chk_nginx
    }

slave配置

global_defs {
   notification_email {
178916811--@wqq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_02  #标识id 下面的全部注释
   #vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_garp_interval 0
   #vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/application/nginx-1.18.0/check_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP  #从节点backUP 主节点 MASTER
    interface eth0
    virtual_router_id 51  #不要改,主从到一致
    priority 50   #相当于权重
    advert_int 1
    authentication {
auth_type PASS
auth_pass 1111
    }
    virtual_ipaddress {
192.168.1.188/24    #准备好了虚拟vip,两边一致
    }

track_script {
chk_nginx
   }
}

重启keepalived 

 service keepalived stop 

 service keepalived start

进行主机切换

ip addr

[root@nginx01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 group default qlen 1000
    link/ether fa:16:3e:0c:16:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.110/24 brd 10.18.51.255 scope global noprefixroute dynamic eth0
       valid_lft 57780sec preferred_lft 57780sec
    inet 192.168.1.188/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::b77f:a5a3:fce9:b068/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::6075:b0ef:4dc0:160/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::83f4:aec2:32a8:5aa6/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever

虚拟VIP已经绑定,停掉master ,slave自动 接管

[root@nginx01 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 group default qlen 1000
    link/ether fa:16:3e:0c:16:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.111/24 brd 10.18.51.255 scope global noprefixroute dynamic eth0
       valid_lft 57780sec preferred_lft 57780sec
    inet 192.168.1.188/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::b77f:a5a3:fce9:b068/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::6075:b0ef:4dc0:160/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::83f4:aec2:32a8:5aa6/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever

脚本   script "/application/nginx-1.18.0/check_nginx.sh"

#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/application/nginx-1.18.0/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi

 

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值