评论

收藏

[Linux] keepalived主备配置的方法

服务系统 服务系统 发布于:2021-06-30 17:34 | 阅读数:518 | 评论:0

  记录一些自己的心得以及方法,本着共同学习,互相进步为目的。本次总结了keepalived主备简单的配置,欢迎大家评论留言(以下是keepalived主备简单配置的方法,均在试验机上操作,并且在实际工程环境中有验证过,目前配置过的OS有centos7.7)。
一、环境介绍:  1、系统环境:
root@test1:/root>cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
root@test1:/root>
  2、主机环境:
  MASTER:
root@test1:/root>hostname -I
192.168.100.10 
root@test1:/root>
  BACKUP:
root@test2:/root>hostname -I
192.168.100.20 
root@test2:/root
二、配置keepalived操作:  1、分别登录192.168.100.10、192.168.100.20,确定主机上安装了keepalived:
root@test1:/root>rpm -qa|grep keepalived
keepalived-1.3.5-16.el7.x86_64
root@test1:/root>
  2、若没有安装keepalived,则进行一下操作:
root@test1:/root>rpm -e keepalived-1.3.5-16.el7.x86_64
root@test1:/root>rpm -qa|grep keepalived
root@test1:/root>
root@test1:/root>cd /tmp
root@test1:/tmp>rz -e
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring keepalived-1.2.13-6.el7.x86_64.rpm...
  100%   223 KB   223 KB/sec  00:00:01     0 Errors  
root@test1:/tmp>ls -ltr
total 224
-rw-r--r-- 1 root root 228672 Jun 23 10:53 keepalived-1.2.13-6.el7.x86_64.rpm
root@test1:/tmp>
root@test1:/tmp>
root@test1:/tmp>
root@test1:/tmp>rpm -ivh keepalived-1.2.13-6.el7.x86_64.rpm 
warning: keepalived-1.2.13-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...              ################################# [100%]
Updating / installing...
   1:keepalived-1.2.13-6.el7      ################################# [100%]
root@test1:/tmp>
  3、配置keepalived:
  MATER:
root@test1:/etc/keepalived>ls -ltr
total 8
-rw-r--r-- 1 root root 3562 Jun 23 10:54 keepalived.conf.bak
-rw-r--r-- 1 root root 3516 Jun 23 10:55 keepalived.conf
root@test1:/etc/keepalived>vi keepalived.conf
......
vrrp_instance VI_1 {
  state MASTER      //主用则为master,备用为backup
  interface ens37     //根据实际情况修改为对应的网口
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    192.168.100.9     //根据实际情况修改为浮动IP
  }
......
  BACKUP:
root@test2:/tmp>cd /etc/keepalived/
root@test2:/etc/keepalived>cp keepalived.conf keepalived.conf.bak
root@test1:/etc/keepalived>ls -ltr
total 8
-rw-r--r-- 1 root root 3562 Mar  6  2015 keepalived.conf
-rw-r--r-- 1 root root 3562 Jun 23 10:59 keepalived.conf.bak
root@test2:/etc/keepalived>vi keepalived.conf
vrrp_instance VI_1 {
  state BACKUP           //主用则为master,备用为backup
  interface ens33        //根据实际情况修改为对应的网口
  virtual_router_id 51
  priority 50          //优先级修改为50,比主用的优先级小就行
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    192.168.100.9       //根据实际情况修改为浮动IP
  }
  4、启动服务:
  MASTER:
root@test1:/etc/keepalived>service keepalived start
Redirecting to /bin/systemctl start keepalived.service
root@test1:/etc/keepalived>ps -ef | grep keepalived
root     2752    1  0 16:35 ?    00:00:00 /usr/sbin/keepalived -D
root     2753   2752  0 16:35 ?    00:00:00 /usr/sbin/keepalived -D
root     2754   2752  0 16:35 ?    00:00:00 /usr/sbin/keepalived -D
root     2920   2435  0 16:52 pts/0  00:00:00 grep --color=auto keepalived
root@test1:/etc/keepalived>
  BACKUP:
