Keepalvied 高可用技术实践
| 主机名 | IP地址 | 服务器 |
|---|---|---|
| client.jiang.cloud | 10.1.8.21 | 客户端 |
| web1.jiang.cloud | 10.1.8.11 | web1服务器 |
| web2.jiang.cloud | 10.1.8.12 | web2服务器 |
一、配置 web
bash
[root@web1 web2 ~ 18:37:53]# yum install -y nginx
# 部署 web
[root@web1 web2 ~ 18:38:50]# echo Welcome to $(hostname) > /usr/share/nginx/html/index.html
[root@web1 web2~ 18:39:48]# systemctl enable nginx.service --now
# 访问后端 nginx
[root@client1 ~ 18:37:24]# curl 10.1.8.11
Welcome to web1.jiang.cloud
[root@client1 ~ 18:41:09]# curl 10.1.8.12
Welcome to web2.jiang.cloud
二、配置 keepalived
2.1 配置 web2
web2 作为备节点。
bash
[root@web2 ~ 18:40:57]# yum install -y keepalived
[root@web2 ~ 18:42:02]# cp /etc/keepalived/keepalived.conf{,.ori}
[root@web2 ~ 18:42:19]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id web2
}
vrrp_instance nginx {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass jiang@123
}
virtual_ipaddress {
10.1.8.100/24
}
}
说明:
router_id web2,定义路由器名称,每个节点使用不同的名称。
state BACKUP,定义节点角色为备节点,MASTER则代表主节点。
interface ens33,定义VIP配置到该接口。
virtual_router_id 51 ,定义虚拟路由器ID,范围1-255,每个节点使用相同名称。
priority 100,定义节点优先级,值越大优先级越高。
authentication ,定义心跳认证。
virtual_ipaddress ,定义虚拟VIP。
bash
# 启动 keepalived 服务
[root@web2 ~ 18:45:55]# systemctl enable keepalived.service --now
# 查看 IP
[root@web2 ~ 18:46:10]# ip -br a show ens33
ens33 UP 10.1.8.12/24 10.1.8.100/24 fe80::20c:29ff:fec6:de41/64
2.2 配置 web1
web1 作为主节点。
bash
[root@web1 ~ 18:40:45]# yum -y install keepalived
[root@web1 ~ 18:47:11]# cp /etc/keepalived/keepalived.conf{,.ori}
[root@web1 ~ 18:47:47]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id web1
}
vrrp_instance nginx {
state MASTER
interface ens33
virtual_router_id 51
# master节点优先级要高于BACKUP节点
priority 110
advert_int 1
authentication {
auth_type PASS
auth_pass jiang@123 # 密码长度 不能超过 8 位(超过则 只取前8位
}
virtual_ipaddress {
10.1.8.100/24
}
}
bash
# 启动服务
[root@web1 ~ 18:51:11]# systemctl stop keepalived.service
# 查看 IP,VIP 切换到 web1
[root@web1 ~ 18:50:58]# ip -br a show ens33
ens33 UP 10.1.8.11/24 10.1.8.100/24 fe80::20c:29ff:feea:2198/64
[root@web2 ~ 18:46:23]# ip -br a show ens33
ens33 UP 10.1.8.12/24 fe80::20c:29ff:fec6:de41/64
三、高可用验证
bash
# 访问 web
[root@client ~ 18:54:58]# curl 10.1.8.100
Welcome to web1.jiang.cloud
# 关闭 web1 的 keepalive服务,再次访问
[root@web1 ~ 18:54:49]# systemctl stop keepalived.service
[root@client ~ 18:54:09]# curl 10.1.8.100
Welcome to web2.jiang.cloud
# 再次启动 web1 的keepalive服务,再次访问
[root@web1 ~ 18:54:49]# systemctl start keepalived.service
[root@client ~ 18:54:58]# curl 10.1.8.100
Welcome to web1.jiang.cloud