sonatype Nexus3 install on Kubernetes

一、Nexus安装

1、安装方式一

  • 使用nexus-builder.yaml文件进行安装:
$ kubectl create -f ./nexus-builder.yaml
namespace "nexus" created
deployment "nexus3" created
service "nexus3" created
  • 安装完成后,查找对应端口进行访问:
$ kubectl describe svc -n nexus nexus3 | grep 'NodePort:'
NodePort:                32139/TCP
$ hostname -i
192.168.56.11
  • 恭喜你,已完成Nexus3的安装,接下来你就可以进行访问了192.168.56.11:32139

Note:如果页面无法访问,请等待几分钟后再刷新页面。

2、安装方式二

  • 执行脚本nexus-builder.sh进行安装:
$ kubectl create -f ./run.sh
namespace "nexus" created
deployment "nexus3" created
service "nexus3" created
Now Nexus running on k8s-worker2 : 192.168.56.11:32139
Note:If the page can't be accessed,please wait a few minutes and refresh the page.

参考资料:sonatype/nexus3 - Docker Hub

二、用户操作界面

1、用户登录

  • 根据上面的地址打开用户界面。点击右上角Sign in登陆,默认账号admin,密码admin123

2、修改用户密码

  • 为了系统的安全性考虑,请及时修改密码,下面我们以默认账号admin为例进行修改,在配置页面,选择Security - User,点击用户列表admin

    sonatype Nexus3 install on Kubernetes_第1张图片

  • 进入后选择More - Change password进行密码修改。

    sonatype Nexus3 install on Kubernetes_第2张图片

3、创建maven仓库

  • 在配置界面,选择Repository - repositories,图中红色选线框着的是默认仓库。点击Create repository

    sonatype Nexus3 install on Kubernetes_第3张图片

  • 我们的目的仅仅管理自己开发的组件,选择maven2(hosted)即可。

    sonatype Nexus3 install on Kubernetes_第4张图片

  • 填写仓库配置,Deployment policy选择Allow redeploy

    sonatype Nexus3 install on Kubernetes_第5张图片

version policy,可以选Release或Snapshot,如果仓库开放给所有人,那选Release比较好,如果公司内部或自己用,其中一个都可以。

  • 仓库Hand-repository创建完毕
    sonatype Nexus3 install on Kubernetes_第6张图片

4、创建角色

  • 在配置页面,选择Security - Roles,点击Nexus role
    sonatype Nexus3 install on Kubernetes_第7张图片
  • 填写角色配置,根据所需的权限进行添加权限,然后点击Create role
    sonatype Nexus3 install on Kubernetes_第8张图片

5、创建用户

  • 在配置页面,选择Security - User,点击Create user

    sonatype Nexus3 install on Kubernetes_第9张图片

  • 填写用户信息,再点击最底下的Create user即可创建用户,注意如果启用此帐号,Status选择Active

    sonatype Nexus3 install on Kubernetes_第10张图片

  • 创建HandUser用户后,可以Sign out,用HandUser账号登陆了。

三、工作空间设置

1、给单一项目设置远程仓库

  • 在Maven project中的pom.xml文件添加以下信息

    
        nexus
        Nexus3 Repository
        
        http://192.168.56.11:32139/repository/maven-public/
    

  • 代码中url标签的路径在Repositories中选择需要的仓库,点击URL字段下的copy进行复制。
    sonatype Nexus3 install on Kubernetes_第11张图片

2、设置所有项目远程仓库

全局配置文件在Maven安装目录conf文件夹中settings.xml,当前用户配置文件在本地仓库中的settings.xml

若更改当前用户配置信息无效,则修改全局配置信息。

(1)、在本地仓库文件夹下,给settings.xml文件添加以下信息
  • 此种方法如果远程仓库关闭或意外退出,在maven构建时会到中央仓库去查找jar包

    
      NexusRepo
      
        
          nexus
          Nexus3 Repository
          http://192.168.56.11:32139/repository/maven-public/
          
            true
          
          
          
            true
          
        
      
        



      NexusRepo

(2)、在本地仓库文件夹下,给settings.xml文件添加以下信息
  • 添加此配置信息后,上面(1)中的配置将失效
  • Maven构建时可能会出现报错信息,尝试更新索引文件
  
  
    
      nexusMirror
      
      *
      Human Readable Name for this Mirror.
      http://192.168.56.11:32139/repository/maven-public/
    
  

3、Maven默认是无法下载中央仓库snapshots版本jar包的,通过以下设置即可下载


    
      NexusRepo
      
        
          central
          Central Repository
          https://repo.maven.apache.org/maven2
          default
          
            true
          
        
      
        



   NexusRepo

三、发布jar包

  • 配置pom.xml文件,添加以下代码:

    
        maven-releases
        maven releases
        http://192.168.56.11:32139/repository/maven-releases/
    
    
        maven-snapshots
        maven snapshots
        http://192.168.56.11:32139/repository/maven-snapshots/
    

  • 配置settings.xml文件添加以下代码:

    
      maven-releases
      admin
      admin123
    
    
      maven-snapshots
      admin
      admin123
    

  • 执行clean deploy语句,进行构建上传。

Nexus相关信息

这里简单介绍下几种repository的类型:

  • hosted,本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
  • proxy,代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
  • group,仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

Nexus搭建代码清单

nexus-builder.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: nexus
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  labels:
    app: nexus3
  name: nexus3
  namespace: nexus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nexus3
  template:
    metadata:
      labels:
        app: nexus3
    spec:
      containers:
      - name: nexus3
        image: registry.saas.hand-china.com/tools/nexus3:3.2.0
        ports:
        - containerPort: 8081
          protocol: TCP
        volumeMounts:
        - name: nexus-data
          mountPath: /nexus-data
      volumes:
        - name: nexus-data
          hostPath:
            path: /vagrant/nexus-data
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: nexus3
  name: nexus3
  namespace: nexus
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8081
  selector:
    app: nexus3
nexus-builder.sh
#!/bin/bash

cat > nexus-builder.yaml << EOF
apiVersion: v1
kind: Namespace
metadata:
  name: nexus
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  labels:
    app: nexus3
  name: nexus3
  namespace: nexus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nexus3
  template:
    metadata:
      labels:
        app: nexus3
    spec:
      containers:
      - name: nexus3
        image: registry.saas.hand-china.com/tools/nexus3:3.2.0
        ports:
        - containerPort: 8081
          protocol: TCP
        volumeMounts:
        - name: nexus-data
          mountPath: /nexus-data
      volumes:
        - name: nexus-data
          hostPath:
            path: /vagrant/nexus-data
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: nexus3
  name: nexus3
  namespace: nexus
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8081
  selector:
    app: nexus3
EOF

kubectl create -f ./nexus-builder.yaml

HostIP=`hostname -i`
EndPort=`kubectl describe svc -n nexus nexus3 | grep 'NodePort:' | awk '{print $3}'`
HostName=`kubectl get pods -n nexus -o wide | grep 'nexus3' | awk '{print $7}'`
echo "Now Nexus running on ${HostName}:${HostIP} : ${EndPort%/*}"
echo "Note:If the page can't be accessed,please wait a few minutes and refresh the page."

sudo rm -f ./nexus-builder.yaml

你可能感兴趣的:(sonatype Nexus3 install on Kubernetes)