两台虚拟机
两台配置操作一样
环境配置
[root@lb01 /]# yum -y install openssl openssl-devel
[root@lb01 /]# yum -y install kernel-devel
做个软连接 用tab键补全2.6.32-642.6.2.el6.x86_64
[root@lb01 /]# ln -s /usr/src/kernels/2.6.32-642.6.2.el6.x86_64/ /usr/src/linux [root@lb01 /]# ls -l /usr/src/ total 8 drwxr-xr-x. 2 root root 4096 Sep 23 2011 debug drwxr-xr-x. 3 root root 4096 Oct 6 11:05 kernels lrwxrwxrwx 1 root root 43 Oct 6 11:09 linux -> /usr/src/kernels/2.6.32-642.6.2.el6.x86_64/
下载软件、解压、配置、编译、安装
[root@lb01 /]# wget http://www.keepalived.org/software/keepalived-1.2.16.tar.gz
[root@lb01 /]# tar zxvf keepalived-1.2.16.tar.gz [root@lb01 /]# cd keepalived-1.2.16 [root@lb01 keepalived-1.2.16]# ./configure Keepalived configuration ------------------------ Keepalived version : 1.2.16 Compiler : gcc Compiler flags : -g -O2 Extra Lib : -lssl -lcrypto -lcrypt Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : No fwmark socket support : Yes Use VRRP Framework : Yes Use VRRP VMAC : Yes SNMP support : No SHA1 support : No Use Debug flags : No [root@lb01 keepalived-1.2.16]# make [root@lb01 keepalived-1.2.16]# make install
配置规范启动
[root@lb01 keepalived-1.2.16]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ #生成启动脚本 [root@lb01 keepalived-1.2.16]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ #配置启动脚本的参数 [root@lb01 keepalived-1.2.16]# mkdir /etc/keepalived #创建默认的keepalived配置文件路径 [root@lb01 keepalived-1.2.16]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ #把keepalived.conf模板拷贝到/etckeepalived下 [root@lb01 keepalived-1.2.16]# cp /usr/local/sbin/keepalived /usr/sbin/ [root@lb01 keepalived-1.2.16]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@lb01 keepalived-1.2.16]# ps -ef | grep keep root 3490 1 0 11:36 ? 00:00:00 keepalived -D root 3492 3490 0 11:36 ? 00:00:00 keepalived -D root 3493 3490 0 11:36 ? 00:00:00 keepalived -D root 3496 1537 0 11:36 pts/0 00:00:00 grep keep [root@lb01 keepalived-1.2.16]# /etc/init.d/keepalived stop Stopping keepalived: [ OK ]
配置文件详解
[root@lb01 keepalived-1.2.16]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived #注释 global_defs { #全局配置 notification_email { #邮件报警 acassen@firewall.loc #收件人 failover@firewall.loc #收件人 sysadmin@firewall.loc #收件人 } notification_email_from Alexandre.Cassen@firewall.loc #发件人 smtp_server 192.168.200.1 #邮件服务器地址 smtp_connect_timeout 30 #超时时间 router_id LVS_DEVEL #keepavlied ID } vrrp_instance VI_1 { #VRRP的实例 state MASTER #服务器的状态 interface eth0 #通信的端口 virtual_router_id 51 #这个实例的ID priority 100 #优先级 advert_int 1 #心跳的间隔 authentication { #通信的密钥对 auth_type PASS auth_pass 1111 #越简单越好 保持默认就行 } virtual_ipaddress { #VIP地址 192.168.200.16 192.168.200.17 192.168.200.18 } }
修改配置文件
先备份一个配置文件
lb01配置文件
[root@lb01 keepalived-1.2.16]# cd /etc/keepalived/ [root@lb01 keepalived]# cp keepalived.conf keepalived.conf.bak [root@lb01 keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.119.150/24 } }
lb02配置文件
[root@lb02 keepalived-1.2.16]# cd /etc/keepalived/ [root@lb02 keepalived]# cp keepalived.conf keepalived.conf.bak [root@lb02 keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.119.150/24 } }
启动keepalived
[root@lb01 keepalived]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@lb02 keepalived]# /etc/init.d/keepalived start Starting keepalived: [ OK ]
在master(lb01)查看虚拟IP(192.168.119.150)
提示:使用ifconfig是查不到的;使用 ip add查询
[root@lb01 keepalived]# ip add | grep 192.168.119.150 inet 192.168.119.150/24 scope global secondary eth0
在backup(lb02)查看(没有虚拟IP)
[root@lb02 keepalived]# ip add | grep 192.168.119.150
把master的keepalived服务down、虚拟IP就会自动切换到backup
[root@lb01 keepalived]# /etc/init.d/keepalived stop Stopping keepalived: [ OK ] [root@lb01 keepalived]# ip add | grep 192.168.119.150 [root@lb01 keepalived]#
backup查询
[root@lb02 keepalived]# ip add | grep 192.168.119.150 [root@lb02 keepalived]# ip add | grep 192.168.119.150 [root@lb02 keepalived]# ip add | grep 192.168.119.150 [root@lb02 keepalived]# ip add | grep 192.168.119.150 [root@lb02 keepalived]# ip add | grep 192.168.119.150 inet 192.168.119.150/24 scope global secondary eth0 [root@lb02 keepalived]# ip add | grep 192.168.119.150 inet 192.168.119.150/24 scope global secondary eth0
当master的keepalived服务启动时、虚拟IP会自动从backup切回到master