• Nginx+keepalived的高可用集群(三、Keepalived高可用部署)


    1.下载

    Keepalived官方下载地址: http://www.keepalived.org/download.html

    本次下载的是keepalived-1.4.4.tar.gz

    上传到服务器后解压到当前目录:

    tar -zxvf keepalived-1.4.4.tar.gz
    

    2.安装

    进入keepalived-1.4.4目录编译并安装

    #进入keepalived-1.4.4目录
    cd  keepalived-1.4.4
    #检查依赖库是否正确
    ./configure --prefix=/etc/keepalived
    #编译并安装
    make && make install 

     

    复制文件创建软连接

    因为没有用默认安装,需自行移动文件

    mkdir /etc/keepalived
    cp /usr/local/keepalived/etc/keepalived/keepalived.conf   /etc/keepalived/
    cp /usr/local/keepalived-1.4.4/keepalived/etc/init.d/keepalived    /etc/rc.d/init.d/keepalived
    cp /usr/local/keepalived/etc/sysconfig/keepalived   /etc/sysconfig/keepalived
    ln -s /home/bship/keepalived /usr/sbin/
    

       在/etc/keepalived目录创建Nginx启动脚本nginx_check.sh(该脚本用于检测nginx的进程

    #!/bin/bash
    A=`ps -C nginx --no-header | wc -l`
    if [ $A -eq 0 ];then
    	/usr/local/nginx/sbin/nginx -s stop
    	#尝试重新启动nginx
        /usr/bin/nginx 
    	#睡眠2秒
        sleep 2  
        if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
    	    #启动失败,将keepalived服务杀死。将vip漂移到其它备份节点
            killall keepalived 
        fi
    fi
    

     并给nginx_check.sh添加执行权限

    chmod +x /etc/keepalived/nginx_check.sh
    

    3.配置文件

    这里演示配置90服务器上的Keepalived,90服务器为master主机,91,92为备机。

    #进入Keepalived配置目录,进行Keepalived.conf的配置
    cd  /etc/keepalived

    master的keepalived.conf配置如下

    ! Configuration File for keepalived
    #全局配置 删掉了邮箱部分暂时用不到
    global_defs {
       # 负载均衡唯一标识,其实就是一个标识
       router_id Nginx_90
    }
    
    #vrrp_script 定时执行脚本
    vrrp_script chk_nginx {
        # 执行脚本路径
        script "/etc/keepalived/nginx_check.sh"
    	# 执行间隔之间默认1S
        interval 2
    	# 优先级(-254~254)默认2
        weight -20
    }
    
    #vrrp_instance 节点信息
    vrrp_instance VI_1 {
       #指定节点的处所状态MASTER | BACKUP
        state MASTER
        # 网卡信息 ip address查看网卡信息
        interface ens192
        # 让master和backup在同一个虚拟路由里,id必须相同
        virtual_router_id 190
        # 优先级 谁高谁将成为master
        priority 100
        # 设置为不抢占。默认是抢占的,当高优先级的机器恢复后,会抢占低优先级的机器成为MASTER,而不抢占,则允许低优先级的机器继续成为MASTER,
    #即使高优先级的机器已经上线。如果要使用这个功能,则初始化状态必须为BACKUP。 # nopreempt # 指定发送vrrp通告的时间间隔1S advert_int 1 authentication { # 认证方式 PASS(简单密码认证,推荐) | AH(IPSEC认证,不推荐) auth_type PASS # 密码最多8位 auth_pass 1111 } # 指定VIP地址,可虚拟多个VIP地址 virtual_ipaddress { 196.168.0.190 } track_script { chk_nginx } } virtual_server 196.168.0.190 443 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP }

    主机和备机的配置不一致(router_id和state需要修改

     开通端口

    iptables -I INPUT -p 112 -j ACCEPT
    

    4.启动服务

    分别启动master90和backup91,92

    1.启动方式一

    #设置开机自动启动
    systemctl enable keepalived.service 
    #取消开机自动启动    
    systemctl disable keepalived.service    
    #启动服务
    systemctl start keepalived.service    
    #重启服务   
    systemctl restart keepalived.service  
    #停止服务  
    systemctl stop keepalived.service
    #查看服务状态       
    systemctl status keepalived.service        

       启动方式二

    #启动 
    service keepalived start
    #停止
    service keepalived stop
    #重启
    service keepalived restart
    

    查看keepalived启动是否正常显示如下界面表示正常

     

     我们通过keepalived虚拟了一个VIP的地址是196.168.0.190,我们访问这个VIP地址,成功访问到了nginx

     

     尝试把90服务上的nginx挂掉,成功实现了切换到92服务器上,这样就实现了nginx的高可用。

      

     

     通过测试master漂移到了92服务器上

     

     

      

  • 相关阅读:
    分布式存储之GlusterFS
    应用中间件服务-weblogic
    生产环境tomcat升级新版本(tomcat漏洞修复)
    代码版本管理工具-gitlab
    链路追踪-skywalking
    去年前端校招面试的一些经验(阿里美团爱奇艺)
    mantis提交问题时报APPLICATION ERROR #27异常
    mantis无法上传附件,core/file_api.php: 1023异常
    centos7.2下安装mantis2.19.0
    linux常用命令
  • 原文地址:https://www.cnblogs.com/mabaoying/p/12668525.html
Copyright © 2020-2023  润新知