http://blog.lunhui.ren/archives/395
centos6.8
我有三台服务器ip分别是172.16.3.241,172.16.3.242,172.16.3.243
1个虚拟ip:172.16.3.240
目的,当我访问172.16.3.240lvs会根据规则路由到241-243 3台不同的机器
master服务器安装:
安装ipvsadm
- yum install ipvsadm
安装keepalived
- yum install keepalived
安装依赖
yum -y install openssldevel yum installncursesdevel gcc gccc++ make rpmbuild
将keepalived配置成系统服务
cp /usrlocalkeepalivedetcrc.dinitkeepalived cp sysconfig mkdir conf sbin/
修改修改主keepalived配置文件(从服务器只修改router_id、state和priority)
- vi conf
- ! ConfigurationFile for keepalived
- global_defs {
- notification_email
- acassen@firewallloc
- failover@firewallloc
- sysadmin@firewallloc
- }
- notification_email_from Alexandre.Cassen@firewallloc
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_instance VI_1
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication
- auth_type PASS
- auth_pass 1111
- virtual_ipaddress
- 172.163.240
- virtual_server 80
- delay_loop 6
- lb_algo rr
- lb_kind DR
- nat_mask 255.255255.0
- persistence_timeout 50
- protocol TCP
- real_server 3.242
- weight 3
- TCP_CHECK
- connect_timeout
- nb_get_retry
- delay_before_retry
- real_server 3.243
- real_server 3.241
- }
配置ipvsadm for Master
把一下内容保存成:lvs_server.sh,并放置在/etc/init.d目录下
- #!/bin/bash #把一下内容保存成:lvs_server.sh#并放置在/etc/init.d目录下#如果想启动LVS Server执行:/etc/init.d/lvs_server.sh start#如果想停止LVS Server执行:/etc/init.d/lvs_server.sh stop#如果想重启LVS Server执行:/etc/init.d/lvs_server.sh restart VIP=#虚拟IP,更具具体情况而变#有几个输入几个,与下面的配置对应,同时必须与KeepAlived.config配置对应 RIP1#实际的服务器IP RIP2 RIP3functions # 如果提示权限不够,那么先在命令行执行: chmod 777 /etc/rc.d/init.d/functionscase "$1"in start) echo "启动LVS服务器"#设置虚拟IP和同步参数ifconfig eth0:0 $VIP broadcast $VIP netmask up echo "1">/procsysnetipv4ip_forward #清空 IPVS的内存数据ipvsadm C #开启WEB 80 端口服务,并指向RIP1和RIP2的服务器A t $VIPs rr a r $RIP1g r $RIP2r $RIP3g #运行LVSln ;; stop"关闭LVS服务器""0"ip_forward C down restart down echo #设置LVS*)"Usage: $0 {start|stop}" exit esac
/etc/init.d/lvs_server.sh使用方法
[root@RServer2 ~]# chmod 777 /etc/init.d/lvs_server.sh
[root@RServer2 ~]# chmod 777 /etc/rc.d/init.d/functions
[root@RServer2 ~]# /etc/init.d/lvs_server.sh start|stop|restart
从服务器安装配置Master的安装基本一致,只是一些参数略有变化。
变化的参数为止如下:
1、keepalived.conf文件中
原:state MASTER #标示状态为MASTER 备份机为BACKUP
state BACKUP #标示状态改为BACKUP
原:priority 100 #MASTER权重要高于BACKUP 比如BACKUP为99
priority 99 #MASTER权重要高于BACKUP,MASTER为100,那么BACKUP略小于,比如99
RealServer配置步骤
把一下内容保存成:lvs_real_server.sh,并放置在/etc/init.d目录下
#把一下内容保存成:lvs_real_server.sh#并放置在root目录下#如果想启动real Server执行:/root/lvs_real_server.sh start#如果想停止real Server执行:/root/lvs_real_server.sh stop CLUSTER_VIPifconfig lo $CLUSTER_VIP netmask broadcast $CLUSTER_VIP route add host $CLUSTER_VIP dev loconfloarp_ignore echo "2"arp_announce echo allarp_announce sysctl p devnull 2>&"真实服务器启动....."route del $CLUSTER_VIP "真实服务器停止....."esac
/root/lvs_real_server.sh使用方法
[root@RServer2 ~]# chmod 777 /root/lvs_real_server.sh
[root@RServer2 ~]# chmod 777 /etc/rc.d/init.d/functions
[root@RServer2 ~]# /root/lvs_real_server.sh start|stop|restart
为了方便测试 每台机器安装httpd服务
- yum install httpd
每台机器都创建index.html
echo “172.16.3.241” > /var/www/html/index.html
echo “172.16.3.242” > /var/www/html/index.html
echo “172.16.3.243” > /var/www/html/index.html
[root@localhost ~]# service iptables stop #关闭防火墙
[root@localhost ~]# setenforce 0 #临时关闭selinux
测试
http://1172.16.3.240 #访问一直刷新会轮训显示1172.16.3.241-243
常用命令: [root@localhost ~]# ipvsadm -ln #显示集群中服务器ip信息 [root@localhost ~]# ip addr #显示VTP绑定在哪个服务器上