大纲
一、什么是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、时间同步
[[email protected] ~]# ntpdate s2c.time.edu.cn [[email protected] ~]# ntpdate s2c.time.edu.cn [[email protected] ~]# ntpdate s2c.time.edu.cn [[email protected] ~]# ntpdate s2c.time.edu.cn 可根据需要在每个节点上定义crontab任务 [[email protected] ~]# which ntpdate /sbin/ntpdate [[email protected] ~]# echo "*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null" >> /var/spool/cron/root [[email protected] ~]# crontab -l */5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null
2、主机名称要与uname -n保持一致,并通过/etc/hosts解析
Director1 [[email protected] ~]# hostname Director1 [[email protected] ~]# uname -n Director1 [[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘ /etc/sysconfig/network Director2 [[email protected] ~]# hostname Director2 [[email protected] ~]# uname -n Director2 [[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘ /etc/sysconfig/network node1 [[email protected] ~]# hostname node1.network.com [[email protected] ~]# uname -n node3.network.com [[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘ /etc/sysconfig/network node2 [[email protected] ~]# hostname node2.network.com [[email protected] ~]# uname -n node2.network.com [[email protected] ~]# sed -i ‘[email protected]\(HOSTNAME=\).*@\[email protected]‘ /etc/sysconfig/network node1添加hosts解析 [[email protected] ~]# vim /etc/hosts [[email protected] ~]# 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 [[email protected] ~]# 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 [[email protected] ~]# 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. [email protected]‘s password: hosts 100% 328 0.3KB/s 00:00 拷贝此hosts文件至node2 [[email protected] ~]# 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. [email protected]‘s password: hosts 100% 328 0.3KB/s 00:00
3、关闭iptables和selinux
Director1 [[email protected] ~]# service iptables stop [[email protected] ~]# vim /etc/sysconfig/selinux [[email protected] ~]# 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 [[email protected] ~]# service iptables stop [[email protected] ~]# vim /etc/sysconfig/selinux [[email protected] ~]# 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 [[email protected] ~]# service iptables stop [[email protected] ~]# vim /etc/sysconfig/selinux [[email protected] ~]# 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 [[email protected] ~]# service iptables stop [[email protected] ~]# vim /etc/sysconfig/selinux [[email protected] ~]# 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
时间: 2024-12-23 11:41:58