keepalived+nginx實現nginx高可用

前期準備:

1、實驗拓撲圖

wKiom1VaqoaTQ22iAAFaaYBtMFY636.jpg

2、地址規劃

Master
10.10.0.224(VIP:10.10.0.220)keepalived、nginx
Backup10.10.0.226(VIP:10.10.0.220)keepalived、nginx
Real Server 1(apache1)10.10.0.225httpd
Real Server 2(apache2)10.10.0.221httpd


一、安裝keepalived和nginx

Master和Backup分別安裝:

1、安裝keepalived

[root@node1 ~]# yum install keepalived -y

2、配置repo源

[root@node1 ~]# vim /etc/yum.repos.d/nginx.repo

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/6/$basearch/

gpgcheck=0

enabled=1

3、安裝ngxin

[root@node1 ~]# yum install nginx -y


4、Master修改

(1).修改keepaliaved配置文件

[root@node1 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

[root@node1 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

        root@localhost

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS1

}

vrrp_script chk_nginx{

        script "killall -0 nginx"

        interval 1

        weight -2

        fall 2

        rise 2

}

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 {

        10.10.0.220 dev eth0 label eth0:0

}

track_script {

        chk_nginx

}

}

(3).拷貝一份配置文件到Backup服

[root@node1 ~]# scp -r /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/

[root@node1 ~]# scp -r /etc/nginx/nginx.conf [email protected]:/etc/nginx/

(4).開啓服務並開機自動啓動

[root@node1 ~]# service keepalived restart

[root@node1 ~]# service nginx restart

[root@node1 ~]# chkconfig keepalived on

[root@node1 ~]# chkconfig nginx on

5、Backup修改

(1).修改keepalived配置文件

[root@node2 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

        root@localhost

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS2     #修改唯一標識

}

vrrp_script chk_nginx{

        script "killall -0 nginx"

        interval 1

        weight -2

        fall 2

        rise 2

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.10.0.220 dev eth0 label eth0:0

}

track_script {

        chk_nginx

}

}


(2).開啓服務並開機自動啓動

[root@node2 ~]# service keepalived restart

[root@node2 ~]# service nginx restart

[root@node2 ~]# chkconfig keepalived on

[root@node2 ~]# chkconfig nginx on


6、Real Server 1

(1).安裝httpd

[root@realserver1]# yum install httpd -y

(2).建立測試頁

[root@realserver1]# vim /var/www/html/index.html

<h1>real server 1</h1>

(3).開啓服務

[root@realserver1]# service httpd restart   

[root@realserver1]# chkconfig httpd on

(4).測試

wKiom1Vaz7-AnDHUAABlfW9agRk880.jpg

7、Real Server 2

(1).安裝httpd

[root@realserver2]# yum install httpd -y

(2).建立測試頁

[root@realserver2]# vim /var/www/html/index.html

<h1>real server 2</h1>

(3).開啓服務

[root@realserver2]# service httpd restart   

[root@realserver2]# chkconfig httpd on

(4).測試

wKiom1Vaz_DQDBH_AABqkn6nPKo959.jpg


8、nginx實現後端real server的負載均衡,在Master上配置

(1).修改nginx配置文件

[root@node1 ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

[root@node1 ~]# vim /etc/nginx/nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream apacheweb {

        server 10.10.0.225:80 max_fails=3 fail_timeout=2s;

        server 10.10.0.221:80 max_fails=3 fail_timeout=2s;

        server 10.10.0.224:8080 backup;    #sorry server,當兩個realserver全掛掉後顯示。

    }

server {

  listen 8080;

  location / {

        root /usr/share/nginx/html/a;

        index index.html index.html;

#        proxy_pass http://apacheweb;

    }

}

server {

    listen       80;

    server_name  localhost;

    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {

        root   /usr/share/nginx/html;

        index  index.html index.htm;

    }

    location ~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {

        root        /var/www/html;  #此處定義後端服務器網頁存放路徑    

        proxy_pass   http://apacheweb;

    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }

(2).拷貝一份配置文件到Backup

[root@node1 ~]# scp -r /etc/nginx/nginx.conf [email protected]:/etc/nginx/

(3).重啓nginx服務

[root@node1 ~]# service nginx restart 

[root@node2 ~]# service nginx restart 

9、測試

(1).在瀏覽器中輸入vip地址

wKiom1Va2IaBIXlFAABfyfEEREw464.jpg

刷新瀏覽器之後

wKioL1Va2kbiN5bgAABjtc4G21s898.jpg

(2).模擬故障:

[root@realserver1]# service httpd stop

停掉real server 1的apache服務,刷新瀏覽器發現訪問的內容一直是real server2的內容

wKioL1Va2kbiN5bgAABjtc4G21s898.jpg

啓用之後real server 1會被加入會話。反之停掉real server 2,訪問的內容就一直是real server 1啦

當real server 1和real server 2全部掛掉之後,則顯示10.10.0.224:8080虛擬主機裏的內容。任何一個real server迴歸後,則顯示正常。


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