已整理-安装配置nginx+keepalived群集

 

目录:

  1、keepalived安装与配置
  2、nginx安装与配置
  3、nginx_check.sh安装与配置

 

 

1、keepalived安装与配置

keepalived是一个高性能的服务器高可用解决方案,keepalived主要防止服务器单点故障的发生问题,可以通过nginx的配合实现web服务端的高可用。 

10.10.17.157和10.10.17.158 这2台设备就是用来配置nginx的集群和高可用。我们可以虚拟出来一个10.10.17.150,外界请求访问虚拟ip而不是真正的nginx。 

那什么是高可用呢?高可用是指同一时间提供服务的只有一台设备,主服务器挂掉后,备用服务器便开始提供服务。

 

安装依赖包:

# yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel

nginx的http模块使用pcre来解析正则表达式 pcre-devel是使用pcre开发的一个二次库,nginx也需要。

zlib提供了很多种压缩的方式,nginx使用zlib对http包的内容进行gzip。

nginx支持https,所以需要安装openssl库。 

# tar -zxf keepalived-1.2.18.tar.gz
# cd keepalived-1.2.18
# ./configure --prefix=/usr/local/keepalived
# make && make install

 

 

创建/etc/keepalived目录,我们需要把keepalived做成一个服务,这样可以开机自行启动。

# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

 

将keepalived拷贝到/etc/init.d/目录下:

# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

 

将keepalived拷贝到/etc/sysconfig/目录下:

# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

 

创建第一个软链接:

# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig keepalived on                               #开机自启动

 

具体keepalived配置如下:

# cat keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id ng-01
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
} 

vrrp_instance VI_1 {
    state MASTER                              #备用节点:BACKUP
    interface eth0
    virtual_router_id 77
    mcast_src_ip 10.10.17.157
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        10.10.17.150
    }
}

 

下面我们就具体的讲讲配置内容:

1、global_defs代表配置中的主机名,我就把10.10.17.157设置为ng-1把,所以router_id的值为ng-1:

global_defs {
  router_id ng-1
} 

  

2、每隔2s去执行/etc/keepalived/nginx_check.sh脚本,这一项检查从开始便一直进行,interval表示间隔时间,weight -20代表脚本执行成功后10.10.17.157这个节点的优先级降低20。

vrrp_script chk_nginx {
  script "/etc/keepalived/nginx_check.sh"
  interval 2
  weight -20
}

 

3、state MASTER表示该节点角色为MASTER。

interface eth0                    表示设备的网卡是eth0,

virtual_router_id 77    是取自当前设备的ip也就是10.10.17.157的后两位。

mcast_src_ip 10.10.17.157 指定当前设备的ip,priority 100代表优先级是100,优先级在keepalived实现高可用方面超级重要。keepalived是根据优先级来选择当前提供服务的设备。 

我们这里用10.10.17.157指A设备,用10.10.17.158指B设备。假设A设备的优先级是100,B设备的优先级是90,那么keepalived服务器一开始去检查优先级,发现A设备的优先级很高,于是让A设备当大哥,让它去提供服务。当A设备的nginx挂了后,由于nginx_check脚本每2秒执行一次,发现A设备没有nginx进程后,先会尝试重新启动nginx,如果重新启动还是不行,这就很皮了!然后大发雷霆,就会杀掉所有的keepalived进程。然后回调给keepalived服务器,告知大哥的nginx挂掉了。keepalived服务器得知后,会把A设备的优先级减20,从而A设备的优先级为80。这样下次keepalived来检查优先级时,B设备的优先级比A设备搞,那么B设备就可以对外提供服务。美滋滋,这就实现了高可用。

 

5、这段配置,2个节点要一样,表示它们属于一个组,keepalived会同一组中去做检查并保持高可用。

authentication {
    auth_type PASS
    auth_pass 1111
}

 

6、track_script里面的chk_nginx要与前面定义的定时执行脚本名(vrrp_script chk_nginx { } )保持一致。 

7、自己定义对外提供服务的虚拟ip。

virtual_ipaddress {
    10.10.17.150
}

 

我们发现10.10.17.158的keepalived配置文件与第1台的差别:

router_id  ng-2
state  BACKUP
priority  90
mcast_src_ip  10.10.17.158 

  

这些和10.10.17.157是不一样的。这里要注意的是virtual_router_id必须要保持一致。 

 

2、nginx安装与配置 

# yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel
# tar -zxf nginx-1.11.7.tar.gz
# cd nginx-1.11.7
# ./configure --prefix=/usr/local/nginx
# make && make install

 

以下是nginx的配置文件(增加的红色字体部份):

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    keepalive_timeout  65; 

    upstream fastdfs_tracker {
        server  10.10.17.155:8000 weight=1 max_fails=2 fail_timeout=30s;
        server  10.10.17.156:8000 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /fastdfs {
             root html;
             index index.html index.htm;
             proxy_pass http://fastdfs_tracker/;
             proxy_set_header Host $http_host;
             proxy_set_header Cookie $http_cookie;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
             client_max_body_size 300m;
        } 

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

 

3、nginx_check.sh安装与配置 

# cat  /etc/keepalived/nginx_check.sh
A=`ps -C nginx --no-header |wc -l` 

if [ $A -eq 0 ];then 
    /usr/local/nginx/sbin/nginx 
    sleep 2 

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

fi 

# chmod 777 /etc/keepalived/nginx_check.sh

 

 

 

 

 

 

 

posted on 2023-03-09 15:42  tengq  阅读(101)  评论(0编辑  收藏  举报

导航