码迷,mamicode.com
首页 > 其他好文 > 详细

双主(keepalived+nginx)

时间:2018-02-05 18:42:30      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:err   pre   end   双主   work   机器   include   rip   ipaddr   

实验环境

系统: centos 6.9 mini

机器名    ip                                    虚拟ip

kn1        192.168.126.10

kn2        192.168.126.20               192.168.126.100

web1      192.168.126.30              192.168.126.200

web2      192.168.126.40

 

1、在kn1和kn2上分别安装keepalived 

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

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

 

2、在web1和web2上分别部署web服务,并且启动服务

[root@web1 yum.repos.d]# yum install -y httpd

[root@web1 yum.repos.d]# echo "web1" >/var/www/html/index.html

[root@web1 yum.repos.d]# service httpd restart

停止 httpd:                                               [确定]

正在启动 httpd: 

[root@web2 ~]# yum install -y httpd

[root@web2 ~]# echo "web2">/var/www/html/index.html

[root@web2 ~]# service httpd restart

停止 httpd:                                               [确定]

正在启动 httpd:                                           [确定]

 

3、配置keepalived,编写nginx进程检测脚本nginx.sh

(keepalived是通过检测keepalived进程是否存在判断服务器是否宕机,如果keepalived进程在但是nginx进程不在了那么keepalived是不会做主备切换,所以我们需要写个脚本来监控nginx进程是否存在,如果nginx不存在,则试着启动它,如果启动不成功,就将keepalived进程杀掉。)

 

3.1 在kn1上

[root@kn1 keepalived]# cat nginx.sh

#!/bin/bash  

N=`ps -C nginx --no-header |wc -l`     

if [ $N -eq 0 ];then

        /usr/local/nginx/sbin/nginx   

         sleep 10               

       if [ `ps -C nginx --no-header |wc -l`  -eq 0 ]; then

    killall keepalived

       fi   

fi

[root@kn1 keepalived]# chmod 755 /etc/keepalived/nginx.sh

[root@kn1 ~]# crontab -l

*/2 * * * * /etc/keepalived/nginx.sh

[root@kn1 ~]# vi /etc/keepalived/keepalived.conf

#全局配置

global_defs {

    router_id kn1                     #运行keepalived机器的一个标识,用hostname

}

vrrp_script nginx {

    script "/etc/keepalived/nginx.sh"         ##监控脚本

    interval 2                                      ##时间间隔,2秒

    weight 2                                        ##权重

}

vrrp_instance VI_1 {

    state MASTER               #标示状态为MASTER 备份机为BACKUP

    interface eth0             #设置实例绑定的网卡

    virtual_router_id 51       #同一实例下virtual_router_id必须相同

    priority 100               #MASTER权重要高于BACKUP 

    advert_int 1               #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒

    authentication {

        auth_type PASS         #设置认证

        auth_pass 1111         #主从服务器验证方式

    }

track_script {

        nginx                  #监控脚本

   }

    virtual_ipaddress {        #设置vip

    192.168.126.100          #可以多个虚拟IP,换行即可                             

    }

}

vrrp_instance VI_2 {

    state BACKUP

    interface eth0

    virtual_router_id 52

    priority 98

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 2222

    }

track_script {

        nginx       

   }

    virtual_ipaddress {

    192.168.126.200

    }

}

[root@kn1 ~]# /etc/init.d/keepalived restart

停止 keepalived:                                          [失败]

正在启动 keepalived:                                      [确定]

 

3.2 在kn2上

[root@kn1 keepalived]# cat nginx.sh

#!/bin/bash  

N=`ps -C nginx --no-header |wc -l`     

if [ $N -eq 0 ];then

        /usr/local/nginx/sbin/nginx   

         sleep 10              

       if [ `ps -C nginx --no-header |wc -l`  -eq 0 ]; then

       killall keepalived

       fi   

fi

