【实验拓扑】
【实验任务】
主调度器负责分配客户机请求,若出现故障则由从调度器自动接替服务
启用邮件通知功能,以便及时了解各web节点服务器的健康状况
【实验步骤】
一、 配置负载调度器
【主服務器】
1、 Keepalived 的安装与服务控制
安装支持软件与ipvsadm管理工具[r1]
[root@rhel-1 ~]# ifconfig eth1 172.16.16.173
[root@rhel-1 ~]# yun – y install kernel-devel openssl-devel popt-devel
[root@rhel-1 ~]# rpm -ivh /media/RHEL_6.1\ i386\ Disc\ 1/Packages/ipvsadm-1.25-9.el6.i686.rpm
warning: /media/RHEL_6.1 i386 Disc 1/Packages/ipvsadm-1.25-9.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ipvsadm ########################################### [100%]
编译安装Keepalived
[root@rhel-1 /]# tar zxvf keepalived-1.2.7.tar.gz
[root@rhel-1 /]# cd keepalived-1.2.7
[root@rhel-1 keepalived-1.2.7]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-131.0.15.el6.i686/
[root@rhel-1 ~]# modprobe ip_vs // 加载LVS内核模块
[root@rhel-1 /]# make && make install
执行make inatall后,会自动生成/etc/init.d/keepalived脚本文件,但还需要手动添加为系统服务,这样就可以使用service、chkconfig工具来对Keepalived服务程序进行管理
[root@rhel-1 ~]# ls – l /etc/init.d/keepalived
[root@rhel-1 ~]# chkconfig --add keepalived
[root@rhel-1 ~]# chkconfig keepalived
2、 调整/proc响应参数
对于LR群集模式来说,由于LVS负载调度器和各节点需要共用VIP地址,为了避免网络内的ARP解析出现异常,应该关闭linux内核的重定向参数响应
[root@rhel-1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
[root@rhel-1 ~]# sysctl -p
……
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
3、 主服务器配置
Keepalived服务的配置目录位于/etc/keepalived/.其中keepalived.conf是主配置文件;另外包括一个子目录samples/,提供了许多配置样例作为参考
在Keepalived配置文件中,[global_defs{ … }]区段指定全局参数,使用[vrrp_instance 实例名称 { … }]区段指定VRRP 热备参数,注释文字以“!”符号开头
[root@rhel-1 ~]# cd /etc/keepalived/
[root@rhel-1 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@rhel-1 keepalived]# vim keepalived.conf
global_defs {
router_id LVS_Keep2 // 本路由器(服务器)的名称
}
vrrp_instance VI_1 { // 定义VRRP 热备实例
state MASTER // 热备状态,MASTER 表示主服务器
interface eth1 // 承载VIP 地址的物理接口
virtual_router_id 1 // 虚拟路由器的ID 号,每个热备组保持一致
priority 100 // 优先级,数值越大优先级越高
advert_int 1 / / 通告间隔秒数(心跳频率)
authentication { // 认证信息,每个热备组保持一致
auth_type PASS // 认证类型
auth_pass 123456 // 密码字串
}
airtual_ipaddress { // 指定漂移地址(VIP ),可以有多个
172.16.16.172
}
}
[root@rhel-1 keepalived]# service keepalived start
[root@rhel-1 keepalived]# ip addr show dev eth0
确认上述配置无误,然后启动Keepalived 服务。实际状态为MASTER 的主服务器将为eth0
[root@rhel-1 ~]# ip addr show dev eth1
2: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d2:93:5e brd ff:ff:ff:ff:ff:ff
inet 172.16.16.173/16 brd 172.16.255.255 scope global eth1
inet 172.16.16.172/32 scope global eth1
inet6 fe80::20c:29ff:fed2:935e/64 scope link
valid_lft forever preferred_lft forever
在Keepalived 的热备配置基础上,添加【virtual_server VIP port { …. } 】区段来配置虚拟服务器。主要包括对负载调度算法、群集工作模式、健康检查间隔、真实服务器地址等参数的设置
[root@rhel-1 keepalived]# vim keepalived.conf
virtual_server 172.16.16.172 80 { //虚拟服务器地址(VIP)、端口
delay_loop 15 //健康检查的间隔时间(秒)
lb_algo rr //轮询(rr)调度算法
lb_kind DR //直接路由(DR)群集工作模式
! persistence 60 //连接保持时间(秒),若启用请去掉!号
protocol TCP //应用服务采用的是TCP协议
real_server 172.16.16.175 80 { //第一个web节点的地址、端口
weight 1 //节点的权重
TCP_CHECK { //健康检查方式
connect_port 80 //检查的目标端口
connect_timeout 3 // 连接超时(秒)
nb_get_retry 3 //重试次数
delay_before_retry 4 //重试间隔(秒)
}
}
real_server 172.16.16.176 80 { //第二个web节点的地址、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
【从服務器】
从调度器的配置与主调度器基本相同,也包括全局配置、热备配置、服务器池配置,只需要调整router_id、state、priority参数即可,其余内容完成相同
1 、Keepalived的安装与服务控制
安装支持软件与ipvsadm管理工具
[root@bukyong ~]# ifconfig eth1 172.16.16.174
[root@bukyong ~]# yun – y install kernel-devel openssl-devel popt-devel
[root@bukyong ~]# rpm -ivh /media/RHEL_6.1\ i386\ Disc\ 1/Packages/ipvsadm-1.25-9.el6.i686.rpm
warning: /media/RHEL_6.1 i386 Disc 1/Packages/ipvsadm-1.25-9.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ipvsadm ########################################### [100%]
编译安装Keepalived
[root@bukyong /]# tar zxvf keepalived-1.2.7.tar.gz
[root@bukyong /]# cd keepalived-1.2.7
[root@bukyong keepalived-1.2.7]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-131.0.15.el6.i686/
[root@bukyong ~]# modprobe ip_vs // 加载LVS内核模块
[root@bukyong /]# make && make install
执行make inatall后,会自动生成/etc/init.d/keepalived脚本文件,但还需要手动添加为系统服务,这样就可以使用service、chkconfig工具来对Keepalived服务程序进行管理
[root@bukyong ~]# ls – l /etc/init.d/keepalived
[root@bukyong ~]# chkconfig --add keepalived
[root@bukyong ~]# chkconfig keepalived
2 、调整/proc响应参数
对于LR群集模式来说,由于LVS负载调度器和各节点需要共用VIP地址,为了避免网络内的ARP解析出现异常,应该关闭linux内核的重定向参数响应
[root@bukyong ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
[root@bukyong ~]# sysctl -p
……
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
3 、主服务器配置
Keepalived服务的配置目录位于/etc/keepalived/.其中keepalived.conf是主配置文件;另外包括一个子目录samples/,提供了许多配置样例作为参考
在Keepalived配置文件中,[global_defs{ … }]区段指定全局参数,使用[vrrp_instance 实例名称 { … }]区段指定VRRP 热备参数,注释文字以“!”符号开头
[root@bukyong ~]# cd /etc/keepalived/
[root@bukyong keepalived]# cp keepalived.conf keepalived.conf.bak
[root@bukyong keepalived]# vim keepalived.conf
global_defs {
router_id LVS_Keep2 // 与主服务器的区别处
}
vrrp_instance VI_1 {
state SLAVE // 与主服务器的区别处
interface eth1
virtual_router_id 1
priority 90 // 与主服务器的区别处
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
airtual_ipaddress {
172.16.16.172
}
}
[root@bukyong keepalived]# service keepalived start
[root@bukyong keepalived]# ip addr show dev eth1
确认上述配置无误,然后启动Keepalived 服务。实际状态为MASTER 的主服务器将为eth0
2: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d2:93:5e brd ff:ff:ff:ff:ff:ff
inet 172.16.16. 174 /16 brd 172.16.255.255 scope global eth1
inet 172.16.16.172/32 scope global eth1
inet6 fe80::20c:29ff:fed2:935e/64 scope link
valid_lft forever preferred_lft forever
在Keepalived 的热备配置基础上,添加【virtual_server VIP port { …. } 】区段来配置虚拟服务器。主要包括对负载调度算法、群集工作模式、健康检查间隔、真实服务器地址等参数的设置
[root@bukyong keepalived]# vim keepalived.conf
virtual_server 172.16.16.172 80 {
delay_loop 15
lb_algo rr
lb_kind DR
! persistence 60
protocol TCP
real_server 172.16.16.175 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 172.16.16.176 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
二、 配置NFS共享存储服务
NFS(Network File System 网络文件系统)是一种基于TCP/IP传输的网络文件系统协议,最早由SUN公司开发。通过使用NFS协议,客户机可以像访问本地目录一样访问远程服务器中的共享资源。对于大多数群集来说,使用NFS协议来共享数据存储是比较常见的做法,NFS也是NAS 存储设备必然支持的一种协议
依赖于RPC (远端过程调用)机制,以完成远程到本地的映射:需安装nfs-utils 、rpcbind 软件包;系统服务:nfs 、rpcbind ;共享配置文件:/etc/exports
1 、安装nfs-utils、rpcbind软件包
基本配置、共享源及网页文件、安装、启用等
[root@rhel-1 ~]# ifconfig eth1 172.16.16.250/16
[root@rhel-1 ~]# echo 'this is LVS+Keepalived web server!!!' > /var/www/html/index.html
[root@rhel-1 ~]# rpm -ivh /media/RHEL_6.1\ i386\ Disc\ 1/Packages/nfs-utils-1.2.3-7.el6.i686.rpm
warning: /media/RHEL_6.1 i386 Disc 1/Packages/nfs-utils-1.2.3-7.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
package nfs-utils-1:1.2.3-7.el6.i686 is already installed
[root@rhel-1 ~]# chkconfig nfs on
[root@rhel-1 ~]# chkconfig rpcbind on
[root@rhel-1 ~]# vim /etc/exports
/ywlin *(rw,sync,no_root_squash)
[root@rhel-1 ~]# service rpcbind start
[root@rhel-1 ~]# service nfs start
启动 NFS 服务: [确定]
关掉 NFS 配额: [确定]
启动 NFS 守护进程: [确定]
启动 NFS mountd: [确定]
[root@rhel-1 ~]# netstat -anpt | grep :111
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1596/rpcbind
tcp 0 0 127.0.0.1:111 127.0.0.1:917 ESTABLISHED 1596/rpcbind
tcp 0 0 127.0.0.1:917 127.0.0.1:111 ESTABLISHED -
tcp 0 0 :::111 :::* LISTEN 1596/rpcbind
[root@rhel-1 ~]# showmount -e 172.16.16.250
Export list for 172.16.16.250:
/var/www/html *
三、 配置节点服务器(web1)
使用DR模式时,节点服务器也需要配置VIP地址,并调整内核的ARP响应参数以阻止更新VIP的MAC地址,避免发生冲突
1 、配置虚拟IP地址(VIP)
在每个节点服务器,同样需要需要具有VIP地址172.16.16.172 ,但地址仅用作发送Web响应数据包的源地址,并不需要监听客户机的访问请求(改由调度器监听并分发)。因此使用虚接口lo:0来承载VIP地址,并为本机添加一条路由记录,将访问VIP的数据限制在本地以避免通信紊乱
[root@rhel-1 ~]# ifconfig eth1 172.16.16.177/16
[root@rhel-1 ~]# cat > /etc/sysconfig/network-scripts/ifcfg-lo:0 IPADDR=172.16.16.172
> NETMASK=255.255.255.255
> ONBOOT=yes
> end
[root@rhel-1 ~]# ifup lo:0
活跃连接状态:激活的
活跃连接路径:/org/freedesktop/NetworkManager/ActiveConnection/2
[root@rhel-1 ~]# ifconfig lo:0
lo:0 Link encap:Local Loopback
inet addr:172.16.16.172 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
[root@rhel-1 ~]# route add -host 172.16.16.172 dev lo:0
//添加VIP本地访问路由生产环境中与入/etc/rc.local文件
2 、调整/proc响应参数
服务状态:“0”表示停用 、“1”表示启用、 “2”表示关闭
[root@rhel-1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@rhel-1 ~]# sysctl – p
…………
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
3 、安装rpcbind软件包,并启动服务
[root@rhel-1 ~]# rpm -ivh /media/RHEL_6.1\ i386\ Disc\ 1/Packages/rpcbind-0.2.0-8.el6.i686.rpm
warning: /media/RHEL_6.1 i386 Disc 1/Packages/rpcbind-0.2.0-8.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
package rpcbind-0.2.0-8.el6.i686 is already installed
[root@rhel-1 ~]# chkconfig rpcbind on
[root@rhel-1 ~]# service rpcbind start
4 、安装httpd,创建测试网页来源(NFS-Server),手动挂载NFS共享目录
这里已安装httpd服务,就不再安装了
[root@rhel-1 ~]# mount 172.16.16.250:/var/www/html /var/www/html/
[root@rhel-1 ~]# showmount -e 172.16.16.250
Export list for 172.16.16.250:
/ywlin *
5 、启动httpd服务,由于网站根目录是运程挂载到本地,所以应……
[root@rhel-1 html]# getsebool -a | grep httpd
httpd_enable_homedirs --> off
httpd_execmem --> off
[root@rhel-1 html]# setsebool httpd_enable_homedirs on
[root@rhel-1 html]# service httpd start
正在启动 httpd: [确定 ]
四、 配置节点服务器(web2)
Web2 的配置与Web1基本相同,不同的只是IP地址
有更多的服务器节点也是如此
五、 LVS+Keepalived 集成测试
在test机上访问LVS-Keep调度服器的虚拟IP地址:172.16.16.172访问网站
验证调度服务器的主、从状态
从上两张图记录列表可知,此时LVS+Keep1 为主调度服务器,LVS+Keep2 为从调度服务器
禁用主调度服务器eht1网卡,后再从test机上访问VIP
从上两张图记录列表可知,此时LVS+Keep2 自动成为了主调度服务器,继续工作
重新启用主调度服务器eht1网卡,后再从test机上访问VIP
从上两张图记录列表可知,此时LVS+Keep1 再次自动成为了主调度服务器,LVS+Keep2 为从调度服务器
禁用web服务器池的一部分节点,确认对web群集的访问仍然正常
六、 启用邮件通知功能(主、从调度器)
1 、安装sendmail软件包、启用sendmail服务,为本机提供邮件发送服务
这里用YUM安装
[root@rhel-1 ~]# rm -rf /etc/yum.repos.d/*
[root@rhel-1 ~]# cat >/etc/yum.repos.d/local.repo baseurl=file:///media/RHEL_6.0\ i386\ Disc\ 1/
> enabled=1
> gpgcheck=1
> gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
> end
[root@rhel-1 ~]# yum -y install sendmail
Loaded plugins: refresh-packagekit, rhnplugin
Repository 'local' is missing name in configuration, using id
This system is not registered with RHN.
RHN support will be disabled.
local | 3.7 kB 00:00 ...
local/primary_db | 2.3 MB 00:00 ...
Setting up Install Process
Resolving Dependencies
--> Running transaction check
……
[root@rhel-1 ~]# chkconfig --add sendmail
[root@rhel-1 ~]# chkconfig sendmail on
[root@rhel-1 ~]# service sendmail start
正在启动 sendmail: [确定]
启动 sm-client: [确定]
2 、在Keepalived的全局配置中添加邮件通知设置,重新加载服务
[root@rhel-1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_Keep1
notification_email {
ywlin163@163.com
}
notification_email_from TsengYia root@rhel-1.localdomain
smtp_server 127.0.0.1
smtp_connect_timeout 30
}
[root@rhel-1 ~]# service keepalived restart
3 、先停止部分节点的WEB服务,过几分钟再恢复,查问通知邮件内容
[r1] 主要操作选项
-A 添加虚拟服务器
-t 设置群集地址(VIP ,Virtual IP )
-s 指定负载调度算法
-a 添加真实服务器
-d 删除真实服务器
-D 删除整个虚拟服务器
-r 指定真实服务器(Real Server )的地址
-m 使用NAT 模式;-g 、-i 分别对应DR 、TUN 模式
-w 为节点服务器设置权重默认为1
-L 查看LVS 虚拟服务器,可以指定只查看某一个VIP 地址
-n 以数字形式显示地址、端口等信息,结合其他选项使用
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com