• Keepalived高可用实战笔记


    Keepalived高可用实战笔记

    创建虚拟机
    vmcentos[0]=CentOS7X64-keep01
    vmcentos[1]=CentOS7X64-keep02
    
    
    • 1
    • 2
    • 3
    parentPath="/media/WNTime/本地磁盘/VirtualMachine.Spaces"
    vmsour="$parentPath/CentOS7X64.Core/CentOS7X64.Core.vmx"
    for item in ${vmcentos[*]}; 
    do
    	vmdest="$parentPath/$item/$item.vmx"
    	echo "Clone: $vmdest"
    	vmrun -T ws clone $vmsour  $vmdest  full -cloneName=$item
    	sleep 5s;
    done;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    快照备份
    parentPath="/media/WNTime/本地磁盘/VirtualMachine.Spaces"
    for item in ${vmcentos[*]}; 
    do
    	vmdest="$parentPath/$item/$item.vmx"
    	vmshot=`date +%Y%m%d`
    	echo "Snapshot: $vmdest"
    	vmrun -T ws snapshot $vmdest $vmshot
    	vmrun -T ws listSnapshots $vmdest
    	sleep 5s;
    done;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    启动虚拟机
    parentPath="/media/WNTime/本地磁盘/VirtualMachine.Spaces"
    for item in ${vmcentos[*]}; 
    do
    	vmdest="$parentPath/$item/$item.vmx"
    	vmshot=`date +%Y%m%d%H%M`
    	echo "Start: $vmdest"
    	vmrun -T ws start $vmdest
    	sleep 5s;
    	read -p "按任意键继续..."
    done;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    挂起虚拟机
    parentPath="/media/WNTime/本地磁盘/VirtualMachine.Spaces"
    for item in ${vmcentos[*]}; 
    do
    	vmdest="$parentPath/$item/$item.vmx"
    	vmshot=`date +%Y%m%d%H%M`
    	echo "Suspend: $vmdest"
    	vmrun -T ws suspend $vmdest
    	sleep 5s;
    done;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    还原虚拟机
    parentPath="/media/WNTime/本地磁盘/VirtualMachine.Spaces"
    vmshot=`date +%Y%m%d`
    for item in ${vmcentos[*]}; 
    do
    	vmdest="$parentPath/$item/$item.vmx"
    	vmshot=`date +%Y%m%d`
    	echo "Revert: $vmdest"
    	vmrun -T ws revertToSnapshot $vmdest $vmshot
    	sleep 5s;
    done;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    关闭虚拟机
    parentPath="/media/WNTime/本地磁盘/VirtualMachine.Spaces"
    for item in ${vmcentos[*]}; 
    do
    	vmdest="$parentPath/$item/$item.vmx"
    	vmshot=`date +%Y%m%d%H%M`
    	echo "Stop: $vmdest"
    	vmrun -T ws stop $vmdest
    	sleep 5s;
    done;	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    实验网络规划
    KeepalivedKeepalived
    网段keep-101keep-102
    192.168.86.0(NAT模式)192.168.88.101192.168.88.102
    VIP1192.168.86.251(MASTER)192.168.86.251(BACKUP)
    VIP2192.168.86.252(BACKUP)192.168.86.252(MASTER)

    注意:一定要关闭防火墙,或是添加相关端口!

    配置hostname及ipaddress
    ## 101
    # vim /etc/sysconfig/network-script/ifcfg-en33
    sudo hostnamectl set-hostname keep-101
    sudo sed -i 's/192.168.86.6/192.168.86.101/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sudo systemctl restart network
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    ## 102
    # vim /etc/sysconfig/network-script/ifcfg-en33
    sudo hostnamectl set-hostname keep-102
    sudo sed -i 's/192.168.86.6/192.168.86.102/g' /etc/sysconfig/network-scripts/ifcfg-ens33
    sudo systemctl restart network
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    安装keepalived

    keep-101, keep-102

    sudo yum makecache
    sudo yum install -y wget
    sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
    sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    sudo yum makecache
    
    wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz --no-check-certificate
    sudo yum install -y  libnl libnl-devel libnl3-devel net-snmp-devel libnfnetlink-devel gcc make openssl-devel popt-devel curl
    
    tar -xzf keepalived-2.1.5.tar.gz
    cd keepalived-2.1.5/
    ./configure --prefix=/usr/local/keepalived --sysconf=/etc
    make && sudo make install
    # 测试安装
    /usr/local/keepalived/sbin/keepalived -v
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    安装成功

    ls -l /etc/keepalived/
    
    
    • 1
    • 2

    在这里插入图片描述

    配置系统服务

    keep-101, keep-102

    # ~/keepalived-2.1.5 是解压出来的文件夹,根据你的解压路径来,不是安装目录
    sudo cp ~/keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/
    sudo cp ~/keepalived-2.1.5/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
    
    sudo systemctl daemon-reload
    sudo systemctl enable keepalived
    sudo systemctl status keepalived
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    高可用配置

    keep-101

    global_defs {
       router_id keep_vip1
    }
    
    vrrp_instance VIP_251 {
        # 主用机设置为 MASTER
        state MASTER
        interface ens33
        virtual_router_id 251
        priority 100
        advert_int 1
        nopreempt
        #preempt_delay 300
        authentication {
            auth_type PASS
            auth_pass 12345678
        }
        virtual_ipaddress {
            192.168.86.251
        }
    }
    
    vrrp_instance VIP_252{
        # 主用机设置为 BACKUP
        state BACKUP
        interface ens33
        virtual_router_id 252
        priority 90
        advert_int 1
        nopreempt
        #preempt_delay 300
        authentication {
            auth_type PASS
            auth_pass 12345678
        }
        virtual_ipaddress {
            192.168.86.252
        }
    }
    
    /usr/local/keepalived/sbin/keepalived -t
    sudo systemctl restart keepalived
    sudo systemctl status keepalived
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    keep-102

    global_defs {
       router_id keep_vip2
    }
    
    vrrp_instance VIP_251 {
        # 主用机设置为 BACKUP
        state BACKUP
        interface ens33
        virtual_router_id 251
        priority 90
        advert_int 1
        nopreempt
        #preempt_delay 300
        authentication {
            auth_type PASS
            auth_pass 12345678
        }
        virtual_ipaddress {
            192.168.86.251
        }
    }
    
    vrrp_instance VIP_252{
        # 主用机设置为 MASTER
        state MASTER
        interface ens33
        virtual_router_id 252
        priority 100
        advert_int 1
        nopreempt
        preempt_delay 300
        authentication {
            auth_type PASS
            auth_pass 12345678
        }
        virtual_ipaddress {
            192.168.86.252
        }
    }
    
    /usr/local/keepalived/sbin/keepalived -t
    sudo systemctl restart keepalived
    sudo systemctl status keepalived
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    安装 web服务测试

    keep-101, keep-102

    sudo yum install -y httpd
    sudo systemctl stop firewalld
    echo " sed -i 's/123/$HOSTNAME/' /usr/share/httpd/noindex/index.html" | sudo bash -
    sudo systemctl restart httpd
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 测试服务是否可以访问
    while true; do curl http://192.168.86.251 -s |grep keep && date; sleep  1s; done;
    while true; do curl http://192.168.86.252 -s |grep keep && date; sleep  1s; done;
    
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    下线 keep-102

    在这里插入图片描述

    上线 keep-102

    在这里插入图片描述

    下线 keep-101

    在这里插入图片描述

    上线 keep-101

    在这里插入图片描述

  • 相关阅读:
    代码随想录算法训练营 动态规划part06
    上班都在刷八股文,老板都想要一份?Alibaba 架构师的”Java 面试突击手册“到底有多火?
    【高等数学基础进阶】函数、极限、连续-补充+练习 & 导数与微分-练习
    考研顺序表的初始化、销毁、打印、封装、增删改查代码看这一篇就够了
    回顾复习【矩阵分析】初等因子 和 矩阵的相似 || 由不变因子求初等因子 || 由初等因子和秩求Smith标准形(不变因子)
    Servlet的使用手把手教学(一)
    基于cortex-M3,M4下硬件层、驱动层的解耦软件框架设计
    Kotlin
    实用电脑软件分享,来看看有没有你正在用的
    Electron基本介绍
  • 原文地址:https://blog.csdn.net/yidichaxiang/article/details/126852418