nginx部署高可用集群

节点IP服务单主实例
keepalived0110.0.0.24keepalived+nginxmaster
keepalived0210.0.0.8keepalived+nginxslave
web0110.0.0.13nginx
web0210.0.0.14nginx
client10.0.0.15测试

实验一:部署keepalived+nginx集群(一主一从)

1.准备前工作

 关闭防火墙
systemctl stop firewalld
关闭selinux
setenforce 0

2.master和slave安装 keepalived软件

[root@master ~]# yum -y install  keepalived pcre-devel
[root@slave ~]# yum -y install  keepalived pcre-devel

3.修改配置文件(master和slave都执行) 

a.为了防止修改错误,先进性备份工作

 [root@master ~]# yum -y install  keepalived pcre-devel
[root@slave ~]# yum -y install  keepalived pcre-devel

1>修改master

 

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 18.192.16.101   ##SMTP配置地址,可以不进行修改
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
}
vrrp_script chk_nginx {      ##指定nginx的检查脚本,用于启动nginx
script /root/check_ng.sh   ##指定脚本位置
interval 3   
}

 vrrp_instance VI_1 {
state MASTER     #指定为MASTER
interface ens33   #指定接口
virtual_router_id 52
priority 100    #指定优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.200     #指定虚拟IP地址(根据自己的网段进行设置)
}
track_script {
chk_nginx       #调用nginx管理脚本
}
}
 

2>配置slave

 

! 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.1.102      #广播smtp
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   }
   vrrp_script chk_nginx {
     script  "/root/check_ng.sh"   #用于检查本地的Nginx是否存活
     interval 3         #检查的间隔次数
   
}

vrrp_instance VI_1 {
    state BACKUP       #指定为BACKUP
    interface ens33      
    virtual_router_id 52    #使用非默认的路由器ID
    priority 90         #值越大优先级越高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.200          # 虚拟IP地址 
    } 
    track_script {
     chk_nginx
   }
}
 

3> 编写/root/check_ng.sh(用于检查keepalived中nginx的状态)

[root@master ~]# cat check_ng.sh 
 #!/bin/bash

d=`date --date today +%Y%m%d_%H:%M:%S`

n=`ps -C nginx --no-heading|wc -l`

if [ $n -eq "0" ]; then
       systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
       echo "$d nginx,keepalived will stop" >> /var/log/check_ng.log
       systemctl stop keepalived
  fi
fi

4.在web端安装httpd作为测试

[root@web01 ~]# yum -y install httpd
[root@web01 ~]# cd /var/www/html
[root@web01 html]echo 10.0.0.13 > index.html
[root@web01 ~]#ststemctl restart httpd

[root@web02 ~]# yum -y install httpd
[root@web02 ~]# cd /var/www/html
[root@web02 html]echo 10.0.0.14 > index.html
[root@web02 ~]#ststemctl restart httpd

5.配置nginx作为转发器代理服务

 [root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
upstream web {
   server 10.0.0.13:80;      #在http中添加这段配置
   server 10.0.0.14:80;

}
server {

 location / {
      proxy_pass http://web; #添加这段配置
}
[root@nginx ~]#systemctl restart nginx

 6.启动keepalived

[root@master ~]# systemctl restart  keepalived

7.模拟master挂掉后,查看IP是否能切换

 

实验二:部署nginx+keepalived 集群(多主)

实验环境延续上面,只需修改keepalived的配置

1.修改10.0.0.24的配置,再新增一个vip的配置

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication { 
        auth_type PASS
        auth_pass 1111
    }     
    virtual_ipaddress {
        10.0.0.200
    } 
   track_script {
     chk_nginx
   }
}
 ##增加一个slave的配置
vrrp_instance VI_2 { 
    state BACKUP
    interface ens33
    virtual_router_id 53
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.210
    }
   track_script {
     chk_nginx
   }
}

修改10.0.0.8的配置,再新增一个VIP的配置

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.200
    }
  track_script {
     chk_nginx
   }
}

#增加一个master的配置
vrrp_instance VI_2 {    
    state MASTER
    interface ens33
    virtual_router_id 53
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.210
    }
  track_script {
     chk_nginx
   }
}

2个服务器的配置互为主备,也就是10.0.0.24上的10.0.0.200为VIP,而10.0.0.8上的10.0.0.210为VIP地址

重启keepalived服务,查看地址

[root@master ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:61:00:d1 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.24/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.200/32 scope global ens33   #VIP地址
       valid_lft forever preferred_lft forever
    inet6 fe80::c70b:d801:199:1f06/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@slave ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:30:c4:28 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.8/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 10.0.0.210/32 scope global ens33     #VIP地址
       valid_lft forever preferred_lft forever
    inet6 fe80::ba87:58a3:a30c:bf23/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

当master服务器的keepalived停止后,他的vip地址会飘到10.0.0.8/上

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值