大纲
一、什么是Keepalived
二、Keepalived工作原理
三、Keepalived + LVS的实现
一、什么是Keepalived
Keepalived是用C写的简单的一个路由软件,这个项目的主要目标是对Linux系统和基于Linux的基础设施提供简单而强大负载均衡和高可用性。负载均衡架构依赖于众所周知的和广泛使用的Linux虚拟服务器(IPVS)内核模块提供第四层负载均衡。另一方面,高可用性是通过VRRP协议实现。
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
二、Keepalived工作原理
Layer3,4&7工作在IP/TCP协议栈的IP层,TCP层,及应用层,原理分别如下:
Layer3:Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服务器发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效,并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。
Layer4:如果您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的状态来决定服务器工作正常与否。如web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除。
Layer7:Layer7就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带宽也要大一些。Keepalived将根据用户的设定检查服务器程序的运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除。
三、Keepalived + LVS实现Web的高可用
系统环境
CentOS5.8 x86_64
Director
Director1 172.16.1.101
Director2 172.16.1.105
RealServer
node1.network.com node1 172.16.1.103
node2.network.com node2 172.16.1.104
软件包
ipvsadm-1.24-13.el5.x86_64.rpm
keepalived-1.2.1-5.el5.x86_64.rpm
httpd-2.2.15-47.el6.centos.1.x86_64.rpm
拓扑图
1、时间同步
[root@Director1 ~]# ntpdate s2c.time.edu.cn [root@Director2 ~]# ntpdate s2c.time.edu.cn [root@node1 ~]# ntpdate s2c.time.edu.cn [root@node2 ~]# ntpdate s2c.time.edu.cn 可根据需要在每个节点上定义crontab任务 [root@Director1 ~]# which ntpdate /sbin/ntpdate [root@Director1 ~]# echo "*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null" >> /var/spool/cron/root [root@Director1 ~]# crontab -l */5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null
2、主机名称要与uname -n保持一致,并通过/etc/hosts解析
Director1 [root@Director1 ~]# hostname Director1 [root@Director1 ~]# uname -n Director1 [root@Director1 ~]# sed -i ‘s@\(HOSTNAME=\).*@\1Director1@g‘ /etc/sysconfig/network Director2 [root@Director2 ~]# hostname Director2 [root@Director2 ~]# uname -n Director2 [root@node2 ~]# sed -i ‘s@\(HOSTNAME=\).*@\1Director2@g‘ /etc/sysconfig/network node1 [root@node1 ~]# hostname node1.network.com [root@node1 ~]# uname -n node3.network.com [root@node1 ~]# sed -i ‘s@\(HOSTNAME=\).*@\1node1.network.com@g‘ /etc/sysconfig/network node2 [root@node2 ~]# hostname node2.network.com [root@node2 ~]# uname -n node2.network.com [root@node2 ~]# sed -i ‘s@\(HOSTNAME=\).*@\1node2.network.com@g‘ /etc/sysconfig/network node1添加hosts解析 [root@Director1 ~]# vim /etc/hosts [root@Director1 ~]# cat /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 CentOS5.8 CentOS5 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 172.16.1.101 Director1 172.16.1.105 Director2 172.16.1.103 node1.network.com node1 172.16.1.104 node2.network.com node2 拷贝此hosts文件至Director2 [root@Director1 ~]# scp /etc/hosts Director2:/etc/ The authenticity of host ‘director2 (172.16.1.105)‘ can‘t be established. RSA key fingerprint is 13:42:92:7b:ff:61:d8:f3:7c:97:5f:22:f6:71:b3:24. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘director2‘ (RSA) to the list of known hosts. hosts 100% 328 0.3KB/s 00:00 拷贝此hosts文件至node1 [root@Director1 ~]# scp /etc/hosts node1:/etc/ The authenticity of host ‘node1 (172.16.1.103)‘ can‘t be established. RSA key fingerprint is 1e:87:cd:f0:95:ff:a8:ef:19:bc:c6:e7:0a:87:6b:fa. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘node1,172.16.1.103‘ (RSA) to the list of known hosts. root@node1‘s password: hosts 100% 328 0.3KB/s 00:00 拷贝此hosts文件至node2 [root@Director1 ~]# scp /etc/hosts node2:/etc/ The authenticity of host ‘node2 (172.16.1.104)‘ can‘t be established. RSA key fingerprint is 1e:87:cd:f0:95:ff:a8:ef:19:bc:c6:e7:0a:87:6b:fa. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘node2,172.16.1.104‘ (RSA) to the list of known hosts. root@node2‘s password: hosts 100% 328 0.3KB/s 00:00
3、关闭iptables和selinux
Director1 [root@Director1 ~]# service iptables stop [root@Director1 ~]# vim /etc/sysconfig/selinux [root@Director1 ~]# cat /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - SELinux is fully disabled. #SELINUX=permissive SELINUX=disabled # SELINUXTYPE= type of policy in use. Possible values are: # targeted - Only targeted network daemons are protected. # strict - Full SELinux protection. SELINUXTYPE=targeted Director2 [root@Director2 ~]# service iptables stop [root@Director2 ~]# vim /etc/sysconfig/selinux [root@Director2 ~]# cat /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - SELinux is fully disabled. #SELINUX=permissive SELINUX=disabled # SELINUXTYPE= type of policy in use. Possible values are: # targeted - Only targeted network daemons are protected. # strict - Full SELinux protection. SELINUXTYPE=targeted node1 [root@node1 ~]# service iptables stop [root@node1 ~]# vim /etc/sysconfig/selinux [root@node1 ~]# cat /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - SELinux is fully disabled. #SELINUX=permissive SELINUX=disabled # SELINUXTYPE= type of policy in use. Possible values are: # targeted - Only targeted network daemons are protected. # strict - Full SELinux protection. SELINUXTYPE=targeted node2 [root@node2 ~]# service iptables stop [root@node2 ~]# vim /etc/sysconfig/selinux [root@node2 ~]# cat /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - SELinux is fully disabled. #SELINUX=permissive SELINUX=disabled # SELINUXTYPE= type of policy in use. Possible values are: # targeted - Only targeted network daemons are protected. # strict - Full SELinux protection. SELINUXTYPE=targeted
本文出自 “Hello,Linux” 博客,请务必保留此出处http://soysauce93.blog.51cto.com/7589461/1734765
原文地址:http://soysauce93.blog.51cto.com/7589461/1734765