(一)keepalived介绍:
Keepalived是Linux下轻量级的高可用的解决方案。Keepalived主要是通过虚拟路由冗余来实现高可用功能,具有部署和使用非常简单,只需一个配置文件即可。它是根据TCP/IP参考模型的第三,第四和第五层交换机检测到每个服务接点的状态,如果某个服务节点出现异常,或者出现故障,keepalived将检测到,并将出现故障的服务节点从集群中剔除,而在故障节点恢复后,keepalived又可以自动将此服务节点重新加入服务器集群中,这些工作全部自动完成,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。(2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性)。keepalived采用的是vrrp来实现的。
Keepalived是一个高可用解决方案,主要是用来防止服务器单点发生故障,可以通过和Nginx配合来实现Web服务的高可用。(其实,Keepalived不仅仅可以和Nginx配合,还可以和很多其他服务配合)
Keepalived+Nginx实现高可用的思路:
第一:请求不要直接打到Nginx上,应该先通过Keepalived(这就是所谓虚拟IP,VIP)
第二:Keepalived应该能监控Nginx的生命状态(提供一个用户自定义的脚本,定期检查Nginx进程状态,进行权重变化,,从而实现Nginx故障切换)
(二)安装和配置:
1,编译安装:
1
2
3
4
5
6
7
8
9
|
[root@localhost install ] #cd /tmp/install
[root@localhost install ] # yum install gcc gcc-c++ openssl* kernel-devel net-snmp* libnl* -y
[root@localhost install ] #yum install -y libnfnetlink-devel
[root@localhost install ] #wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
###或者直接在官网上下载 [root@localhost install ] #tar zxvf keepalived-1.2.19.tar.gz
[root@localhost install ] #cd keepalived-1.2.19
[root@localhost install ] #./configure --prefix=/usr/local/keepalived
[root@localhost install ] #make && make install
|
2,配置:
(1)环境说明
hostname |
PI | 安装软件 | OS |
Master |
192.168.122.120 | nginx(已安装).keepalived | Centos6.5 |
backup | 192.168.122.121 | nginx(已安装).keepalived | Centos6.5 |
vip(虚拟IP) |
192.168.122.130 |
备注:依次在192.168.122.120和192.168.122.121进行编译安装如上边的步骤,安装完成之后,会在/usr/local下生成bin、etc、sbin三个文件夹,主要的配置文件在etc目录下,执行文件在sbin目录下
(2)依次在120和121上操作:
(a)####keepalive启动时默认会去读/etc/keepalived/keepalived.conf配置文件,但编译安转后,没有自动创建这个文件,而是放到了/usr/local/etc/keepalived/keepalived.conf
为了以后配置方便,我们手动将/usr/local/etc/keepalived/keepalived.conf放到/etc/keepalived/下
1
2
3
|
[root@localhost install ] #mkdir /etc/keepalived/
[root@localhost install ] #cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost install ] #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
|
(b)#####建立服务启动脚本
1
2
3
4
5
6
|
[root@localhost install ] #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@localhost install ] #chmod +x /etc/init.d/keepalived
####下边是centos7.x版本做成服务 [root@localhost install ] #cp /tmp/install/keepalived-1.4.0/keepalived/keepalived.service /lib/systemd/system/keepalived.service
[root@localhost install ] #systemctl enable keepalived.service
Created symlink from /etc/systemd/system/multi-user .target.wants /keepalived .service to /usr/lib/systemd/system/keepalived .service.
|
(c)####更改启动脚本中keepalive启动参数文件的位置,脚本文件路径是/etc/sysconfig/keepalived(如果是通过yum安装,则这个位置就是正确的),而实际文件是在/usr/local/keepalived/etc/sysconfig/keepalived
#sed -i 's#/etc/sysconfig/keepalived#/usr/local/etc/sysconfig/keepalived#g' /etc/init.d/keepalived
[root@localhost install]#vi /etc/init.d/keepalived
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#!/bin/sh # # Startup script for the Keepalived daemon # # processname: keepalived # pidfile: /var/run/keepalived.pid # config: /etc/keepalived/keepalived.conf # chkconfig: - 21 79 # description: Start and stop Keepalived # Source function library . /etc/rc .d /init .d /functions
# Source configuration file (we set KEEPALIVED_OPTIONS there) . /usr/local/keepalived/etc/sysconfig/keepalived
RETVAL=0 prog= "keepalived"
start() { echo -n $ "Starting $prog: "
daemon keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL - eq 0 ] && touch /var/lock/subsys/ $prog
} "/etc/init.d/keepalived" 74L, 1329C
|
(d)给/usr/local/keepalived/sbin/keepalived做个软连接,否则会报错
1
2
3
|
[root@localhost sbin] # /etc/init.d/keepalived start
Starting keepalived: /bin/bash : keepalived: command not found
[FAILED]
|
1
2
3
4
|
[root@localhost sbin] # ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/
[root@localhost sbin] # /etc/init.d/keepalived start
Starting keepalived: [ OK ] [root@localhost sbin] #
|
(3)配置keepalived.conf 主要配置三个地方:global_defs、vrrp_instance、virtual_ip address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
root@localhost sbin] # vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { #全局设置
router_id LVS_DEVEL #定义路由标识信息,相同局域网唯一,可自定义
} vrrp_script check_http { #首先在vrrp_script区域定义脚本名字和脚本执行的间隔和脚本执行的优先级变更
script "/opt/nginx_pid.sh" #然后在实例(vrrp_instance)里面引用,有点类似脚本里面的函数引用一样:先定义,后引用函数名
interval 2 #脚本执行间隔时间
weight 10 #利用权重值和优先级进行运算,从而降低主服务优先级使之变为备服务器(建议先忽略)
} vrrp_instance VI_1 { #定义一个vrrp实例名,可自定义
state MASTER #设定初始状态,可以是MASTER或BACKUP,不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER,因此该项其实没有实质用途。
interface eth0 #具有固定ip的网卡,用来接收和发送vrrp包。如果没有设定mcast_src_ip发送多播数据包的地址,那么将使用这个网卡的ip来发送。
virtual_router_id 51 #虚拟路由的id号,范围为0-255,取值在0-255之间,用来区分多个instance的VRRP组播。相同的VRID为一个组,他将决定多播的MAC地址
priority 99 #当前keepalived的优先级,数字越大,优先级越高
advert_int 1 #组播信息发送间隔,两个节点设置必须一样,默认为1秒
authentication { #设置节点间验证信息,所以所有节点必须一致
auth_type PASS #验证类型
auth_pass 1111 #密码
}
track_script { #调用上面定义的检测脚本
chk_http_port weight 20 }
virtual_ipaddress { #设置vip,如果不指定网卡,那么默认绑定到interface指定的网卡,可以通过添加dev ethx的方式,绑定到指定的网卡。
192.168.122.130 }
} |
同理,在121上进行操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
vi /etc/keepalived/keepalived .conf
! Configuration File for keepalived
global_defs { router_id LVS_DEVEL
} vrrp_script chk_http_port { script "/root/chk_httpd.sh"
interval 2
weight 10
} vrrp_instance VI_1 { state BACKUP #此处为BACKUP
interface eth0
virtual_router_id 51
priority 96 #此处比MASTER要小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
/opt/nginx_pid .sh
weight 20
}
virtual_ipaddress {
192.168.122.130
}
} |
(4)在两台服务器上创建定义 vrrp_script check_http的脚本(nginx_pid.sh脚本的作用是当nginx关闭时自动启动nginx)
1
2
3
4
5
6
7
8
9
10
|
[root@zabbix ~]# cat /opt/nginx_pid.sh #!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi |
(5)120和121同时启动keepalived
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@hadoop1 ~] # /etc/init.d/keepalived start
Starting keepalived: [ OK ] [root@hadoop1 ~] # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:05:7b:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.120 /24 brd 192.168.122.255 scope global eth0
inet 192.168.122.130 /32 scope global eth0
inet6 fe80::5054:ff:fe05:7b31 /64 scope link
valid_lft forever preferred_lft forever
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@hadoop2 ~] # /etc/init.d/keepalived start
Starting keepalived: [ OK ] [root@hadoop2 ~] # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:c6:65:80 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.121 /24 brd 192.168.122.255 scope global eth0
inet6 fe80::5054:ff:fec6:6580 /64 scope link
valid_lft forever preferred_lft forever
|
打开浏览器进行查看:
(5)验证实验结果:
实验一:关闭120的keepalived服务看是否切换:
1
2
3
4
5
6
7
8
9
10
11
|
[root@hadoop1 ~] # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:05:7b:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.120 /24 brd 192.168.122.255 scope global eth0
inet6 fe80::5054:ff:fe05:7b31 /64 scope link
valid_lft forever preferred_lft forever
|
在查看下121的IP地址:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@hadoop2 ~] # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1 /8 scope host lo
inet6 ::1 /128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:c6:65:80 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.121 /24 brd 192.168.122.255 scope global eth0
inet 192.168.122.130 /32 scope global eth0
inet6 fe80::5054:ff:fec6:6580 /64 scope link
valid_lft forever preferred_lft forever
[root@hadoop2 ~] #
|
已成功切换,在打开浏览器:
本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1735397