keepalived+ngnix

keepalived+nginx實驗來實現負載均衡

keepalived實現高可用
ngnix實現負載均衡

實驗環境 rhel6.5 selinux and iptables disabled
高可用虛擬機:172.25.12.1 server1
172.25.12.1 server1
負載均衡虛擬機: 172.25.12.1 server3
172.25.12.1 server3
在server1上
所需軟件包
nginx-1.8.1.tar.gz
keepalived-1.2.20.tar.gz

nginx的安裝與配置
tar zxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
註釋掉dubuge
vim auto/cc/gcc
178 # debug
179 #CFLAGS=”$CFLAGS -g”
./configure –prefix=/usr/local/nginx
解決依賴
yum install -y gcc
yum install -y pcre-devel

make && make install
修改配置文件,配置文件如下:

[root@server1 log]# cat  /usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
 upstream westos {
server 172.25.12.3:80;
server 172.25.12.4:80;

    }
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

        location / {

            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen 80;
    server_name www.westos.org;
    location / {
        proxy_pass http://westos;
    }
    }     
}

keepalived的安裝與配置
tar zxf keepalived-1.2.20.tar.gz
cd keepalived-1.2.20
./configure –prefix=/usr/local/keepalived –with-init=SYSV
make && make install

ln -s /usr/local/keepalived/sbin/keepalived /sbin/
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived   /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived   /etc/init.d/
 chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived

vim /etc/keepalived/keepalived.conf

[root@server1 opt]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script nginx_check {
    script /opt/nginx_check.sh
    interval 2

    }

global_defs {
   notification_email {
      root@localhost
   }

   notification_email_from keepalived@server4
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 12
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.12.200
    }
   track_script {
nginx_check

    }

}

keepalived調用的腳本

[root@server1 log]# cat /opt/nginx_check.sh
#!/bin/bash

curl http://127.0.0.1/index.html -o /dev/null -s || /usr/local/nginx/sbin/nginx

if [ $? -ne 0  ];then
/etc/init.d/keepalived stop &> /dev/null
fi

注意腳本要有執行權限

將安裝好的ngnix和keepalived複製到另一個節點
cd /usr/local
scp -r nginx [email protected]:/usr/local
scp -r keepalived [email protected]:/usr/local
cd /opt/
scp nginx_check.sh [email protected]:/opt/

在server2 上查看 並修改keepalived的配置文件

[root@server2 opt]# ll nginx_check.sh
-rwxr-xr-x 1 root root 160 Jul 30 15:35 nginx_check.sh
[root@server2 opt]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script nginx_check {
    script /opt/nginx_check.sh
    interval 2

    }

global_defs {
   notification_email {
      root@localhost
   }

   notification_email_from keepalived@server2
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BUCKUP
    interface eth0
    virtual_router_id 12
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.12.200
    }
   track_script {
nginx_check

    }

}

資源服務器:
server3:
[root@server3 ~]# yum install -y httpd
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# ls
[root@server3 html]# echo server3 > index.html
[root@server3 html]# /etc/init.d/httpd start

server4:
[root@server4 ~]# yum install -y httpd
[root@server4 ~]# cd /var/www/html/
[root@server4 html]# ls
[root@server4 html]# echo server4-redhat > index.html
[root@server4 html]# /etc/init.d/httpd start

測試:
添加解析:
vim /etc/hosts
172.25.12.1 www.westos.org
測試:
[root@localhost day07]# curl www.westos.org
server3
[root@localhost day07]# curl www.westos.org
server4-redhat
在server1查看虛擬IP

[root@server1 nginx-1.8.1]# ip addr show eth0
1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:48:ea:87 brd ff:ff:ff:ff:ff:ff
    inet 172.25.12.1/24 brd 172.25.12.255 scope global eth0
    inet 172.25.12.200/32 scope global eth0
    inet6 fe80::5054:ff:fe48:ea87/64 scope link
       valid_lft forever preferred_lft forever

在server2上查看虛擬IP

[root@server2 opt]# ip addr show eth0
1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:6c:37:0b brd ff:ff:ff:ff:ff:ff
    inet 172.25.12.2/24 brd 172.25.12.255 scope global eth0
    inet6 fe80::5054:ff:fe6c:370b/64 scope link
       valid_lft forever preferred_lft forever

然後將servrer1的keepalived關掉,虛擬IP轉到了server2,即server2接管了服務,然後在將server1的keepalived打開,服務又回切了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章