JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

号外号外:Keepalive+nginx部署高可用集群

wys521 2024-10-17 15:54:01 精选教程 28 ℃ 0 评论

准备4台服务器,IP如下:xxx.xxx.xxx.144xxx.xxx.xxx.145xxx.xxx.xxx.146xxx.xxx.xxx.147另外准备一个IP xxx.xxx.xxx.100作为应用的访问地址,避免暴露应用的真实IP。

1、安装keepalive、nginx。2、然后编辑keepalived.conf文件,(包含但不仅限下列内容)

global_defs {    router_id ha_master                                          #主机为ha_master,备机为ha_slaver    script_user root    #enable_script_security}vrrp_script check_nginx {    script /keepalived/script/check_nginx.sh                     #check_nginx.sh的路径,后面需要创建该文件    interval 5}vrrp_instance VI_1 {    state MASTER                                                  #主机为MASTER,备机为BACKUP    interface eth0                                                   #此处修改为ip addr显示的网卡名称    virtual_router_id 100    priority 100                                                  #主机为100,备机为90    advert_int 1    authentication {        auth_type PASS        auth_pass 1234    }    track_script {        check_nginx    }    virtual_ipaddress {        xxx.xxx.xxx.100                                          #集群访问的正式IP    }}

3、增加检测nginx脚本,并赋可执行权限mkdir –p /keepalived/scriptvi /keepalived/script/check_nginx.sh,chmod 755 /keepalived/script/check_nginx.sh

修改为以下内容

#!/bin/bashif [ `ps -C nginx --no-header | wc -l` -eq 0 ];then    #如果nginx没有启动就启动nginx    /etc/nginx/sbin/nginx                                           ## nginx路径    sleep 1    if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then        exit 10    fifi

编辑nginx.conf文件,修改http段中的如下内容

```shell
upstream eams {        server xxx.xxx.xxx.144:8080;                                ##8080为应用端口        server xxx.xxx.xxx.145:8080;        server xxx.xxx.xxx.146:8080;        server xxx.xxx.xxx.147:8080;        ip_hash;                                                    ##使用hash算法        check interval=3000 rise=2 fall=2 timeout=1000 type=http;        check_keepalive_requests 1;        check_http_send "HEAD / HTTP/1.1\r\n\r\n";        check_http_expect_alive http_2xx http_3xx http_4xx http_5xx;    }
server {    listen       80;    server_name  xxx.xxx.xxx.100;                               ##集群访问的正式IP    #charset koi8-r;    #access_log  logs/host.access.log  main;    location /xxx  {                                            ##xxx表示应用名称        proxy_set_header Host $host;        proxy_set_header X-real-ip $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_pass http://xxx;                                  ##xxx表示应用名称    }

```

启动keepalive、nginx服务。验证是否成功

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表