1. 说明

Nexus3 项目依赖如下镜像:

1
2
docker.io/sonatype/nexus3:3.41.1
docker.io/library/nginx:1.21.1

部署完成后,将具有以下功能:

  • 私有镜像仓库

  • 私有 Maven 仓库

  • 私有 YUM/DEB 仓库

其他功能可自行添加

2. 部署 Nexus3

2.1. 创建持久卷

导入以下配置,创建持久卷;由于 Nexus 要存储 Docker 镜像和 Maven 编译结果以及 RPM/DEB 镜像包,可适当调大容量。

点击展开配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nexus-data
  namespace: core-app
  labels:
    app: nexus
spec:
  storageClassName: 'sc-nfs-share'
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 300Gi

2.2. 创建 Nexus3 Nginx 反向代理配置

由于要配置复杂的分流规则,需要 Nginx 容器完成流量 ,使用以下配置导入 Nginx 配置。

点击展开配置
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
apiVersion: v1
kind: ConfigMap
metadata:
  name: conf-nexus-nginx
  namespace: core-app
  labels:
    app: nexus
data:
  default.conf: |
    server_tokens off;
  web-panel.conf: |
    server {
        listen       80;
        listen  [::]:80;
        location / {
            proxy_http_version 1.1;
            proxy_pass http://127.0.0.1:8081; (1)
            proxy_buffering off;
            client_max_body_size 0;
            proxy_connect_timeout 3600s;
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
            send_timeout 3600s;
            proxy_request_buffering off;
        }
    }
  docker-public.conf: |
    upstream nexus_public_docker_hosted {
    server 127.0.0.1:11000; (2)
    }
    upstream nexus_public_docker_group {
    server 127.0.0.1:11001; (3)
    }
    server {
    listen 81;
    listen  [::]:81;
    client_max_body_size 10G;
    chunked_transfer_encoding on;
    set $upstream "nexus_public_docker_hosted";
    if ( $request_method ~* 'GET') {
    set $upstream "nexus_public_docker_group";
    }
    if ($request_uri ~ '/search') {
    set $upstream "nexus_public_docker_hosted";
    }
    location / {
    proxy_pass http://$upstream;
    proxy_connect_timeout 3600s;
    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;
    proxy_set_header Host $host;
    proxy_buffering off;
    client_max_body_size 0;
    send_timeout 3600s;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_request_buffering off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }

  repos.conf: |
    server {
        listen 82;
        listen  [::]:82;
        client_max_body_size 10G;
        chunked_transfer_encoding on;

        location  ~ ^/maven(.*)$ {
            set $matched_url http://127.0.0.1:8081/repository/maven-releases/;
            if ($uri ~ "SNAPSHOT") {
                set $matched_url http://127.0.0.1:8081/repository/maven-snapshots/;
            }
            if ( $request_method ~* 'GET') {
                set $matched_url http://127.0.0.1:8081/repository/maven-group/;
            }
            rewrite ^/maven(.*)$ /$1 break;
            proxy_pass $matched_url$1;
            proxy_set_header Host $host;
            proxy_buffering off;
            proxy_http_version 1.1;
            client_max_body_size 0;
            proxy_request_buffering off;
        }
        location  ~ ^/yum(.*)$ {
            set $matched_url http://127.0.0.1:8081/repository/yum-host/;
            if ( $request_method ~* 'GET') {
                set $matched_url http://127.0.0.1:8081/repository/yum-group/;
            }
            rewrite ^/yum(.*)$ /$1 break;
            proxy_pass $matched_url$1;
            proxy_set_header Host $host;
            proxy_buffering off;
            proxy_http_version 1.1;
            client_max_body_size 0;
            proxy_request_buffering off;
        }
        location  ~ ^/apt(.*)$ {
            rewrite ^/apt(.*)$ /$1 break;
            proxy_pass http://127.0.0.1/repository/apt-host$1;
            proxy_set_header Host $host;
            proxy_buffering off;
            proxy_http_version 1.1;
            proxy_request_buffering off;
        }
    }
其中
1 此处配置的是 nexus3 web面板的地址
2 此处配置的是 Nexus3 Docker Host 的地址
3 此处配置的是 Nexus3 Docker Group 的地址

2.3. 创建 Nexus3 启动配置

使用以下配置创建 Nexus3 启动配置。

点击展开配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sts-nexus
  namespace: core-app
  labels:
    app: nexus
spec:
  serviceName: svc-nexus
  selector:
    matchLabels:
      app: nexus
  replicas: 1
  template:
    metadata:
      labels:
        app: nexus
    spec:
      containers:
        - name: nexus
          image: docker.io/sonatype/nexus3:3.41.1
          resources:
            requests:
              memory: 3Gi
          volumeMounts:
            - name: nexus-data
              mountPath: /nexus-data
          env:
            - name: INSTALL4J_ADD_VM_PARAMS
              value: "-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m -Djava.util.prefs.userRoot=${NEXUS_DATA}/javaprefs"
          ports:
            - containerPort: 8081
              hostPort: 8081
              name: work-port
          startupProbe:
            httpGet:
              port: work-port
              path: /service/rest/v1/status
            failureThreshold: 60
            periodSeconds: 5
          livenessProbe:
            httpGet:
              port: work-port
              path: /service/rest/v1/status
            initialDelaySeconds: 120
            periodSeconds: 10
            failureThreshold: 5
        - name: nginx
          image: docker.io/library/nginx:1.21.1
          volumeMounts:
            - name: nginx-data
              mountPath: /etc/nginx/conf.d/
      volumes:
        - name: nexus-data
          persistentVolumeClaim:
            claimName: pvc-nexus-data
        - name: nginx-data
          configMap:
            name: conf-nexus-nginx

2.4. 暴露 Nexus3 Service

使用以下配置创建 Nexus3 Service。

点击展开配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nexus
  name: svc-nexus
  namespace: core-app
spec:
  ports:
    - name: nexus-web
      protocol: TCP
      port: 80
    - name: nexus-docker
      protocol: TCP
      port: 81
    - name: nexus-repos
      protocol: TCP
      port: 82

  selector:
    app: nexus

2.5. 创建 Ingress 映射

创建 Service 完成后,将其映射到 Ingress 上。

点击展开配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nexus
  namespace: core-app
  labels:
    app: nexus
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: "0" (1)
spec:
  ingressClassName: nginx-private
  tls:
    - hosts:
        - nexus.internal.d7z.net
      secretName: tls-pri-d7z
  rules:
    - host: nexus.internal.d7z.net
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: svc-nexus
                port:
                  name: nexus-web
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nexus-public
  namespace: core-app
  labels:
    app: nexus
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: "0" (1)
spec:
  ingressClassName: nginx-public
  tls:
    - hosts:
        - registry.d7z.net
        - repos.d7z.net
      secretName: tls-pub-d7z
  rules:
    - host: registry.d7z.net
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: svc-nexus
                port:
                  name: nexus-docker
    - host: repos.d7z.net
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: svc-nexus
                port:
                  name: nexus-repos
其中
1 配置Nginx不限制最大上传镜像大小

3. 管理 Nexus3

部署完成后,你需要对 Nexus3 进行一些配置。

3.1. 查看 Nexus3 部署情况

使用以下命令查看 nexus3 部署状态。

1
kubectl get pods,configmaps,secrets,statefulsets.apps,service,ingress  -n core-app -l app=nexus

3.2. 获取管理员密码

Nexus3 密码在程序第一次启动时自动生成 ,可使用以下命令获取密码。

1
kubectl exec -it -n core-app pods/sts-nexus-0  --container nexus  -- cat /nexus-data/admin.password

得到密码后,访问 https://nexus.internal.d7z.net 来管理。

请将 nexus 的管理员密码设置的足够复杂,以防止被暴力破解!

3.3. 绑定 LDAP

登陆 Nexus 后,访问 https://nexus.internal.d7z.net/#admin/security/ldap ,添加一个 LDAP 服务。具体配置参加如下图片即可。

01.a bind ldap
Figure 1. 配置LDAP连接

其中,密码为 LDAP 用户 service-account 的密码。点击按钮验证输入是否正确,验证完成后开始添加绑定。

01.b bind ldap
Figure 2. 配置LDAP用户

UserFilter 的内容如下所示

1
(|(memberOf=cn=admin,ou=groups,dc=cluster,dc=local)(memberOf=cn=oci,ou=groups,dc=cluster,dc=local)(memberOf=cn=package,ou=groups,dc=cluster,dc=local)(memberOf=cn=library,ou=groups,dc=cluster,dc=local)(memberOf=cn=deploy,ou=groups,dc=cluster,dc=local))

配置完成后,点击 Verify user mapping , 检查是否找到用户和角色。一切无误后保存即可。

3.4. 绑定角色权限

前往 https://nexus.internal.d7z.net/#admin/security/roles 页面,对LDAP相关的角色进行管理。

3.4.1. 绑定管理员角色

绑定管理员角色的参考如下,注意配置权限:

02.a bind admin roles
Figure 3. 绑定管理员角色

3.4.2. 绑定镜像操作员角色

具有镜像操作员角色的用户可向 Nexus 推送OCI镜像,按如下配置添加即可:

02.b bind oci roles
Figure 4. 绑定镜像操作员角色

3.4.3. 绑定依赖库操作员角色

具有依赖库操作员角色的用户可向 Nexus 推送相关的依赖包,按如下配置添加即可:

02.c bind library roles
Figure 5. 绑定依赖库操作员

3.4.4. 绑定软件包仓库操作员角色

具有软件包仓库操作员角色的用户可向 Nexus 推送相关的软件包,按如下配置添加即可:

02.d bind package roles
Figure 6. 绑定软件包仓库操作员

4. 创建仓库

4.1. 创建 Docker 仓库

前往 https://nexus.internal.d7z.net/#admin/repository/repositories,删除默认创建的所有仓库,然后点击新建,选择 docker (hosted), 创建本地私有仓库。具体配置如下:

03.a create docker hosted
Figure 7. 创建 Docker 本地仓库

在创建时,需与之前 Nginx 配置的端口一致。创建完成后,继续创建镜像代理。选择 docker (proxy) ,依照如下配置填写。

03.b create docker proxy
Figure 8. 创建 Docker仓库代理

代理创建完成后,需要将两者进行归组。选择 docker (group) ,按如下配置填写。

03.c create docker group
Figure 9. 归组

一切完成后,使用以下命令测试配置是否成功。

1
2
3
4
5
# 登陆 docker
podman login registry.d7z.net
podman pull docker.io/library/nginx:1.21.1
podman tag docker.io/library/nginx:1.21.1 registry.d7z.net/library/nginx:1.21.1
podman push registry.d7z.net/library/nginx:1.21.1

如果推送一切无误,则表明部署成功。

4.2. 创建 Maven 仓库

前往 https://nexus.internal.d7z.net/#admin/repository/repositories,点击新建,选择 maven2 (hosted), 创建本地 snapshots 仓库。具体配置如下:

04.b create m2 host snapshots

本地 snapshots 仓库创建完成后,即可开始创建本地 releases 仓库。

04.a create m2 host releases

本地 snapshots 仓库创建完成后,即可开始创建仓库代理,这里使用的是官方 maven 仓库。

04.c create m2 proxy default

最后,组合所有仓库到同一个 Group。

04.d create m2 group

至此,Maven 仓库配置完成。

4.3. 添加 yum 仓库

前往 https://nexus.internal.d7z.net/#admin/repository/repositories,然后点击新建,选择 yum (hosted), 创建本地仓库。具体配置如下:

05.a create yum host

然后重复之前的步骤,创建 yum(group) ( yum(proxy) 可自行创建):

05.b create yum group

其中,需要的 gpg 密钥配置可参考 Nexus 文档

4.4. 添加 apt 仓库

前往 https://nexus.internal.d7z.net/#admin/repository/repositories,然后点击新建,选择 apt (hosted), 创建本地仓库。具体配置如下:

06.a create apt host

其中,需要的 gpg 密钥配置可参考 Nexus 文档