集群相关基础性知识基本就这些了。还有其他的这里就暂不介绍了,相关更多知识会在以后的集群博客中介绍。这里知识帮助理解。
首先,解决时间同步的问题: service ntpd stop #在四台主机上关闭本机上时间同步的服务,因我们常挂起虚拟机,各虚拟机时间可能不相同,因此我们不使用系统时间同步的服务 ntpdate 172.16.0.1#在各主机上执行该命令,同步时间与该地址的时间相同,该地址最好可以上网,这里是与本地网关服务器的时间同步 crontab -e #在各主机上使用该命令添加cron任务,每5分钟同步一下时间,同步后不管输出什么信息都送到/dev/null中,否则,你会每5分钟收到一封邮件 */5****/sbin/ntpdate 172.16.0.1&> /dev/null 然后,配置主机名解析: 我们先来修改各主机名: hostname node1.langdu.com #使用该命令修改主机名,可立即生效,使用logout退出虚拟机后在登录即可看到效果 vim /etc/sysconfig/network #编辑IP为172.16.32.30的主机,修改其主机名 HOSTNAME=node1.langdu.com #修改主机名 同样在其他主机上也修改主机名。 hostname node2.langdu.com #使用该命令修改主机名,可立即生效,使用logout退出虚拟机后在登录即可看到效果 vim /etc/sysconfig/network #编辑IP为172.16.32.31的主机,修改其主机名 HOSTNAME=node2.langdu.com #修改主机名 hostname node3.langdu.com #使用该命令修改主机名,可立即生效,使用logout退出虚拟机后在登录即可看到效果 vim /etc/sysconfig/network #编辑IP为172.16.32.32的主机,修改其主机名 HOSTNAME=node3.langdu.com #修改主机名 hostname node4.langdu.com #使用该命令修改主机名,可立即生效,使用logout退出虚拟机后在登录即可看到效果 vim /etc/sysconfig/network #编辑IP为172.16.32.33的主机,修改其主机名 HOSTNAME=node4.langdu.com #修改主机名 vim /etc/hosts #在IP为172.16.32.30的主机上,编辑该文件,添加如下四行信息 172.16.32.30node1.langdu.com node1 172.16.32.31node2.langdu.com node2 172.16.32.32node3.langdu.com node3 172.16.32.33node4.langdu.com node4 在node1主机上使用scp命令将该文件传给其他三台主机: scp /etc/hosts 172.16.32.31:/etc/#执行该命令时,需输入各主机的密码,可能有点麻烦 scp /etc/hosts 172.16.32.32:/etc/ scp /etc/hosts 172.16.32.33:/etc/ 最后,实现双机互信: 因我们这里是两台主机做keepalived,两台主机做http,因此,我们将前两台实现双机互信,后两台实现双机互信,即node1和node2双机互信,node3和node4双机互信。 首先在node1上: ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' #在/root目录下执行该命令,生成密钥文件,密码为空 ssh-copy-id-i .ssh/id_rsa.pub [email protected]#使用该命令将该密钥传给另一台主机,身份为root,确保当前处于/root目录下 接着在node2上: ssh-keygen -t rsa -P '' #在/root目录下执行该命令,生成密钥文件,密码为空 ssh-copy-id-i .ssh/id_rsa.pub [email protected]#使用该命令将该密钥传给另一台主机,身份为root,确保当前处于/root目录下 这样,以后在node1和node2之间通信时,我们就不需要输入密码了 ssh node2 'ifconfig'#在node1主机上,使用该命令,查看下是否已实现双机互信 同样在另外两台主机上也需要执行上述命令 在node3主机上,执行如下命令: ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' #在/root目录下执行该命令,生成密钥文件,密码为空 ssh-copy-id-i .ssh/id_rsa.pub [email protected]#使用该命令将该密钥传给另一台主机,身份为root,确保当前处于/root目录下 接着在node4上: ssh-keygen -t rsa -P '' #在/root目录下执行该命令,生成密钥文件,密码为空 ssh-copy-id-i .ssh/id_rsa.pub [email protected]#使用该命令将该密钥传给另一台主机,身份为root,确保当前处于/root目录下
准备工作已经完毕,现在来安装我们的keepalived软件包。这里我们通过安装rpm包来实现,有兴趣的读者也可以从网上下载源码包,自己编译安装。本rpm是经过源码编译制作的rpm包,相关的脚本及示例配置文件都已制作进来,所以,直接在本地安装即可。假如你是从网上下载的rpm包,里边是没有示例配置文件和相关脚本的,需要自己写。
global_defs { notification_email { #通知邮件收件人 [email protected] [email protected] [email protected] } notification_email_from [email protected] #定义通知邮件的来源 smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { #vrrp实例部分 state MASTER #定义初始状态下虚拟路由状态 interface eth0 #定义配置在哪个端口上 virtual_router_id 51#虚拟路由组ID号 priority 100#优先级 advert_int 1#每隔1秒进行通告 authentication { #实现认证 auth_type PASS #采用字符认证 auth_pass 1111#认证需要的字符串,最好是随机生成的,两边主机需一样 } virtual_ipaddress { #在对应接口上配置虚拟IP,可根据需要进行添加或删除 192.168.200.16 192.168.200.17 192.168.200.18 } } virtual_server 192.168.200.100443{ #虚拟服务部分,虚拟IP和端口分别是多少, delay_loop 6#获取服务时的等待时间 lb_algo rr #集群调度策略,默认为轮询,可自行修改 lb_kind NAT #集群转发方式 nat_mask 255.255.255.0#虚拟IP的掩码 persistence_timeout 50#集群持久连接超时时长,不想支持持久连接可去掉该项 protocol TCP #协议为TCP sorry_server 192.168.200.2001358#定义当real_server都down之后,该怎么办 real_server 192.168.201.100443{ #真实后台服务器IP及端口号,当有多个RIP时可出现多次 weight 1#该服务器的权重 SSL_GET { #采用SSL进行健康检查,还有其他方式也可实现健康检查 url { path /#通过443端口到指定路径下获取相关服务 digest ff20ad2481f97b1754ef3e12ecd3a9cc #摘要码 } url { path /mrtg/ status_code 200#状态码,访问正常时状态码为200 } connect_timeout 3#定义多长时间检查一次 nb_get_retry 3#检查不健康后,重试次数 delay_before_retry 3#多长时间重试一次 } } }
#!/bin/bash # # Script to start LVS DR real server. # chkconfig: - 90 10 # description: LVS DR real server # . /etc/rc.d/init.d/functions VIP=172.16.32.5#定义VIP host=`/bin/hostname` case "$1"in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up echo 1> /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2> /proc/sys/net/ipv4/conf/lo/arp_announce echo 1> /proc/sys/net/ipv4/conf/all/arp_ignore echo 2> /proc/sys/net/ipv4/conf/all/arp_announce /sbin/ifconfig lo:0$VIP broadcast $VIP netmask 255.255.255.255up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0down echo 0> /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0> /proc/sys/net/ipv4/conf/lo/arp_announce echo 0> /proc/sys/net/ipv4/conf/all/arp_ignore echo 0> /proc/sys/net/ipv4/conf/all/arp_announce ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0| grep $VIP` isrothere=`netstat -rn | grep "lo:0"| grep $VIP` if[ ! "$islothere"-o ! "isrothere"];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac
ifconfig #在node3上执行该命令,查看是否有VIP,使用ssh node4 'ifconfig'命令查看node4上是否也有VIP eth0 Link encap:Ethernet HWaddr 00:0C:29:7F:8F:44 inet addr:172.16.32.33Bcast:172.16.255.255Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500Metric:1 RX packets:162748errors:0dropped:0overruns:0frame:0 TX packets:2368errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:1000 RX bytes:26502458(25.2MiB) TX bytes:200681(195.9KiB) Interrupt:59Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436Metric:1 RX packets:10errors:0dropped:0overruns:0frame:0 TX packets:10errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RX bytes:666(666.0b) TX bytes:666(666.0b) lo:0Link encap:Local Loopback inet addr:172.16.32.5Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:16436Metric:1 route -n #查看是否有配置的VIP特定路由 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.16.32.50.0.0.0255.255.255.255UH 000lo 169.254.0.00.0.0.0255.255.0.0U 000eth0 172.16.0.00.0.0.0255.255.0.0U 000eth0 0.0.0.0172.16.0.10.0.0.0UG 000eth0 cat /proc/sys/net/ipv4/conf/all/arp_ignore 1 cat /proc/sys/net/ipv4/conf/all/arp_announce 2
vim /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost #有通告信息时将邮件发给管理员 } notification_email_from keepalived@localhost #通告邮件来自哪里 smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_httpd { #定义该vrrp脚本,上边应提到keepalived就是靠这些脚本来实现其相关功能的 script "killall -0 httpd"#尝试杀死该服务,但不是真正杀死该服务,仅为了测试该服务是否启动而已 interval 2 # check every 2 seconds weight -2 # if failed, decrease 2 of the priority fall 2 # require 2 failures for failures rise 1 # require 1 sucesses for ok } vrrp_script chk_schedown { #定义该vrrp脚本,来实现手动转移IP地址,待会演示效果 script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"#如果有这个文件,则返回1,否则返回0 interval 2 weight -2#优先级减2 } vrrp_instance VI_1 { state MASTER #定义该主机为master路由 interface eth0 virtual_router_id 132#定义虚拟路由组ID号,同一网段内不要使用相同的组ID,否则会报错 priority 101#定义其优先级 advert_int 1 authentication { auth_type PASS auth_pass langdu #字符串认证时使用的字符串,可自行修改,但要保证两个keepalived主机上的字符串相同 } virtual_ipaddress { 172.16.32.5/16dev eth0 label eth0:0#定义VIP,并制定设备和别名 } track_script { #健康检查脚本 chk_httpd chk_schedown } notify_master "/etc/keepalived/notify.sh master"#如果是master路由器,传递master参数 notify_backup "/etc/keepalived/notify.sh backup"#如果是backup路由,传递backup参数 notify_fault "/etc/keepalived/notify.sh fault"#如果失败了,传递fault参数 } virtual_server 172.16.32.580{ #定义虚拟服务器,因我们测试的是http服务,所以端口为80 delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.0.0#该掩码为虚拟服务器的掩码 # persistence_timeout 50 #为了待会刷新界面时查看效果,这里我没有启用持久连接,而是将其注释掉了 protocol TCP real_server 172.16.32.3280{ #定义RIP和端口号 weight 1#权重,在rr调度方式下,该值没有实际意义 HTTP_GET { #使用HTTP进行健康检查,假如你使用的是https服务,就需要使用基于SSL的健康检查 url { path / status_code 200#状态码 } connect_timeout 2 nb_get_retry 3 delay_before_retry 2 } } real_server 172.16.32.3380{ #指定另一个RIP和端口号,上边已经提到,当有多个realserver时,该项可以出现多次 weight 2 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 3 } } } 下面这个脚本时实现健康检查用的。即上边用到的notify.sh脚本。 #!/bin/bash # Author: onlyyou # description: An example of notify script # ifalias=${2:-eth0:0} interface=$(echo $ifalias | awk -F: '{print $1}') vip=$(ip addr show $interface | grep $ifalias | awk '{print $2}') contact='root@localhost' workspace=$(dirname $0) notify() { subject="$ip change to $1" body="$ip change to $1 $(date '+%F %H:%M:%S')" echo $body | mail -s "$1 transition"$contact #实现发送邮件 } case "$1"in master) notify master exit 0 ;; backup) notify backup /etc/rc.d/init.d/httpd restart exit 0 ;; fault) notify fault exit 0 ;; *) echo 'Usage: $(basename $0) {master|backup|fault}' exit 1 ;; esac scp /etc/keepalived/keepalived.conf node2:/etc/keepalived/#将配置文件发给另一个keepalived主机,这里发给node2,假如你的主机不是node2,请做相应修改 在node2主机上,修改刚传过来的keepalived的配置文件。这里只需修改两项即可: vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state BACKUP #设置该node2主机为backup路由 interface eth0 virtual_router_id 132 priority 100#设定node2主机的优先级为100,低于node1 advert_int 1 authentication { auth_type PASS auth_pass langdu } virtual_ipaddress { 172.16.32.5/16dev eth0 label eth0:0 } track_script { chk_httpd chk_schedown } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
tail /var/log/messages #查看日志 May 1618:19:22node1 Keepalived_vrrp[789]: Using LinkWatch kernel netlink reflector... May 1618:19:22node1 Keepalived_vrrp[789]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)] May 1618:19:22node1 Keepalived_vrrp[789]: VRRP_Instance(VI_1) Transition to MASTER STATE #传输master状态 May 1618:19:22node1 Keepalived_vrrp[789]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election May 1618:19:23node1 Keepalived_vrrp[789]: VRRP_Instance(VI_1) Entering MASTER STATE #进入master状态 May 1618:19:23node1 Keepalived_vrrp[789]: VRRP_Instance(VI_1) setting protocol VIPs. #设置VIP地址 May 1618:19:23node1 Keepalived_vrrp[789]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 May 1618:19:23node1 Keepalived_vrrp[789]: Netlink reflector reports IP 172.16.32.5added May 1618:19:23node1 Keepalived_healthcheckers[788]: Netlink reflector reports IP 172.16.32.5added May 1618:19:28node1 Keepalived_vrrp[789]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 从上边的日志可以看到,我们的配置已经生效。那一起看下ipvs规则吧。 ipvsadm -l -n #使用该命令,查看是否有ipvs规则,显示如下: IP Virtual Server version 1.2.1(size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.16.32.5:80rr -> 172.16.32.33:80Route 100 -> 172.16.32.32:80Route 100 ifconfig #查看下node1上的IP配置情况 eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:2F:AF inet addr:172.16.32.30Bcast:172.16.255.255Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500Metric:1 RX packets:290653errors:1dropped:0overruns:0frame:0 TX packets:13874errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:1000 RX bytes:46393162(44.2MiB) TX bytes:2014631(1.9MiB) Interrupt:59Base address:0x2000 eth0:0Link encap:Ethernet HWaddr 00:0C:29:9F:2F:AF inet addr:172.16.32.5Bcast:0.0.0.0Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500Metric:1 Interrupt:59Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436Metric:1 RX packets:10errors:0dropped:0overruns:0frame:0 TX packets:10errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RX bytes:666(666.0b) TX bytes:666(666.0b)
cd /etc/keepalived/#进入该目录 touch down #创建该文件,用来实现手动漂移IP地址 tail /var/log/messages #停几秒钟后,查看日志 May 1619:15:32node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Entering MASTER STATE May 1619:15:32node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) setting protocol VIPs. May 1619:15:32node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 May 1619:15:32node1 Keepalived_healthcheckers[2815]: Netlink reflector reports IP 172.16.32.5added May 1619:15:32node1 Keepalived_vrrp[2816]: Netlink reflector reports IP 172.16.32.5added May 1619:15:32node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Received higher prio advert #收到更高优先级的通告信息 May 1619:15:32node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Entering BACKUP STATE #进入backup状态 May 1619:15:32node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) removing protocol VIPs. #转移VIP地址 May 1619:15:32node1 Keepalived_healthcheckers[2815]: Netlink reflector reports IP 172.16.32.5removed May 1619:15:32node1 Keepalived_vrrp[2816]: Netlink reflector reports IP 172.16.32.5removed ifconfig #使用该命令,查看下node1主机的VIP是否存在,可看到已转移到其他主机 eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:2F:AF inet addr:172.16.32.30Bcast:172.16.255.255Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500Metric:1 RX packets:347881errors:1dropped:0overruns:0frame:0 TX packets:21333errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:1000 RX bytes:50760780(48.4MiB) TX bytes:2653767(2.5MiB) Interrupt:59Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436Metric:1 RX packets:10errors:0dropped:0overruns:0frame:0 TX packets:10errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RX bytes:666(666.0b) TX bytes:666(666.0b) 在node2主机上查看其日志。 tail /var/log/messages #查看node2的日志信息 May 1619:15:32node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) forcing a new MASTER election May 1619:15:32node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) forcing a new MASTER election May 1619:15:33node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Transition to MASTER STATE May 1619:15:34node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Entering MASTER STATE #进入master状态 May 1619:15:34node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) setting protocol VIPs. #设定VIP May 1619:15:34node1 Keepalived_healthcheckers[2463]: Netlink reflector reports IP 172.16.32.5added May 1619:15:34node1 avahi-daemon[3375]: Registering new address record for172.16.32.5on eth0. May 1619:15:34node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 May 1619:15:34node1 Keepalived_vrrp[2464]: Netlink reflector reports IP 172.16.32.5added May 1619:15:39node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5
rm /etc/keepalived/down #删除node1主机上该文件 rm: remove regular empty file`down'? y tail /var/log/messages #查看日志信息 May 1619:15:32node1 Keepalived_vrrp[2816]: Netlink reflector reports IP 172.16.32.5removed May 1619:27:54node1 Keepalived_vrrp[2816]: VRRP_Script(chk_schedown) succeeded May 1619:27:55node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) forcing a new MASTER election May 1619:27:55node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) forcing a new MASTER election May 1619:27:56node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Transition to MASTER STATE May 1619:27:57node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Entering MASTER STATE #进入master状态 May 1619:27:57node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) setting protocol VIPs. May 1619:27:57node1 Keepalived_vrrp[2816]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 May 1619:27:57node1 Keepalived_healthcheckers[2815]: Netlink reflector reports IP 172.16.32.5added May 1619:27:57node1 Keepalived_vrrp[2816]: Netlink reflector reports IP 172.16.32.5added
至此,我们已成功实现了keepalived的相关功能。以上演示的仅仅是主从模式下地址漂移。那我们能否实现在双主模式下实现地址漂移呢?答案是肯定的。
vim /etc/keepalived/keepalived.conf #其他信息不变,仅修改如下信息 vrrp_instance VI_2 { #添加虚拟路由组2 state BACKUP #定义该路由在虚拟路由组2中为backup路由 interface eth0 virtual_router_id 232#定义组ID priority 200#定义在该组中的优先级 advert_int 1 authentication { auth_type PASS auth_pass langdu } virtual_ipaddress { 172.16.32.6/16dev eth0 label eth0:1#因是双主模型,定义VIP及设备和别名,同上边定义的别名要区分开 } track_script { chk_httpd chk_schedown } notify_master "/etc/keepalived/notify.sh master eth0:1"#修改这三项,因我们有意定义成其他别名 notify_backup "/etc/keepalived/notify.sh backup eth0:1" notify_fault "/etc/keepalived/notify.sh fault eth0:1" } 同时注释掉virtual_server部分。在双主模型下,我们不使用virtual_server部分。 scp /etc/keepalived/keepalived.conf node2:/etc/keepalived/#将该配置文件传给另一个主机,即node2 在node2上,修改keepalived配置文件 vim /etc/keepalived/keepalived.conf vrrp_instance VI_2 { #在该主机上修改虚拟路由组2 state MASTER #修改在该组中本路由为master路由 interface eth0 virtual_router_id 232#定义组ID priority 201#修改在该组中的优先级,一定要高于node1中虚拟路由组2的优先级 advert_int 1 authentication { auth_type PASS auth_pass langdu } virtual_ipaddress { 172.16.32.6/16dev eth0 label eth0:1#因是双主模型,定义VIP及设备和别名,同上边定义的别名要区分开 } track_script { chk_httpd chk_schedown } notify_master "/etc/keepalived/notify.sh master eth0:1" notify_backup "/etc/keepalived/notify.sh backup eth0:1" notify_fault "/etc/keepalived/notify.sh fault eth0:1" }
cd /etc/keepalived #进入该目录 touch down #创建该文件 tail /var/log/messages #查看日志信息 May 1621:25:25node1 named[16646]: zone localhost/IN: loaded serial 0 May 1621:25:25node1 named[16646]: zone managed-keys.bind/IN/_meta: loaded serial 10 May 1621:25:25node1 named[16646]: running May 1621:25:25node1 named[16646]: zone langdu.com/IN: sending notifies (serial 2013005) May 1621:45:59node1 Keepalived_vrrp[11785]: VRRP_Script(chk_schedown) failed May 1621:46:00node1 Keepalived_vrrp[11785]: VRRP_Instance(VI_1) Received higher prio advert May 1621:46:00node1 Keepalived_vrrp[11785]: VRRP_Instance(VI_1) Entering BACKUP STATE #可看到进入backup状态 May 1621:46:00node1 Keepalived_vrrp[11785]: VRRP_Instance(VI_1) removing protocol VIPs. #VIP漂移成功 May 1621:46:00node1 Keepalived_vrrp[11785]: Netlink reflector reports IP 172.16.32.5removed May 1621:46:00node1 Keepalived_healthcheckers[11784]: Netlink reflector reports IP 172.16.32.5removed 现在,一起看下node2上的日志信息: tail /var/log/messages May 1621:46:00node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) forcing a new MASTER election May 1621:46:00node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) forcing a new MASTER election May 1621:46:01node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) Transition to MASTER STATE May 1621:46:02node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) Entering MASTER STATE #进入master状态 May 1621:46:02node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) setting protocol VIPs. May 1621:46:02node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 May 1621:46:02node1 Keepalived_vrrp[11537]: Netlink reflector reports IP 172.16.32.5added #添加另一个VIP May 1621:46:02node1 Keepalived_healthcheckers[11536]: Netlink reflector reports IP 172.16.32.5added May 1621:46:02node1 avahi-daemon[3375]: Registering new address record for172.16.32.5on eth0. May 1621:46:07node1 Keepalived_vrrp[11537]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for172.16.32.5 现在我们来安装DNS服务器,实现将同一个域名解析到不同的IP地址上。这里我们将node1作为DNS服务器来负责域名解析。因此,以下命令在node1主机上执行。 yum -y install bind97 bind97-utils #安装bind97,来提供DNS服务,在安装前请确保你的虚拟机上没有安装其他bind软件包 vim /etc/named.rfc1912.zones #编辑该文件,添加如下信息 zone "langdu.com"IN { #添加该区域,DNS相关配置请查看我的相关博客,里边有详细介绍 typemaster; file"langdu.com.zone"; }; zone "32.16.172.in-addr.arpa"IN { typemaster; file"172.16.32.zone"; }; cd /etc/named vim 172.16.32.zone#编辑该文件,添加如下内容: $TTL 600 @ IN SOA ns.langdu.com. admin.langdu.com. ( 2013005 4H 5M 3D 1D) IN NS ns.langdu.com. 30IN PTR ns.langdu.com. 6IN PTR www.langdu.com. 5IN PTR www.langdu.com. vim langdu.com.zone #编辑该文件,添加如下内容: $TTL 600 @ IN SOA ns.langdu.com. admin.langdu.com. ( 2013005 4H 5M 3D 1D) IN NS ns IN NS www IN NS www ns IN A 172.16.32.30 www IN A 172.16.32.5#实现将同一个域名解析到不同的IP上 www IN A 172.16.32.6 named-checkconf #检查配置文件 named-checkzone "langdu.com"/var/named/langdu.com.zone #检查区域文件 named-checkzone "32.16.172.in-addr.arpa"/var/named/172.16.32.zone 在我们的物理机上修改hosts文件。 打开C:\Windows\System32\drivers\etc/hosts文件,添加两行信息: 172.16.32.5www.langdu.com 172.16.32.6www.langdu.com 检查没有问题后启动服务。 service named start dig -t A www.langdu.com @172.16.32.30#使用该命令查询下DNS服务器是否可用,以下是显示结果: ; <<>> DiG 9.7.0-P2-RedHat-9.7.0-6.P2.el5_7.4<<>> -t A www.langdu.com @172.16.32.30 ;; globaloptions: +cmd ;; Got answer: ;; ->>HEADER<<-opcode: QUERY, status: NOERROR, id: 5351 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 1 ;; QUESTION SECTION: ;www.langdu.com. IN A ;; ANSWER SECTION: www.langdu.com. 600IN A 172.16.32.5#解析成功,可看到同一域名解析出两个IP地址 www.langdu.com. 600IN A 172.16.32.6 ;; AUTHORITY SECTION: langdu.com. 600IN NS ns.langdu.com. langdu.com. 600IN NS www.langdu.com. ;; ADDITIONAL SECTION: ns.langdu.com. 600IN A 172.16.32.30 ;; Query time: 5msec ;; SERVER: 172.16.32.30#53(172.16.32.30) ;; WHEN: Thu May 1621:42:072013 ;; MSG SIZE rcvd: 111
本文出自 “呼伦贝尔―写在人生路上” 博客,转载请与作者联系!