愿你得不到的都释怀
想要的都拥有

Keepalived+Nginx搭建

(Keepalived+Nginx)单实例主备模式

1.环境准备

CentOS :7.4

Keepalived :1.3.5

nginx :1.12.0

nginx-1 :192.168.178.8 (master)

nginx-2:192.168.178.9 (backup)

安装依赖包,临时关闭selinux 和firewalld

[root@8 /]# yum install gcc-c++ pcre-devel openssl-devel -y
[root@8 /]# setenforce 0
[root@8 /]# systemctl stop firewalld

2.在master和backup端安装nginx(也可以选择YUM安装,如已安装可以跳过进行Keepalived

[root@8 /]# cd /tools/
[root@8 tools]# ls
nginx-1.12.0.tar.gz
[root@8 tools]# tar -zxf nginxnx-1.12.0.tar.gz 
[root@8 tools]# ls
nginx-1.12.0  nginxnx-1.12.0.tar.gz
[root@8 tools]# cd nginxnx-1.12.0
[root@8 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=www --group=www  --with-http_stub_status_module --with-http_ssl_module
[root@8 nginx-1.12.0]# make&&make install
[root@8 sbin]# useradd www
[root@8 sbin]# /usr/local/nginx/sbin/nginx

以上nginxnx安装完成,打开浏览器访问 192.168.178.8 192.168.178.9 能访问到 Nginx页面

3.安装Keepalived,在master和backup端安装Keepalived,并配置Keepalived

yum install keepalived -y

3.1 配置Keepalived: MASTER keepalived 配置文件

! Configuration File for keepalived
global_defs {
  notification_email {
      support@jfedu.net
 }
    notification_email_from wgkgood@163.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30 
    router_id LB01                                 #(注意master和backup不可以一样)
 }

 vrrp_script chk_nginx {
    script  "/data/sh/check_ngx.sh"
    interval 2
    weight 2
 }
 # VIP1
 vrrp_instance VI_1 {
     state MASTER                                 #(注意master和backup不可以一样)
     interface ens33
     virtual_router_id 151
     priority 150                                 #(注意master和backup不可以一样)
     advert_int 5
     authentication {
         auth_typePASS
         auth_pass  1111

     }
     virtual_ipaddress {
         192.168.178.188
     }
     track_script {
     chk_nginx
    }
 }

3.2 配置 BACKUP keepalived 配置文件

! Configuration File for keepalived
global_defs {
  notification_email {
      support@jfedu.net
 }
    notification_email_from wgkgood@163.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30 
    router_id LB02                                #(注意backup和master不可以一样)
 }

 vrrp_script chk_nginx {
    script  "/data/sh/check_ngx.sh"
    interval 2
    weight 2
 }
 # VIP1
 vrrp_instance VI_1 {
     state BACKUP                                   #(注意backup和master不可以一样)
     interface ens33
     virtual_router_id 151
     priority 100                                   #(注意backup和master不可以一样)
     advert_int 5
     authentication {
         auth_typePASS
         auth_pass  1111

     }
     virtual_ipaddress {
         192.168.178.188
     }
      track_script {
     chk_nginx
    }
 }

4.在master和backup端编写检测Nginx脚本,目的是 检测Nginx是否存活如果不存活就关闭Keepalived

[root@8 /]# mkdir -p /data/sh/
[root@8 /]# cd /data/sh/

[root@8 sh]# cat << EOF >/data/sh/check_ngx.sh
> #!/bin/bash 
> #auto  check  nginx  process 
> #2019年6月7日12:45:49
> #by  author  xiaojian
> 
> NUM=`ps -ef |grep -v grep |grep -c nginx`
> if  [[ $NUM -eq 0 ]]  ;then
> systemctl stop keepalived
> sleep 3
> fi
> EOF


5.启动master 端查看keepalived 状态还有VIP地址

[root@8 sh]# systemctl start keepalived 
[root@8 sh]# tail -fn  10 /var/log/messages
[root@8 sh]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:0e:48:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.178.8/24 brd 192.168.178.255 scope global ens33  #master端本机IP地址
       valid_lft forever preferred_lft forever                  
    inet 192.168.178.188/32 scope global ens33                    #(VIP 地址)
       valid_lft forever preferred_lft forever
    inet6 fe80::ca0c:d857:75a7:d0e4/64 scope link 
       valid_lft forever preferred_lft forever
    inet6 fe80::661e:3774:68bb:3e82/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
[root@8 sh]#


5.1再看backup端

[root@9 ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fd:88:05 brd ff:ff:ff:ff:ff:ff
    inet 192.168.178.9/24 brd 192.168.178.255 scope global ens33    #backup端只有本机IP地址
       valid_lft forever preferred_lft forever
    inet6 fe80::661e:3774:68bb:3e82/64 scope link 
       valid_lft forever preferred_lft forever

6.测试是否成功

在master 端杀死nginx 会发现vip自动切换到backup端,此时backup会变成master,如下图

 

以上搭建实验的只是keealived单点主备模式,还可以做主主模式,当然了主主模式更好

 

 

 

赞(5) 打赏
转载请带上原链接:你为谁隐身 » Keepalived+Nginx搭建
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

不忘初心,方得始终!

页面存档标签云

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