一、keepalived是什么?
1、keepalived是干嘛的?
keepalived是一种检测web服务器健康状态的软件。通俗的说,就是检测集群中的web服务器是正常运行还是挂掉了,如果某台web服务器挂掉了,则它会自动把它从集群中剔除掉,如果web服务器从挂掉的状态恢复到正常运行的状态,又会自动的把这台web服务器加入到集群中,这些过程都是自动的,不需要人工干预。(当然服务器挂掉了,让它重新恢复正常,则需要人工去干预)
更多的资料可以参考官方文档:http://www.keepalived.org/documentation.html
2、keepalived安装与配置
可以参考keepalived源码包中的INSTALL文件
wget
tar -zxvf keepalived-1.2.17.tar.gz
cd keepalived-1.2.17
./configure
make&&make install
将keepalived加入开机服务
mkdir /etc/keepalived
cp keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
cp keepalived/etc/init.d/keepalived.sysconfig /etc/sysconfig/keepalived
cp keepalived/etc/init.d/keepalived.init /etc/init.d/keepalived
ln -s /usr/local/sbin/keepalived /usr/sbin/keepalived
chkconfig --add keepalived
二、nginx+keepalived实现nginx主备切换
1、如何检测一台机器或者某个服务挂掉或者正在运行呢?
我们这里以nginx服务为例,检测nginx服务是否正常,可以使用通过nginx的status模块,检测nginx的进程,检测nginx所在端口,或者访问nginx中某些页面来检测,我们这里以检测端口来实现,检测端口就不得不提一个软件叫nmap的软件。
注:nmap需要我们自己先用yum -y install nmap安装一下
比如我来扫描一下新浪的端口开放情况(只是例子)
nmap www.sina.com.cn
执行过程
[root@server200 src]# nmap www.sina.com.cn
Starting Nmap 5.51 ( http://nmap.org ) at 2015-06-16 15:15 CST
Nmap scan report for www.sina.com.cn (202.102.75.147)
Host is up (0.017s latency).
Not shown: 986 closed ports
PORT STATE SERVICE
21/tcp filtered ftp
22/tcp filtered ssh
23/tcp filtered telnet
80/tcp open http
135/tcp filtered msrpc
139/tcp filtered netbios-ssn
161/tcp filtered snmp
389/tcp filtered ldap
445/tcp filtered microsoft-ds
873/tcp filtered rsync
3389/tcp filtered ms-term-serv
4444/tcp filtered krb524
5631/tcp filtered pcanywheredata
5900/tcp filtered vnc
查看本地某台机器的端口
[root@server200 src]# nmap 192.168.18.201
Starting Nmap 5.51 ( http://nmap.org ) at 2015-06-16 15:17 CST
Nmap scan report for 192.168.18.201 (192.168.18.201)
Host is up (0.00027s latency).
Not shown: 995 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
139/tcp open netbios-ssn
445/tcp open microsoft-ds
3306/tcp open mysql
MAC Address: 00:0C:29:AA:97:B7 (VMware)
查看本地某几台机器 的端口
nmap 192.168.18.200-203
查看某台机器上某个端口(如80端口)
[root@server200 src]# nmap -p 80 192.168.18.200
Starting Nmap 5.51 ( http://nmap.org ) at 2015-06-16 15:19 CST
Nmap scan report for 192.168.18.200 (192.168.18.200)
Host is up (0.000034s latency).
PORT STATE SERVICE
80/tcp open http
Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
2、keepalived的配置
keepalived的配置文件与nginx类似,也是分模块的。具体配置参数详解,请参考https://github.com/acassen/keepalived/blob/master/doc/keepalived.conf.SYNOPSIS
(1)集群中主服务器的配置
! Configuration File for keepalived
global_defs {
router_id NGINX_CLUSTER
}
vrrp_script check_nginx_status {
script "/usr/local/sysscripts/keepalived/check_nginx_status.sh"
interval 3
weight -3
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.180
}
}
track_script {
check_nginx_status
}
~
(2)集群中备份服务器的配置
! Configuration File for keepalived
global_defs {
router_id NGINX_CLUSTER
}
vrrp_script check_nginx_status {
script "/usr/local/sysscripts/keepalived/check_nginx_status.sh"
interval 3
weight -3
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.180
}
}
track_script {
check_nginx_status
}
3、nginx状态检测脚本的编写(主从服务器上都需要这个脚本)
mkdir -p /usr/local/sysscripts/keepalived
vim check_nginx_status.sh
脚本代码如下:
#!/bin/bash
# check nginx status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
nmap localhost -p $PORT | grep "$PORT/tcp open"
if [ $? -ne 0 ]; then
$NGINX -s stop
$NGINX
sleep 3
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne 0 ] && /etc/init.d/keepalived stop
fi
4、检测两台服务器是否已经拥有虚拟IP192.168.18.180
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 UNKNOWN qlen 1000
link/ether 00:0c:29:52:a1:cd brd ff:ff:ff:ff:ff:ff
inet 192.168.18.200/24 brd 192.168.18.255 scope global eth0
inet 192.168.18.180/32 scope global eth0
inet6 fe80::20c:29ff:fe52:a1cd/64 scope link
valid_lft forever preferred_lft forever
5、我们把其中一台机器的nginx停掉,看是否可以访问192.168.18.180这个虚拟IP
注:在同一时间只有一台机器可以使用这个虚拟IP
三、nginx+keepalived实现高可用性负载均衡
1、服务器情况:
192.168.18.200 虚拟IP192.168.18.180
192.168.18.201 虚拟IP192.168.18.180
192.168.18.202
192.168.18.203 这是一个负载均衡调度服务器
架构如下:
2、负载均衡服务器配置
upstream backend {
server 192.168.18.180 weight=3;
server 192.168.18.202;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /webdata/www;
proxy_pass http://backend;
index index.html index.htm index.php;
}
}
3、测试,关掉200或者201是的某一台服务器