sonatype Nexus3 install on Kubernetes

本文详细介绍了Nexus3的两种安装方法、用户界面的基本操作、如何配置Maven项目来使用Nexus作为仓库,以及如何发布jar包等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、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:               <unset> 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


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


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


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


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


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

  • 仓库Hand-repository创建完毕

4、创建角色
  • 在配置页面,选择Security - Roles,点击Nexus role

  • 填写角色配置,根据所需的权限进行添加权限,然后点击Create role



5、创建用户
  • 在配置页面,选择Security - User,点击Create user


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



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

三、工作空间设置

1、给单一项目设置远程仓库
  • 在Maven project中的pom.xml文件添加以下信息
    <repositories>
      <repository>
          <id>nexus</id>
          <name>Nexus3 Repository</name>
          <!-- 此为仓库地址,应用 group 类型可以相当于同时添加多个仓库地址 -->
          <url>http://192.168.56.11:32139/repository/maven-public/</url>
      </repository>
    </repositories>
  • 代码中url标签的路径在Repositories中选择需要的仓库,点击URL字段下的copy进行复制。

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

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

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

(1)、在本地仓库文件夹下,给settings.xml文件添加以下信息
  • 此种方法如果远程仓库关闭或意外退出,在maven构建时会到中央仓库去查找jar包
    <profiles>
    <profile>
      <id>NexusRepo</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <name>Nexus3 Repository</name>
          <url>http://192.168.56.11:32139/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <!-- snapshots默认是关闭的,需要手动开启 -->
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>    
    </profiles>
    <!-- 只有激活后才生效,此代码为激活代码  -->
    <activeProfiles>
      <activeProfile>NexusRepo</activeProfile>
    </activeProfiles>
(2)、在本地仓库文件夹下,给settings.xml文件添加以下信息
  • 添加此配置信息后,上面(1)中的配置将失效
  • Maven构建时可能会出现报错信息,尝试更新索引文件
    <!-- 工厂的镜像,只要mirrorOf中的工厂要被访问,都会自动来找镜像,如果无法访问就不会再去中央工厂,推荐这个配置 -->
    <mirrors>
    <mirror>
      <id>nexusMirror</id>
      <!-- *号代表所有仓库,此处也可以单独设置,以逗号隔开 -->
      <mirrorOf>*</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://192.168.56.11:32139/repository/maven-public/</url>
    </mirror>
    </mirrors>
3、Maven默认是无法下载中央仓库snapshots版本jar包的,通过以下设置即可下载
<profiles>
    <profile>
      <id>NexusRepo</id>
      <repositories>
        <repository>
          <id>central</id>
          <name>Central Repository</name>
          <url>https://repo.maven.apache.org/maven2</url>
          <layout>default</layout>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>    
</profiles>
<!-- 只有激活后才生效,此代码为激活代码  -->
<activeProfiles>
   <activeProfile>NexusRepo</activeProfile>
</activeProfiles>

三、发布jar包

  • 配置pom.xml文件,添加以下代码:
    <distributionManagement>
      <repository>
          <id>maven-releases</id>
          <name>maven releases</name>
          <url>http://192.168.56.11:32139/repository/maven-releases/</url>
      </repository>
      <snapshotRepository>
          <id>maven-snapshots</id>
          <name>maven snapshots</name>
          <url>http://192.168.56.11:32139/repository/maven-snapshots/</url>
      </snapshotRepository>
    </distributionManagement>
  • 配置settings.xml文件添加以下代码:
    <servers>
      <server>
        <id>maven-releases</id>
        <username>admin</username>
        <password>admin123</password>
      </server>
      <server>
        <id>maven-snapshots</id>
        <username>admin</username>
        <password>admin123</password>
      </server>
    </servers>
  • 执行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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值