配置环境: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 kB00: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 ResolutionDependencies Resolved=====================================================================================================================
PackageArchVersionRepositorySize
=====================================================================================================================
Installing:
keepalivedx86_641.3.5-19.el7name332 k
Installing for dependencies:
net-snmp-agent-libsx86_641:5.7.2-49.el7name708 kTransaction Summary
=====================================================================================================================
Install1 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 kB00:00:00
(2/2): net-snmp-agent-libs-5.7.2-49.el7.x86_64.rpm| 708 kB00:00:00
---------------------------------------------------------------------------------------------------------------------
Total1.1 MB/s | 1.0 MB00: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_641/2
Installing : keepalived-1.3.5-19.el7.x86_642/2
Verifying: 1:net-snmp-agent-libs-5.7.2-49.el7.x86_641/2
Verifying: keepalived-1.3.5-19.el7.x86_642/2 Installed:
keepalived.x86_64 0:1.3.5-19.el7Dependency Installed:
net-snmp-agent-libs.x86_64 1:5.7.2-49.el7Complete!
[root@yyq ~]#
2.配置keepalived
配置keepalived的配置文件keepalived.conf:
vi /etc/keepalived/keepalived.conf
文件内容如下
(其中的interface、priority和virtual_ipaddress按需修改,两台服务器的priority需要不同,priority较大的默认是主服务器):
interface对应网卡名称
查看网卡
[root@yyq ~]#ifconfig
eth0: flags=4111mtu 1450
inet 192.168.1.98netmask 255.255.255.0broadcast 192.168.1.255
inet6 1111::f111:1111:1111:1111prefixlen 64scopeid 0x20
ether 11:11:11:11:11:11txqueuelen 1000(Ethernet)
RX packets 9122848bytes 6131066305 (5.7 GiB)
RX errors 0dropped 0overruns 0frame 0
TX packets 3059946bytes 1479130540 (1.3 GiB)
TX errors 0dropped 0 overruns 0carrier 0collisions 0
keepalived.conf配置
[root@yyq ~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalivedvrrp_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=4111mtu 1450
inet 192.168.1.98netmask 255.255.255.0broadcast 192.168.1.255
inet6 1111::f111:1111:1111:1111prefixlen 64scopeid 0x20
ether 11:11:11:11:11:11txqueuelen 1000(Ethernet)
RX packets 9122848bytes 6131066305 (5.7 GiB)
RX errors 0dropped 0overruns 0frame 0
TX packets 3059946bytes 1479130540 (1.3 GiB)
TX errors 0dropped 0 overruns 0carrier 0collisions 0
【Linux|【Linux】centos下进行keepalived安装部署】参考:
内部部署文档
推荐阅读
- Linux|Let‘s Encrypt申请ssl证书
- OS|多进程和多线程的区别是什么(多进程和多线程的优缺点分析)
- python|2021-06最新亲测CentOS-7下安装Redis的详细过程
- Linux|C语言程序设计现代方法v2 K.N.King 笔记及课后习题解答
- 运维|【干货-K8S系列】Kubernetes调度核心解密:从Google Borg说起
- 运维|Kubernetes安全三步谈(如何监控与控制Kubernetes中的资源消耗问题)
- mysql|【centos7 + MySQL5.7 安装】centos7 安装MySQL5.7
- supervisor|centos7安装supervisor及supervisor web管理界面、supervisor守护进程使用
- Linux笔记|Linux Nginx-Web