
2.环境设定
#部署rs1和rh2(单网卡NAT模式)
root@rs1 \~\]# vmset.sh eth0 172.25.254.10 rs1
\[root@rs1 \~\]# dnf install httpd -y
\[root@rs1 \~\]# echo RS1 - 172.25.254.10 \> /var/www/html/index.html
\[root@rs1 \~\]# systemctl enable --now httpd
\[root@rs2 \~\]# vmset.sh eth0 172.25.254.20 rs2
\[root@rs2 \~\]# dnf install httpd -y
\[root@rs2 \~\]# echo RS2 - 172.25.254.20 \> /var/www/html/index.html
\[root@rs2 \~\]# systemctl enable --now httpd
#测试:
\[Administrator.DESKTOP-VJ307M3\] ➤ curl 172.25.254.10
RS1 - 172.25.254.10
✔
─────────────────────────────────────────────────────────────────────────────────────────────────────
\[2026-01-28 10:36.42\] \~
\[Administrator.DESKTOP-VJ307M3\] ➤ curl 172.25.254.20
RS2 - 172.25.254.20
#设定ka1和ka2
\[root@KA1 \~\]# vmset.sh eth0 172.25.254.50 KA1
\[root@KA2 \~\]# vmset.sh eth0 172.25.254.60 KA6
#设定本地解析
\[root@KA1 \~\]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.50 KA1
172.25.254.60 KA2
172.25.254.10 rs1
172.25.254.20 rs2
\[root@KA1 \~\]# for i in 60 10 20
\> do
\> scp /etc/hosts 172.25.254.$i:/etc/hosts
\> done
#在所有主机中查看/etc/hosts
#在ka1中开启时间同步服务
\[root@KA1 \~\]# vim /etc/chrony.conf
26 allow 0.0.0.0/0
29 local stratum 10
\[root@KA1 \~\]# systemctl restart chronyd
\[root@KA1 \~\]# systemctl enable --now chronyd
#在ka2中使用ka1的时间同步服务
\[root@KA2 \~\]# vim /etc/chrony.conf
pool 172.25.254.50 iburst
\[root@KA2 \~\]# systemctl restart chronyd
\[root@KA2 \~\]# systemctl enable --now chronyd
\[root@KA2 \~\]# chronyc sources -v
.-- Source mode '\^' = server, '=' = peer, '#' = local clock.
/ .- Source state '\*' = current best, '+' = combined, '-' = not combined,
\| / 'x' = may be in error, '\~' = too variable, '?' = unusable.
\|\| .- xxxx \[ yyyy \] +/- zzzz
\|\| Reachability register (octal) -. \| xxxx = adjusted offset,
\|\| Log2(Polling interval) --. \| \| yyyy = measured offset,
\|\| \\ \| \| zzzz = estimated error.
\|\| \| \| \\
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
\^\* KA1 3 6 17 13 +303ns\[+6125ns\] +/- 69ms
## 1.抢占模式( 默认的,谁优先级高就把vip放到哪里)
## 2.非抢占模式(持有vip只要vrrp通告正常就不做vip迁移)
#kA1中
\[root@KA1 \~\]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
state BACKUP #非抢占模式互为backup
interface eth0
virtual_router_id 51
nopreempt #启动非抢占模式
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
}
\[root@KA1 \~\]# systemctl stop keepalived.service
#KA2中
\[root@KA2 \~\]# vim /etc/keepalived/keepalived.conf
vrrp_instance WEB_VIP {
state BACKUP
interface eth0
virtual_router_id 51
nopreempt #开启非抢占模式
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.100/24 dev eth0 label eth0:0
}
}
\[root@KA2 \~\]# systemctl stop keepalived.service
#测试:
\[root@KA1 \~\]# systemctl start keepalived.service
\[root@KA2 \~\]# systemctl start keepalived.service
\[root@KA1 \~\]# ifconfig
eth0: flags=4163\