使用nginx mirror 制作nexus 的简单ha

主要是运行两台nexus 机器,通过nexus 的host 以及proxy 以及public 模型,结合nginx 的mirror 将对于host 的get 请求
mirror 到另一台机器proxy nexus 的仓库,变相的cache 需要的仓库代码。

参考架构图

  • 来自nexus 的一个分享

  • 说明
    我们需要做的就是将对于host 模型的repo 流量镜像到proxy 当前host repo 的机器

配置说明

使用openresty 以及docker-compose 运行

  • docker-compose 文件
version: "3"
services:
  nginx:
    image: openresty/openresty:alpine-fat
    ports:
    - "80:80"
    volumes:
    - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  nexus1:
    image: sonatype/nexus3
    ports:
    - "8081:8081"
    volumes:
    - "./nexus-data1:/nexus-data"
    - "./backup1/:/backup/"
  nexus2:
    image: sonatype/nexus3
    ports:
    - "8082:8081"
    volumes:
    - "./nexus-data2:/nexus-data"
    - "./backup2/:/backup/"
  • nginx 配置文件
worker_processes 1;
user root;  
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    lua_code_cache off;
    lua_need_request_body on;
    gzip on;
    resolver 127.0.0.11 ipv6=off;          
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    gzip_min_length 2k;
    gzip_buffers 4 16k;
    gzip_comp_level 4;
    gzip_types text/plain text/css image/png application/javascript image/jpeg image/gif;
    server {
        listen 80;
        server_name app;
        charset utf-8;
        default_type text/html;
        location / {
            client_max_body_size 1G;
            mirror /mirror;
            proxy_set_header Host nexus1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host nexus1;
            proxy_pass http://nexus1:8081; 
        }
        location = /favicon.ico {
            root /opt/app/static;
        }
        location = /mirror {
           internal;
           if ($request_method != GET) {
            return 403;
           }
           proxy_connect_timeout 30;
           proxy_set_header Host nexus2;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Host nexus2;
           proxy_pass http://nexus2:8081$request_uri;
        }
        location = /empty {
            empty_gif;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

    }
}

启动&&测试

  • 启动
docker-compose up -d
  • 测试
    我们先在一台机器添加host 模型的rerpo,这个可以直接使用内置的,然后我们上传一个jar 包文件,如下


修改maven settings 文件,将对于maven 的地址指向我们的public。
另外一个机器配置proxy repo 指向上边host 模型的repo如下:

将proxy repo 添加到public 组(很重要,不然会查找不到)

编写一个简单的maven 项目,添加上传jar 包的引用,然后执行构建,不出意料,应用能看到有像nexus2 get 的请求,这样repo 就被cache 到
另外一个机器了,
通过nginx 以及nexus2 的日志也可以看出来,但是对于mirror 的log 需要单独配置,可以查看参考资料的连接

说明

原理实际上很简答,但是有几点需要注意,host 模型的repo 需要在两台机器都包含,同时proxy 模型的需要互相添加,如果对于基本变动很少
的repo 模型,使用这种方法进行一个简单nexus ha 的方案搭建还是很不错的,但是有一些几个缺点

  • 和上边说的一样,需要多机都需要添加同样模型的host repo(主要是url)
  • 如果真的故障发生了,仓库代码可能是分散在多处的,这个我们需要注意,但是基本问题不大,因为每台机器基本都包含了必须的repo
  • 对于每台机器的数据备份我们还是需要做的(要以出现事故了。。。)

参考资料

https://my.oschina.net/andChow/blog/2873870 
http://nginx.org/en/docs/http/ngx_http_mirror_module.html 
https://alex.dzyoba.com/blog/nginx-mirror/ 
https://github.com/rongfengliang/nginx-mirrorr-nexus-ha

原文地址:https://www.cnblogs.com/rongfengliang/p/10787442.html