使用两台虚拟机简单搭建一个 keepalived 的单实例环境
环境介绍:主机名 IP地址
keepalived-01 192.168.240.137
keepalived-02 192.168.240.139
内核版本(两台主机系统相同)
[root@keepalived-01 ~]# uname -r
2.6.32-573.el6.x86_64
[root@keepalived-01 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
########################keepalived-01上的操作###########################
创建软连接
[root@keepalived-01 ~]# ln -s /usr/src/kernels/2.6.32-573.el6.x86_64/ /usr/src/linux
[root@keepalived-01 ~]# ll /usr/src/
total 8
drwxr-xr-x. 2 root root 4096 Sep 23 2011 debug
drwxr-xr-x. 3 root root 4096 May 23 17:59 kernels
lrwxrwxrwx 1 root root 39 Jun 22 20:40 linux -> /usr/src/kernels/2.6.32-573.el6.x86_64/
安装依赖软件
[root@keepalived-01 ~]# yum -y install openssl* libnl* popt*
安装 ip_vs 软件
[root@keepalived-01 ~]# pwd
/root
[root@keepalived-01 ~]# ls
anaconda-ks.cfg install.log.syslog keepalived-1.1.19.tar.gz install.log ipvsadm-1.26.tar.gz
[root@keepalived-01 ~]# tar zxf ipvsadm-1.26.tar.gz
[root@keepalived-01 ~]# cd ipvsadm-1.26
[root@keepalived-01 ipvsadm-1.26]# make && make install
安装完成后需要在执行一遍命令才能生效
[root@keepalived-01 ipvsadm-1.26]# /sbin/ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
安装完成后要使用 lsmod 命令查看 ip_vs 是否存在
[root@keepalived-01 ~]# lsmod | grep ip_vs
ip_vs 126534 0
libcrc32c 1246 1 ip_vs
ipv6 335589 270 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
编译安装 keepalived 软件
[root@keepalived-01 ~]# tar zxf keepalived-1.1.19.tar.gz
[root@keepalived-01 ~]# cd keepalived-1.1.19
[root@keepalived-01 keepalived-1.1.19]# ./configure
[root@keepalived-01 keepalived-1.1.19]# make && make install
拷贝文件,用于 /etc/init.d/ 启动
/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
/bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived -p
/bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
/bin/cp /usr/local/sbin/keepalived /usr/sbin/
启动 keepalived 服务
/etc/init.d/keepalived start
检查 keepalived 服务
ps -ef | grep keep
root 34234 1 0 06:49 ? 00:00:00 keepalived -D
root 34236 34234 0 06:49 ? 00:00:00 keepalived -D
root 34237 34234 0 06:49 ? 00:00:00 keepalived -D
root 34243 28089 0 06:50 pts/1 00:00:00 grep keep
服务启动成功后,需要停止服务
/etc/init.d/keepalived stop
keepalived 的配置文件是在 /etc/keepalived/ 目录下的 keepalived.conf 文件
cd /etc/keepalived/
先将源文件备份
mv keepalived.conf keepalived.conf.ori
配置文件
vim keepalived.conf
#!/bin/bash
! Configure File for keepadlived #注释
global_defs {
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER #状态
interface eth0 #端口
virtual_router_id 55 #虚拟路由id,在两个keeplived之间必须一样
priority 150 #优先级
advert_int 1 #接管的间隔,超过1秒接管
authentication { #授权
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.240.10/24 #vip地址
}
}
########################keepalived-01操作结束###########################
########################keepalived-02上的操作###########################
在 keepalived-02 上的操作和 01相同,但是要注意配置文件 keepalived.conf 文件
实验中的 keepalived-02 的配置文件如下
#!/bin/bash
! Configure File for keepadlived
global_defs {
router_id LVS_2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.240.10/24
}
}
########################keepalived-02操作结束###########################
以上标黄的都是两个配置文件绝对不能一样的地方
两台服务器的优先级有 priority 为主,这次实验的两台机器 keepalived-01 priority 值为 150,keepalived-02 priority 值为 100
(官方文档介绍最好两台相差 50)
所以主为 keepalived-01
两台机器分别重启 keepalived 服务
[root@keepalived-01 ~]# /etc/init.d/keepalived restart
[root@keepalived-02 ~]# /etc/init.d/keepalived restart
到此为止一个简单的 keepalived 单实例环境搭建完成
检查环境搭建是否成功
要查看 vip 地址需要使用命令 ip add 查看
在 keepalived-01 上查看 vip 地址
在 keepalived-02 上查看 vip 地址
并没有发现 vip 地址
正常情况下
keepalived-01 有 vip 地址
keepalived-02 没有 vip 地址
如果两台服务器都有 vip 地址,说明 keepalived 发生脑裂现象
进行高可用测试,停止 keepalived-01 主机上的 keepalived 服务,应该自动切换到 keepalived-02 上
[root@keepalived-01 ~]# /etc/init.d/keepalived stop
[root@keepalived-02 ~]# ip add
在 keepalived-02 上成功查看到 vip 地址
一次简单的 keepalived 单实例高可用环境搭建完成
转载于:https://blog.51cto.com/aby028/1945012