keepalived实现Nginx高可用

Nginx安装

  • rz上传Nginx到服务器
  • Nginx解压到指定目录
    tar -zxvf nginx-1.16.1.tar.gz -C /usr/local/src
    
  • 安装Nginx
    进入Nginx
     cd /usr/local/src/nginx-1.16.1/
    
    检查安装环境,并指定将来要安装的路径
    ./configure --prefix=/usr/local/nginx
    
    安装依赖
    yum -y install gcc pcre-devel openssl openssl-devel
    
    安装
    make && make install
    
    启动Nginx
    前面指定了Nginx的安装路径,现在只需要进入到安装路径下的sbin目录运行Nginx即可(默认加载的配置文件在/usr/local/nginx/conf/nginx.conf)
    /usr/local/nginx/sbin/nginx
    
    查看是否启动成功
    netstat -ntlp | grep 80
    
    配置nginx.conf文件实现负载均衡
    在http这个节下面配置一个叫upstream的,后面的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致。
    http {
        是在http里面的, 已有http, 不是在server里,在server外面
        upstream tomcats { 
            server hadoop1:8080 weight=1;#weight表示多少个
            server hadoop2:8080 weight=1;
            server hadoop3:8080 weight=1;
    	}
    	#卸载server里
    	location ~ .*\.(jsp|do|action) {    #拦截后缀名为.jsp .do .action的请求
    	    proxy_pass http://tomcats;        #tomcats是后面的tomcat服务器组的逻辑组号
    	}
    }
    
    在这里插入图片描述

keepalived安装

下载keepalived
在这里插入图片描述
keepalived解压到指定目录

tar -zxvf keepalived-2.0.15.tar.gz -C /usr/local/src/

进入到keepalived目录

cd /usr/local/src/keepalived-2.0.15/

指定keepalived安装录了

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

编译安装

make && make install

将keepalived添加到系统服务中

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp -R /usr/local/src/keepalived-2.0.15/keepalived/etc/init.d /etc/init.d/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived	
chkconfig keepalived on

添加到系统服务器可以通过以下命令操作keepalived

service keepalived start  # 启动keepalived
service keepalived stop  # 停止keepalived
service keepalived restart # 重启keepalived
service keepalived status # 查看keepalived状态

修改keepalived配置文件

  • MASTER节点
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   #注释掉这一行,不然ping不通
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_instance VI_1 {
    state MASTER   #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface ens33    #绑定虚拟IP的网络接口
    virtual_router_id 51   #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100   #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1  #组播信息发送间隔,两个节点设置必须一样
    authentication {    #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {    #指定虚拟IP, 两个节点设置必须一样
        192.168.189.140/24    #如果两个nginx的ip分别是192.168.33.61,,...62,则此处的虚拟ip跟它俩同一个网段即可
    }
}
  • BACKUP节点
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   #注释掉这一行,不然ping不通
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_instance VI_1 {
    state MASTER   #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface ens33    #绑定虚拟IP的网络接口
    virtual_router_id 51   #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 99   #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1  #组播信息发送间隔,两个节点设置必须一样
    authentication {    #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {    #指定虚拟IP, 两个节点设置必须一样
        192.168.189.140/24    #如果两个nginx的ip分别是192.168.33.61,,...62,则此处的虚拟ip跟它俩同一个网段即可
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值