keepalived高可用搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived高可用搭建相关的知识,希望对你有一定的参考价值。

- [1. keepalived 简介 ##](#1-keepalived-简介-)
        - [1. keepalived](#1-keepalived)
        - [2. 安装keepalived](#2-安装keepalived)
- [2. keepalived 高可用配置 ##](#2-keepalived-高可用配置-)
        - [1. 安装keepalived](#1-安装keepalived)
        - [2. 配置keepalived以实现虚拟IP绑定](#2-配置keepalived以实现虚拟ip绑定)

1. keepalived 简介

1. keepalived

Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。

2. 安装keepalived

Centos7 默认的keepalived版本有问题,直接源码编译安装
这里下载的keepadlived版本为2.0.7
首先安装依赖文件(两台机器)

# yum install -y gcc gcc-c++  openssl-devel

然后执行编译安装(两台机器)

# tar xvf keepalived-2.0.7.tar
# cd keepalived-2.0.7
# ./configure
# make
# make install

复制配置文件(两台机器)

# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

2. keepalived 高可用配置

这里的两台服务器使用keepalived执行虚拟IP的动态切换,具体要求如下:

1. 安装keepalived

首先两台服务器均要按照上一节描述的方法来进行Keepalived的安装

2. 配置keepalived以实现虚拟IP绑定

修改配置文件:/etc/keepalived/keepalived.conf(主)

1 ! Configuration File for keepalived
2
3 global_defs {
4    router_id master
5 }
6
7 vrrp_instance VI_1 {
8     state MASTER
9     interface em2
10     virtual_router_id 52
11     priority 100
12     advert_int 1
13     authentication {
14         auth_type PASS
15         auth_pass 1111
16     }
17     virtual_ipaddress {
18         172.16.0.8/24
19     }
20 }
21
22 virtual_server 192.168.200.100 443 {

注: 文件其他部分不动

修改配置文件:/etc/keepalived/keepalived.conf(备)

1 ! Configuration File for keepalived
2
3 global_defs {
4    router_id backup
5 }
6
7 vrrp_instance VI_1 {
8     state BACKUP
9     interface em2
10     virtual_router_id 52
11     priority 90
12     advert_int 1
13     authentication {
14         auth_type PASS
15         auth_pass 1111
16     }
17     virtual_ipaddress {
18         172.16.0.8/24
19     }
20 }
21
22 virtual_server 192.168.200.100 443 {

注: 文件其他部分不动

两机器都设置服务启动,并通过ps查看进程是否运行

# systemctl start keepalived
# systemctl enable keepalived

然后通过 ip a 来查看虚拟IP是否绑定成功

# ip a

以上是关于keepalived高可用搭建的主要内容,如果未能解决你的问题,请参考以下文章

LVS+Keepalived 高可用群集的介绍和搭建步骤

MySQL集群搭建-双主+keepalived高可用

基于keepalived搭建mysql双主高可用

Keepalived+Nginx搭建主从高可用并带nginx检测

通过keepalived搭建lvs高可用集群

基于keepalived搭建MySQL的高可用集群