1、创建两台虚拟机,分别为主机和从机,区别两台虚拟机的IP地址

2、 将keepalived上传到linux系统当中

3、 cd /usr/local目录 

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传

4、 tar -zxvf keepalived-1.2.18.tar.gz

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_02

5、 cd keepalived-1.2.18目录

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_03

6、 yum install -y openssl openssl-devel (需要安装一个软件包)

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_04

7、 ./configure --prefix=/usr/local/keepalived 编译,将编译文件放到/usr/local/keepalived

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_05

8、 make

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_06

9、 make install

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_07

10、 在/etc/keepalived 创建目录: mkdir /etc/keepalived

11、 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_08

12、 cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_09

13、 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_10

14、 ln -s /usr/local/sbin/keepalived /usr/sbin/

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_11

  (ln -s /usr/local/keepalived/sbin/keepalived /sbin/)

15、 将nginx启动脚本和keepalived.conf配置上传到/etc/keepalived

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_12

 

   

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_13

16、 主机:修改keepalived.conf配置文件  vim /etc/keepalived/keepalived.conf

  interface 配置成与主机相同的网络 ip a
  mcast_src_ip 配置成自己机器的IP
  virtual_ipaddress {
    192.168.33.135 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
  }

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_14

 

17、 启动keepalived   启动命令service keepalived start

  如果报错:Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_15

18、解决办法:rm /usr/sbin/keepalived

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_16

19、ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_17

 20、再次启动

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_18

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_19

21、可以设置开机启动:chkconfig keepalived on  

22、自动重启不了,解决办法

  查看脚本是否有运行的权限,如果你是 root 登陆的话(不是的话,切换到 root 用户,对*.sh 赋可执行的权限)

  赋予权限命令:chmod 777 *.sh

  赋予所有权限:chmod +x *.sh

  然后运行就 OK 了,即有权限对文件进行删除等操作。

23、备机安装步骤同上,备机修改配置文件步骤如下

24、备机:修改keepalived.conf配置文件  vim /etc/keepalived/keepalived.conf

   state BACKUP 备机名称
  interface 配置成与主机相同的网络 ip a
  mcast_src_ip 配置成自己机器的IP
  priority 90  权重配置  
  virtual_ipaddress {
    192.168.33.135  # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
  }

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_20

25、从机启动步骤同上

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_21

26、效果展示  访问虚拟IP地址192.168.33.135

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_22

27、模拟主机挂掉,高可用工具Keepalived实现自动启动备机,防止宕机

  注意:为确保区分主机与备机,在此修改了静态资源HTML的标题

  

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_23

   ①、挂掉主机

    

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_24

  ②、虚拟IP地址192.168.33.135继续访问备机

    

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_25

  ③、 虚拟IP访问效果

    

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_26

  ④、恢复主机

    

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_配置文件_27

    

  ⑤、虚拟IP地址192.168.33.135继续访问主机

    

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_上传_28

   

  ⑥、虚拟IP访问效果

    

keepalived 配置修改 需要重新加载吗 keepalived重新加载配置文件_IP_29

29、keepalived 常用命令

  启动Keepalived:service keepalived start

  关闭Keepalived:service keepalived stop

30、注意事项

  区分主机与备机名称、

  区分主机与备机IP地址、

  区分主机与备机的权重  主机权重>备机权重

简单步骤概括
2.1 将keepalived上传到linux系统当中
2.2 cd /usr/local目录
2.3 tar -zxvf keepalived-xxxxxxxxx
2.4 切换到解压目录
2.5 yum install -y openssl openssl-devel
2.5 ./configure --prefix=/usr/local/keepalived 编译,将编译文件放到/usr/local/keepalived
2.6 make
2.7 make install
2.8 在/etc/keepalived 创建目录: mkdir /etc/keepalived
2.9 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
2.10 cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
2.11 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
2.12 ln -s /usr/local/sbin/keepalived /usr/sbin/
2.13 ln -s /usr/local/keepalived/sbin/keepalived /sbin/
2.14 将nginx启动脚本和keepalived.conf配置上传到/etc/keepalived
2.15 主机:修改keepalived.conf配置文件
interface 配置成与主机相同的网络 ip a
mcast_src_ip 配置成自己机器的IP
virtual_ipaddress {
192.168.7.88 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}

2.16 启动keepalived 如果报错:Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.
解决办法:rm /usr/sbin/keepalived
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

2.17 备机keepalived.conf
state BACKUP 备机
interface 配置成与主机相同的网络 ip a
mcast_src_ip 配置成自己机器的IP
priority 90
virtual_ipaddress {
192.168.7.88 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}