實驗環境及軟體版本:
CentOS版本: 6.6(2.6.32.-504.el6.x86_64)
nginx版本: nginx-1.6.2
keepalived版本:keepalived-1.2.7
準備工作:
關閉防火牆:service iptables stop
chkconfig iptables off
關閉selinux: sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config
init 6 重新開機系統一定要!
1.準備安裝依賴包:
yum -y install pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++ popt-devel
安裝keepalived出現configure: error: Popt libraries is required
解決方法:yum -y install popt-devel
2.下載下傳安裝包
wget http://nginx.org/download/nginx-1.6.2.tar.gz
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
3.安裝NginX
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure
make && make install
4.安裝keepalived
tar zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
5.加入開機自啟動服務
方法一
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
echo "/etc/init.d/keepalived start" >> /etc/rc.local
方法二
[root@nginx keepalived-1.2.7]# cat >>/etc/rc.local<<EOF
> /usr/local/nginx/sbin/nginx
> /etc/init.d/keepalived start
> EOF
[root@nginx keepalived-1.2.7]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start
6.配置
6.1 配置nginx
兩台接入伺服器的nginx的配置完全一樣,主要配置/usr/local/nginx/conf/nginx.conf的http.其中多域名指向是通過虛拟主機(配置http下面的server)實作;同一域名的不同虛拟目錄通過每
個server下面的不同location實作.
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$re
quest" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream myserver{
server 192.168.0.110:80 max_fails=3 fail_timeout=20s;
server 192.168.0.111:80 max_fails=3 fail_timeout=20s;
ip_hash;
}
server {
listen 80;
server_name 192.168.0.150;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /var/www/html;
index index.php index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_next_upstream http_500 http_502 http_503 error timeo
ut invalid_header;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://myserver;
}
}
6.2 配置keepalived
按照上面的安裝方法,keepalived的配置檔案在/etc/keepalived/keepalived.conf.主、從伺服器的
配置相關聯但有所不同。如下:
Master:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.etiantian.org
smtp_connect_timeout 30
router_id nginx_master
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 {
192.168.0.150
Backup:
router_id nginx_backup
state BACKUP
priority 99
7.驗證:
先後在主、從伺服器上啟動/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start
在主伺服器上檢視是否已經綁定了虛拟IP(VIP):ip addr
[root@nginx ~]# 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 UP qlen 1000
link/ether 00:0c:29:af:00:4e brd ff:ff:ff:ff:ff:ff
inet 192.168.0.130/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.150/32 scope global eth0
inet6 fe80::20c:29ff:feaf:4e/64 scope link
停止主伺服器上的keepalived:/etc/init.d/keepalived stop然後在從伺服器上檢視是否已經綁
定了虛拟IP(VIP):
[root@nginx_back ~]# ip addr
link/ether 00:0c:29:21:b6:b1 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.131/24 brd 192.168.0.255 scope global eth0
inet6 fe80::20c:29ff:fe21:b6b1/64 scope link
啟動主伺服器上的keepalived:/etc/init.d/keepalived start,再看看主伺服器能否重新接管虛拟IP(VIP)
本文轉自 linuxzkq 51CTO部落格,原文連結:http://blog.51cto.com/linuxzkq/1631209