Linux负载均衡实现之一:搭建keepalived服务
<pre name="code" class="cpp">Linux负载均衡实现之一:搭建keepalived服务一,keepalived安装
1,下载keepalived
2,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 keepalived
global_defs {
notification_email {
}
router_id LVS_DEVEL
}
vrrp_instance VI_1 {##VI_1是名称,可以自取
state MASTER##主机就是MASTER,备机就是BACKUP
interface eth0##网卡设备入口,用ifconfig查看已激活网卡设备
virtual_router_id 51##该id在整个负载均衡生态种必须相同
priority 150##优先权限,主机应该大于备机
advert_int 1##广播时间间隔
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {##虚拟ip地址,估计很多新手就不明白这是什么,为了让你们印象更深刻,所以自己去专研吧
192.100.1.186##多个可以写多行
}
}
virtual_server 192.100.1.186 3000 {##虚拟机相关设定
delay_loop 6
lb_algo wlc
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.100.1.150 3000 {##真实服务器
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3000
}
}
real_server 192.100.1.150 3000 {##真实服务器
weight 2
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3000
}
}
-----------------------------end---------------------------
------备机------------------begin--------------------------
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.100.1.186
}
}
virtual_server 192.100.1.186 3000 {
delay_loop 6
lb_algo wlc
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.100.1.129 3000 {
weight 2
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3000
}
}
real_server 192.100.1.150 3000 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3000
}
}
}
-----------------------------end---------------------------
2,启动web或者是你要检查的服务,然后启动主机和备机的keepalived服务:service keepalived start
3,如果需要查看日志则tail -f /var/log/message
4,在地址栏输入虚拟ip地址,可以看到是由主机来响应的,现在停掉主句的服务,在访问,就会发现已经有备机来处理了。
5,这时就可以在备机工作的时候来处理主机的问题了
6,如果发现无法访问虚拟ip但是又能ping通,那说明你防火墙未对该端口开放,要么关掉防火墙,要么在防火墙中开启该端口
7,done~
备注,虚拟ip可以直接通过ifconfig 设备:标识 ip netmask 掩码 up 来设置临时性的虚拟ip
也可以直接到/etc/sysconfig/network-scripts/目录下复制一个网络设备配置文件来修改
修改完毕后service netwrok restart 就能永久生效
页:
[1]