[root@kn1 keepalived]# chmod 755 /etc/keepalived/nginx.sh

[root@kn1 ~]# crontab -l

*/2 * * * * /etc/keepalived/nginx.sh

[root@kn2 ~]# cat /etc/keepalived/keepalived.conf

global_defs {

   router_id kn2

}

vrrp_script nginx {

    script "/etc/keepalived/nginx.sh"        

    interval 2                                    

    weight 2                                      

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 98

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

track_script {

        nginx       

   }

    virtual_ipaddress {

    192.168.126.100

    }

}

vrrp_instance VI_2 {

    state MASTER

    interface eth0

    virtual_router_id 52

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 2222

    }

track_script {

        nginx       

   }

    virtual_ipaddress {

    192.168.126.200

    }

}

[root@kn2 ~]# /etc/init.d/keepalived restart

停止 keepalived:                                          [确定]

正在启动 keepalived:                                      [确定]

 

 

4、安装并且配置nginx(kn1和kn2的操作是一样的)

4.1 安装依赖包

[root@kn1 ~]#yum -y install gcc pcre-devel zlib-devel openssl-devel wget

4.2 安装nginx

[root@kn1 ~]#cd /usr/local/src/

[root@kn1 src]#wget http://nginx.org/download/nginx-1.9.5.tar.gz

[root@kn1 src]#tar zxvf nginx-1.9.5.tar.gz

[root@kn1 src]#cd nginx-1.9.5

[root@kn1 src]#./configure --with-http_stub_status_module

[root@kn1 src]#make && make install

 

4.3 配置nginx(红色的部分就是添加的)

[root@kn1 ~]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       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"‘;

    upstream web_up {

                  server 192.168.126.30 max_fails=3 fail_timeout=60s weight=1;

                  server 192.168.126.40 max_fails=3 fail_timeout=60s weight=2;            

    }

 

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

 

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

            proxy_pass http://web_up;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for;

        }

4.4 启动服务

[root@kn1 ~]#/usr/local/nginx/sbin/nginx

[root@kn1 ~]#/usr/local/nginx/sbin/nginx -s reload

[root@kn1 ~]#/etc/init,d/keepalive restart

 

5、测试

5.1 在kn1,kn2上查看虚拟ip

[root@kn1 ~]# ip addr list

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:d1:9e:5c brd ff:ff:ff:ff:ff:ff

    inet 192.168.126.20/24 brd 192.168.126.255 scope global eth0

    inet 192.168.126.100/32 scope global eth0

    inet6 fe80::20c:29ff:fed1:9e5c/64 scope link tentative dadfailed

       valid_lft forever preferred_lft forever

[root@kn2 ~]# ip addr list

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:d1:9e:5c brd ff:ff:ff:ff:ff:ff

    inet 192.168.126.30/24 brd 192.168.126.255 scope global eth0

    inet 192.168.126.200/32 scope global eth0

    inet6 fe80::20c:29ff:fed1:9e5c/64 scope link tentative dadfailed

       valid_lft forever preferred_lft forever

 

5.2 当kn1上的keepalived 服务停了,两个vip会都在kn2上,轮询访问虚拟ip没有问题

[root@kn1 ~]# /etc/init.d/keepalived stop

[root@kn2 ~]# ip addr list

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:35:6d:f2 brd ff:ff:ff:ff:ff:ff

    inet 192.168.126.20/24 brd 192.168.126.255 scope global eth0

    inet 192.168.126.100/32 scope global eth0

    inet 192.168.126.200/32 scope global eth0

[root@kn2 ~]# curl http://192.168.126.100

Web1

[root@kn2 ~]# curl http://192.168.126.200

web2

[root@kn2 ~]# curl http://192.168.126.200

web2

 

双主(keepalived+nginx)

标签:err   pre   end   双主   work   机器   include   rip   ipaddr   

原文地址:https://www.cnblogs.com/derrickrose/p/8418508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!