3、Linux云计算系列-CentOS6-高可用集群
高可用集群
环境说明:
公司决定使用Keepalived实现DR-LVS的高可用,现在有两台LVS服务器,两台Apache;
案例拓扑:
步骤:
一、LVS主服务器1:(master)
1. 关闭网卡守护进程
service NetworkManager stop && chkconfig NetworkManager off |
2. 修改网卡配置文件
cd /etc/sysconfig/network-scripts/ cp -a ifcfg-eth0 ifcfg-eth0:0 vim ifcfg-eth0:0 DEVICE=eth0:0 ONBOOT=yes BOOTPROTO=static IPADDR=10.10.10.100 NETMASK=255.255.255.0 ifup eth0:0 |
3. 关闭网卡重定向功能
vim /etc/sysctl.conf net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 sysctl -p :(刷新内核参数) |
4. 安装ipvsadm,并配置
yum -y install ipvsadm ipvsadm -A -t 10.10.10.100:80 -s rr ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.13:80 -g ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.14:80 -g service ipvsadm save #保存 ipvsadm集群内容至文件,进行持久化存储 chkconfig ipvsadm on #设置为开机自启 |
5. 上传Keepalived.iso镜像,并挂载该镜像
mount -o loop Keepalived.iso /iso1/ |
6. 复制文件到/root下
cp -a /iso1/* . |
7. 解压keepalived-1.2.2.tar.gz软件包,编译安装
tar -xf keepalived-1.2.2.tar.gz cd keepalived-1.2.2 yum -y install kernel-devel openssl-devel popt-devel gcc* #安装相关依赖 ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-642.el6.x86_64/ make make install |
8. 查看并加入到开机自启
ll /etc/init.d/keepalived chkconfig --add keepalived #设置开机自启 chkconfig keepalived on |
9. 修改keepalived的主配置文件vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id R1 #名称根据情况填写,不能重复 } vrrp_instance VI_1 { state MASTER #设置服务类型主/从(MASTER/SLAVE) interface eth0 #指定那块网卡用来监听 virtual_router_id 66 #设置组号,如果是一组就是相同的 ID 号 priority 80 #服务器优先级,主服务器优先级高,主和从一般差50 advert_int 1 #心跳时间,检测对方存活 authentication { #存活验证密码 auth_type PASS auth_pass 1111 } virtual_ipaddress { } } delay_loop 2 #健康检查间隔 lb_algorr #使用轮询调度算法 lb_kind DR #DR模式的群集 protocol TCP #使用的协议 real_server 10.10.10.13 80 { #管理的网站节点以及使用端口 weight 1 #权重,优先级 在原文件基础上删除修改 TCP_CHECK { #状态检查方式 connect_port 80 #检查的目标端口 connect_timeout 3 #连接超时(秒) nb_get_retry 3 #重试次数 delay_before_retry 4 #重试间隔(秒) } } real_server 10.10.10.14 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 4 } } } |
10. 启动并查看:
service keepalived start cat /var/log/messages |
二、LVS主服务器2:(slave)
1. 修改网卡:eth0:0为10.10.10.100
报错提示:
解决方式:(vim ifup-eth文件注释掉)
2. 上传Keepalived.iso镜像,并挂载该镜像
mount -o loop Keepalived.iso /iso1/ |
3. 复制文件到/root下
cp -a /iso1/* . |
4. 解压keepalived-1.2.2.tar.gz软件包,编译安装
tar -xf keepalived-1.2.2.tar.gz cd keepalived-1.2.2 yum -y install kernel-devel openssl-devel popt-devel gcc* #安装相关依赖 ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-642.el6.x86_64/ make make install |
5. 查看并加入到开机自启
ll /etc/init.d/keepalived chkconfig --add keepalived #设置 Keepalived 开机自启 chkconfig keepalived on |
6. 将主服务器的配置文件,拷贝到从服务器
scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/keepalived.conf(主服务器使用) |
实例:
7. 安装ipvsadm工具
yum -y install ipvsadm service ipvsadm start chkconfig ipvsadm on service keepalived start ipvsadm -Ln #查看LVS规则 |
8. 优化步骤:
vim /etc/sysctl.conf #关闭网卡重定向功能 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 sysctl -p #刷新内核参数 |
三、真实服务器1
1. 开启httpd服务器,并添加网站
service httpd start echo "this is server 1" >> /var/www/html/index.html curl 127.0.0.1 |
2. 修改网卡配置文件
cd /etc/sysconfig/network-scripts/ cp -a ifcfg-lo ifcfg-lo:0 vim !$ DEVICE=lo:0 IPADDR=10.10.10.100 NETMASK=255.255.255.255 |
3. 关闭对应 ARP 响应及公告功能
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 sysctl -p #刷新内核参数 ifup lo:0 |
4. 添加网关记录
route add -host 10.10.10.100 dev lo:0 echo "route add -host 10.10.10.100 dev lo:0" >> /etc/rc.local |
四、真实服务器2:(与服务器一样)
service httpd start && echo "this is server 2" >> /var/www/html/index.html |
五、验证
结论:当断开主LVS服务器时,从服务器进行替代,实现高可用。