keepalive主从搭建

准备两台主机

10.1.1.127

10.1.1.128

两台主机都配置

rocky主机ip配置

1770285927394_A125EE84-746E-4edb-ABB7-A49C6F80F9EE

# 1. 关闭防火墙并禁用开机自启

systemctl stop firewalld && systemctl disable firewalld

# 2. 临时关闭SELINUX(重启失效),永久关闭(重启生效)

setenforce 0

sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 3. 关闭NetworkManager(避免网络配置冲突,CentOS7必做)

systemctl stop NetworkManager && systemctl disable NetworkManager

# 4. 安装基础依赖(后续安装需要)

yum install -y wget net-tools gcc pcre-devel zlib-devel openssl-devel

# 5. 重启网络(确保配置生效)

systemctl restart network

安装nginx

yum install -y nginx

10.1.1.127主机nginx页面修改

1770286341654_C8F946CD-F382-4ebe-9049-1D86F052D7B4

10.1.1.128主机nginx页面修改

1770286375856_838FE155-1AA3-4db6-B45D-A5DBD772DD27

安装keepalive

Master节点安装

yum install -y keepalived

验证

1770286495477_212BB5D1-14FD-45d4-9CD0-38717597B007

# 备份原有配置文件(避免出错)

添加配置

[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

# router id, global unique, host uuid

router_id keep_17 #唯一标识,主备不一致

}

# vrrp conf, keepalived + nginx

vrrp_instance VI_1 {

# host -> master

state MASTER #主备不一致,主是master

# host interface name

interface ens160 #根据主机的网卡实际名称填写

# ensure master and backup are same

virtual_router_id 51 #主备一致

# weight, the larger, more chance to be master after master down

priority 100 #优先级,主的大

# between m & b time interval, default 1s

advert_int 1

# authenticate pwd, default

authentication {

auth_type PASS

auth_pass 1111 #密码,主备一致

}

# vip conf

virtual_ipaddress {

10.1.1.129 #虚拟ip,主备一致

}

}

Back节点配置

[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

# router id, global unique, host uuid

router_id keep_18

}

# vrrp conf, keepalived + nginx

vrrp_instance VI_1 {

# host -> master

state BACKUP

# host interface name

interface ens160

# ensure master and backup are same

virtual_router_id 51

# weight, the larger, more chance to be master after master down

priority 80

# between m & b time interval, default 1s

advert_int 1

# authenticate pwd, default

authentication {

auth_type PASS

auth_pass 1111

}

# vip conf

virtual_ipaddress {

10.1.1.129

}

}

验证虚拟ip

1770286692290_D6AEB654-290D-454c-86F7-8D6C1468C124

停掉10.1.1.127的keepalive

1770286798560_0E7E7648-B760-4f96-8A5C-BE3C0F2103D8

访问虚拟ip10.1.1129,跳转到backup

1770286847632_70104836-0083-4242-AF27-33F27F440A55

posted @ 2026-04-05 21:55  小蓝莓  阅读(0)  评论(0)    收藏  举报