一、 安装keepalived
使用源码先安装keepalived 1.2.6
wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar zxf keepalived-1.2.6.tar.gz
cd keepalived-1.2.6
./configure --prefix=/usr/local/keepalived #安装到/usr/local/keepalived 目录下
make
make install
如果提示缺少组件Openssl,就先去安装(yum很方便)
二、配置keeplived为全局服务
为了能用 service + 服务名 + stop | start | restart 这样的命令去启动服务,需要做以下配置。(再centos7以后的版本都是systemctl + 服务名 + 。。。)
1. 建立服务启动脚本,以便使用service命令控制之
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
chmod +x /etc/init.d/keepalived
因为我们使用非默认路径(/usr/local)安装keepalived, 故需要修改几处路径,以保证keepalived能正常启动, 需要修改的文件如下:
2. 修改/etc/init.d/keepalived, 寻找大约15行左右的.
/etc/sysconfig/keepalived, 修改为:
. /usr/local/keepalived/etc/sysconfig/keepalived, 即指向正确的文件位置
同时在上述行下添加以下内容(将keepavlied主程序所在路径导入到环境变量PATH中):
PATH="$PATH:/usr/local/keepalived/sbin"
export PATH
3. 修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
4. 经过以上修改,keepalived基本安装即可完成,启动测试之:
service keepalived restart
5. 切勿忘记将此服务设置为开机启动
chkconfig keepalived on
TPS:在redhat中查看vip使用的命令是ip add
三、简单的主备配置
配置文件如下:
master:
! Configuration File for keepalived global_defs { notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc root@localhost } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 router_id NodeMaster } vrrp_script checkrun { script "/usr/local/keepalived/etc/keepalived/check.sh" interval 5 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 92 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { checkrun } virtual_ipaddress { 192.168.163.252 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 90 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.163.251 } }
测试脚本代码如下:
#!/bin/bash echo "hello world" >> /usr/local/keepalived/etc/keepalived/err.log exit 1
backup配置如下
! Configuration File for keepalived global_defs { notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc root@localhost } notification_email_from root@local host smtp_server localhost smtp_connect_timeout 30 router_id NODEB } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 92 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.163.252 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 90 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.163.251 } }
关于配置文件的解释下次再好好看下