zoukankan      html  css  js  c++  java
  • 使用Keepalived实现linux高可用集群

    安装

    apt install libipset-dev  keepalived -y
    

    创建账户

    useradd -s/usr/sbin/nologin -M -g root keepalived_script
    

    修改Linux

    vim /etc/sysctl.conf 
    #在末尾追加一行:net.ipv4.ip_nonlocal_bind=1
    

    创建Keepalived配置文件

    vim /etc/keepalived/keepalived.conf
    
    
    vrrp_script chk_appsvc {
        #定义一个脚本文件,这个脚本文件将用来检测本机是否可用,如果可用则返回0
        script /etc/keepalived/keepalived-check-appsvc.sh
        interval 1
        fall 2
        rise 2
    }
    
    vrrp_instance VI_1 {
        #选择一个网卡
        interface ens7
     
        authentication {
            auth_type PASS
            auth_pass secr3t
        }
    
        virtual_router_id 51
    
        virtual_ipaddress {
            10.1.96.100 #整个集群的对外地址
        }
    
        track_script {
            chk_appsvc
        }
        
        #状态:MASTER  BACKUP STOP FAULT
        state MASTER
        priority 101
    
        unicast_src_ip 10.1.96.5 #本机地址(DIP)
        unicast_peer {
            10.1.96.3 #对端地址,其它机器的ip(DIP)
        }
        #状态切换动作脚本,定义了状态切换时执行的动作
        notify /etc/keepalived/keepalived-action.sh
    }
    

    创建状态切换动作脚本

    vim /etc/keepalived/keepalived-action.sh
    chmod +x /etc/keepalived/keepalived-action.sh
    
    #!/bin/bash
    
    TYPE=$1
    NAME=$2
    STATE=$3
    
    case $STATE in
    	"MASTER")
    		  systemctl start nginx
    		  exit 0
    		  ;;
    	"BACKUP"|"STOP")
    		       #systemctl stop nginx
    		  exit 0
    		  ;;
    	"FAULT")
    		       #systemctl stop nginx
    		  exit 0
    		  ;;
    	*)
    		  exit 1
    		  ;;
    esac
    
    #当某个服务器成为MASTER的时候,启动nginx服务。
    
    

    创建状态检测脚本

    vim /etc/keepalived/keepalived-action.sh
    chmod +x /etc/keepalived/keepalived-check-appsvc.sh
    
    #!/bin/bash
    exit 0
    
    #返回0表示当前主机状态正常.如果返回非0,或者设备无法连接,就会触发转移.
    
  • 相关阅读:
    java8新特性→方法和构造函数引用:替代Lambda表达式
    java8新特性→Stream流:用于解决已有集合类库既有的弊端
    java8新特性→函数式接口
    java8新特新→Lambda表达式
    子查询
    Vue之监听数据变化watch、computed、methods
    Vue路由-使用命名视图实现经典布局
    Vue路由-使用children属性实现路由
    Vue之路由传参
    Vue路由之touter-link、router-direct的使用
  • 原文地址:https://www.cnblogs.com/DragonStart/p/8183995.html
Copyright © 2011-2022 走看看