配置环境:centos7.9
[root@yyq ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
配置步骤
1.安装keepalived模块
yum install -y keepalived
日志:
[root@yyq ~]# yum install -y keepalived
Loaded plugins: fastestmirror, priorities, versionlock
Loading mirror speeds from cached hostfile
name | 3.6 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-19.el7 will be installed
--> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.3.5-19.el7.x86_64
--> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.3.5-19.el7.x86_64
--> Running transaction check
---> Package net-snmp-agent-libs.x86_64 1:5.7.2-49.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================
Installing:
keepalived x86_64 1.3.5-19.el7 name 332 k
Installing for dependencies:
net-snmp-agent-libs x86_64 1:5.7.2-49.el7 name 708 k
Transaction Summary
=====================================================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 1.0 M
Installed size: 3.0 M
Downloading packages:
(1/2): keepalived-1.3.5-19.el7.x86_64.rpm | 332 kB 00:00:00
(2/2): net-snmp-agent-libs-5.7.2-49.el7.x86_64.rpm | 708 kB 00:00:00
---------------------------------------------------------------------------------------------------------------------
Total 1.1 MB/s | 1.0 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:net-snmp-agent-libs-5.7.2-49.el7.x86_64 1/2
Installing : keepalived-1.3.5-19.el7.x86_64 2/2
Verifying : 1:net-snmp-agent-libs-5.7.2-49.el7.x86_64 1/2
Verifying : keepalived-1.3.5-19.el7.x86_64 2/2
Installed:
keepalived.x86_64 0:1.3.5-19.el7
Dependency Installed:
net-snmp-agent-libs.x86_64 1:5.7.2-49.el7
Complete!
[root@yyq ~]#
2.配置keepalived
配置keepalived的配置文件keepalived.conf:
vi /etc/keepalived/keepalived.conf
文件内容如下
(其中的interface、priority和virtual_ipaddress按需修改,两台服务器的priority需要不同,priority较大的默认是主服务器):
interface对应网卡名称
查看网卡
[root@yyq ~]# ifconfig
eth0: flags=4111<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 192.168.1.98 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 1111::f111:1111:1111:1111 prefixlen 64 scopeid 0x20<link>
ether 11:11:11:11:11:11 txqueuelen 1000 (Ethernet)
RX packets 9122848 bytes 6131066305 (5.7 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3059946 bytes 1479130540 (1.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
keepalived.conf配置
[root@yyq ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 94
priority 110
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.1.101
}
}
~
3.启动keepalived
在服务器上执行:
systemctl start keepalived
systemctl enable keepalived
日志
[root@yyq ~]# systemctl start keepalived
[root@yyq ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@yyq ~]#
4.检查虚拟IP是否正常
在任意一台服务器上执行:
ssh <虚拟IP地址>
如果可以成功登陆priority较大的那台服务器,则表示配置成功。
日志:
[root@yyq /etc/keepalived]# ssh 192.168.1.101
The authenticity of host '192.168.1.101 (192.168.1.101)' can't be established.
ED25519 key fingerprint is SHA256:V1111111111111111.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: 192.168.1.197
~/.ssh/known_hosts:4: 192.168.1.210
~/.ssh/known_hosts:5: 192.168.1.196
~/.ssh/known_hosts:6: 192.168.1.203
~/.ssh/known_hosts:7: 192.168.1.211
~/.ssh/known_hosts:11: 192.168.1.208
~/.ssh/known_hosts:12: 192.168.1.215
~/.ssh/known_hosts:13: 192.168.1.204
(2 additional names omitted)
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.101' (ED25519) to the list of known hosts.
Authorized only. All activity will be monitored and reported
root@192.168.1.101's password:
Last login: Mon Mar 21 11:01:15 2022 from 192.168.1.236
Authorized users only. All activity may be monitored and reported
[root@yyq ~]# ifconfig
eth0: flags=4111<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 192.168.1.98 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 1111::f111:1111:1111:1111 prefixlen 64 scopeid 0x20<link>
ether 11:11:11:11:11:11 txqueuelen 1000 (Ethernet)
RX packets 9122848 bytes 6131066305 (5.7 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3059946 bytes 1479130540 (1.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
参考:
内部部署文档