root@test2:/etc/keepalived>service keepalived start
Redirecting to /bin/systemctl start keepalived.service
root@test1:/etc/keepalived>ps -ef|grep keepalived
root     2104    1  0 11:06 ?    00:00:00 /usr/sbin/keepalived -D
root     2105   2104  0 11:06 ?    00:00:00 /usr/sbin/keepalived -D
root     2106   2104  0 11:06 ?    00:00:00 /usr/sbin/keepalived -D
root     2108   1798  0 11:06 pts/0  00:00:00 grep --color=auto keepalived
root@test1:/etc/keepalived>
  5、加入开机自启动(两台都操作):
root@test2:/etc/keepalived>systemctl list-unit-files |grep keepalived
keepalived.service              disabled
root@test2:/etc/keepalived>chkconfig   keepalived   on
Note: Forwarding request to 'systemctl enable keepalived.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
root@test2:/etc/keepalived>systemctl list-unit-files |grep keepalived
keepalived.service              enabled 
root@test2:/etc/keepalived>
  6、检查主keepalived是否有浮动IP:
  在主用上有浮动IP即为配置正常,现象如下:
root@test1:/etc/keepalived>ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
     valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host 
     valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:d1:5f:bc brd ff:ff:ff:ff:ff:ff
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:d1:5f:c6 brd ff:ff:ff:ff:ff:ff
  inet 192.168.100.10/24 brd 192.168.100.255 scope global ens37
     valid_lft forever preferred_lft forever
  inet 192.168.100.9/32 scope global ens37        //MASTER上有浮动IP即为正常
     valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fed1:5fc6/64 scope link 
     valid_lft forever preferred_lft forever
root@test1:/etc/keepalived>
三、测试:  1、宕机主用keepalived,浮动切到备用上,现象如下:
root@test1:/etc/keepalived>service keepalived stop
Redirecting to /bin/systemctl stop keepalived.service
root@test1:/etc/keepalived>ps -ef|grep keepalived
root     2177   1798  0 11:08 pts/0  00:00:00 grep --color=auto keepalived
root@test1:/etc/keepalived>
root@test2:/etc/keepalived>ip a
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:d1:5f:bc brd ff:ff:ff:ff:ff:ff
  inet 192.168.100.20/24 brd 192.168.100.255 scope global noprefixroute ens33
     valid_lft forever preferred_lft forever
  inet 192.168.100.9/32 scope global ens33
     valid_lft forever preferred_lft forever
  inet6 fe80::8c23:eb29:ae15:f2d6/64 scope link noprefixroute 
     valid_lft forever preferred_lft forever
root@test2:/etc/keepalived>
  由以上可以看出,浮动192.168.100.9已经切到backup上。
  2、接着把master上的keepalive起来:
root@test1:/etc/keepalived>service keepalived start
Redirecting to /bin/systemctl start keepalived.service
root@test1:/etc/keepalived>ps -ef|grep keepalived
root     2200    1  0 11:10 ?    00:00:00 /usr/sbin/keepalived -D
root     2201   2200  0 11:10 ?    00:00:00 /usr/sbin/keepalived -D
root     2202   2200  0 11:10 ?    00:00:00 /usr/sbin/keepalived -D
root     2204   1798  0 11:11 pts/0  00:00:00 grep --color=auto keepalived
root@test1:/etc/keepalived>ip a
......
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:d1:5f:c6 brd ff:ff:ff:ff:ff:ff
  inet 192.168.100.10/24 brd 192.168.100.255 scope global ens37
     valid_lft forever preferred_lft forever
  inet 192.168.100.9/32 scope global ens37
     valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fed1:5fc6/64 scope link 
     valid_lft forever preferred_lft forever
root@test1:/etc/keepalived>
把master上的keepalived起来后,浮动切回master上,因为priority优先级高。
关注下面的标签,发现更多相似文章