Linux负载均衡实现之一:搭建keepalived服务

来源:互联网 发布:淘宝十字绣成品 编辑:程序博客网 时间:2024/05/16 00:48
<pre name="code" class="cpp">Linux负载均衡实现之一:搭建keepalived服务一,keepalived安装1,下载keepalived2,tar -xvf 解包3,如果没有安装kernel,或 openssl的,请使用yum install kernel-devel 和yum install openssl-devel进行安装4,进入到解包后的keepalived的主目录里5,如果使用默认安装路径则直接./configure,如果想指定安装路径则./configure --PERFIX=路径,新手推荐使用默认,一下文件路径都是按照默认路径来描述的6,接着使用命令 make && make install 来进行安装7,安装完毕后,为了方便服务的启动,我们需要把启动文件复制到init.d目录,命令如下:cp /usr/bin/keepalived /etc/rc.d/init.d/keepalived   复制完毕后我们就可以直接使用service keepalived 的相关命令了,当然我们现在不需要启动它二,keepalived的配置1,keepalived的相关配置文件在目录/etc/keepalived/keepalived.conf,打开看一下发现里面内容挺多的,别着急,我们需要的不需要这么多参考(from begin to end,begin&end not included):------主机------------------begin--------------------------! Configuration File for keepalivedglobal_defs {   notification_email {   }   router_id LVS_DEVEL}vrrp_instance VI_1 {##VI_1是名称,可以自取state MASTER##主机就是MASTER,备机就是BACKUPinterface eth0##网卡设备入口,用ifconfig查看已激活网卡设备virtual_router_id 51##该id在整个负载均衡生态种必须相同priority 150##优先权限,主机应该大于备机advert_int 1##广播时间间隔authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {##虚拟ip地址,估计很多新手就不明白这是什么,为了让你们印象更深刻,所以自己去专研吧   192.100.1.186##多个可以写多行}}virtual_server 192.100.1.186 3000 {##虚拟机相关设定delay_loop 6lb_algo wlclb_kind DRnat_mask 255.255.255.0persistence_timeout 50protocol TCPreal_server 192.100.1.150 3000 {##真实服务器weight 3TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3000}}real_server 192.100.1.150 3000 {##真实服务器weight 2TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3000}}-----------------------------end---------------------------------备机------------------begin--------------------------! Configuration File for keepalivedglobal_defs {   notification_email {   }   router_id LVS_DEVEL}vrrp_instance VI_1 {state BACKUPinterface eno16777736virtual_router_id 51priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {   192.100.1.186}}virtual_server 192.100.1.186 3000 {delay_loop 6lb_algo wlclb_kind DRnat_mask 255.255.255.0persistence_timeout 50protocol TCPreal_server 192.100.1.129 3000 {weight 2TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3000}}real_server 192.100.1.150 3000 {weight 3TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3000}}}-----------------------------end---------------------------2,启动web或者是你要检查的服务,然后启动主机和备机的keepalived服务:service keepalived start3,如果需要查看日志则tail -f /var/log/message4,在地址栏输入虚拟ip地址,可以看到是由主机来响应的,现在停掉主句的服务,在访问,就会发现已经有备机来处理了。5,这时就可以在备机工作的时候来处理主机的问题了6,如果发现无法访问虚拟ip但是又能ping通,那说明你防火墙未对该端口开放,要么关掉防火墙,要么在防火墙中开启该端口7,done~备注,虚拟ip可以直接通过ifconfig 设备:标识 ip netmask 掩码 up 来设置临时性的虚拟ip也可以直接到/etc/sysconfig/network-scripts/目录下复制一个网络设备配置文件来修改修改完毕后service netwrok restart 就能永久生效


                                             
0 0
原创粉丝点击