一、准备两台机器
node1 192.168.157.128 master 主
node2 192.168.157.129 backup 备
二、安装nginx
分别在两台机器上安装nginx
1. 安装nginx依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2. 下载nginx安装包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
3. 解压安装
- # 解压
- tar -zxvf nginx-1.18.0.tar.gz
- # 进入nginx目录
- cd nginx-1.18.0
- # 编译
- ./configure
- # 安装
- make & make install
4. 启动nginx
安装完成后nginx的目录为 /usr/local/nginx
- cd /usr/local/nginx
- #启动
- ./sbin/nginx
5. 查看是否启动成功
node1 128
node2 129

三、 安装keepalived
安装的目录在/etc/keepalived下
yum install -y keepalived
四、 脚本准备
在/usr/local/src目录下创建nginx_check.sh脚本,内容如下。
脚本所做的工作就是判断nginx是否还在运行,没有运行就重新启动一下nginx,如果重新启动还是失败的话,就停止keepalived服务。
- #!/bin/bash
- counter=$(ps -C nginx |grep -v PID |wc -l)
- if [ $counter == 0 ];then
- /usr/local/nginx/sbin/nginx
- sleep 3
- counter=$(ps -C nginx |grep -v PID |wc -l)
- if [ $counter == 0 ];then
- systemctl stop keepalived
- fi
- fi
五、 双机主从模式
双机主从模式:一台主服务器和一台热备服务器。正常情况下主服务器提供服务,只有当主服务器发生故障的时候热备服务器才会接管主服务器。但是当主服务器不出现故障的时候,则热备服务器就会处于浪费状态。
1. 编写/etc/keepalived/keepalived.conf
这里确定node1为master,node2 为 backup。
node1 配置如下
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- smtp_server 192.168.157.128
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
-
- vrrp_script chk_http_port {
- script "/usr/local/src/nginx_check.sh"
- interval 2
- weight 2
- }
-
- vrrp_instance VI_1 {
- state MASTER
- interface eno16777736
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.157.50
- }
- }
node2 配置如下
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- smtp_server 192.168.157.129
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
-
- vrrp_script chk_http_port {
- script "/usr/local/src/nginx_check.sh"
- interval 2
- weight 2
- }
-
- vrrp_instance VI_1 {
- state BACKUP
- interface eno16777736
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.157.50
- }
- }
2. 配置讲解
vrrp_script chk_http_port 用来配置脚本信息
script 脚本的路径
interval 检测脚本执行的时间间隔(秒)
weight 权重
vrrp_instance VI_1 实例配置
state 表示这台机器的身份 MASTER (主)BACKUP(备) 两种
interface 网卡名字,使用ifconfig查看
virtual_router_id 虚拟路由id,主备一致
advert_int 每隔多少秒发送心跳
virtual_ipaddress 虚拟地址,访问虚拟地址,会转到具体的nginx中配置的地址
3. 测试
修改index.html
node1 中修改标题为welcome to nginx master!
node2 中修改标题为welcome to nginx backup!
分别启动node1、node2中的nginx和keepalived
- #启动nginx
- /usr/local/nginx/sbin/nginx
- #启动keepalived
- systemctl start keepalived
访问虚拟ip 192.168.157.50

停止node1上的nginx和keepalived后访问192.168.157.50
- # 停止keepalived
- systemctl stop keepalived.service
- # 停止nginx
- /usr/local/nginx/sbin/nginx -s stop

4. 总结
双机主备模式下,只有master停止服务之后,从服务器才能提供服务。
六、 双机主主模式
双机主主模式:使用两台负载均衡服务器,互为主备,两台服务器都属于活动状态,只有当其中一台发生故障时由另一台接管故障服务器的请求。这种方案比较经济实惠。
1. 修改keepalived.conf配置,主要是增加一个 vrrp_instance,原来的MASTER变为BACKUP,原来的BACKUP变为MASTER
node1 的keepalived配置
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- smtp_server 192.168.157.128
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
-
- vrrp_script chk_http_port {
- script "/usr/local/src/nginx_check.sh"
- interval 2
- weight 2
- }
-
- vrrp_instance VI_1 {
- state MASTER
- interface eno16777736
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.157.50
- }
- }
- vrrp_instance VI_2 {
- state BACKUP
- interface eno16777736
- virtual_router_id 52
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.157.51
- }
- }
node2 的keepalived配置
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- smtp_server 192.168.157.129
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
-
- vrrp_script chk_http_port {
- script "/usr/local/src/nginx_check.sh"
- interval 2
- weight 2
- }
-
- vrrp_instance VI_1 {
- state BACKUP
- interface eno16777736
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.157.50
- }
- }
-
- vrrp_instance VI_2 {
- state MASTER
- interface eno16777736
- virtual_router_id 52
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.157.51
- }
- }
2. 重新启动nginx和keepalived
访问192.168.157.50 和 192.168.157.51,50得到的是master,51得到的是backup


七、总结
到此nginx+keepalived集群搭建完成,大家也去试试吧。