# 高可用配置

image_2023-02-01-14-11-02

# 安装 Keepalived

# 编译安装

下载地址:

https://www.keepalived.org/download.html

使用 ./configure 编译安装

如遇报错提示:

configure: error :
!!! OpenSSL is not properly installed on your system !!!
!!! Can not include OpenSSL headers files !!!

安装依赖

yum install openssl-devel

yum 安装

yum install keepalived

配置

使用 yum 安装后配置文件在

/etc/keepalived/Keepalived.conf

最小配置

第一台机器

! Configuration File for keepalived

global_defs {
	router_id lb111
}
#在内网当中通讯的协议 atguigu:实例名称
vrrp_instance atguigu {
    state MASTER
    interface ens33 #对应自己的网卡的名字
    virtual_router_id 51
    #主备竞选的的优先级高谁就是Master
    priority 100 #优先级
    advert_int 1 #间隔检测的时间
    authentication { #在内网当认证服务器,同一组要保持一致
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress { #虚拟的ip地址
        192.168.244.200
    }
}

启动 Keepalived

systemctl start keepalived

查看 ip

image_2023-02-01-21-27-59

  • 注意:使用 ip addr 来查看 ifconfig 查看不到

第二台机器

! Configuration File for keepalived

global_defs {
   router_id lb110
}

vrrp_instance atguigu {
    state BACKUP #备机
    interface ens33 
    virtual_router_id 51
    priority 50 #优先级要比Master低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.244.200
    }
}

ip add 查看

image_2023-02-01-21-38-30

只有一个 ip

在 win 下 ping 一下虚拟 ip 查看效果:

image_2023-02-01-21-40-23

我们将这个 111 的给断开直接关机

image_2023-02-01-21-42-13

断了一下有重新 ping 上了查看下 110 的 ip 状态

image_2023-02-01-21-43-20

可以看到 110 用 IP add 既然查看到了虚拟 ip

这就是 ip 飘逸通过 keepalived 飘逸

访问页面时也是通过虚拟 ip 来访问

image_2023-02-01-21-45-28

# 高可用场景及解决方案

image_2023-02-01-14-11-16

# 选举方式

优先级决定选举:谁的优先级高就越容易 Master